WC_QuickPay_Base_Order

The WooCommerce QuickPay WC QuickPay Base Order class.

Defined (1)

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

/classes/base/woocommerce-quickpay-base-order.php  
  1. class WC_QuickPay_Base_Order extends WC_Order { 
  2. /** */ 
  3. const META_PAYMENT_METHOD_CHANGE_COUNT = '_quickpay_payment_method_change_count'; 
  4. /** */ 
  5. const META_FAILED_PAYMENT_COUNT = '_quickpay_failed_payment_count'; 
  6.  
  7. /** 
  8. * get_order_id_from_callback function. 
  9. * Returns the order ID based on the ID retrieved from the QuickPay callback. 
  10. * @access static public 
  11. * @param object - the callback data 
  12. * @return int 
  13. */ 
  14. public static function get_order_id_from_callback($callback_data) 
  15. // Check for the post ID reference on the response object. 
  16. // This should be available on all new orders. 
  17. if (!empty($callback_data->variables) && !empty($callback_data->variables->order_post_id)) { 
  18. return $callback_data->variables->order_post_id; 
  19. } else if (isset($_GET['order_post_id'])) { 
  20. return trim($_GET['order_post_id']); 
  21.  
  22. // Fallback 
  23. preg_match('/\d{4, }$/', $callback_data->order_id, $order_number); 
  24. $order_number = (int)end($order_number); 
  25.  
  26. return $order_number; 
  27.  
  28. /** 
  29. * get_subscription_id_from_callback function. 
  30. * Returns the subscription ID based on the ID retrieved from the QuickPay callback, if present. 
  31. * @access static public 
  32. * @param object - the callback data 
  33. * @return int 
  34. */ 
  35. public static function get_subscription_id_from_callback($callback_data) 
  36. // Check for the post ID reference on the response object. 
  37. // This should be available on all new orders. 
  38. if (!empty($callback_data->variables) && !empty($callback_data->variables->subscription_post_id)) { 
  39. return $callback_data->variables->subscription_post_id; 
  40. } else if (isset($_GET['subscription_post_id'])) { 
  41. return trim($_GET['subscription_post_id']); 
  42.  
  43. return NULL; 
  44.  
  45.  
  46.  
  47. /** 
  48. * get_payment_id function 
  49. * If the order has a payment ID, we will return it. If no ID is set we return FALSE. 
  50. * @access public 
  51. * @return string 
  52. */ 
  53. public function get_payment_id() 
  54. return get_post_meta(version_compare( WC_VERSION, '3.0', '<' ) ? $this->id : $this->get_id(), 'QUICKPAY_PAYMENT_ID', TRUE); 
  55.  
  56. /** 
  57. * set_payment_id function 
  58. * Set the payment ID on an order 
  59. * @access public 
  60. * @return void 
  61. */ 
  62. public function set_payment_id($payment_link) 
  63. update_post_meta(version_compare( WC_VERSION, '3.0', '<' ) ? $this->id : $this->get_id(), 'QUICKPAY_PAYMENT_ID', $payment_link); 
  64.  
  65. /** 
  66. * delete_payment_id function 
  67. * Delete the payment ID on an order 
  68. * @access public 
  69. * @return void 
  70. */ 
  71. public function delete_payment_id() 
  72. delete_post_meta(version_compare( WC_VERSION, '3.0', '<' ) ? $this->id : $this->get_id(), 'QUICKPAY_PAYMENT_ID'); 
  73.  
  74. /** 
  75. * get_payment_link function 
  76. * If the order has a payment link, we will return it. If no link is set we return FALSE. 
  77. * @access public 
  78. * @return string 
  79. */ 
  80. public function get_payment_link() 
  81. return get_post_meta(version_compare( WC_VERSION, '3.0', '<' ) ? $this->id : $this->get_id(), 'QUICKPAY_PAYMENT_LINK', TRUE); 
  82.  
  83. /** 
  84. * set_payment_link function 
  85. * Set the payment link on an order 
  86. * @access public 
  87. * @return void 
  88. */ 
  89. public function set_payment_link($payment_link) 
  90. update_post_meta(version_compare( WC_VERSION, '3.0', '<' ) ? $this->id : $this->get_id(), 'QUICKPAY_PAYMENT_LINK', $payment_link); 
  91.  
  92. /** 
  93. * delete_payment_link function 
  94. * Delete the payment link on an order 
  95. * @access public 
  96. * @return void 
  97. */ 
  98. public function delete_payment_link() 
  99. delete_post_meta(version_compare( WC_VERSION, '3.0', '<' ) ? $this->id : $this->get_id(), 'QUICKPAY_PAYMENT_LINK'); 
  100.  
  101. /** 
  102. * get_transaction_order_id function 
  103. * If the order has a transaction order reference, we will return it. If no transaction order reference is set we 
  104. * return FALSE. 
  105. * @access public 
  106. * @return string 
  107. */ 
  108. public function get_transaction_order_id() 
  109. return get_post_meta(version_compare( WC_VERSION, '3.0', '<' ) ? $this->id : $this->get_id(), 'TRANSACTION_ORDER_ID', TRUE); 
  110.  
  111. /** 
  112. * set_transaction_order_id function 
  113. * Set the transaction order ID on an order 
  114. * @access public 
  115. * @return void 
  116. */ 
  117. public function set_transaction_order_id($transaction_order_id) 
  118. update_post_meta(version_compare( WC_VERSION, '3.0', '<' ) ? $this->id : $this->get_id(), 'TRANSACTION_ORDER_ID', $transaction_order_id); 
  119.  
  120. /** 
  121. * add_transaction_fee function. 
  122. * Adds order transaction fee to the order before sending out the order confirmation 
  123. * @access public 
  124. * @param $fee_amount 
  125. * @return bool 
  126. */ 
  127.  
  128. public function add_transaction_fee($fee_amount) 
  129. if ($fee_amount > 0) { 
  130. $amount = $fee_amount / 100; 
  131. $fee = (object) array( 
  132. 'name' => __('Payment Fee', 'woo-quickpay'),  
  133. 'amount' => wc_format_decimal($amount),  
  134. 'taxable' => FALSE,  
  135. 'tax_class' => NULL,  
  136. 'tax_data' => array(),  
  137. 'tax' => 0,  
  138. ); 
  139.  
  140. if (version_compare( WC_VERSION, '3.0', '<' )) { 
  141. $this->add_fee($fee); 
  142. } else { 
  143.  
  144. $item = new WC_Order_Item_Fee(); 
  145. $item->set_props( array( 
  146. 'name' => $fee->name,  
  147. 'tax_class' => $fee->tax_class,  
  148. 'total' => $amount,  
  149. 'total_tax' => 0,  
  150. 'order_id' => $this->get_id(),  
  151. ) ); 
  152. $item->save(); 
  153. $this->add_item( $item ); 
  154.  
  155. $this->set_total( $this->get_total() + $amount ); 
  156.  
  157. return TRUE; 
  158. return FALSE; 
  159.  
  160. /** 
  161. * subscription_is_renewal_failure function. 
  162. * Checks if the order is currently in a failed renewal 
  163. * @access public 
  164. * @return boolean 
  165. */ 
  166. public function subscription_is_renewal_failure() 
  167. $renewal_failure = FALSE; 
  168.  
  169. if (WC_QuickPay_Subscription::plugin_is_active()) { 
  170. $renewal_failure = (WC_QuickPay_Subscription::is_renewal($this) AND $this->get_status() == 'failed'); 
  171.  
  172. return $renewal_failure; 
  173.  
  174. /** 
  175. * @param WC_Order|int $order The WC_Order object or ID of a WC_Order order. 
  176. * @return bool 
  177. */ 
  178. public function order_contains_switch() { 
  179. if (function_exists('wcs_order_contains_switch')) { 
  180. return wcs_order_contains_switch( $this ); 
  181. return FALSE; 
  182.  
  183. /** 
  184. * note function. 
  185. * Adds a custom order note 
  186. * @access public 
  187. * @return void 
  188. */ 
  189. public function note($message) 
  190. if (isset($message)) { 
  191. $this->add_order_note('QuickPay: ' . $message); 
  192.  
  193. /** 
  194. * get_transaction_params function. 
  195. * Returns the necessary basic params to send to QuickPay when creating a payment 
  196. * @access public 
  197. * @return void 
  198. */ 
  199. public function get_transaction_params() 
  200. $is_subscription = $this->contains_subscription() || $this->is_request_to_change_payment(); 
  201.  
  202. $params_subscription = array(); 
  203.  
  204. if ($is_subscription) { 
  205. $params_subscription = array( 
  206. 'description' => 'woocommerce-subscription',  
  207. ); 
  208.  
  209. $params = array_merge(array( 
  210. 'order_id' => $this->get_order_number_for_api(),  
  211. 'basket' => $this->get_transaction_basket_params(),  
  212. 'shipping_address' => $this->get_transaction_shipping_params(),  
  213. 'invoice_address' => $this->get_transaction_invoice_params(),  
  214. ), $this->get_custom_variables()); 
  215.  
  216. return array_merge($params, $params_subscription); 
  217.  
  218. /** 
  219. * Creates an array of order items formatted as "QuickPay transaction basket" format. 
  220. * @return array 
  221. */ 
  222. public function get_transaction_basket_params() 
  223. // Contains order items in QuickPay basket format 
  224. $basket = array(); 
  225.  
  226. foreach( $this->get_items() as $item_line ) { 
  227. $basket[] = $this->get_transaction_basket_params_line_helper( $item_line ); 
  228.  
  229. // Add shipping row 
  230. $basket[] = $this->get_transaction_basket_params_shipping(); 
  231.  
  232. return apply_filters( 'woocommerce_quickpay_transaction_params_basket', $basket ); 
  233.  
  234. /** 
  235. * @param $line_item 
  236. * @return array 
  237. */ 
  238. private function get_transaction_basket_params_line_helper($line_item) { 
  239. // Before WC 3.0 
  240. if (version_compare( WC_VERSION, '3.0', '<' )) { 
  241. // Get expanded meta data for the item 
  242. $item_meta = $this->expand_item_meta( $line_item ); 
  243. // Get tax rate 
  244. $product = wc_get_product( $line_item['variation_id'] ? $line_item['variation_id'] : $line_item['product_id'] ); 
  245. // Get tax rates 
  246. $taxes = WC_Tax::get_rates( $product->get_tax_class() ); 
  247. //Get rates of the product 
  248. $rates = array_shift($taxes); 
  249. //Take only the item rate and round it. 
  250. $vat_rate = round(array_shift($rates)); 
  251.  
  252. $data = array( 
  253. 'qty' => $item_meta['qty'],  
  254. 'item_no' => $item_meta['product_id'],  
  255. 'item_name' => $item_meta['name'],  
  256. 'item_price' => $product->get_price_including_tax(),  
  257. 'vat_rate' => $vat_rate,  
  258. ); 
  259. // After WC 3.0 
  260. else { 
  261. /** 
  262. * @var WC_Order_Item_Product $line_item 
  263. */ 
  264. $taxes = WC_Tax::get_rates( $line_item->get_tax_class() ); 
  265. //Get rates of the product 
  266. $rates = array_shift($taxes); 
  267. //Take only the item rate and round it. 
  268. $vat_rate = round(array_shift($rates)); 
  269.  
  270. $data = array( 
  271. 'qty' => $line_item->get_quantity(),  
  272. 'item_no' => $line_item->get_product_id(),  
  273. 'item_name' => $line_item->get_name(),  
  274. 'item_price' => wc_get_price_including_tax( $line_item->get_product() ),  
  275. 'vat_rate' => $vat_rate,  
  276. ); 
  277.  
  278. return array( 
  279. 'qty' => $data['qty'],  
  280. 'item_no' => $data['item_no'], // 
  281. 'item_name' => esc_attr($data['item_name']),  
  282. 'item_price' => WC_QuickPay_Helper::price_multiply( $data['item_price'] ),  
  283. 'vat_rate' => $data['vat_rate'] / 100 // Basket item VAT rate (ex. 0.25 for 25%) 
  284. ); 
  285.  
  286. /** 
  287. * Creates shipping basket row. 
  288. * @return array 
  289. */ 
  290. private function get_transaction_basket_params_shipping() { 
  291. $shipping_tax = $this->get_shipping_tax(); 
  292. $shipping_total = version_compare( WC_VERSION, '3.0', '<' ) ? $this->get_total_shipping() : $this->get_shipping_total(); 
  293. $shipping_incl_vat = $shipping_total; 
  294. $shipping_vat_rate = 0; 
  295.  
  296. if ($shipping_tax) { 
  297. $shipping_incl_vat += $shipping_tax; 
  298. $shipping_vat_rate = $shipping_tax / $shipping_total; // Basket item VAT rate (ex. 0.25 for 25%) 
  299.  
  300. return array( 
  301. 'qty' => 1,  
  302. 'item_no' => 0, // 
  303. 'item_name' => __('Shipping', 'woocommerce'),  
  304. 'item_price' => WC_QuickPay_Helper::price_multiply( $shipping_incl_vat ),  
  305. 'vat_rate' => $shipping_vat_rate,  
  306. ); 
  307.  
  308. public function get_transaction_invoice_params() { 
  309. $billing_first_name = version_compare( WC_VERSION, '3.0', '<' ) ? $this->billing_first_name : $this->get_billing_first_name(); 
  310. $billing_last_name = version_compare( WC_VERSION, '3.0', '<' ) ? $this->billing_last_name : $this->get_billing_last_name(); 
  311.  
  312. $params = array( 
  313. 'name' => $billing_first_name . ' ' . $billing_last_name,  
  314. 'street' => version_compare( WC_VERSION, '3.0', '<' ) ? $this->billing_address_1 : $this->get_billing_address_1(),  
  315. 'city' => version_compare( WC_VERSION, '3.0', '<' ) ? $this->billing_city : $this->get_billing_city(),  
  316. 'region' => version_compare( WC_VERSION, '3.0', '<' ) ? $this->billing_state : $this->get_billing_state(),  
  317. 'zip_code' => version_compare( WC_VERSION, '3.0', '<' ) ? $this->billing_postcode : $this->get_billing_postcode(),  
  318. 'country_code' => WC_QuickPay_Countries::getAlpha3FromAlpha2(version_compare( WC_VERSION, '3.0', '<' ) ? $this->billing_country : $this->get_billing_country()),  
  319. 'phone_number' => version_compare( WC_VERSION, '3.0', '<' ) ? $this->billing_phone : $this->get_billing_phone(),  
  320. 'email' => version_compare( WC_VERSION, '3.0', '<' ) ? $this->billing_email : $this->get_billing_email(),  
  321. ); 
  322.  
  323. return apply_filters( 'woocommerce_quickpay_transaction_params_invoice', $params ); 
  324.  
  325. public function get_transaction_shipping_params() { 
  326. $shipping_first_name = version_compare( WC_VERSION, '3.0', '<' ) ? $this->shipping_first_name : $this->get_shipping_first_name(); 
  327. $shipping_last_name = version_compare( WC_VERSION, '3.0', '<' ) ? $this->shipping_last_name : $this->get_shipping_last_name(); 
  328.  
  329. $params = array( 
  330. 'name' => $shipping_first_name . ' ' . $shipping_last_name,  
  331. 'street' => version_compare( WC_VERSION, '3.0', '<' ) ? $this->shipping_address_1 : $this->get_shipping_address_1(),  
  332. 'city' => version_compare( WC_VERSION, '3.0', '<' ) ? $this->shipping_city : $this->get_shipping_city(),  
  333. 'region' => version_compare( WC_VERSION, '3.0', '<' ) ? $this->shipping_state : $this->get_shipping_state(),  
  334. 'zip_code' => version_compare( WC_VERSION, '3.0', '<' ) ? $this->shipping_postcode : $this->get_shipping_postcode(),  
  335. 'country_code' => WC_QuickPay_Countries::getAlpha3FromAlpha2(version_compare( WC_VERSION, '3.0', '<' ) ? $this->shipping_country : $this->get_shipping_country()),  
  336. 'phone_number' => version_compare( WC_VERSION, '3.0', '<' ) ? $this->billing_phone : $this->get_billing_phone(),  
  337. 'email' => version_compare( WC_VERSION, '3.0', '<' ) ? $this->billing_email : $this->get_billing_email(),  
  338. ); 
  339.  
  340. return apply_filters( 'woocommerce_quickpay_transaction_params_shipping', $params ); 
  341.  
  342. /** 
  343. * contains_subscription function 
  344. * Checks if an order contains a subscription product 
  345. * @access public 
  346. * @return boolean 
  347. */ 
  348. public function contains_subscription() 
  349. $has_subscription = FALSE; 
  350.  
  351. if (WC_QuickPay_Subscription::plugin_is_active()) { 
  352. $has_subscription = wcs_order_contains_subscription($this); 
  353.  
  354. return $has_subscription; 
  355.  
  356. /** 
  357. * is_request_to_change_payment 
  358. * Check if the current request is trying to change the payment gateway 
  359. * @return bool 
  360. */ 
  361. public function is_request_to_change_payment() 
  362. $is_request_to_change_payment = FALSE; 
  363.  
  364. if (WC_QuickPay_Subscription::plugin_is_active()) { 
  365. $is_request_to_change_payment = WC_Subscriptions_Change_Payment_Gateway::$is_request_to_change_payment; 
  366.  
  367. return apply_filters( 'woocommerce_quickpay_is_request_to_change_payment', $is_request_to_change_payment ); 
  368.  
  369. /** 
  370. * get_order_number_for_api function. 
  371. * Prefix the order number if necessary. This is done 
  372. * because QuickPay requires the order number to contain at least 
  373. * 4 chars. 
  374. * @access public 
  375. * @return string 
  376. */ 
  377. public function get_order_number_for_api($recurring = FALSE) 
  378. $minimum_length = 4; 
  379.  
  380. $order_id = version_compare( WC_VERSION, '3.0', '<' ) ? $this->id : $this->get_id(); 
  381.  
  382. // When changing payment method on subscriptions 
  383. if (WC_QuickPay_Subscription::is_subscription($order_id)) { 
  384. $order_number = $order_id; 
  385. // On initial subscription authorizations 
  386. else if (!$this->order_contains_switch() && $this->contains_subscription() && !$recurring) { 
  387. // Find all subscriptions 
  388. $subscriptions = WC_QuickPay_Subscription::get_subscriptions_for_order($order_id); 
  389. // Get the last one and base the transaction on it. 
  390. $subscription = end($subscriptions); 
  391. // Fetch the ID of the subscription, not the parent order. 
  392. $order_number = version_compare( WC_VERSION, '3.0', '<' ) ? $subscription->id : $subscription->get_id(); 
  393.  
  394. // If an initial payment on a subscription failed (recurring payment), create a new subscription with appended ID. 
  395. if ($this->get_failed_quickpay_payment_count() > 0) { 
  396. $order_number .= sprintf( '-%d', $this->get_failed_quickpay_payment_count() ); 
  397. // On recurring / payment attempts 
  398. else { 
  399. // Normal orders - get the order number 
  400. $order_number = $this->get_clean_order_number(); 
  401. // If an initial payment on a subscription failed (recurring payment), create a new subscription with appended ID. 
  402. if ($this->get_failed_quickpay_payment_count() > 0) { 
  403. $order_number .= sprintf( '-%d', $this->get_failed_quickpay_payment_count() ); 
  404. // If manual payment of renewal, append the order number to avoid duplicate order numbers. 
  405. else if (WC_QuickPay_Subscription::cart_contains_failed_renewal_order_payment()) { 
  406. // Get the last one and base the transaction on it. 
  407. $subscription = WC_QuickPay_Subscription::get_subscriptions_for_renewal_order($this->id, TRUE); 
  408. $order_number .= sprintf( '-%d', $subscription->get_failed_payment_count() ); 
  409. // FIXME: This is for backwards compatability only. Before 4.5.6 orders were not set to 'FAILED' when a recurring payment failed. 
  410. // FIXME: To allow customers to pay the outstanding, we must append a value to the order number to avoid errors with duplicate order numbers in the API. 
  411. else if(WC_QuickPay_Subscription::cart_contains_renewal()) { 
  412. $order_number .= sprintf('-%d', time()); 
  413.  
  414. if ($this->is_request_to_change_payment()) { 
  415. $order_number .= sprintf('-%d', $this->get_payment_method_change_count() ); 
  416.  
  417. $order_number_length = strlen($order_number); 
  418.  
  419. if ($order_number_length < $minimum_length) { 
  420. preg_match('/\d+/', $order_number, $digits); 
  421.  
  422. if (!empty($digits)) { 
  423. $missing_digits = $minimum_length - $order_number_length; 
  424. $order_number = str_replace($digits[0], str_pad($digits[0], strlen($digits[0]) + $missing_digits, 0, STR_PAD_LEFT), $order_number); 
  425. return $order_number; 
  426.  
  427. /** 
  428. * Increase the amount of payment attemtps done through QuickPay 
  429. * @return int 
  430. */ 
  431. public function get_failed_quickpay_payment_count() { 
  432. $order_id = version_compare( WC_VERSION, '3.0', '<' ) ? $this->id : $this->get_id(); 
  433. $count = get_post_meta( $order_id, self::META_FAILED_PAYMENT_COUNT, TRUE ); 
  434. if (empty($count)) { 
  435. $count = 0; 
  436. return $count; 
  437.  
  438. /** 
  439. * Increase the amount of payment attemtps done through QuickPay 
  440. * @return int 
  441. */ 
  442. public function increase_failed_quickpay_payment_count() { 
  443. $order_id = version_compare( WC_VERSION, '3.0', '<' ) ? $this->id : $this->get_id(); 
  444. $count = $this->get_failed_quickpay_payment_count(); 
  445. update_post_meta( $order_id, self::META_FAILED_PAYMENT_COUNT, ++$count ); 
  446. return $count; 
  447.  
  448. /** 
  449. * Reset the failed payment attempts made through the QuickPay gateway 
  450. */ 
  451. public function reset_failed_quickpay_payment_count() { 
  452. $order_id = version_compare( WC_VERSION, '3.0', '<' ) ? $this->id : $this->get_id(); 
  453. delete_post_meta( $order_id, self::META_FAILED_PAYMENT_COUNT ); 
  454.  
  455. /** 
  456. * get_clean_order_number function 
  457. * Returns the order number without leading # 
  458. * @access public 
  459. * @return integer 
  460. */ 
  461. public function get_clean_order_number() 
  462. return str_replace('#', '', $this->get_order_number()); 
  463.  
  464. /** 
  465. * get_custom_variables function. 
  466. * Returns custom variables chosen in the gateway settings. This information will 
  467. * be sent to QuickPay and stored with the transaction. 
  468. * @access public 
  469. * @return array 
  470. */ 
  471. public function get_custom_variables() 
  472. $custom_vars_settings = (array)WC_QP()->s('quickpay_custom_variables'); 
  473. $custom_vars = array(); 
  474.  
  475. // Single: Order Email 
  476. if (in_array('customer_email', $custom_vars_settings)) { 
  477. $custom_vars[__('Customer Email', 'woo-quickpay')] = version_compare( WC_VERSION, '3.0', '<' ) ? $this->billing_email : $this->get_billing_email(); 
  478.  
  479. // Single: Order Phone 
  480. if (in_array('customer_phone', $custom_vars_settings)) { 
  481. $custom_vars[__('Customer Phone', 'woo-quickpay')] = version_compare( WC_VERSION, '3.0', '<' ) ? $this->billing_phone : $this->get_billing_phone(); 
  482.  
  483. // Single: Browser User Agent 
  484. if (in_array('browser_useragent', $custom_vars_settings)) { 
  485. $custom_vars[__('User Agent', 'woo-quickpay')] = version_compare( WC_VERSION, '3.0', '<' ) ? $this->customer_user_agent : $this->get_customer_user_agent(); 
  486.  
  487. // Single: Shipping Method 
  488. if (in_array('shipping_method', $custom_vars_settings)) { 
  489. $custom_vars[__('Shipping Method', 'woo-quickpay')] = $this->get_shipping_method(); 
  490.  
  491. // Save a POST ID reference on the transaction 
  492. $custom_vars['order_post_id'] = version_compare( WC_VERSION, '3.0', '<' ) ? $this->id : $this->get_id(); 
  493.  
  494. // Get the correct order_post_id. We want to fetch the ID of the subscription to store data on subscription (if available). 
  495. // But only on the first attempt. In case of failed auto capture on the initial order, we dont want to add the subscription ID. 
  496. // If we are handlong a product switch, we will not need this ID as we are making a regular payment. 
  497. if ( ! $this->order_contains_switch() ) { 
  498. $subscription_id = WC_QuickPay_Subscription::get_subscription_id($this); 
  499. if ($subscription_id) { 
  500. $custom_vars['subscription_post_id'] = $subscription_id; 
  501.  
  502. if ($this->is_request_to_change_payment()) { 
  503. $custom_vars['change_payment'] = TRUE; 
  504.  
  505. $custom_vars = apply_filters('woocommerce_quickpay_transaction_params_variables', $custom_vars); 
  506.  
  507. ksort($custom_vars); 
  508.  
  509. return array('variables' => $custom_vars); 
  510.  
  511. /** 
  512. * get_transaction_link_params function. 
  513. * Returns the necessary basic params to send to QuickPay when creating a payment link 
  514. * @access public 
  515. * @return array 
  516. */ 
  517. public function get_transaction_link_params() 
  518. $is_subscription = $this->contains_subscription() || $this->is_request_to_change_payment(); 
  519. $amount = $this->get_total(); 
  520.  
  521. if ($is_subscription) { 
  522. $amount = $this->get_total(); 
  523.  
  524. return array( 
  525. 'order_id' => $this->get_order_number_for_api(),  
  526. 'continueurl' => $this->get_continue_url(),  
  527. 'cancelurl' => $this->get_cancellation_url(),  
  528. 'amount' => WC_QuickPay_Helper::price_multiply($amount),  
  529. ); 
  530.  
  531.  
  532. /** 
  533. * get_continue_url function 
  534. * Returns the order's continue callback url 
  535. * @access public 
  536. * @return string 
  537. */ 
  538. public function get_continue_url() 
  539. if (method_exists($this, 'get_checkout_order_received_url')) { 
  540. return $this->get_checkout_order_received_url(); 
  541.  
  542. return add_query_arg('key', $this->order_key, add_query_arg( 
  543. 'order', version_compare( WC_VERSION, '3.0', '<' ) ? $this->id : $this->get_id(),  
  544. get_permalink(get_option('woocommerce_thanks_page_id')) 
  545. ); 
  546.  
  547. /** 
  548. * get_cancellation_url function 
  549. * Returns the order's cancellation callback url 
  550. * @access public 
  551. * @return string 
  552. */ 
  553. public function get_cancellation_url() 
  554. if (method_exists($this, 'get_cancel_order_url')) { 
  555. return str_replace('&', '&', $this->get_cancel_order_url()); 
  556.  
  557. return add_query_arg('key', $this->get_order_key(), add_query_arg( 
  558. array( 
  559. 'order' => version_compare( WC_VERSION, '3.0', '<' ) ? $this->id : $this->get_id(),  
  560. 'payment_cancellation' => 'yes',  
  561. ),  
  562. get_permalink(get_option('woocommerce_cart_page_id'))) 
  563. ); 
  564.  
  565. /** 
  566. * Determine if we should enable autocapture on the order. This is based on both the 
  567. * plugin configuration and the product types. If the order contains both virtual 
  568. * and non-virtual products, we will default to the 'quickpay_autocapture'-setting. 
  569. */ 
  570. public function get_autocapture_setting() 
  571. // Get the autocapture settings 
  572. $autocapture_default = WC_QP()->s('quickpay_autocapture'); 
  573. $autocapture_virtual = WC_QP()->s('quickpay_autocapture_virtual'); 
  574.  
  575. $has_virtual_products = FALSE; 
  576. $has_nonvirtual_products = FALSE; 
  577.  
  578. // If the two options are the same, return immediately. 
  579. if ($autocapture_default === $autocapture_virtual) { 
  580. return $autocapture_default; 
  581.  
  582. // Check order items type. 
  583. $order_items = $this->get_items('line_item'); 
  584.  
  585. // Loop through the order items 
  586. foreach ($order_items as $order_item) { 
  587. // Get the product 
  588. $product = $this->get_product_from_item($order_item); 
  589.  
  590. // Is this product virtual? 
  591. if ($product->is_virtual()) { 
  592. $has_virtual_products = TRUE; 
  593. } // This was a non-virtual product. 
  594. else { 
  595. $has_nonvirtual_products = TRUE; 
  596.  
  597. // If the order contains both virtual and nonvirtual products,  
  598. // we use the 'quickpay_autopay' as the option of choice. 
  599. if ($has_virtual_products AND $has_nonvirtual_products) { 
  600. return $autocapture_default; 
  601. } // Or check if the order contains virtual products only 
  602. else if ($has_virtual_products) { 
  603. return $autocapture_virtual; 
  604. } // Or default 
  605. else { 
  606. return $autocapture_default; 
  607.  
  608. /** 
  609. * has_quickpay_payment function 
  610. * Checks if the order is paid with the QuickPay module. 
  611. * @since 4.5.0 
  612. * @access public 
  613. * @return bool 
  614. */ 
  615. public function has_quickpay_payment() 
  616. $order_id = version_compare( WC_VERSION, '3.0', '<' ) ? $this->id : $this->get_id(); 
  617. return in_array(get_post_meta($order_id, '_payment_method', TRUE), array('quickpay', 'mobilepay', 'viabill', 'sofort', 'swipp', 'klarna')); 
  618.  
  619. /** 
  620. * Gets the amount of times the customer has updated his card. 
  621. * @return int 
  622. */ 
  623. public function get_payment_method_change_count() { 
  624. $order_id = version_compare( WC_VERSION, '3.0', '<' ) ? $this->id : $this->get_id(); 
  625. $count = get_post_meta( $order_id, self::META_PAYMENT_METHOD_CHANGE_COUNT, TRUE ); 
  626.  
  627. if( !empty( $count ) ) { 
  628. return $count; 
  629.  
  630. return 0; 
  631.  
  632. /** 
  633. * Increases the amount of times the customer has updated his card. 
  634. * @return int 
  635. */ 
  636. public function increase_payment_method_change_count() { 
  637. $count = $this->get_payment_method_change_count(); 
  638. $order_id = version_compare( WC_VERSION, '3.0', '<' ) ? $this->id : $this->get_id(); 
  639.  
  640. update_post_meta( $order_id, self::META_PAYMENT_METHOD_CHANGE_COUNT, ++$count ); 
  641.  
  642. return $count; 
  643.  
  644. /** 
  645. * @param string $context 
  646. * @return mixed|string 
  647. */ 
  648. protected function base_get_transaction_id($context = 'view') { 
  649. if (version_compare( WC_VERSION, '3.0', '<' )) { 
  650. $transaction_id = parent::get_transaction_id(); 
  651. } else { 
  652. $transaction_id = parent::get_transaction_id($context); 
  653.  
  654. $order_id = version_compare( WC_VERSION, '3.0', '<' ) ? $this->id : $this->get_id(); 
  655.  
  656. if (empty($transaction_id)) { 
  657. // Search for custom transaction meta added in 4.8 to avoid transaction ID 
  658. // sometimes being empty on subscriptions in WC 3.0. 
  659. $transaction_id = get_post_meta($order_id, '_quickpay_transaction_id', TRUE); 
  660. if (empty($transaction_id)) { 
  661. // Search for original transaction ID. The transaction might be temporarily removed by 
  662. // subscriptions. Use this one instead (if available). 
  663. $transaction_id = get_post_meta($order_id, '_transaction_id_original', TRUE); 
  664. if (empty($transaction_id)) { 
  665. // Check if the old legacy TRANSACTION ID meta value is available. 
  666. $transaction_id = get_post_meta($order_id, 'TRANSACTION_ID', TRUE); 
  667. return $transaction_id;