Changeset 2617790
- Timestamp:
- 10/21/2021 12:40:23 PM (4 years ago)
- Location:
- infobvandevliet-nl/tags
- Files:
-
- 2 edited
-
1.3.3/includes/class-wc-gateway-seerbit.php (modified) (2 diffs)
-
1.3.4/class-wc-gateway-seerbit.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
infobvandevliet-nl/tags/1.3.3/includes/class-wc-gateway-seerbit.php
r2617781 r2617790 441 441 error_log(print_r('TRANS VALIDATION RESPONSE', true)); 442 442 error_log(print_r($seerbit_response, true)); 443 if ( PaymentStatus::STATUS_SUCCESSFUL == $seerbit_response->data->code) {443 if (SeerBitStatus::STATUS_SUCCESSFUL == $seerbit_response->data->code) { 444 444 error_log(print_r('TRANS VALIDATION RESPONSE: SUCCESS', true)); 445 445 error_log(print_r($seerbit_response, true)); … … 459 459 exit; 460 460 }else if ( 461 PaymentStatus::STATUS_PENDING == $seerbit_response->data->code462 || PaymentStatus::STATUS_PENDING_2 == $seerbit_response->data->code463 || PaymentStatus::STATUS_PENDING_3 == $seerbit_response->data->code461 SeerBitStatus::STATUS_PENDING == $seerbit_response->data->code 462 || SeerBitStatus::STATUS_PENDING_2 == $seerbit_response->data->code 463 || SeerBitStatus::STATUS_PENDING_3 == $seerbit_response->data->code 464 464 ) { 465 465 //transaction pending -
infobvandevliet-nl/tags/1.3.4/class-wc-gateway-seerbit.php
r2617781 r2617790 1 1 <?php 2 2 if (!defined('ABSPATH')) { 3 exit;3 exit; 4 4 } 5 require_once ('SeerBitStatus.php'); 5 6 6 7 class WC_Gateway_Seerbit extends WC_Payment_Gateway_CC 7 8 { 8 /** 9 * Seerbit test public key. 10 * 11 * @var string 12 */ 13 public $public_key; 14 15 /** 16 * Seerbit test secret key. 17 * 18 * @var string 19 */ 20 public $secret_key; 21 /** 22 * Should custom metadata be enabled? 23 * 24 * @var bool 25 */ 26 public $custom_metadata; 27 /** 28 * Should the order id be sent as a custom metadata to Seerbit? 29 * 30 * @var bool 31 */ 32 public $meta_order_id; 33 34 /** 35 * Should the customer name be sent as a custom metadata to Seerbit? 36 * 37 * @var bool 38 */ 39 public $meta_name; 40 41 /** 42 * Should the billing email be sent as a custom metadata to Seerbit? 43 * 44 * @var bool 45 */ 46 public $meta_email; 47 48 /** 49 * Should the billing phone be sent as a custom metadata to Seerbit? 50 * 51 * @var bool 52 */ 53 public $meta_phone; 54 55 /** 56 * Should the billing address be sent as a custom metadata to Seerbit? 57 * 58 * @var bool 59 */ 60 public $meta_billing_address; 61 62 /** 63 * Should the shipping address be sent as a custom metadata to Seerbit? 64 * 65 * @var bool 66 */ 67 public $meta_shipping_address; 68 69 /** 70 * Should the order items be sent as a custom metadata to Seerbit? 71 * 72 * @var bool 73 */ 74 public $meta_products; 75 /** 76 * Constructor 77 */ 78 public function __construct() 79 { 80 $this->base_url = "https://seerbitapi.com/api/v2/"; //API URL 81 $this->website_url = "https://seerbit.com"; 82 $this->docs_url = "https://docs.seerbit.com"; 83 $this->seerbit_auth_url = "https://seerbitapi.com/sbt/api/v1/auth"; 84 $this->checkout_url = "https://checkout.seerbitapi.com/api/v1/seerbit.js"; 85 $this->order_url = "https://seerbitapi.com/api/v2/payments/order"; 86 $this->seerbit_token_encrypt_url = $this->base_url . "encrypt/keys"; 87 $this->seerbit_transaction_verify = "https://seerbitapi.com/api/v2/payments/query/"; 88 89 $this->id = 'seerbit'; 90 $this->method_title = __('Seerbit', 'seerbit-payment'); 91 $this->method_description = sprintf(__('Seerbit **********', 'seerbit-payment'), $this->website_url, $this->docs_url); 92 $this->has_fields = true; 93 94 95 // Load the form fields 96 $this->init_form_fields(); 97 // Load the settings 98 $this->init_settings(); 99 100 // Get setting values 101 $this->title = $this->get_option('title'); 102 $this->description = $this->get_option('description'); 103 $this->enabled = $this->get_option('enabled'); 104 105 $this->public_key = $this->get_option('public_key'); 106 $this->secret_key = $this->get_option('secret_key'); 107 $this->meta_products = $this->get_option('meta_products') === 'yes' ? true : true; 108 // Hooks 109 add_action('wp_enqueue_scripts', array($this, 'payment_scripts')); 110 111 add_action('admin_notices', array($this, 'admin_notices')); 9 /** 10 * Seerbit test public key. 11 * 12 * @var string 13 */ 14 public $public_key; 15 16 /** 17 * Seerbit test secret key. 18 * 19 * @var string 20 */ 21 public $secret_key; 22 /** 23 * Should custom metadata be enabled? 24 * 25 * @var bool 26 */ 27 public $custom_metadata; 28 /** 29 * Should the order id be sent as a custom metadata to Seerbit? 30 * 31 * @var bool 32 */ 33 public $meta_order_id; 34 35 /** 36 * Should the customer name be sent as a custom metadata to Seerbit? 37 * 38 * @var bool 39 */ 40 public $meta_name; 41 42 /** 43 * Should the billing email be sent as a custom metadata to Seerbit? 44 * 45 * @var bool 46 */ 47 public $meta_email; 48 49 /** 50 * Should the billing phone be sent as a custom metadata to Seerbit? 51 * 52 * @var bool 53 */ 54 public $meta_phone; 55 56 /** 57 * Should the billing address be sent as a custom metadata to Seerbit? 58 * 59 * @var bool 60 */ 61 public $meta_billing_address; 62 63 /** 64 * Should the shipping address be sent as a custom metadata to Seerbit? 65 * 66 * @var bool 67 */ 68 public $meta_shipping_address; 69 70 /** 71 * Should the order items be sent as a custom metadata to Seerbit? 72 * 73 * @var bool 74 */ 75 public $meta_products; 76 77 /** 78 * Should the order items status be moved to completed after successful transaction to Seerbit? 79 * 80 * @var bool 81 */ 82 public $auto_complete; 83 84 public $checkout_url; 85 86 public $id; 87 /** 88 * Constructor 89 */ 90 public function __construct() 91 { 92 $this->base_url = "https://seerbitapi.com/api/v2/"; //API URL 93 $this->website_url = "https://seerbit.com"; 94 $this->docs_url = "https://docs.seerbit.com"; 95 $this->seerbit_auth_url = "https://seerbitapi.com/sbt/api/v1/auth"; 96 $this->checkout_url = "https://checkout.seerbitapi.com/api/v2/seerbit.js"; 97 $this->order_url = "https://seerbitapi.com/api/v2/payments/order"; 98 $this->seerbit_token_encrypt_url = $this->base_url . "encrypt/keys"; 99 $this->seerbit_transaction_verify = "https://seerbitapi.com/api/v2/payments/query/"; 100 101 $this->id = 'seerbit'; 102 $this->method_title = __('SeerBit', 'seerbit-payment'); 103 $this->method_description = sprintf(__('SeerBit - Experience seamless payment with Card, Banking, Transfer, Mobile Money, USSD', 'seerbit-payment'), $this->website_url, $this->docs_url); 104 $this->has_fields = true; 105 106 107 // Load the form fields 108 $this->init_form_fields(); 109 // Load the settings 110 $this->init_settings(); 111 112 // Get setting values 113 $this->title = $this->get_option('title'); 114 $this->description = $this->get_option('description'); 115 $this->enabled = $this->get_option('enabled'); 116 $this->public_key = $this->get_option('public_key'); 117 $this->secret_key = $this->get_option('secret_key'); 118 $this->meta_products = $this->get_option('meta_products') === 'yes' ? true : true; 119 $this->auto_complete = $this->get_option( 'auto_complete' ) === 'yes' ? true : false; 120 121 // Hooks 122 add_action('wp_enqueue_scripts', array($this, 'payment_scripts')); 123 124 add_action('admin_notices', array($this, 'admin_notices')); 112 125 113 126 add_action( 114 'woocommerce_update_options_payment_gateways_' . $this->id,115 array($this, 'process_admin_options')116 );127 'woocommerce_update_options_payment_gateways_' . $this->id, 128 array($this, 'process_admin_options') 129 ); 117 130 118 131 add_action( 'woocommerce_api_callback', array($this, 'verify_seerbit_transaction')); 119 132 add_action('woocommerce_receipt_' . $this->id, array($this, 'receipt_page')); 120 133 121 add_action('woocommerce_api_wc_gateway_seerbit', array($this, 'verify_seerbit_transaction') ); 122 123 // Check if the gateway can be used. 124 if (!$this->is_valid_for_use()) { 125 $this->enabled = false; 126 } 127 } 128 129 130 /** 131 * Check if this gateway is enabled and available in the user's country. 132 */ 133 public function is_valid_for_use() 134 { 135 136 if (!in_array(get_woocommerce_currency(), apply_filters('woocommerce_seerbit_supported_currencies', array('NGN', 'USD', 'GBP', 'GHS')))) { 137 138 $this->msg = sprintf(__('Seerbit does not support your store currency. Kindly set it to either NGN (₦), GHS (₵), USD ($) or GBP (£) <a href="%s">here</a>', 'seerbit-payment'), admin_url('admin.php?page=wc-settings&tab=general')); 139 140 return false; 141 } 142 143 return true; 144 } 145 146 /** 147 * Display seerbit payment icon. 148 */ 149 public function get_icon() 150 { 151 152 $icon = '<img src="' . plugins_url('assets/img/seerbit-card.png', WC_SEERBIT_FILE) . '" alt="seerbit" style="width:100px" />'; 153 154 return apply_filters('woocommerce_gateway_icon', $icon, $this->id); 155 } 156 157 /** 158 * Check if Seerbit merchant details is filled. 159 */ 160 public function admin_notices() 161 { 162 if ($this->enabled == 'no') { 163 return; 164 } 165 // Check required fields. 166 if (!($this->public_key && $this->secret_key)) { 167 echo '<div class="error"><p>' . sprintf(__('Please enter your Seerbit merchant details <a href="%s">here</a> to be able to use the Seerbit WooCommerce plugin.', 'seerbit-payment'), admin_url('admin.php?page=wc-settings&tab=checkout§ion=seerbit')) . '</p></div>'; 168 return; 169 } 170 } 171 172 /** 173 * Check if Seerbit gateway is enabled. 174 * 175 * @return bool 176 */ 177 public function is_available() 178 { 179 180 if ('yes' == $this->enabled) { 181 182 if (!($this->public_key && $this->secret_key)) { 183 184 return false; 185 } 186 187 return true; 188 } 189 190 return false; 191 } 192 193 /** 194 * Admin Panel Options. 195 */ 196 public function admin_options() 197 { 198 199 ?> 200 <h2><?php _e('Seerbit', 'seerbit-payment'); ?> 201 <?php 202 if (function_exists('wc_back_link')) { 203 wc_back_link(__('Return to payments', 'seerbit-payment'), admin_url('admin.php?page=wc-settings&tab=checkout')); 204 } 205 ?> 206 </h2> 207 208 <?php 209 210 if ($this->is_valid_for_use()) { 211 212 echo '<div class="seerbit_styling">'; 213 echo '<table class="form-table">'; 214 $this->generate_settings_html(); 215 echo '</table>'; 216 echo '</div>'; 217 } else { 218 ?> 219 <div class="inline error"> 220 <p><strong><?php _e('Seerbit Payment Gateway Disabled', 'seerbit-payment'); ?></strong>: <?php echo $this->msg; ?></p> 221 </div> 222 223 <?php 224 } 225 } 226 227 /** 228 * Initialise Gateway Settings Form Fields. 229 */ 230 public function init_form_fields() 231 { 232 233 $form_fields = array( 234 'enabled' => array( 235 'title' => __('Enable/Disable', 'seerbit-payment'), 236 'label' => __('Enable Seerbit', 'seerbit-payment'), 237 'type' => 'select', 238 'default' => 'no', 239 'desc_tip' => true, 240 'options' => array( 241 'yes' => __('Yes', 'seerbit-payment'), 242 'no' => __('No', 'seerbit-payment') 243 ), 244 ), 245 'title' => array( 246 'title' => __('Title', 'seerbit-payment'), 247 'type' => 'text', 248 'default' => __('Pay with SeerBit', 'seerbit-payment'), 249 'desc_tip' => true, 250 ), 251 'description' => array( 252 'title' => __('Description', 'seerbit-payment'), 253 'type' => 'textarea', 254 'default' => __('Make payment using your debit and credit cards', 'seerbit-payment'), 255 'desc_tip' => true, 256 ), 257 'public_key' => array( 258 'title' => __('Public Key', 'seerbit-payment'), 259 'type' => 'text', 260 'default' => '', 261 ), 262 'secret_key' => array( 263 'title' => __('Secret Key', 'seerbit-payment'), 264 'type' => 'text', 265 'default' => '', 266 ), 267 'meta_products' => array( 268 'title' => __('Product(s) Orders', 'seerbit-payment'), 269 'label' => __('Send Product(s) Ordered', 'seerbit-payment'), 270 'type' => 'select', 271 'description' => __('If checked, the product(s) paid for will be sent to Seerbit', 'seerbit-payment'), 272 'default' => 'yes', 273 'desc_tip' => true, 274 'options' => array( 275 'true' => __('Yes', 'seerbit-payment'), 276 'false' => __('No', 'seerbit-payment') 277 ), 278 ) 279 ); 280 281 $this->form_fields = $form_fields; 282 } 283 284 /** 285 * Payment form on checkout page 286 */ 287 public function payment_fields() 288 { 289 290 if ($this->description) { 291 echo wpautop(wptexturize($this->description)); 292 } 293 294 if (!is_ssl()) { 295 return; 296 } 297 298 if ($this->supports('tokenization') && is_checkout() && $this->saved_cards && is_user_logged_in()) { 299 $this->tokenization_script(); 300 $this->saved_payment_methods(); 301 $this->save_payment_method_checkbox(); 302 } 303 } 304 305 /** 306 * Outputs scripts used for seerbit payment. 307 */ 308 public function payment_scripts() 309 { 310 if (!is_checkout_pay_page()) { 311 return; 312 } 313 if ($this->enabled === 'no') { 314 return; 315 } 316 317 if (isset($_GET['linkingreference'])){ 318 $this->verify_seerbit_transaction(); 319 return; 320 } 321 322 $order_key = urldecode($_GET['key']); 323 $order_id = absint(get_query_var('order-pay')); 134 135 // add_action('woocommerce_api_wc_gateway_seerbit', array($this, 'verify_seerbit_transaction') ); 136 137 // Check if the gateway can be used. 138 if (!$this->is_valid_for_use()) { 139 $this->enabled = false; 140 } 141 } 142 143 144 /** 145 * Check if this gateway is enabled and available in the user's country. 146 */ 147 public function is_valid_for_use() 148 { 149 150 if (!in_array(get_woocommerce_currency(), apply_filters('woocommerce_seerbit_supported_currencies', array('NGN', 'USD', 'GBP', 'GHS','KES','TZS','XOF')))) { 151 152 $this->msg = sprintf(__('Seerbit does not support your store currency. Kindly set it to either NGN (₦), GHS (₵), USD ($), KES, TZS, XOF or GBP (£) <a href="%s">here</a>', 'seerbit-payment'), admin_url('admin.php?page=wc-settings&tab=general')); 153 154 return false; 155 } 156 157 return true; 158 } 159 160 /** 161 * Display seerbit payment icon. 162 */ 163 public function get_icon() 164 { 165 166 $icon = '<img src="' . plugins_url('assets/img/seerbit.png', WC_SEERBIT_FILE) . '" alt="seerbit" style="width:100px;margin-top: 5px"/>'; 167 168 return apply_filters('woocommerce_gateway_icon', $icon, $this->id); 169 } 170 171 /** 172 * Check if Seerbit merchant details is filled. 173 */ 174 public function admin_notices() 175 { 176 if ($this->enabled == 'no') { 177 return; 178 } 179 // Check required fields. 180 if (!($this->public_key && $this->secret_key)) { 181 echo '<div class="error"><p>' . sprintf(__('Please enter your SeerBit merchant details <a href="%s">here</a> to be able to use the SeerBit WooCommerce plugin.', 'seerbit-payment'), admin_url('admin.php?page=wc-settings&tab=checkout§ion=seerbit')) . '</p></div>'; 182 return; 183 } 184 if ($this->auto_complete) { 185 echo '<div class="notice notice-warn is-dismissible"><p>' . __('Auto Complete order status is active. Successful transactions will automatically update linked order status to completed.', 'seerbit-payment') . '</p></div>'; 186 }else{ 187 echo ''; 188 } 189 } 190 191 192 /** 193 * Check if Seerbit gateway is enabled. 194 * 195 * @return bool 196 */ 197 public function is_available() 198 { 199 200 if ('yes' == $this->enabled) { 201 202 if (!($this->public_key && $this->secret_key)) { 203 204 return false; 205 } 206 207 return true; 208 } 209 210 return false; 211 } 212 213 /** 214 * Admin Panel Options. 215 */ 216 public function admin_options() 217 { 218 219 ?> 220 <h2><?php _e('Seerbit', 'seerbit-payment'); ?> 221 <?php 222 if (function_exists('wc_back_link')) { 223 wc_back_link(__('Return to payments', 'seerbit-payment'), admin_url('admin.php?page=wc-settings&tab=checkout')); 224 } 225 ?> 226 </h2> 227 228 <?php 229 230 if ($this->is_valid_for_use()) { 231 232 echo '<div class="seerbit_styling">'; 233 echo '<table class="form-table">'; 234 $this->generate_settings_html(); 235 echo '</table>'; 236 echo '</div>'; 237 } else { 238 ?> 239 <div class="inline error"> 240 <p><strong><?php _e('Seerbit Payment Gateway Disabled', 'seerbit-payment'); ?></strong>: <?php echo $this->msg; ?></p> 241 </div> 242 243 <?php 244 } 245 } 246 247 /** 248 * Initialise Gateway Settings Form Fields. 249 */ 250 public function init_form_fields() 251 { 252 $this->form_fields = require( dirname( __FILE__ ).'/admin/seerbit-settings.php' ); 253 } 254 255 /** 256 * Payment form on checkout page 257 */ 258 public function payment_fields() 259 { 260 261 if ($this->description) { 262 echo wpautop(wptexturize($this->description)); 263 } 264 265 if (!is_ssl()) { 266 return; 267 } 268 } 269 270 /** 271 * Outputs scripts used for seerbit payment. 272 */ 273 public function payment_scripts() 274 { 275 //check if the page is checkout or cart page 276 if ( ! is_checkout_pay_page()) { 277 return; 278 } 279 280 //check if payment option is enabled 281 if ($this->enabled === 'no') { 282 return; 283 } 284 285 //check if the merchant has set up public key 286 if ( empty( $this->public_key ) ) { 287 return; 288 } 289 290 //if checkout redirects with linkingReference, then verify the transaction 291 if (isset($_GET['reference'])){ 292 $this->verify_seerbit_transaction(); 293 return; 294 } 295 296 $order_key = urldecode($_GET['key']); 297 $order_id = absint(get_query_var('order-pay')); 324 298 325 299 if ( ! $order = wc_get_order( $order_id ) ) { … … 329 303 $payment_method = method_exists($order, 'get_payment_method') ? $order->get_payment_method() : $order->payment_method; 330 304 331 if ($this->id !== $payment_method) { 332 return; 333 } 334 335 wp_enqueue_script('jquery'); 336 wp_enqueue_script('seerbit', $this->checkout_url, array('jquery'), WC_SEERBIT_VERSION, false); 337 wp_enqueue_script('wc_seerbit', plugins_url('assets/js/seerbit.js', WC_SEERBIT_FILE), array('jquery', 'seerbit'), WC_SEERBIT_VERSION, false); 338 339 $params = array( 340 'public_key' => $this->public_key 341 ); 342 if (is_checkout_pay_page() && get_query_var('order-pay')) { 343 344 $amount = $order->get_total(); 345 $tranref = $order_id . '_' . time(); 346 $the_order_id = method_exists($order, 'get_id') ? $order->get_id() : $order->id; 347 $the_order_key = method_exists($order, 'get_order_key') ? $order->get_order_key() : $order->order_key; 348 $first_name = method_exists($order, 'get_billing_first_name') ? $order->get_billing_first_name() : $order->billing_first_name; 349 $last_name = method_exists($order, 'get_billing_last_name') ? $order->get_billing_last_name() : $order->billing_last_name; 350 $currency = get_woocommerce_currency(); 351 if ($the_order_id == $order_id && $the_order_key == $order_key) { 352 $params['tranref'] = $tranref; 353 $params['currency'] = $currency; 354 $params['country'] = $order->get_billing_country(); 355 $params['amount'] = $amount; 356 $params['customer_name'] = $first_name . ' ' . $last_name; 357 $params['phone_number'] = method_exists($order, 'get_billing_phone') ? $order->get_billing_phone() : $order->billing_phone; 358 $params['encrypted_token'] = $this->GetEncryptedToken(); 359 $params['endpoint'] = $this->order_url; 360 $params['customer_email'] = method_exists($order, 'get_billing_email') ? $order->get_billing_email() : $order->billing_email; 361 } 362 363 if ($this->meta_products) { 364 $line_items = $order->get_items(); 365 $products = array(); 366 foreach ($line_items as $item) { 367 $name = $item['name']; 368 $quantity = $item['qty']; 369 $description = $name . ' (Qty: ' . $quantity . ')'; 370 $price = $item["subtotal"]; 371 $array = array( 372 "productId" => $item['product_id'], 373 "productDescription" => $description, 374 "orderId" => $the_order_id, 375 "currency" => $currency, 376 "amount" => $price 377 ); 378 array_push($products, $array); 379 } 380 381 $params['meta_products'] = $products; 382 }; 383 update_post_meta($order_id, '_seerbit_tran_ref', $tranref); 384 } 385 wp_localize_script('wc_seerbit', 'wc_params', $params); 386 } 387 388 public function GetEncryptedToken() 389 { 390 $client_secret = $this->secret_key . "." . $this->public_key; 391 $seerbit_auth_args = 392 array( 393 'headers' => array('Content-Type' => 'application/json; charset=utf-8'), 394 'body' => json_encode(['key' => $client_secret], true), 395 'method' => 'POST' 396 ); 397 $token_request = wp_remote_post($this->seerbit_token_encrypt_url, $seerbit_auth_args); 398 $token_response = json_decode(wp_remote_retrieve_body($token_request)); 399 return $token_response; 400 } 401 /** 402 * Process the payment. 403 * 404 * @param int $order_id 405 * 406 * @return array|void 407 */ 408 public function process_payment($order_id) 409 { 410 $order = new WC_Order( $order_id ); 411 412 return array( 413 'result' => 'success', 414 'redirect' => $order->get_checkout_payment_url(true), 415 ); 416 } 417 418 /** 419 * Displays the payment page. 420 * 421 * @param $order_id 422 */ 423 public function receipt_page($order_id) 424 { 425 echo '<p>' . __('Thank you for your order, please click the button below to pay with SeerBit.', 'seerbit-payment') . '</p>'; 426 echo '<div id="seerbit_form"><form id="order_review" method="post" action="' . WC()->api_request_url('WC_Gateway_Seerbit') . '"></form><button class="button alt" id="seerbit-payment-button">' . __('Pay Now', 'seerbit-payment') . '</button> <a class="button cancel" href="' . wc_get_cart_url() . '">' . __('Cancel', 'seerbit-payment') . '</a></div>'; 427 } 428 /** 429 * Verify Seerbit payment. 430 */ 431 public function verify_seerbit_transaction() 432 { 433 @ob_clean(); 305 if ($this->id !== $payment_method) { 306 return; 307 } 308 309 wp_enqueue_script('jquery'); 310 wp_enqueue_script('seerbit', $this->checkout_url, array('jquery'), date("h:i:s"), false); 311 wp_enqueue_script('wc_seerbit', plugins_url('assets/js/seerbit.js', WC_SEERBIT_FILE), array('jquery', 'seerbit'), WC_SEERBIT_VERSION, false); 312 313 $params = array( 314 'public_key' => $this->public_key 315 ); 316 if (is_checkout_pay_page() && get_query_var('order-pay')) { 317 318 $amount = $order->get_total(); 319 $tranref = $order_id . '_' . time(); 320 $the_order_id = method_exists($order, 'get_id') ? $order->get_id() : $order->id; 321 $the_order_key = method_exists($order, 'get_order_key') ? $order->get_order_key() : $order->order_key; 322 $first_name = method_exists($order, 'get_billing_first_name') ? $order->get_billing_first_name() : $order->billing_first_name; 323 $last_name = method_exists($order, 'get_billing_last_name') ? $order->get_billing_last_name() : $order->billing_last_name; 324 $currency = get_woocommerce_currency(); 325 if ($the_order_id == $order_id && $the_order_key == $order_key) { 326 $params['tranref'] = $tranref; 327 $params['currency'] = $currency; 328 $params['country'] = $order->get_billing_country(); 329 $params['amount'] = $amount; 330 $params['customer_name'] = $first_name . ' ' . $last_name; 331 $params['phone_number'] = method_exists($order, 'get_billing_phone') ? $order->get_billing_phone() : $order->billing_phone; 332 $params['encrypted_token'] = $this->GetEncryptedToken(); 333 $params['endpoint'] = $this->order_url; 334 $params['customer_email'] = method_exists($order, 'get_billing_email') ? $order->get_billing_email() : $order->billing_email; 335 } 336 337 if ($this->meta_products) { 338 $line_items = $order->get_items(); 339 $products = array(); 340 foreach ($line_items as $item) { 341 $name = $item['name']; 342 $quantity = $item['qty']; 343 $description = $name . ' (Qty: ' . $quantity . ')'; 344 $price = $item["subtotal"]; 345 $array = array( 346 "productId" => $item['product_id'], 347 "productDescription" => $description, 348 "orderId" => $the_order_id, 349 "currency" => $currency, 350 "amount" => $price 351 ); 352 array_push($products, $array); 353 } 354 355 $params['meta_products'] = $products; 356 }; 357 update_post_meta($order_id, '_seerbit_tran_ref', $tranref); 358 } 359 wp_localize_script('wc_seerbit', 'wc_params', $params); 360 } 361 362 public function GetEncryptedToken() 363 { 364 $client_secret = $this->secret_key . "." . $this->public_key; 365 $seerbit_auth_args = 366 array( 367 'headers' => array('Content-Type' => 'application/json; charset=utf-8'), 368 'body' => json_encode(['key' => $client_secret], true), 369 'method' => 'POST' 370 ); 371 $token_request = wp_remote_post($this->seerbit_token_encrypt_url, $seerbit_auth_args); 372 $token_response = json_decode(wp_remote_retrieve_body($token_request)); 373 return $token_response; 374 } 375 /** 376 * Process the payment. 377 * 378 * @param int $order_id 379 * 380 * @return array|void 381 */ 382 public function process_payment($order_id) 383 { 384 $order = wc_get_order( $order_id ); 385 386 // Return thank you page redirect. 387 return array( 388 'result' => 'success', 389 'redirect' => $order->get_checkout_payment_url( true ), 390 ); 391 } 392 393 /** 394 * Displays the payment page. 395 * 396 * @param $order_id 397 */ 398 public function receipt_page($order_id) 399 { 400 echo '<p>' . __('Thank you for your order, please click the button below to pay with SeerBit.', 'seerbit-payment') . '</p>'; 401 echo '<div id="seerbit_form"><form id="order_review" method="post" action="' . WC()->api_request_url('WC_Gateway_Seerbit') . '"></form><button class="button alt" id="seerbit-payment-button">' . __('Pay Now', 'seerbit-payment') . '</button> <a class="button cancel" href="' . wc_get_cart_url() . '">' . __('Cancel', 'seerbit-payment') . '</a></div>'; 402 } 403 /** 404 * Verify Seerbit payment. 405 */ 406 public function verify_seerbit_transaction() 407 { 408 409 @ob_clean(); 434 410 435 411 if ( isset( $_REQUEST['reference'] ) ) { … … 444 420 445 421 if (!is_wp_error($token_request) && 200 === wp_remote_retrieve_response_code($token_request)) { 446 447 422 $token_response = json_decode(wp_remote_retrieve_body($token_request)); 448 423 $token = (string)$token_response->data->EncryptedSecKey->encryptedKey; … … 464 439 465 440 $seerbit_response = json_decode(wp_remote_retrieve_body($request)); 466 467 if ('00' == strtolower($seerbit_response->data->code)) { 441 error_log(print_r('TRANS VALIDATION RESPONSE', true)); 442 error_log(print_r($seerbit_response, true)); 443 if (SeerBitStatus::STATUS_SUCCESSFUL == $seerbit_response->data->code) { 444 error_log(print_r('TRANS VALIDATION RESPONSE: SUCCESS', true)); 445 error_log(print_r($seerbit_response, true)); 446 //transaction successful 468 447 $order_details = explode('_', $trans_ref); 469 448 $order_id = (int)$order_details[0]; 470 449 $order = wc_get_order($order_id); 471 450 472 if (in_array($order->get_status(), array('processing', 'completed', 'on-hold'))) { 473 wp_redirect($this->get_return_url($order)); 474 exit; 475 } 476 $order->payment_complete($order_id); 477 $order->add_order_note(sprintf(__('Payment via SeerBit was successful (Transaction Reference: %s)', 'seerbit-payment'), $trans_ref)); 478 WC()->cart->empty_cart(); 479 } else { 451 //check if order has been paid for already 452 if($order->needs_payment()){ 453 $order->payment_complete($order_id); 454 if ($this->auto_complete){ 455 $order->update_status('completed',sprintf(__('Payment via SeerBit was successful and order was auto completed (Transaction Reference: %s)', 'seerbit-payment'), $trans_ref)); 456 } 457 } 458 wp_redirect($this->get_return_url($order)); 459 exit; 460 }else if ( 461 SeerBitStatus::STATUS_PENDING == $seerbit_response->data->code 462 || SeerBitStatus::STATUS_PENDING_2 == $seerbit_response->data->code 463 || SeerBitStatus::STATUS_PENDING_3 == $seerbit_response->data->code 464 ) { 465 //transaction pending 466 $order_details = explode('_', $trans_ref); 467 $order_id = (int)$order_details[0]; 468 $order = wc_get_order($order_id); 469 $order->update_status('pending', __('Payment confirmation is pending from SeerBit.', 'seerbit-payment')); 470 exit; 471 } else 472 { 473 //transaction failed 480 474 $order_details = explode('_', $trans_ref); 481 475 $order_id = (int)$order_details[0]; 482 476 $order = wc_get_order($order_id); 483 477 $order->update_status('failed', __('Payment was declined by SeerBit.', 'seerbit-payment')); 478 exit; 484 479 } 480 }else{ 481 wc_add_notice( 'Please try again.', 'error' ); 482 return; 485 483 } 486 wp_redirect( $this->get_return_url( $order ) );487 exit;488 484 } 489 } 490 wp_redirect(wc_get_page_permalink('cart')); 491 exit; 492 } 493 494 /** 495 * Get custom fields to pass to Seerbit. 496 * 497 * @param int $order_id WC Order ID 498 * 499 * @return array 500 */ 501 public function get_custom_fields($order_id) 502 { 503 504 $order = wc_get_order($order_id); 505 506 $custom_fields = array(); 507 508 $custom_fields[] = array( 509 'display_name' => 'Plugin', 510 'variable_name' => 'plugin', 511 'value' => 'seerbit-payment', 512 ); 513 514 if ($this->meta_order_id) { 515 516 $custom_fields[] = array( 517 'display_name' => 'Order ID', 518 'variable_name' => 'order_id', 519 'value' => $order_id, 520 ); 521 } 522 523 if ($this->meta_name) { 524 525 $first_name = method_exists($order, 'get_billing_first_name') ? $order->get_billing_first_name() : $order->billing_first_name; 526 $last_name = method_exists($order, 'get_billing_last_name') ? $order->get_billing_last_name() : $order->billing_last_name; 527 528 $custom_fields[] = array( 529 'display_name' => 'Customer Name', 530 'variable_name' => 'customer_name', 531 'value' => $first_name . ' ' . $last_name, 532 ); 533 } 534 535 if ($this->meta_email) { 536 537 $email = method_exists($order, 'get_billing_email') ? $order->get_billing_email() : $order->billing_email; 538 539 $custom_fields[] = array( 540 'display_name' => 'Customer Email', 541 'variable_name' => 'customer_email', 542 'value' => $email, 543 ); 544 } 545 546 if ($this->meta_phone) { 547 548 $billing_phone = method_exists($order, 'get_billing_phone') ? $order->get_billing_phone() : $order->billing_phone; 549 550 $custom_fields[] = array( 551 'display_name' => 'Customer Phone', 552 'variable_name' => 'customer_phone', 553 'value' => $billing_phone, 554 ); 555 } 556 557 if ($this->meta_products) { 558 559 $line_items = $order->get_items(); 560 561 $products = ''; 562 563 foreach ($line_items as $item_id => $item) { 564 $name = $item['name']; 565 $quantity = $item['qty']; 566 $products .= $name . ' (Qty: ' . $quantity . ')'; 567 $products .= ' | '; 568 } 569 570 $products = rtrim($products, ' | '); 571 572 $custom_fields[] = array( 573 'display_name' => 'Products', 574 'variable_name' => 'products', 575 'value' => $products, 576 ); 577 } 578 579 if ($this->meta_billing_address) { 580 581 $billing_address = $order->get_formatted_billing_address(); 582 $billing_address = esc_html(preg_replace('#<br\s*/?>#i', ', ', $billing_address)); 583 584 $params['meta_billing_address'] = $billing_address; 585 586 $custom_fields[] = array( 587 'display_name' => 'Billing Address', 588 'variable_name' => 'billing_address', 589 'value' => $billing_address, 590 ); 591 } 592 593 if ($this->meta_shipping_address) { 594 595 $shipping_address = $order->get_formatted_shipping_address(); 596 $shipping_address = esc_html(preg_replace('#<br\s*/?>#i', ', ', $shipping_address)); 597 598 if (empty($shipping_address)) { 599 600 $billing_address = $order->get_formatted_billing_address(); 601 $billing_address = esc_html(preg_replace('#<br\s*/?>#i', ', ', $billing_address)); 602 603 $shipping_address = $billing_address; 604 } 605 $custom_fields[] = array( 606 'display_name' => 'Shipping Address', 607 'variable_name' => 'shipping_address', 608 'value' => $shipping_address, 609 ); 610 } 611 612 return $custom_fields; 613 } 614 /** 615 * Checks if WC version is less than passed in version. 616 * 617 * @param string $version Version to check against. 618 * 619 * @return bool 620 */ 621 public static function is_wc_lt($version) 622 { 623 return version_compare("WC_VERSION", $version, '<'); 624 } 485 wc_add_notice( 'Please try again.', 'error' ); 486 return; 487 } 488 489 wp_redirect( wc_get_page_permalink( 'cart' ) ); 490 exit; 491 } 492 493 /** 494 * Get custom fields to pass to Seerbit. 495 * 496 * @param int $order_id WC Order ID 497 * 498 * @return array 499 */ 500 public function get_custom_fields($order_id) 501 { 502 503 $order = wc_get_order($order_id); 504 505 $custom_fields = array(); 506 507 $custom_fields[] = array( 508 'display_name' => 'Plugin', 509 'variable_name' => 'plugin', 510 'value' => 'seerbit-payment', 511 ); 512 513 if ($this->meta_order_id) { 514 515 $custom_fields[] = array( 516 'display_name' => 'Order ID', 517 'variable_name' => 'order_id', 518 'value' => $order_id, 519 ); 520 } 521 522 if ($this->meta_name) { 523 524 $first_name = method_exists($order, 'get_billing_first_name') ? $order->get_billing_first_name() : $order->billing_first_name; 525 $last_name = method_exists($order, 'get_billing_last_name') ? $order->get_billing_last_name() : $order->billing_last_name; 526 527 $custom_fields[] = array( 528 'display_name' => 'Customer Name', 529 'variable_name' => 'customer_name', 530 'value' => $first_name . ' ' . $last_name, 531 ); 532 } 533 534 if ($this->meta_email) { 535 536 $email = method_exists($order, 'get_billing_email') ? $order->get_billing_email() : $order->billing_email; 537 538 $custom_fields[] = array( 539 'display_name' => 'Customer Email', 540 'variable_name' => 'customer_email', 541 'value' => $email, 542 ); 543 } 544 545 if ($this->meta_phone) { 546 547 $billing_phone = method_exists($order, 'get_billing_phone') ? $order->get_billing_phone() : $order->billing_phone; 548 549 $custom_fields[] = array( 550 'display_name' => 'Customer Phone', 551 'variable_name' => 'customer_phone', 552 'value' => $billing_phone, 553 ); 554 } 555 556 if ($this->meta_products) { 557 558 $line_items = $order->get_items(); 559 560 $products = ''; 561 562 foreach ($line_items as $item_id => $item) { 563 $name = $item['name']; 564 $quantity = $item['qty']; 565 $products .= $name . ' (Qty: ' . $quantity . ')'; 566 $products .= ' | '; 567 } 568 569 $products = rtrim($products, ' | '); 570 571 $custom_fields[] = array( 572 'display_name' => 'Products', 573 'variable_name' => 'products', 574 'value' => $products, 575 ); 576 } 577 578 if ($this->meta_billing_address) { 579 580 $billing_address = $order->get_formatted_billing_address(); 581 $billing_address = esc_html(preg_replace('#<br\s*/?>#i', ', ', $billing_address)); 582 583 $params['meta_billing_address'] = $billing_address; 584 585 $custom_fields[] = array( 586 'display_name' => 'Billing Address', 587 'variable_name' => 'billing_address', 588 'value' => $billing_address, 589 ); 590 } 591 592 if ($this->meta_shipping_address) { 593 594 $shipping_address = $order->get_formatted_shipping_address(); 595 $shipping_address = esc_html(preg_replace('#<br\s*/?>#i', ', ', $shipping_address)); 596 597 if (empty($shipping_address)) { 598 599 $billing_address = $order->get_formatted_billing_address(); 600 $billing_address = esc_html(preg_replace('#<br\s*/?>#i', ', ', $billing_address)); 601 602 $shipping_address = $billing_address; 603 } 604 $custom_fields[] = array( 605 'display_name' => 'Shipping Address', 606 'variable_name' => 'shipping_address', 607 'value' => $shipping_address, 608 ); 609 } 610 611 return $custom_fields; 612 } 613 /** 614 * Checks if WC version is less than passed in version. 615 * 616 * @param string $version Version to check against. 617 * 618 * @return bool 619 */ 620 public static function is_wc_lt($version) 621 { 622 return version_compare("WC_VERSION", $version, '<'); 623 } 625 624 }
Note: See TracChangeset
for help on using the changeset viewer.