Create custom Payment Gateway For RestroPress

Bibhu Prakash

March 24, 2023

Through this blog am going to show you how to add a custom payment gateway method like your own.

Here are some steps you need to follow:

  1. Registering the gateway
  2. Setting up the credit card form, if any
  3. Processing the payment
  4. Adding the gateway settings for API keys and such

Registering the Gateway

By registering the payment gateway, we make it available for use.
Through this process the gateway appears in the Payment Gateways list, as like below screenshot:

You can add the gateway in a very simple way by using this filter add_filter( 'rpress_payment_gateways', 'Your function' );

You need to add these custom codes through your child theme functions.php file so that you will not face any issues after your parent theme update.

// registers the gateway
function custom_rpress_register_gateway( $gateways ) {
$gateways['sample_gateway'] = array(
'admin_label' => __( 'Sample Gateway', 'restropress' ), //lable for gateway list
'checkout_label' => __('Sample Gateway', 'restropress' ) // lable for chekout page
);
return $gateways;
}
add_filter( 'rpress_payment_gateways' , 'custom_rpress_register_gateway' );

 

Now you will able to see the gateway name in the setting list and after you checked the custom gateway you will see it on the Checkout page.

After this, you will need to remove Restropress Default credit card forms by adding the following Code.

Remove credit Card forms

function custom_rpress_sample_gateway_cc_form() {
// register the action to remove the default CC form
return;
}
add_action('rpress_sample_gateway_cc_form', 'custom_rpress_sample_gateway_cc_form');

Now we will move to process payment

Add the following codes to process the payment after clicking the place order button.

// processes the payment
function custom_rpress_process_payment($purchase_data) {

global $rpress_settings;

/**********************************
* set transaction mode
**********************************/

if(rpress_is_test_mode()) {
// set test credentials here
} else {
// set live credentials here
}

// check for any stored errors
$errors = rpress_get_errors();
if(!$errors) {

$purchase_summary = rpress_get_purchase_summary($purchase_data);

/**********************************
* setup the payment details
**********************************/

$payment = array( 
'price' => $purchase_data['price'], 
'date' => $purchase_data['date'], 
'user_email' => $purchase_data['user_email'],
'purchase_key' =>$purchase_data['purchase_key'],
'currency' => $rpress_settings['currency'],
'cart_details' => $purchase_data['cart_details'],
'user_info' => $purchase_data['user_info'],
'status' => 'pending'
);

// record the pending payment
$payment = rpress_insert_payment($payment);

$merchant_payment_confirmed = false;

/**********************************
* Process the credit card here.
* If not using a credit card
* then redirect to merchant
* and verify payment with an IPN
**********************************/

// if the merchant payment is complete, set a flag
$merchant_payment_confirmed = true; 

if($merchant_payment_confirmed) { // this is used when processing credit cards on site

// once a transaction is successful, set the purchase to complete
rpress_update_payment_status($payment, 'processing');

// go to the success page 
rpress_send_to_success_page();

} else {
$fail = true; // payment wasn't recorded
}

} else {
$fail = true; // errors were detected
}

if( $fail !== false ) {
// if errors are present, send the user back to the purchase page so they can be corrected
rpress_send_back_to_checkout('?payment-mode=' . $purchase_data['post_data']['rpress-gateway']);
}
}
add_action('rpress_gateway_sample_gateway', 'custom_rpress_process_payment');

Final Gateway Code:

 

<?php
/**
* Plugin Name: RestroPress - Custom Gateway
* Plugin URI: https://restropress.com/extensions/
* Description: This plugin will help you to add a custom payment gateway method for RestroPress
* Version: 1.0
* Author: Magnigenie
* Author URI: https://magnigenie.com
* Contributors: Bibhu Prakash
*/

// registers the gateway
function custom_rpress_register_gateway( $gateways ) {
$gateways['sample_gateway'] = array(
'admin_label' => __( 'Sample Gateway', 'restropress'), //lable for gateway list
'checkout_label' => __('Sample Gateway', 'restropress' ) // lable for chekout page
);
return $gateways;
}
add_filter('rpress_payment_gateways', 'custom_rpress_register_gateway');

function custom_rpress_sample_gateway_cc_form() {
// register the action to remove default CC form
return;
}
add_action('rpress_sample_gateway_cc_form', 'custom_rpress_sample_gateway_cc_form');

// processes the payment
function custom_rpress_process_payment($purchase_data) {

global $rpress_settings;

/**********************************
* set transaction mode
**********************************/

if(rpress_is_test_mode()) {
// set test credentials here
} else {
// set live credentials here
}

// check for any stored errors
$errors = rpress_get_errors();
if(!$errors) {

$purchase_summary = rpress_get_purchase_summary($purchase_data);

/**********************************
* setup the payment details
**********************************/

$payment = array(
'price' => $purchase_data['price'],
'date' => $purchase_data['date'],
'user_email' => $purchase_data['user_email'],
'purchase_key' =>$purchase_data['purchase_key'],
'currency' => $rpress_settings['currency'],
'cart_details' => $purchase_data['cart_details'],
'user_info' => $purchase_data['user_info'],
'status' => 'pending'
);

// record the pending payment
$payment = rpress_insert_payment($payment);

$merchant_payment_confirmed = false;

/**********************************
* Process the credit card here.
* If not using a credit card
* then redirect to merchant
* and verify payment with an IPN
**********************************/

// if the merchant payment is complete, set a flag
$merchant_payment_confirmed = true;

if($merchant_payment_confirmed) { // this is used when processing credit cards on site

// once a transaction is successful, set the purchase to complete
rpress_update_payment_status($payment, 'processing');

// go to the success page
rpress_send_to_success_page();

} else {
$fail = true; // payment wasn't recorded
}

} else {
$fail = true; // errors were detected
}

if( $fail !== false ) {
// if errors are present, send the user back to the purchase page so they can be corrected
rpress_send_back_to_checkout('?payment-mode=' . $purchase_data['post_data']['rpress-gateway']);
}
}
add_action('rpress_gateway_sample_gateway', 'custom_rpress_process_payment');

 

Leave a Reply

Your email address will not be published. Required fields are marked *

We trade to make your work effortless, Subscribe our newsletter to stay updated.

We respect your security and won’t disclose your email to anyone.
Experience remarkable WordPress
products with a new level of power,
beauty, and human-centered designs.
© Magnigenie - All Right Reserved 2023
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram