After researching and implementing a new payment gateway for your site, this tutorial will help you show the new payment gateway on your site along with existing gateways like: Paypal and 2Checkout.
In order to integrate new gateway, you must follow these steps:
- Render payment button
- Setup payment
- Process payment
The first step is rendering payment button for a new payment gateway. In this tutorial, we name the button “custom-payment”
To render payment button in your theme, you may add a filter in functions.php to your child theme. In case you haven’t known how to activate child theme, check out here: http://support.enginethemes.com/customer/portal/articles/897324-how-to-get-and-use-child-theme-to-customize-your-site
add_action('after_payment_list', 'ae_stripe_render_button');
function ae_stripe_render_button() {
?>
<li>
<span class="title-plan stripe-payment" data-type="stripe">
<?php
_e("Custom payment", ET_DOMAIN); ?>
<span><?php
_e("Send your payment to our custom payment", ET_DOMAIN); ?></span>
</span>
<a href="#" class="btn btn-submit-price-plan select-payment" data-type="custom-payment"><?php
_e("Select", ET_DOMAIN); ?></a>
</li>
<?php
}
Noted: “data-type” is the name of payment gateway
When users select a payment gateway, an Ajax request includes payment gateway name and billing details will be sent to server. Data sent to server is:
// post id
ID: this.model.id,
// author
author: this.model.get('post_author'),
// package sku id
packageID: this.model.get('et_payment_package'),
// payment gateway
paymentType: paymentType,
// send coupon code if exist
coupon_code: view.$('#coupon_code').val()
},
Then, server will catch the Ajax request and process it. Ajax callback locates at includes/aecore/payments.php line 166, in function setup_payment.
To integrate new payment gateway, you need to use filter ae_setup_payment. This filter need to return a response:
//Success: true/false
//Data: include received data and the most important is URL
//(URL will direct user to payment page, it should contain your custom payment required parameter such as: return url, price, item ... )
//Payment type: name of payment gateway
add_filter('ae_setup_payment', 'ae_custom_setup_payment', 10, 3);
function ae_custom_setup_payment($response, $paymentType, $order) {
if( $paymentType == 'custom-payment') {
// your payment setup code
}
return $response;
}
The response will be send back to user client and process user to payment gateway.
Note: The transaction is executed at template page-process-payment.php, you can get the URL of the page with function et_get_page_link(‘process-payment’, array(‘paymentType’ => ‘custom-payment’)); . You should setup the return url parameter for your custom gateway is process payment page url.
After the transaction is successful, the page is going to be re-directed to a process payment page. Here you can use hook ae_process_payment to analyze received data.
add_filter( 'ae_process_payment', 'ae_stripe_process_payment', 10 ,2 );
function ae_stripe_process_payment ( $payment_return, $data) {
$payment_type = $data['payment_type'];
$order = $data['order'];
if( $payment_type == 'custom-payment') {
// your payment process code
}
return $payment_return;
}
$payment_return is an array including parameters:
ACK: true/false Payment_status: pending/completed Payment: ‘custom-payment"