Change payment gateway programmatically

My site enables some customers to enter a coupon code entitling them to 100% discount, on submission they are given the subscription free of charge...great.

The problem is as they pay no money they are automatically processed via the Admin payment gateway, this is a problem as the subscription page notifies them their account is set to auto renew with no renewal date or option to renew via paypal.

I need to programmatically change their gateway as per the screenshot, the admin area allows to change the gateway which resolves all my problems, but I need this to be automatic.

Is there a setting I could change or a function I could call (perhaps on welcome screen page) that would change this?

Any help very much appreciated.

Kind Regards

James

  • Tyler Postle

    Hey James,

    Hope you're doing well today and thanks for your question!

    This would definitely take some custom development, I'll call in one of our SLS(coding experts) to take a closer look at this as they will be able to better advise on the best solution here :slight_smile:

    SLS do deal with more complex threads so sometimes their responses can be a little slower than normal.

    Please do let us know if you have any further questions in the meantime!

    All the best,
    Tyler

  • Jose

    Hi there @James,

    I just put something together for you. The following funtion would change from admin to paypal gateway:

    function move_gateway($user_id = false, $sub_id = false){
                if ($user_id) {
                    $factory = Membership_Plugin::factory();
                    $member = $factory->get_member($user_id);
                    $relationships = $member->get_relationships();
                    foreach ($relationships as $rel) {
                        if ($rel->usinggateway == 'admin') {
                            $member->update_relationship_gateway($rel->rel_id, 'admin', 'paypalsolo');
                        }
                    }
                }
            }

    You would still need to hook this function into the right action.

    Maybe something like this might work:
    add_action('membership_payment_subscr_signup', 'move_gateway', 20, 2);

    This is just a draft, so please be careful and make sure to not test this code in a live site. :slight_smile:

    Let me know if this pointer is useful for you, and don't hesitate to ask if you need further assistance.

    Have a great weekend!

    Cheers,
    Jose

  • Jose

    Hey there @James,

    Sorry for the delay to jump here again.

    Could I not change parameter to = 'paypalsolo' or would this have adverse implications ?

    You can change it and it would be effective. The adverse implications is that you would need to re-apply the changes after every plugin update.

    Of course, changing the default gateway to something different migth affect some uses cases that relies on the admin gateway being applied, but that's not the case for you.
    If you want to implement some other payment gateway in the future you will need to test this again, and possibly add some conditional in order to apply the workaround only in certain circumstances.

    With that said, I finally got the time to code and test the snippet for you, so there is no need to touch the core :slight_smile:

    You simply need to download the attached .php file and put it under the following folder:
    ../wp-content/mu-plugins
    (create the folder if it doesn't exists).

    The code is almost the same as the snippet above, but I changed the hook to 'membership_add_subscription'.

    This is the final code, just for reference:

    if(! class_exists('membership_move_gateway') ):
    
        class membership_move_gateway{
    
            function __construct(){
                add_action('membership_add_subscription', array($this, 'move_gateway'), 20, 4);
            }
    
            function move_gateway($tosub_id = false, $tolevel_id = false, $to_order = false, $user_id = false){
                if ($user_id) {
                    $factory = Membership_Plugin::factory();
                    $member = $factory->get_member($user_id);
                    $relationships = $member->get_relationships();
                    foreach ($relationships as $rel) {
                        if ($rel->usinggateway == 'admin') {
                            $member->update_relationship_gateway($rel->rel_id, 'admin', 'paypalsolo');
                        }
                    }
                }
            }
    
         }
        new membership_move_gateway;
    endif;

    Please let me know how it works for you.

    Cheers,
    Jose

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.