| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Mintpay |
| 4 |
* Plugin URI: https://www.mintpay.lk |
| 5 |
* Description: WooCommerce plugin of Mintpay. Sri Lanka's first buy now pay later platform, that allows consumers to split their payment into 3 interst-free installments. |
| 6 |
* Version: 1.0.1 |
| 7 |
* Author: Mintpay (Private) Limited |
| 8 |
* Author URI: https://www.mintpay.lk |
| 9 |
* Text Domain: mintpay |
| 10 |
* Requires at least: 4.6 |
| 11 |
* Requires PHP: 5.6 |
| 12 |
* |
| 13 |
* @package Mintpay |
| 14 |
*/ |
| 15 |
|
| 16 |
add_action('plugins_loaded', 'woocommerce_gateway_mintpay_init', 0); |
| 17 |
define('mintpay_IMG', WP_PLUGIN_URL . "/" . plugin_basename(dirname(__FILE__)) . '/assets/img/'); |
| 18 |
|
| 19 |
function woocommerce_gateway_mintpay_init() |
| 20 |
{ |
| 21 |
if (!class_exists('WC_Payment_Gateway')) return; |
| 22 |
|
| 23 |
if (!session_id()) { |
| 24 |
session_start([ |
| 25 |
'read_and_close' => true, |
| 26 |
]); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Gateway class |
| 31 |
*/ |
| 32 |
class WC_Gateway_mintpay extends WC_Payment_Gateway |
| 33 |
{ |
| 34 |
/** |
| 35 |
* Make __construct() |
| 36 |
**/ |
| 37 |
public function __construct() |
| 38 |
{ |
| 39 |
$this->id = 'mintpay'; // ID for WC to associate the gateway values |
| 40 |
$this->icon = "https://static.mintpay.lk/static/base/logo/mintpay_logo.png"; |
| 41 |
$this->method_title = "Mintpay"; // Gateway Title as seen in Admin Dashboad |
| 42 |
$this->method_description = "Sri Lanka's first buy now pay later platform, that allows consumers to split their payment into 3 interst-free installments."; |
| 43 |
$this->has_fields = false; // Inform WC if any fileds have to be displayed to the visitor in Frontend |
| 44 |
|
| 45 |
$this->init_form_fields(); // defines your settings to WC |
| 46 |
$this->init_settings(); // loads the Gateway settings into variables for WC |
| 47 |
|
| 48 |
$this->title = 'Mintpay'; |
| 49 |
$this->description = "Pay in 3 interest-free instalments using your <b>" . esc_html__('Debit / Credit', 'mintpay') . "</b> card."; |
| 50 |
$this->merchant_id = $this->get_option('merchant_id'); |
| 51 |
$this->merchant_secret = $this->get_option('merchant_secret'); |
| 52 |
$this->test_mode = 'yes' === $this->get_option('test_mode'); |
| 53 |
|
| 54 |
$this->msg['message'] = ''; |
| 55 |
$this->msg['class'] = ''; |
| 56 |
|
| 57 |
add_action('init', array(&$this, 'check_mintpay_response')); |
| 58 |
add_action('woocommerce_api_' . strtolower(get_class($this)), array($this, 'check_mintpay_response')); //update for woocommerce >2.0 |
| 59 |
add_action('woocommerce_gateway_icon', array($this, 'modify_gateway_icon_css'), 10, 2); |
| 60 |
|
| 61 |
if (version_compare(WOOCOMMERCE_VERSION, '2.0.0', '>=')) { |
| 62 |
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array(&$this, 'process_admin_options')); //update for woocommerce >2.0 |
| 63 |
} else { |
| 64 |
add_action('woocommerce_update_options_payment_gateways', array(&$this, 'process_admin_options')); // WC-1.6.6 |
| 65 |
} |
| 66 |
add_action('woocommerce_receipt_mintpay', array(&$this, 'receipt_page')); |
| 67 |
} //END-__construct |
| 68 |
|
| 69 |
function modify_gateway_icon_css($icon_html, $payment_gateway_id) |
| 70 |
{ |
| 71 |
if ($payment_gateway_id != $this->id) { |
| 72 |
return $icon_html; |
| 73 |
} |
| 74 |
|
| 75 |
$new_css = 'class="ph-logo-style" src'; |
| 76 |
$icon_html = preg_replace('/(src){1}/', $new_css, $icon_html); |
| 77 |
|
| 78 |
return $icon_html; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Initiate Form Fields in the Admin Backend |
| 83 |
**/ |
| 84 |
function init_form_fields() |
| 85 |
{ |
| 86 |
$this->form_fields = array( |
| 87 |
// Activate the Gateway |
| 88 |
'enabled' => array( |
| 89 |
'title' => __('Enable', 'mintpay'), |
| 90 |
'type' => 'checkbox', |
| 91 |
'label' => __('Enable mintpay gateway', 'mintpay'), |
| 92 |
'default' => 'yes', |
| 93 |
'description' => __('Show mintpay as a payment option at checkout', 'mintpay'), |
| 94 |
'desc_tip' => true |
| 95 |
), |
| 96 |
|
| 97 |
// Activate Test mode |
| 98 |
'test_mode' => array( |
| 99 |
'title' => __('Test Mode', 'mintpay'), |
| 100 |
'type' => 'checkbox', |
| 101 |
'label' => __('Enable test mode', 'mintpay'), |
| 102 |
'default' => 'yes', |
| 103 |
'description' => __('Test mintpay payment gateway in sandbox environment', 'mintpay'), |
| 104 |
'desc_tip' => true |
| 105 |
), |
| 106 |
|
| 107 |
// LIVE Key-ID |
| 108 |
'merchant_id' => array( |
| 109 |
'title' => __('Merchant ID', 'mintpay'), |
| 110 |
'type' => 'text', |
| 111 |
'description' => __('Your mintpay Merchant ID'), |
| 112 |
'desc_tip' => true |
| 113 |
), |
| 114 |
// LIVE Key-Secret |
| 115 |
'merchant_secret' => array( |
| 116 |
'title' => __('Merchant Secret', 'mintpay'), |
| 117 |
'type' => 'text', |
| 118 |
'description' => __('Your mintpay Merchant Secret'), |
| 119 |
'desc_tip' => true |
| 120 |
), |
| 121 |
); |
| 122 |
} //END-init_form_fields |
| 123 |
|
| 124 |
/** |
| 125 |
* Admin Panel Options |
| 126 |
* - Show info on Admin Backend |
| 127 |
**/ |
| 128 |
public function admin_options() |
| 129 |
{ |
| 130 |
_e('<h3> Mintpay </h3>', 'mintpay'); |
| 131 |
_e('<p> WooCommerce payment plugin of Mintpay payment gateway. Sri Lanka\'s first buy now pay later platform, that allows consumers to split their payment into 3 interst-free installments </p>', 'mintpay'); |
| 132 |
_e('<table class="form-table">', 'mintpay'); |
| 133 |
// Generate the HTML For the settings form. |
| 134 |
$this->generate_settings_html(); |
| 135 |
_e('</table>', 'mintpay'); |
| 136 |
} //END-admin_options |
| 137 |
|
| 138 |
/** |
| 139 |
* There are no payment fields, but we want to show the description if set. |
| 140 |
**/ |
| 141 |
function payment_fields() |
| 142 |
{ |
| 143 |
if ($this->description) { |
| 144 |
_e($this->description); |
| 145 |
} |
| 146 |
} //END-payment_fields |
| 147 |
|
| 148 |
/** |
| 149 |
* Receipt Page |
| 150 |
**/ |
| 151 |
function receipt_page($order) |
| 152 |
{ |
| 153 |
_e('<p><strong>' . esc_html__('Thank you for your order') . '.<br/>' . esc_html__('The payment page will open soon.') . '</strong></p>', 'mintpay'); |
| 154 |
_e($this->generate_mintpay_form($order)); |
| 155 |
} //END-receipt_page |
| 156 |
|
| 157 |
/** |
| 158 |
* Generate button link |
| 159 |
**/ |
| 160 |
function generate_mintpay_form($order_id) |
| 161 |
{ |
| 162 |
global $woocommerce; |
| 163 |
|
| 164 |
$order = wc_get_order($order_id); |
| 165 |
|
| 166 |
$merchant_id = $this->merchant_id; |
| 167 |
$merchant_secret = $this->merchant_secret; |
| 168 |
$amount = $order->get_total(); |
| 169 |
|
| 170 |
$success_hash = hash_hmac('sha256', $merchant_id . sprintf("%.02f", round($amount, 2)) . $order_id, $merchant_secret); |
| 171 |
$fail_hash = hash_hmac('sha256', $order_id, $merchant_secret); |
| 172 |
|
| 173 |
|
| 174 |
$_SESSION['order_id'] = $order_id; |
| 175 |
|
| 176 |
$redirect_url = $order->get_checkout_order_received_url(); |
| 177 |
|
| 178 |
$notify_url = ""; |
| 179 |
// Redirect URL : For WooCoomerce 2.0 |
| 180 |
if (version_compare(WOOCOMMERCE_VERSION, '2.0.0', '>=')) { |
| 181 |
$notify_url = add_query_arg('wc-api', get_class($this), $redirect_url); |
| 182 |
} |
| 183 |
|
| 184 |
$success_url = $notify_url . '&orderId=' . $order_id . '&hash=' . base64_encode($success_hash); |
| 185 |
$fail_url = $notify_url . '&orderId=' . $order_id . '&hash=' . base64_encode($fail_hash); |
| 186 |
|
| 187 |
|
| 188 |
if ($this->test_mode) { |
| 189 |
$api_url = "https://dev.mintpay.lk/user-order/api/"; |
| 190 |
$form_url = "https://dev.mintpay.lk/user-order/login/"; |
| 191 |
} else { |
| 192 |
$api_url = "https://app.mintpay.lk/user-order/api/"; |
| 193 |
$form_url = "https://app.mintpay.lk/user-order/login/"; |
| 194 |
} |
| 195 |
|
| 196 |
foreach ($order->get_items() as $item_id => $item) { |
| 197 |
|
| 198 |
$order_items[] = array( |
| 199 |
'name' => $item->get_name(), |
| 200 |
'product_id' => $item->get_product_id(), |
| 201 |
'sku' => $item->get_type(), |
| 202 |
'quantity' => $item->get_quantity(), |
| 203 |
'unit_price' => $item->get_total(), |
| 204 |
'created_date' => "2001-10-01 01:10:01", |
| 205 |
'updated_date' => "2001-10-01 01:10:01", |
| 206 |
'discount' => "0.00" |
| 207 |
); |
| 208 |
} |
| 209 |
|
| 210 |
// request body sent to mintpay api |
| 211 |
$postData = [ |
| 212 |
'merchant_id' => $merchant_id, |
| 213 |
'order_id' => $order_id, |
| 214 |
'total_price' => $amount, |
| 215 |
'discount' => $order->get_total_discount(), |
| 216 |
'customer_id' => $order->get_customer_id(), |
| 217 |
'customer_email' => $order->get_billing_email(), |
| 218 |
'customer_telephone' => $order->get_billing_phone(), |
| 219 |
'ip' => $order->get_customer_ip_address(), |
| 220 |
'x_forwarded_for' => $order->get_customer_ip_address(), |
| 221 |
'delivery_street' => $order->get_billing_address_1() . $order->get_billing_address_2(), |
| 222 |
'delivery_region' => $order->get_billing_city(), |
| 223 |
'delivery_postcode' => $order->get_billing_postcode(), |
| 224 |
'cart_created_date' => date_format($order->get_date_created(), "Y-m-d H:i:s"), |
| 225 |
'cart_updated_date' => date_format($order->get_date_modified(), "Y-m-d H:i:s"), |
| 226 |
'success_url' => $success_url, |
| 227 |
'fail_url' => $fail_url, |
| 228 |
'products' => $order_items, |
| 229 |
'currency_code' => $order->get_currency(), |
| 230 |
'currency_symbol' => get_woocommerce_currency_symbol($order->get_currency()) |
| 231 |
]; |
| 232 |
|
| 233 |
// headers sent with the request |
| 234 |
$headers = [ |
| 235 |
'Authorization' => 'Token ' . $merchant_secret, |
| 236 |
'Content-Type' => 'application/json', |
| 237 |
]; |
| 238 |
|
| 239 |
// create arguments for the post request |
| 240 |
$args = array( |
| 241 |
'body' => json_encode($postData), |
| 242 |
'headers' => $headers, |
| 243 |
'timeout' => '15', |
| 244 |
'redirection' => '5', |
| 245 |
'httpversion' => '1.0', |
| 246 |
'blocking' => true, |
| 247 |
); |
| 248 |
|
| 249 |
// receive the response and retreive the body and decode json |
| 250 |
$response = wp_remote_post($api_url, $args); |
| 251 |
$response_body = wp_remote_retrieve_body($response); |
| 252 |
$mintpayRequestData = json_decode($response_body, true); |
| 253 |
|
| 254 |
/** |
| 255 |
* when debugging API issues use logs (i.e: file_put_contents) |
| 256 |
*/ |
| 257 |
|
| 258 |
if (isset($mintpayRequestData['message']) && $mintpayRequestData['message'] == 'Success') { |
| 259 |
|
| 260 |
return '<form action="' . $form_url . '"method="post" id="mintpay_payment_form"> |
| 261 |
<input type="hidden" name="purchase_id" value="' . $mintpayRequestData['data'] . '" > |
| 262 |
<input type="submit" class="button-alt" id="submit_mintpay_payment_form" value="' . __('Pay via Mintpay', 'mintpay') . '" /> <a class="button cancel" href="' . $order->get_cancel_order_url() . '">' . __('Cancel order & restore cart', 'mintpay') . '</a> |
| 263 |
<script type="text/javascript"> |
| 264 |
jQuery(function(){ |
| 265 |
jQuery("body").block({ |
| 266 |
message: "' . __('Thanks for your order! We are now redirecting you to Mintpay payment gateway to make the payment.', 'mintpay') . '", |
| 267 |
overlayCSS: { |
| 268 |
background : "#fff", |
| 269 |
opacity : 0.8 |
| 270 |
}, |
| 271 |
css: { |
| 272 |
padding : 20, |
| 273 |
textAlign : "center", |
| 274 |
color : "#333", |
| 275 |
border : "1px solid #eee", |
| 276 |
backgroundColor : "#fff", |
| 277 |
cursor : "wait", |
| 278 |
lineHeight : "32px" |
| 279 |
} |
| 280 |
}); |
| 281 |
jQuery("#submit_mintpay_payment_form").click();}); |
| 282 |
</script> |
| 283 |
</form>'; |
| 284 |
} else { |
| 285 |
return wp_redirect(wc_get_cart_url()); |
| 286 |
} |
| 287 |
} //END-generate_mintpay_form |
| 288 |
|
| 289 |
|
| 290 |
function check_mintpay_response() |
| 291 |
{ |
| 292 |
global $woocommerce; |
| 293 |
|
| 294 |
if (isset($_GET['key']) && isset($_GET['hash'])) { |
| 295 |
|
| 296 |
if (isset($_GET['orderId'])) { |
| 297 |
$order = wc_get_order($_GET['orderId']); |
| 298 |
|
| 299 |
$post_success_hash = hash_hmac('sha256', $this->merchant_id . sprintf("%.02f", round($order->get_total(), 2)) . $_GET['orderId'], $this->merchant_secret); |
| 300 |
|
| 301 |
$post_failed_hash = hash_hmac('sha256', $_GET['orderId'], $this->merchant_secret); |
| 302 |
|
| 303 |
if (base64_decode($_GET['hash']) == $post_success_hash) { |
| 304 |
$order->payment_complete(); |
| 305 |
$order->add_order_note(__('Mintpay payment completed', 'mintpay')); |
| 306 |
$woocommerce->cart->empty_cart(); |
| 307 |
wp_redirect($order->get_checkout_order_received_url()); |
| 308 |
} else if (base64_decode($_GET['hash']) == $post_failed_hash) { |
| 309 |
$cancelled_text = "Payment failed."; |
| 310 |
wc_add_notice($cancelled_text, 'error'); |
| 311 |
$order->update_status('failed', $cancelled_text); |
| 312 |
wp_redirect($order->get_checkout_payment_url()); |
| 313 |
} else { |
| 314 |
$cancelled_text = "Suspicious response."; |
| 315 |
$order->update_status('cancelled', $cancelled_text); |
| 316 |
wc_add_notice(__('Payment error:', 'woothemes') . $cancelled_text, 'error'); |
| 317 |
wp_redirect($order->get_checkout_payment_url()); |
| 318 |
} |
| 319 |
} else { |
| 320 |
wp_redirect(wc_get_checkout_url() . "A"); |
| 321 |
} |
| 322 |
} else { |
| 323 |
wp_redirect(wc_get_checkout_url() . "B"); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* Process the payment and return the result |
| 329 |
**/ |
| 330 |
function process_payment($order_id) |
| 331 |
{ |
| 332 |
global $woocommerce; |
| 333 |
$order = new WC_Order($order_id); |
| 334 |
|
| 335 |
if (version_compare(WOOCOMMERCE_VERSION, '2.1.0', '>=')) { // For WC 2.1.0 |
| 336 |
$checkout_payment_url = $order->get_checkout_payment_url(true); |
| 337 |
} else { |
| 338 |
$checkout_payment_url = get_permalink(get_option('woocommerce_pay_page_id')); |
| 339 |
} |
| 340 |
|
| 341 |
return array( |
| 342 |
'result' => 'success', |
| 343 |
'redirect' => add_query_arg( |
| 344 |
'order', |
| 345 |
$order->get_id(), |
| 346 |
add_query_arg( |
| 347 |
'key', |
| 348 |
$order->get_order_key(), |
| 349 |
$checkout_payment_url |
| 350 |
) |
| 351 |
) |
| 352 |
); |
| 353 |
} //END-process_payment |
| 354 |
|
| 355 |
|
| 356 |
/** |
| 357 |
* Get Page list from WordPress |
| 358 |
**/ |
| 359 |
function mintpay_get_pages($title = false, $indent = true) |
| 360 |
{ |
| 361 |
$wp_pages = get_pages('sort_column=menu_order'); |
| 362 |
$page_list = array(); |
| 363 |
if ($title) $page_list[] = $title; |
| 364 |
foreach ($wp_pages as $page) { |
| 365 |
$prefix = ''; |
| 366 |
// show indented child pages? |
| 367 |
if ($indent) { |
| 368 |
$has_parent = $page->post_parent; |
| 369 |
while ($has_parent) { |
| 370 |
$prefix .= ' - '; |
| 371 |
$next_page = get_post($has_parent); |
| 372 |
$has_parent = $next_page->post_parent; |
| 373 |
} |
| 374 |
} |
| 375 |
// add to page list array array |
| 376 |
$page_list[$page->ID] = $prefix . $page->post_title; |
| 377 |
} |
| 378 |
return $page_list; |
| 379 |
} //END-mintpay_get_pages |
| 380 |
|
| 381 |
} //END-class |
| 382 |
|
| 383 |
/** |
| 384 |
* Add the Gateway to WooCommerce |
| 385 |
**/ |
| 386 |
function woocommerce_add_gateway_mintpay_gateway($methods) |
| 387 |
{ |
| 388 |
$methods[] = 'WC_Gateway_mintpay'; |
| 389 |
return $methods; |
| 390 |
} //END-wc_add_gateway |
| 391 |
|
| 392 |
add_filter('woocommerce_payment_gateways', 'woocommerce_add_gateway_mintpay_gateway'); |
| 393 |
} //END-init |
| 394 |
|
| 395 |
/** |
| 396 |
* 'Settings' link on plugin page |
| 397 |
**/ |
| 398 |
add_filter('plugin_action_links', 'mintpay_add_action_plugin', 10, 5); |
| 399 |
function mintpay_add_action_plugin($actions, $plugin_file) |
| 400 |
{ |
| 401 |
static $plugin; |
| 402 |
|
| 403 |
if (!isset($plugin)) |
| 404 |
$plugin = plugin_basename(__FILE__); |
| 405 |
if ($plugin == $plugin_file) { |
| 406 |
|
| 407 |
$settings = array('settings' => '<a href="admin.php?page=wc-settings&tab=checkout§ion=wc_gateway_mintpay">' . __('Settings') . '</a>'); |
| 408 |
|
| 409 |
$actions = array_merge($settings, $actions); |
| 410 |
} |
| 411 |
|
| 412 |
return $actions; |
| 413 |
} //END-settings_add_action_link |
| 414 |
|
| 415 |
include_once("price_break_down.php"); |