woocommerce_globalpay_init

Plugin Name: Woocommerce GlobalPay Plugin URI: http://twitter.com/Feyisayo Description: Allows payments to be made to a Woocommerce shop via GlobalPay Author: Feyisayo Akinboboye Author URI: http://twitter.com/Feyisayo Version: 3.3.

Description

woocommerce_globalpay_init(); 

Usage

  1. if ( !function_exists( 'woocommerce_globalpay_init' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce-globalpay/woocommerce-globalpay.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = woocommerce_globalpay_init(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/woocommerce-globalpay.php  
  1. function woocommerce_globalpay_init () { 
  2. // The GlobalPay plugin should only be loaded if Woocommerce is installed. 
  3. if (!class_exists('WC_Payment_Gateway')) { 
  4. return; 
  5.  
  6. class WC_GlobalPay extends WC_Payment_Gateway { 
  7. /** 
  8. * Array that will hold payment information. 
  9. * The response from GlobalPay's transaction lookup webservice is in XML. 
  10. * So SimpleXML is used to manipulate it but eventually it will be made 
  11. * into a single-dimension array so that it can stored as part of the 
  12. * order meta information. This variable needs to declared at class level 
  13. * at the function that converts the simpleXML object into an array is 
  14. * recursive. 
  15. * @var array 
  16. * @access private 
  17. */ 
  18. private $paymentInfo = array(); 
  19.  
  20. /** 
  21. * Contstructor. 
  22. */ 
  23. public function __construct() { 
  24. $this->id = 'globalpay'; 
  25. $this->icon = apply_filters('woocommerce_globalpay_icon', plugins_url('/images/globalpay_logo.png', __FILE__)); 
  26. $this->has_fields = FALSE; 
  27. $this->liveurl = 'https://www.globalpay.com.ng/Paymentgatewaycapture.aspx'; 
  28. $this->testurl = 'https://demo.globalpay.com.ng/globalpay_demo/paymentgatewaycapture.aspx'; 
  29. $this->method_title = __('GlobalPay', 'woocommerce'); 
  30.  
  31. // Load the form fields. 
  32. $this->init_form_fields(); 
  33.  
  34. // Load the settings. 
  35. $this->init_settings(); 
  36.  
  37. // Define user set variables. 
  38. $this->title = $this->settings['title']; 
  39. $this->description = $this->settings['description']; 
  40. $this->testmode = $this->settings['testmode']; 
  41. $this->debug = $this->settings['debug']; 
  42. $this->thanks_message = $this->settings['thanks_message']; 
  43. $this->error_message = $this->settings['error_message']; 
  44. $this->feedback_message = ''; 
  45. $this->merchant_id = $this->settings['merchant_id']; 
  46. $this->webservice_user = $this->settings['webservice_user']; 
  47. $this->webservice_password = $this->settings['webservice_password']; 
  48.  
  49. // Actions. 
  50. add_action('woocommerce_receipt_globalpay', array(&$this, 'receipt_page')); 
  51. add_action('woocommerce_thankyou_' . $this->id, array(&$this, 'thankyou_page')); 
  52. add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options')); 
  53.  
  54. // Logs. 
  55. if ('yes' == $this->debug) { 
  56. $this->log = new WC_Logger(); 
  57.  
  58. if (!$this->is_valid_for_use()) { 
  59. $this->enabled = FALSE; 
  60.  
  61. function is_valid_for_use() { 
  62. if (!in_array(get_option('woocommerce_currency'), array('NGN'))) { 
  63. return FALSE; 
  64. } else { 
  65. return TRUE; 
  66.  
  67. function admin_options() { 
  68. echo '<h3>' . __('GlobalPay', 'woocommerce') . '</h3>'; 
  69. echo '<p>' . __('GlobalPay works by sending the user to GlobalPay to enter their payment information.', 'woocommerce') . '</p>'; 
  70. echo '<table class="form-table">'; 
  71.  
  72. if ($this->is_valid_for_use()) { 
  73. $this->generate_settings_html(); 
  74. } else { 
  75. echo '<div class="inline error"><p><strong>' . __('Gateway Disabled', 'woocommerce') . '</strong>: ' . __('GlobalPay does not support your store currency.', 'woocommerce') . '</p></div>'; 
  76.  
  77. echo '</table>'; 
  78.  
  79. function init_form_fields() { 
  80. $this->form_fields = array( 
  81. 'enabled' => array( 
  82. 'title' => __('Enable/Disable', 'woocommerce'),  
  83. 'type' => 'checkbox',  
  84. 'label' => __('Enable GlobalPay', 'woocommerce'),  
  85. 'default' => 'yes' 
  86. ),  
  87. 'title' => array( 
  88. 'title' => __('Title', 'woocommerce'),  
  89. 'type' => 'text',  
  90. 'description' => __('This controls the title which the user sees during checkout.', 'woocommerce'),  
  91. 'default' => __('GlobalPay', 'woocommerce') 
  92. ),  
  93. 'description' => array( 
  94. 'title' => __('Description', 'woocommerce'),  
  95. 'type' => 'textarea',  
  96. 'description' => __('This controls the description which the user sees during checkout.', 'woocommerce'),  
  97. 'default' => __('Pay via GlobalPay', 'woocommerce') 
  98. ),  
  99. 'merchant_id' => array( 
  100. 'title' => __('GlobalPay Merchant ID', 'woocommerce'),  
  101. 'type' => 'text',  
  102. 'description' => __('Merchant ID given to you by GlobalPay', 'woocommerce'),  
  103. 'default' => __('', 'woocommerce') 
  104. ),  
  105. 'thanks_message' => array( 
  106. 'title' => __('Thanks message', 'woocommerce'),  
  107. 'type' => 'textarea',  
  108. 'description' => __('The message to show on a successful payment', 'woocommerce'),  
  109. 'default' => __('Thank you. Your payment was successful. Your order is status is now <strong>processing</strong>', 'woocommerce') 
  110. ),  
  111. 'error_message' => array( 
  112. 'title' => __('Failure message', 'woocommerce'),  
  113. 'type' => 'textarea',  
  114. 'description' => __('The message to show when a payment has failed', 'woocommerce'),  
  115. 'default' => __('Sorry. Your payment was not successful', 'woocommerce') 
  116. ),  
  117. 'testmode' => array( 
  118. 'title' => __('GlobalPay Test Mode', 'woocommerce'),  
  119. 'type' => 'checkbox',  
  120. 'label' => __('Enable GlobalPay Test Mode', 'woocommerce'),  
  121. 'default' => 'yes' 
  122. ),  
  123. 'webservice_user' => array( 
  124. 'title' => __('GlobalPay webservice user ID', 'woocommerce'),  
  125. 'type' => 'text',  
  126. 'description' => __('The user ID for the GlobalPay transaction lookup service', 'woocommerce'),  
  127. 'label' => __('GlobalPay webservice user ID', 'woocommerce') 
  128. ),  
  129. 'webservice_password' => array( 
  130. 'title' => __('GlobalPay webservice password', 'woocommerce'),  
  131. 'type' => 'text',  
  132. 'description' => __('The password for the GlobalPay transaction lookup service', 'woocommerce'),  
  133. 'label' => __('GlobalPay webservice user ID', 'woocommerce') 
  134. ),  
  135. 'debug' => array( 
  136. 'title' => __('Debug', 'woocommerce'),  
  137. 'type' => 'checkbox',  
  138. 'label' => __('Enable logging (<code>wc-logs/globalpay.txt</code>)', 'woocommerce'),  
  139. 'default' => 'no' 
  140. ); 
  141.  
  142. function get_globalpay_args ($order) { 
  143. $txn_ref = get_current_user_id() . '-' . $order->id . '-' . time(); 
  144. update_post_meta($order->id, 'merch_txnref', $txn_ref); 
  145. update_post_meta($order->id, '_transaction_id', $txn_ref); 
  146.  
  147. $order_total = $order->get_total(); 
  148.  
  149. if ($this->debug == 'yes') { 
  150. $this->log->add($this->id, 'Generating payment form for order #' . $order->id . '.'); 
  151.  
  152. $name = ''; 
  153. if ($order->billing_first_name || $order->billing_last_name) { 
  154. $name = trim($order->billing_first_name . ' ' . $order->billing_last_name); 
  155. } else { 
  156. $name = __('Guest', 'woocommerce'); 
  157.  
  158. $globalpay_args = array( 
  159. 'merchantid' => $this->merchant_id,  
  160. 'amount' => $order_total,  
  161. 'currency' => 'NGN',  
  162. 'merch_txnref' => $txn_ref,  
  163. 'name' => $name,  
  164. 'names' => $name,  
  165. 'email_address' => $order->billing_email,  
  166. 'phone_number' => $order->billing_phone 
  167. ); 
  168.  
  169. return apply_filters('woocommerce_globalpay_args', $globalpay_args); 
  170.  
  171. function generate_globalpay_form ($order_id) { 
  172. // It appears some themes call the checkout page twice thus causing the 
  173. // transaction reference to be generated twice. Below we shall prevent 
  174. // it by ensuring the referrer has no URL arguments. 
  175. $query_args = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY); 
  176. if (0 != substr_count($query_args, 'key') || 0 != substr_count($query_args, 'order')) { 
  177. if ('yes' == $this->debug) { 
  178. $this->log->add($this->id, __FUNCTION__ . ' aborted because of possible duplicate call'); 
  179. return; 
  180.  
  181. $order = new WC_Order($order_id); 
  182. $globalpay_args = $this->get_globalpay_args($order); 
  183. $globalpay_args_array = array(); 
  184.  
  185. $globalpay_adr = $this->liveurl; 
  186. if ($this->testmode == 'yes') { 
  187. $globalpay_adr = $this->testurl; 
  188.  
  189. foreach ($globalpay_args as $key => $value) { 
  190. $globalpay_args_array[] = '<input type="hidden" name="' . esc_attr($key) . '" value="' . esc_attr($value) . '" />'; 
  191.  
  192. wc_enqueue_js(' 
  193. jQuery("body").block({ 
  194. message: "' . __('Thank you for your order. We are now redirecting you to GlobalPay to make payment.', 'woocommerce') . '",  
  195. overlayCSS: { 
  196. background: "#fff",  
  197. opacity: 0.6 
  198. },  
  199. css: { 
  200. padding: 20,  
  201. textAlign: "center",  
  202. color: "#555",  
  203. border: "3px solid #aaa",  
  204. backgroundColor:"#fff",  
  205. cursor: "wait",  
  206. lineHeight: "32px" 
  207. }); 
  208. jQuery("#submit_globalpay_payment_form").click(); 
  209. '); 
  210.  
  211. $form = '<form action="' . esc_url($globalpay_adr) . '" method="post" id="globalpay_payment_form">' 
  212. . implode('', $globalpay_args_array) 
  213. . '<input type="submit" class="button-alt" id="submit_globalpay_payment_form" value="' . __('Pay via GlobalPay', 'woocommerce') . '" />' 
  214. . '<a class="button cancel" href="' . esc_url($order->get_cancel_order_url()) . '">' . __('Cancel order & restore cart', 'woocommerce') . '</a>' 
  215. . '</form>'; 
  216.  
  217. if ('yes' == $this->debug) { 
  218. $this->log->add($this->id,  
  219. 'User redirected to GlobalPay with the following:' . "\r\n" 
  220. . 'GlobalPay URL: ' . $globalpay_adr . "\r\n" 
  221. . print_r($globalpay_args, TRUE)); 
  222.  
  223. // Place the order ID and redirect URL in the session. To be used when the 
  224. // user is redirected back from GlobalPay. 
  225. $_SESSION['globalpay_order_id'] = $order_id; 
  226. $_SESSION['globalpay_redirect_url'] = $this->get_return_url($order); 
  227.  
  228. return $form; 
  229.  
  230. function check_transaction_on_user_return() { 
  231. @ob_clean(); 
  232. global $woocommerce; 
  233.  
  234. if ('yes' == $this->debug) { 
  235. $this->log->add($this->id,  
  236. 'Transaction details received on user return from GlobalPay:' . "\r\n" 
  237. . print_r($_GET, TRUE)); 
  238.  
  239. if (!isset($_SESSION['globalpay_order_id'])) { 
  240. wp_redirect(home_url()); 
  241. exit; 
  242. $order_id = $_SESSION['globalpay_order_id']; 
  243. unset($_SESSION['globalpay_order_id']); 
  244.  
  245. $order = new WC_Order((int) $order_id); 
  246. // $merch_txnref = get_post_meta($order->id, 'merch_txnref', true); 
  247. if (!$order) { 
  248. // @todo: notify user and admin of this ie order not found 
  249.  
  250. $_GET['key'] = $order->order_key; 
  251. $_GET['order'] = $order->id; 
  252.  
  253. $this->get_transaction_status($order); 
  254. foreach ($this->paymentInfo as $k => $v) { 
  255. if ('status' != $k) { 
  256. update_post_meta((int) $order_id, $k, $v); 
  257.  
  258. if ('completed' == $this->paymentInfo['status']) { 
  259. // Payment completed. 
  260. $order->add_order_note(__('Payment completed', 'woocommerce')); 
  261. $order->payment_complete(); 
  262. $woocommerce->cart->empty_cart(); 
  263.  
  264. if ($this->debug == 'yes') { 
  265. $this->log->add($this->id, 'Payment complete.'); 
  266.  
  267. update_post_meta((int) $order_id, 'Payment Method', $this->method_title); 
  268.  
  269. $this->send_mail_successful_payment( 
  270. $order->id,  
  271. $order->get_total(),  
  272. $this->paymentInfo['txnref'] 
  273. ); 
  274. wc_add_notice('Payment successful', 'success'); 
  275. } elseif ('failed' == $this->paymentInfo['status']) { 
  276. $error_code = $this->paymentInfo['payment_status_description']; 
  277. $order->add_order_note(__('Payment Failed - ' . $error_code, 'woocommerce')); 
  278. $order->update_status('failed'); 
  279. wc_add_notice('Transaction Failed: ' . $error_code, error); 
  280. } elseif ('on-hold' == $this->paymentInfo['status']) { 
  281. $order->update_status('on-hold', sprintf(__('Payment pending: %s', 'woocommerce'), 'Amount discrepancy')); 
  282. $error_code = 'Amount discrepancy'; 
  283. // Notify admin of discrepancy in amount. 
  284. $this->send_mail_discrepancy_in_payment( 
  285. $order->id,  
  286. $order->get_total(),  
  287. $this->paymentInfo['amount'] 
  288. ); 
  289. wc_add_notice('Order on hold: ' . $error_code, error); 
  290. } else if (FALSE == $this->paymentInfo['status']) { 
  291. $order->update_status('on-hold', sprintf(__('Payment pending: %s', 'woocommerce'), $error)); 
  292. $order->add_order_note(__('Payment on-hold - ' . $error, 'woocommerce')); 
  293. $order->update_status('on-hold'); 
  294. $this->send_mail_payment_info_pending( 
  295. $order->id,  
  296. $order->billing_first_name . ' ' . $order->billing_last_name 
  297. ); 
  298. wc_add_notice('There was anerrorwhile looking up the details of your payment information. A sales person has been notified', error); 
  299.  
  300. function thankyou_page($order_id) { 
  301. $order = new WC_Order($order_id); 
  302. // All elements of $order_payment_info are arrays. So when getting the 
  303. // value get the value at the end of the array which should represent 
  304. // the most current value. 
  305. $order_payment_info = get_post_meta($order_id); 
  306. if ('completed' == $order->status || 'processing' == $order->status) { 
  307. $this->feedback_message = $this->thanks_message 
  308. . '<br/>Below are the details of your payment transaction:' 
  309. . '<br/><strong>Transaction reference:</strong> ' . end($order_payment_info['merch_txnref']) 
  310. . '<br/><strong>Customer name:</strong> ' . end($order_payment_info['name']) 
  311. . '<br/><strong>Amount paid:</strong> ' 
  312. . number_format(end($order_payment_info['amount']), 2) 
  313. . '<br/><strong>Currency:</strong> ' . end($order_payment_info['currency']) 
  314. . '<br/><strong>Payment Channel:</strong> ' . end($order_payment_info['channel']) 
  315. . '<br/><strong>GlobalPay reference:</strong> ' . end($order_payment_info['txnref']) 
  316. . '<br/><strong>Transaction status description:</strong> ' . end($order_payment_info['payment_status_description']); 
  317. } else if ('failed' == $order->status) { 
  318. $this->feedback_message = $this->error_message 
  319. . '<br/>Below are the details of your payment transaction:' 
  320. . '<br/><strong>Transaction reference:</strong> ' . end($order_payment_info['merch_txnref']) 
  321. . '<br/><strong>Customer name:</strong> ' . end($order_payment_info['name']) 
  322. . '<br/><strong>Amount paid:</strong> ' 
  323. . number_format(end($order_payment_info['amount']), 2) 
  324. . '<br/><strong>Currency:</strong> ' . end($order_payment_info['currency']) 
  325. . '<br/><strong>Payment Channel:</strong> ' . end($order_payment_info['channel']) 
  326. . '<br/><strong>GlobalPay reference:</strong> ' . end($order_payment_info['txnref']) 
  327. . '<br/><strong>Transaction status description:</strong> ' . end($order_payment_info['payment_status_description']); 
  328. } else if ('on-hold' == $order->status && TRUE == $order_payment_info['amount_discrepancy']) { 
  329. $this->feedback_message = 'Your payment was successful. ' 
  330. . 'However there was a discrepancy in the amount paid.' 
  331. . '<br/>The order amount is <strong>NGN' . number_format($order->get_total(), 2) . '</strong>' 
  332. . '<br/>while the actual amount paid is <strong>NGN' . number_format(end($order_payment_info['amount']), 2) . '</strong>' 
  333. . '<br/> A sales person has already been notified of this.<br/>' 
  334. . '<br/>Below are the details of your payment transaction:' 
  335. . '<br/><strong>Transaction reference:</strong> ' . end($order_payment_info['merch_txnref']) 
  336. . '<br/><strong>Customer name:</strong> ' . end($order_payment_info['name']) 
  337. . '<br/><strong>Amount paid:</strong> ' 
  338. . number_format(end($order_payment_info['amount']), 2) 
  339. . '<br/><strong>Currency:</strong> ' . end($order_payment_info['currency']) 
  340. . '<br/><strong>Payment Channel:</strong> ' . end($order_payment_info['channel']) 
  341. . '<br/><strong>GlobalPay reference:</strong> ' . end($order_payment_info['txnref']) 
  342. . '<br/><strong>Transaction status description:</strong> ' . end($order_payment_info['payment_status_description']); 
  343.  
  344. echo wpautop($this->feedback_message); 
  345.  
  346. function process_payment($order_id) { 
  347. $order = new WC_Order($order_id); 
  348. return array( 
  349. 'result' => 'success',  
  350. 'redirect' => $order->get_checkout_payment_url(true) 
  351. ); 
  352.  
  353. function receipt_page($order) { 
  354. echo '<p>'.__('Thank you for your order, please click the button below to pay with GlobalPay.', 'woocommerce').'</p>'; 
  355.  
  356. echo $this->generate_globalpay_form($order); 
  357.  
  358. /** 
  359. * Converts an object into an array. 
  360. * A recursive function used to convert an object (in this case a SimpleXML 
  361. * object) into an array. 
  362. * @see http://brian.moonspot.net/2008/06/03/stupid-php-tricks-normalizing-simplexml-data/ 
  363. * @param object $obj 
  364. * Object to be converted 
  365. * @return array 
  366. */ 
  367. private function make_array($obj) { 
  368. $arr = (array) $obj; 
  369.  
  370. if (!empty($arr)) { 
  371. foreach ($arr as $key => $value) { 
  372. if (!is_scalar($value)) { 
  373. $arr[$key] = $this->make_array($value); 
  374. return $arr; 
  375.  
  376. /** 
  377. * Function to be used as callback for array_walk_recursive. 
  378. */ 
  379. function fill_out_payment_info($value, $key) { 
  380. $this->paymentInfo[$key] = $value; 
  381.  
  382. /** 
  383. * Looks up transaction information from GlobalPay's lookup webservice. 
  384. * If the lookup is successful it fills out the class-level array 
  385. * $paymentInfo with the details. Otherwise it set $paymentInfo to FALSE. 
  386. * When filled out $paymentInfo should like so: 
  387. * 'status' => string 'completed' (length=10) 
  388. * 'txnref' => string '9113339143629597' (length=16) 
  389. * 'channel' => string 'mastercard' (length=10) 
  390. * 'amount' => string '2000' (length=4) 
  391. * 'payment_date' => string '12/5/2013 2:36:29 PM' (length=20) 
  392. * 'payment_status' => string 'successful' (length=10) 
  393. * 'names' => string 'John Doe' (length=19) 
  394. * 'acct_desc' => string 'False' (length=5) 
  395. * 'acct_desc_order' => string '0' (length=1) 
  396. * 'hidden' => string 'False' (length=5) 
  397. * 'xpath_field' => string '0' (length=1) 
  398. * 'currency' => string 'NGN' (length=3) 
  399. * 'email_address' => string 'john.doe@example.com' (length=29) 
  400. * 'phone_number' => string '1234567890' (length=10) 
  401. * 'merch_txnref' => string 'ref-1234567897' (length=14) 
  402. * 'payment_status_description' => string 'Transaction Successful - Approved' (length=33) 
  403. * @param stdClass $order 
  404. * @return void 
  405. */ 
  406. function get_transaction_status ($order) { 
  407. require_once 'lib/nusoap.php'; 
  408. // Clear previous payment info. 
  409. $this->paymentInfo = array(); 
  410.  
  411. if ('yes' == $this->testmode) { 
  412. $endpoint = 'https://demo.globalpay.com.ng/GlobalpayWebService_demo/service.asmx?wsdl'; 
  413. $namespace = 'https://www.eazypaynigeria.com/globalpay_demo/'; 
  414. $soap_action = 'https://www.eazypaynigeria.com/globalpay_demo/getTransactions'; 
  415. } else { 
  416. $endpoint = 'https://www.globalpay.com.ng/globalpaywebservice/service.asmx?wsdl'; 
  417. $namespace = 'https://www.eazypaynigeria.com/globalpay/'; 
  418. $soap_action = 'https://www.eazypaynigeria.com/globalpay/getTransactions'; 
  419. $soap_client = new nusoap_client($endpoint, TRUE); 
  420. if ($soap_client->getError()) { 
  421. $this->paymentInfo = FALSE; 
  422. return; 
  423.  
  424. $params = array( 
  425. 'merch_txnref' => get_post_meta($order->id, 'merch_txnref', TRUE),  
  426. 'channel' => '',  
  427. 'merchantID' => $this->merchant_id,  
  428. 'start_date' => '',  
  429. 'end_date' => '',  
  430. 'uid' => $this->webservice_user,  
  431. 'pwd' => $this->webservice_password,  
  432. 'payment_status' => '' 
  433. ); 
  434.  
  435. $this->log->add($this->id, 'Connecting to GlobalPay at ' . $endpoint); 
  436.  
  437. // Connect. 
  438. $result = $soap_client->call( 
  439. 'getTransactions',  
  440. array('parameters' => $params),  
  441. $namespace,  
  442. $soap_action,  
  443. FALSE,  
  444. TRUE 
  445. ); 
  446.  
  447. if ($soap_client->fault) { 
  448. $this->log->add($this->id, 'Error looking transaction\n' . print_r($result, TRUE)); 
  449. $this->paymentInfo = FALSE; 
  450. return; 
  451.  
  452. $err = $soap_client->getError(); 
  453. if ($err) { 
  454. $this->log->add($this->id, 'Error looking transaction\n' . print_r($err, TRUE)); 
  455. $this->paymentInfo = FALSE; 
  456. return; 
  457.  
  458. // Add all $xml's properties to $this->paymentInfo 
  459. $xml = simplexml_load_string($result['getTransactionsResult']); 
  460. $xml_arr = $this->make_array($xml); 
  461. array_walk_recursive($xml_arr, array($this, 'fill_out_payment_info')); 
  462.  
  463. $this->paymentInfo['amount_discrepancy'] = FALSE; 
  464. if ('successful' == $xml->record->payment_status) { 
  465. // If there is an amount discrepancy flag it. 
  466. if ($this->paymentInfo['amount'] != $order->get_total()) { 
  467. $this->paymentInfo['amount_discrepancy'] = TRUE; 
  468. $this->paymentInfo['status'] = 'on-hold'; 
  469. } else { 
  470. $this->paymentInfo['status'] = 'completed'; 
  471. } else { 
  472. $this->paymentInfo['status'] = 'failed'; 
  473.  
  474. // Work-around for name/names API field issue from Globalpay. 
  475. if (isset($this->paymentInfo['name'])) { 
  476. $this->paymentInfo['names'] = $this->paymentInfo['name']; 
  477. } else { 
  478. $this->paymentInfo['name'] = $this->paymentInfo['names']; 
  479.  
  480. if ('yes' == $this->debug) { 
  481. $this->log->add($this->id, 'Response dump from GlobalPay ' . print_r($result, TRUE)); 
  482.  
  483. /** 
  484. * Used by Orders interface to update payment information via AJAX. 
  485. * @param int $order_id 
  486. * @return mixed 
  487. * 'processing', 'failed' or 'on-hold' if the update was successful 
  488. * FALSE otherwise 
  489. */ 
  490. function ajax_update_paymentInfo ($order_id) { 
  491. $order = new WC_Order((int) $order_id); 
  492. $merch_txnref = get_post_meta($order->id, 'merch_txnref', TRUE); 
  493. if (!$merch_txnref) { 
  494. return FALSE; 
  495.  
  496. $this->get_transaction_status($order); 
  497.  
  498. if (FALSE == $this->paymentInfo) { 
  499. return FALSE; 
  500.  
  501. // Update the order information with the response from ISW. 
  502. foreach ($this->paymentInfo as $k => $v) { 
  503. if ('status' != $k) { 
  504. update_post_meta((int)$order->id, $k, $v); 
  505.  
  506. if ('completed' == $this->paymentInfo['status']) { 
  507. // Payment completed. 
  508. $order->add_order_note( __('Payment completed', 'woocommerce') ); 
  509. $order->payment_complete(); 
  510.  
  511. if ('yes' == $this->debug) { 
  512. $this->log->add($this->id, 'Payment complete.'); 
  513.  
  514. update_post_meta((int) $order->id, 'Payment Method', 'GlobalPay'); 
  515.  
  516. return 'completed'; 
  517. } elseif ('on-hold' == $this->paymentInfo['status']) { 
  518. $order->update_status( 
  519. 'on-hold',  
  520. sprintf( 
  521. __('Payment pending: %s', 'woocommerce'),  
  522. $this->paymentInfo['ResponseDescription'] 
  523. ); 
  524.  
  525. return 'on-hold'; 
  526. } elseif ('failed' == $this->paymentInfo['status']) { 
  527. $error_code = $this->paymentInfo['ResponseDescription']; 
  528.  
  529. $order->add_order_note(__('Payment Failed - ' . $error_code, 'woocommerce')); 
  530. $order->update_status('failed'); 
  531.  
  532. return 'failed'; 
  533.  
  534. function send_mail_discrepancy_in_payment ($order_id, $expected_amount, $amount_paid) { 
  535. $order = new WC_Order($order_id); 
  536. $currency_symbol = $order->get_order_currency(); 
  537. $orders_page = admin_url() . 'edit.php?post_type=shop_order'; 
  538. $site_name = get_bloginfo('name'); 
  539. $to = get_bloginfo('admin_email'); 
  540. $subject = "Discrepancy in payment for Order #$order_id"; 
  541. $expected_amount = number_format($expected_amount, 2); 
  542. $amount_paid = number_format($amount_paid, 2); 
  543. $name_of_user = $order->billing_first_name . ' ' . $order->billing_last_name; 
  544. $message = <<<HTML 
  545. <html> 
  546. <head> 
  547. <title>Pending payment transaction</title> 
  548. </head> 
  549. <body> 
  550. <p> 
  551. There was a discrepancy in payment transaction for order <a href = "$orders_page"><strong>$order_id</strong></a> by customer <strong>$name_of_user</strong> via GlobalPay</p> 
  552. <p> 
  553. The expected amount is <strong>$currency_symbol$expected_amount</strong> while the actual paid amount is <strong>$currency_symbol$amount_paid</strong></p> 
  554. <p> 
  555. This message was auto-generated by the GlobalPay payment plugin for Woocommerce at $site_name</p> 
  556. </body> 
  557. </html> 
  558. HTML; 
  559.  
  560. $headers[] = 'From: ' . $site_name . " <$to>"; 
  561. $headers[] = 'Content-type: text/html'; 
  562.  
  563. wp_mail($to, $subject, $message, $headers); 
  564.  
  565. function send_mail_payment_info_pending ($order_id, $customer) { 
  566. $orders_page = admin_url() . 'edit.php?post_type=shop_order'; 
  567. $site_name = get_bloginfo('name'); 
  568. $to = get_bloginfo('admin_email'); 
  569. $subject = "Order #$order_id payment information pending"; 
  570. $message = <<<HTML 
  571. <html> 
  572. <head> 
  573. <title>Pending payment transaction</title> 
  574. </head> 
  575. <body> 
  576. <h1> 
  577. Pending payment transaction</h1> 
  578. <p> 
  579. There was a problem looking up the details of the payment transaction for order <a href = "$orders_page"><strong>$order_id</strong></a> by customer <strong>$customer</strong></p> 
  580. <p> 
  581. You can resolve this by going to the orders page of Woocommerce and clicking the button labeled "R" beside the order.</p> 
  582. <p> 
  583. This message was auto-generated by the GlobalPay payment plugin for Woocommerce at $site_name</p> 
  584. </body> 
  585. </html> 
  586. HTML; 
  587.  
  588. $headers[] = 'From: ' . $site_name . " <$to>"; 
  589. $headers[] = 'Content-type: text/html'; 
  590.  
  591. wp_mail($to, $subject, $message, $headers); 
  592.  
  593. function send_mail_successful_payment ($order_id, $amount, $globalpay_ref) { 
  594. $order = new WC_Order($order_id); 
  595. if (!$order->billing_email) { 
  596. return; 
  597. $currency_symbol = $order->get_order_currency(); 
  598. $site_name = get_bloginfo('name'); 
  599. $name_of_user = $order->billing_first_name . ' ' . $order->billing_last_name; 
  600. $amount = number_format($amount, 2); 
  601.  
  602. $to = $order->billing_email; 
  603. $subject = "Successful payment for order #$order_id"; 
  604. $message = <<<HTML 
  605. <html> 
  606. <head> 
  607. <title>Payment successful</title> 
  608. </head> 
  609. <body> 
  610. <h1> 
  611. Payment successful</h1> 
  612. <p> 
  613. Dear $name_of_user, </p> 
  614. <p> 
  615. This is to inform you that your payment of <strong>$currency_symbol$amount</strong> for order <strong>$order_id</strong> via GlobalPay was successful.</p> 
  616. <p> 
  617. Your GlobalPay payment reference is <strong>$globalpay_ref</strong></p> 
  618. <p> 
  619. Thanks for purchasing at $site_name</p> 
  620. </body> 
  621. </html> 
  622.  
  623. HTML; 
  624.  
  625. $headers[] = 'From: ' . $site_name . ' <' . get_bloginfo('admin_email') . '>'; 
  626. $headers[] = 'Content-type: text/html'; 
  627.  
  628. wp_mail($to, $subject, $message, $headers); 
  629.  
  630.  
  631. /** 
  632. * Add the gateway to WooCommerce. 
  633. */ 
  634. function add_globalpay_gateway($methods) { 
  635. $methods[] = 'WC_GlobalPay'; 
  636. return $methods; 
  637.  
  638. add_filter('woocommerce_payment_gateways', 'add_globalpay_gateway');