custom gateway file: save Membership data via SQL or..?

Hi,

We are building a new Membership payment gateway for our site. I noticed all your normal gateways (paypalsolo, free, 2CO) all use something like this to save transaction and subscription data:

// create_subscription
$member = Membership_Plugin::factory()->get_member($user_id);
if($member) {
$member->create_subscription($sub_id, $this->gateway);
}

do_action('membership_payment_subscr_signup', $user_id, $sub_id);

(That was from the free gateway but seems similar to all the others).

My programmer is insisting we use SQL statements to write directly to the database. I have the nagging feeling that this is a bad idea. Furthermore, the programmer doesn't understand why there are no SQL statements in your standard gateways and how/format of the way you guys save transactions and user subscription info...

Please elaborate and clarify on this?

Thank you very much,

David

  • Fabio Jun

    Hi David,

    sorry for the delay here.

    // create_subscription
    $member = Membership_Plugin::factory()->get_member($user_id);
    if($member) {
    $member->create_subscription($sub_id, $this->gateway);
    }

    do_action('membership_payment_subscr_signup', $user_id, $sub_id);

    (That was from the free gateway but seems similar to all the others).

    Yes, that's the right way to implement it.

    My programmer is insisting we use SQL statements to write directly to the database. I have the nagging feeling that this is a bad idea. Furthermore, the programmer doesn't understand why there are no SQL statements in your standard gateways and how/format of the way you guys save transactions and user subscription info...

    While it could do the job, it is not a very good idea, because the plugin already has the proper functions to do it, creating another level of abstraction.
    The existing function not only creates the subscription relationships (using wp insert that creates the SQL) but checks for coupon usage, proper db tables, apply filters and actions, set associated wp roles, and communication settings.

    Hope this helps.
    Cheers,
    Fabio Jun

  • Rheinard

    Hi @David,

    Since 3.5.1 we've been migrating our code to a more structured implementation. So for your custom gateway, I would recommend looking at the code in our newer Authorize gateway (classes/Membership/Gateway/Authorize.php).

    This gateway also has a good example of how to implement coupons.
    Also on line 721 you will see the code implementing an action 'membership_payment_processed'. This is the hook we use for affiliate.

    The older gateway.paypalexpress.php gateway shows a simpler version of this hook on line 937.

    My recommendation would be to model your gateway on the Authorize.php file as the older gateways will eventually be phased out and recreated using a similar structure.

    This means, your gateway would be contained in a class and extending our Gateway parent object:

    class Membership_Gateway_YourGateway extends Membership_Gateway {
    
    }

    And to have it autoload you will need to put it within the folder structure... classes/Membership/Gateway/YourGateway.php

    We're aiming to make the plugin more extensible so that this process would be easier in future, but for now I hope that this helps you.

    Kind Regards,
    Rheinard

  • David

    Thank you very much, guys, for your help.

    Yes, we are putting our custom gateway in classes/Membership/Gateway/YourGateway.php

    However, every time the plugin is updated, our gateway file is deleted! Is there a mod you can do in Membership so we have a better structure that doesnt require reuploading the file with each update?

    Another issue is, we use Kenya Shillings, and right now, every time we update Membership gateway, we have to take these steps:

    1. Copy pesapal gateway files from the gateways backup folder to the plugin’s gateway files folder

    2. Add KSH to membership\membershipincludes\
    classes\membershipadmin.php

    3. Change Membership currency settings to KES

    Any way we can have that avoided?

  • Patrick

    Hey there @David

    I hope you are well today!

    Yep, updating a plugin with the auto-updater will remove existing files and replace 'em with the new ones. So any customizations that you have made will be lost. That's always been an issue in WordPress as far as I know.

    However, there are ways around that, but such things do need to be included in the core code of the plugin. For example, our eNewsletter plugin has a feature whereby custom themes you create for newsletters are stored in an external folder that is not affected by updates.

    As @Fabio Jun mentioned, a future release of Membership should make the process of customizing & extending things easier but, for now, you would need to proceed in the manner you have described each time you update the plugin.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.