Adding An API Gateway

An API gateway is one whereby the user purchasing on your website never has to leave your website. Instead a backend call is made to the API in question to process the transaction.

Zenbership handles most of the eCommerce aspects, all you need to do is tell Zenbership how to communicate with your payment gateway's APIs.

Useful Guides

We recommend studying how Stripe was integrated to learn how you can integrate your own API payment gateway.

Create Your Payment "Gateway Class"

All payment gateways will need to have their own "Gateway Class" which lives in the pp-cart/gateways folder. A skeleton version of a gateway class looks like this:

<?php
class gw_mygateway extends cart
{
    // Change this to match the unique identifier for the gateway.
    // This MUST start with "gw_" and MUST match the above class name.
    protected $gateway_name = 'gw_mygateway';

    protected $sandbox_url = 'https://sandbox_api.mygateway.com';
    protected $live_url = 'https://api.mygateway.com';

    protected $gateway_data;
    protected $use_order_id;
    protected $use_order;
    protected $return;
    protected $amount;
    protected $url;

    /**
     * All of this is passed into the class directly by
     * Zenbership. You shouldn't need to ever alter the
     * core code or grab this information after the fact.
     */
    public function __construct($price, $billing = '', $use_order_id = '', $shipping = '', $auth_only = '0')
    {
        $this->billing   = $billing;
        $this->shipping  = $shipping;
        $this->amount = $price;
        $this->auth_only = $auth_only;

        // Zenbership Order Number
        if (empty($use_order_id)) {
            $this->use_order_id = $this->generate_cart_id();
        } else {
            $this->use_order_id = $use_order_id;
        }

        // Get gateway
        $q1 = $this->get_gateways('', $this->gateway_name);

        $this->gateway_data = $q1['0'];
    }

    /**
     * Method to get a payment gateway's API's URL.
     * Takes into consideration things like sandbox testing.
     */
    protected function getUrl()
    {
        if ($this->gateway_data['test_mode'] == '1') {
            $this->url = $this->live_url;
        } else {
            $this->url = $this->sandbox_url;
        }
    }

    /**
     * Charge a credit card.
     */
    public function charge()
    {
        // This needs to match whatever fields your gateway
        // requires to process the order.
        $fields = '';

        return $this->call_gateway($fields, 'charges');
    }

    /**
     * Refund an order
     */
    public function refund()
    {
        // This needs to match whatever fields your gateway
        // requires to refund the order.
        $fields = '';

        return $this->call_gateway($fields, $send_name);
    }

    /**
     * Call a gateway
     */
    public function call_gateway($send_data, $reason = 'charge', $cus_req = '')
    {
        // credential1, credential2, credential3 are all set from the
        // administrative dashboard under "Shop > Payment Gateways".
        // $credentials = $this->gateway_data['credential1'] . ':';   

        // $call = $this->curl_call($this->getUrl(), $send_data, '0', $credentials);
        // $reply = $this->handle_reply($call, 'id');
        // return $reply;
    }

    /**
     * Handle a reply and process it into an array that
     * Zenbership can use to complete the transaction.
     */
    public function handle_reply($reply_data, $more = '')
    {
        // Do something with the gateway's reply data.
        // $json = json_decode($reply_data);

        // $return = array();
        // $return['gateway_id_1'] = $this->token;
        // $return['zen_order_id'] = $this->use_order_id;
        // $return['error']     = ($json->error == true) ? 1 : 0;
        // $return['msg']       = $json->message;
        // $return['resp_code'] = '';

        return $return;
    }
}

Create a Record in the Database

You will need to add a row into the ppSD_payment_gateways table as follows:

INSERT INTO ppSD_payment_gateways (
    `fee_flat`,
    `fee_percent`,
    `test_mode`,
    `active`,
    `code`,
    `name`,
    `online`,
    `api`,
    `local_card_storage`,
    `primary`,
    `method_refund`
) VALUES (
    '0.00',
    '0.50',
    '0',
    '0',
    'gw_mygateway',
    'My Gateway Name',
    'http://www.mygatewaywebsite.com/',
    '1',
    '0',
    '0',
    '1'
);

Activate the Gateway In Zenbership

After adding the row into the database, the gateway will now appear on your administrative dashboard under "Shop > Payment Gateways". Click it, active it, and add your gateway credentials in accodance with how you programmed your "Gateway Class".

Last modified 3 weeks ago.