Skip to content

Instantly share code, notes, and snippets.

@hasinhayder
Created May 11, 2021 00:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hasinhayder/0414c0cd4cfbf054e83646b1994c6e0e to your computer and use it in GitHub Desktop.
Save hasinhayder/0414c0cd4cfbf054e83646b1994c6e0e to your computer and use it in GitHub Desktop.
<?php
/**
* WP SmartPay - bKash Gateway
*
* Plugin Name: WP SmartPay - bKash Gateway
* Plugin URI: https://wpsmartpay.com/
* Description:
* Tags: bKash
* Version: 0.1
* Author: WPSmartPay
* Author URI: https://wpsmartpay.com/
* Text Domain: wp-smartpay
*
* Requires PHP: 7.0.0
* Requires at least: 4.9
* Tested up to: 5.4
*/
// Exit if accessed directly.
if (!defined('ABSPATH')) {
exit;
}
final class SmartPaybKashPaymentGateway
{
/**
* The single instance of this class
*/
private static $instance = null;
/**
* Construct bKash class.
*
* @since 0.1
* @access private
*/
private function __construct()
{
add_action('init', [$this, 'process_webhooks']);
add_filter('smartpay_payment_gateways', [$this, 'register_gateway']);
add_action('smartpay_bkash_process_payment', [$this, 'process_payment']);
add_filter('smartpay_settings_sections_gateways', [$this, 'gateway_section']);
add_filter('smartpay_settings_gateways', [$this, 'gateway_settings']);
}
/**
* Main bKash Instance.
*
* Ensures that only one instance of bKash exists in memory at any one
* time. Also prevents needing to define globals all over the place.
*
* @since 0.1
* @return object|bKash
* @access public
*/
public static function instance()
{
if (!isset(self::$instance) && !(self::$instance instanceof bKash)) {
self::$instance = new self();
}
return self::$instance;
}
public function register_gateway($gateways)
{
$gateways['bkash'] = array(
'admin_label' => 'bKash',
'checkout_label' => 'bKash'
);
return $gateways;
}
/**
* Process webhook requests.
*
* @since 1.1.0
* @return void
* @access public
*/
public function process_webhooks()
{
if (isset($_GET['smartpay-listener']) && $_GET['smartpay-listener'] == 'bkash') {
echo 'bKash webhook';
die();
}
}
public function process_payment($payment_data)
{
// $payment_data
$payment_id = smartpay_insert_payment($payment_data);
if ($payment_id) {
$redirect_uri = smartpay_get_success_page_uri();
wp_redirect($redirect_uri);
} else {
die('Error');
}
}
/**
* Add Gateway subsection
*
* @since 1.0.0
* @param array $sections Gateway subsections
* @return array
* @access public
*/
public function gateway_section(array $sections = array()): array
{
$sections['bkash'] = __('bKash', 'wp-smartpay');
return $sections;
}
/**
* Register the gateway settings for bKash
*
* @since 1.1.0
* @param array $settings
* @return array
* @access public
*/
public function gateway_settings(array $settings): array
{
$gateway_settings = array(
array(
'id' => 'bKash_settings',
'name' => '<strong>' . __('bKash Gateway Settings', 'wp-smartpay') . '</strong>',
'desc' => __('Configure your bKash Gateway Settings', 'wp-smartpay'),
'type' => 'header'
),
array(
'id' => 'bkash_vendor_id',
'name' => __('Vendor ID', 'wp-smartpay'),
'desc' => __('Enter your bKash Vendor ID', 'wp-smartpay'),
'type' => 'text'
),
array(
'id' => 'bkash_vendor_auth_code',
'name' => __('Auth Codes', 'wp-smartpay'),
'desc' => __('Get Auth Code from bKash : Developer > Authentication', 'wp-smartpay'),
'type' => 'text'
),
$bKash_webhook_description_text = __(
sprintf(
'<p>For bKash to function completely, you must configure your Instant Notification System. Visit your <a href="%s" target="_blank">account dashboard</a> to configure them. Please add the URL below to all notification types. It doesn\'t work for localhost or local IP.</p><p><b>INS URL:</b> <code>%s</code></p>.',
'https://vendors.bKash.com/alerts-webhooks',
home_url("index.php?smartpay-listener=bKash")
),
'wp-smartpay'
),
$_SERVER['REMOTE_ADDR'] == '127.0.0.1' ? $bKash_webhook_description_text .= __('<p><b>Warning!</b> It seems you are on the localhost.</p>', 'wp-smartpay') : '',
array(
'id' => 'bKash_webhook_description',
'type' => 'descriptive_text',
'name' => __('Instant Notification System (INS)', 'wp-smartpay'),
'desc' => $bKash_webhook_description_text,
),
);
return array_merge($settings, ['bkash' => $gateway_settings]);
}
}
return SmartPaybKashPaymentGateway::instance();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment