/classes/api/woocommerce-quickpay-api-transaction.php

  1. <?php 
  2. /** 
  3. * WC_QuickPay_API_Transaction class 
  4. *  
  5. * Used for common methods shared between payments and subscriptions 
  6. * 
  7. * @class WC_QuickPay_API_Payment 
  8. * @since 4.0.0 
  9. * @package Woocommerce_QuickPay/Classes 
  10. * @category Class 
  11. * @author PerfectSolution 
  12. * @docs http://tech.quickpay.net/api/services/?scope=merchant 
  13. */ 
  14.  
  15. class WC_QuickPay_API_Transaction extends WC_QuickPay_API 
  16. /** 
  17. * get_current_type function. 
  18. *  
  19. * Returns the current payment type 
  20. * 
  21. * @access public 
  22. * @return void 
  23. */  
  24. public function get_current_type()  
  25. $last_operation = $this->get_last_operation(); 
  26.  
  27. if( ! is_object( $last_operation ) )  
  28. throw new QuickPay_API_Exception( "Malformed operation response", 0 );  
  29.  
  30. return $last_operation->type; 
  31.  
  32.  
  33. /** 
  34. * get_last_operation function. 
  35. *  
  36. * Returns the last successful transaction operation 
  37. * 
  38. * @access public 
  39. * @return void 
  40. * @throws QuickPay_API_Exception 
  41. */  
  42. public function get_last_operation()  
  43. if( ! is_object( $this->resource_data ) )  
  44. throw new QuickPay_API_Exception( 'No API payment resource data available.', 0 ); 
  45.  
  46. // Loop through all the operations and return only the operations that were successful (based on the qp_status_code and pending mode). 
  47. $successful_operations = array_filter($this->resource_data->operations, function( $operation ) { 
  48. return $operation->qp_status_code == 20000 || $operation->pending == TRUE; 
  49. } ); 
  50.  
  51. $last_operation = end( $successful_operations ); 
  52.  
  53. if( $last_operation->pending == TRUE ) { 
  54. $last_operation->type = __( 'Pending - check your QuickPay manager', 'woo-quickpay' );  
  55.  
  56. return $last_operation; 
  57.  
  58.  
  59. /** 
  60. * is_test function. 
  61. *  
  62. * Tests if a payment was made in test mode. 
  63. * 
  64. * @access public 
  65. * @return boolean 
  66. * @throws QuickPay_API_Exception 
  67. */  
  68. public function is_test()  
  69. if( ! is_object( $this->resource_data ) ) { 
  70. throw new QuickPay_API_Exception( 'No API payment resource data available.', 0 ); 
  71.  
  72. return $this->resource_data->test_mode; 
  73.  
  74. /** 
  75. * create function. 
  76. *  
  77. * Creates a new payment via the API 
  78. * 
  79. * @access public 
  80. * @param WC_QuickPay_Order $order 
  81. * @return object 
  82. * @throws QuickPay_API_Exception 
  83. */  
  84. public function create( WC_QuickPay_Order $order )  
  85. $base_params = array( 
  86. 'currency' => WC_QP()->get_gateway_currency( $order ),  
  87. 'order_post_id' => version_compare( WC_VERSION, '3.0', '<' ) ? $order->id : $order->get_id(),  
  88. ); 
  89.  
  90. $text_on_statement = WC_QP()->s('quickpay_text_on_statement'); 
  91. if (!empty($text_on_statement)) { 
  92. $base_params['text_on_statement'] = $text_on_statement; 
  93.  
  94. $order_params = $order->get_transaction_params(); 
  95.  
  96. $params = array_merge( $base_params, $order_params ); 
  97.  
  98. $payment = $this->post( '/', $params); 
  99.  
  100. return $payment; 
  101. }  
  102.  
  103.  
  104. /** 
  105. * create_link function. 
  106. *  
  107. * Creates or updates a payment link via the API 
  108. * 
  109. * @since 4.5.0  
  110. * @access public 
  111. * @param int $transaction_id 
  112. * @param WC_QuickPay_Order $order 
  113. * @return object 
  114. * @throws QuickPay_API_Exception 
  115. */  
  116. public function patch_link($transaction_id, WC_QuickPay_Order $order )  
  117. {  
  118. $cardtypelock = WC_QP()->s( 'quickpay_cardtypelock' ); 
  119.  
  120. $payment_method = strtolower(version_compare( WC_VERSION, '3.0', '<' ) ? $order->payment_method : $order->get_payment_method()); 
  121.  
  122. $base_params = array( 
  123. 'language' => WC_QP()->get_gateway_language(),  
  124. 'currency' => WC_QP()->get_gateway_currency( $order ),  
  125. 'callbackurl' => WC_QuickPay_Helper::get_callback_url(),  
  126. 'autocapture' => WC_QuickPay_Helper::option_is_enabled( $order->get_autocapture_setting() ),  
  127. 'autofee' => WC_QuickPay_Helper::option_is_enabled( WC_QP()->s( 'quickpay_autofee' ) ),  
  128. 'payment_methods' => apply_filters('woocommerce_quickpay_cardtypelock_' . $payment_method, $cardtypelock, $payment_method),  
  129. 'branding_id' => WC_QP()->s( 'quickpay_branding_id' ),  
  130. 'google_analytics_tracking_id' => WC_QP()->s( 'quickpay_google_analytics_tracking_id' ),  
  131. 'customer_email' => version_compare( WC_VERSION, '3.0', '<' ) ? $order->billing_email : $order->get_billing_email(),  
  132. ); 
  133.  
  134. $order_params = $order->get_transaction_link_params(); 
  135.  
  136. $merged_params = array_merge( $base_params, $order_params ); 
  137.  
  138. $params = apply_filters( 'woocommerce_quickpay_transaction_link_params', $merged_params, $order, $payment_method ); 
  139.  
  140. $payment_link = $this->put( sprintf( '%d/link', $transaction_id ), $params); 
  141.  
  142. return $payment_link; 
  143.  
  144.  
  145. /** 
  146. * get_cardtype function 
  147. *  
  148. * Returns the payment type / card type used on the transaction 
  149. * @since 4.5.0 
  150. * @return mixed 
  151. * @throws QuickPay_API_Exception 
  152. */ 
  153. public function get_brand() { 
  154. if( ! is_object( $this->resource_data ) ) 
  155. throw new QuickPay_API_Exception( 'No API payment resource data available.', 0 ); 
  156. return $this->resource_data->metadata->brand; 
  157.  
  158. /** 
  159. * get_balance function 
  160. * 
  161. * Returns the transaction balance 
  162. * @since 4.5.0 
  163. * @return mixed 
  164. * @throws QuickPay_API_Exception 
  165. */ 
  166. public function get_balance() { 
  167. if( ! is_object( $this->resource_data ) ) 
  168. throw new QuickPay_API_Exception( 'No API payment resource data available.', 0 ); 
  169. return ! empty($this->resource_data->balance) ? $this->resource_data->balance : NULL; 
  170.  
  171. /** 
  172. * get_formatted_balance function 
  173. * 
  174. * Returns a formatted transaction balance 
  175. * @since 4.5.0 
  176. * @return mixed 
  177. * @throws QuickPay_API_Exception 
  178. */  
  179. public function get_formatted_balance() { 
  180. return WC_QuickPay_Helper::price_normalize( $this->get_balance() ); 
  181.  
  182. /** 
  183. * get_currency function 
  184. * 
  185. * Returns a transaction currency 
  186. * @since 4.5.0 
  187. * @return mixed 
  188. * @throws QuickPay_API_Exception 
  189. */ 
  190. public function get_currency() { 
  191. if( ! is_object( $this->resource_data ) ) 
  192. throw new QuickPay_API_Exception( 'No API payment resource data available.', 0 ); 
  193. return $this->resource_data->currency; 
  194.  
  195. /** 
  196. * get_remaining_balance function 
  197. * 
  198. * Returns a remaining balance 
  199. * @since 4.5.0 
  200. * @return mixed 
  201. * @throws QuickPay_API_Exception 
  202. */  
  203. public function get_remaining_balance() { 
  204. $balance = $this->get_balance(); 
  205.  
  206. $authorized_operations = array_filter($this->resource_data->operations, function($operation) { 
  207. return 'authorize' === $operation->type; 
  208. }); 
  209.  
  210. if ( empty( $authorized_operations ) ) { 
  211. return; 
  212.  
  213. $operation = reset($authorized_operations); 
  214.  
  215. $amount = $operation->amount; 
  216.  
  217. $remaining = $amount; 
  218.  
  219. if ($balance > 0) { 
  220. $remaining = $amount - $balance; 
  221.  
  222. return $remaining; 
  223.  
  224. /** 
  225. * get_formatted_remaining_balance function 
  226. * 
  227. * Returns a formatted transaction balance 
  228. * @since 4.5.0 
  229. * @return mixed 
  230. * @throws QuickPay_API_Exception 
  231. */ 
  232. public function get_formatted_remaining_balance() { 
  233. return WC_QuickPay_Helper::price_normalize( $this->get_remaining_balance() ); 
  234.  
  235. /** 
  236. * Checks if either a specific operation or the last operation was successful. 
  237. * @param null $operation 
  238. * @return bool 
  239. * @since 4.5.0 
  240. * @throws QuickPay_API_Exception 
  241. */ 
  242. public function is_operation_approved( $operation = NULL ) { 
  243. if( ! is_object( $this->resource_data ) ) 
  244. throw new QuickPay_API_Exception( 'No API payment resource data available.', 0 ); 
  245.  
  246. if( $operation === NULL ) { 
  247. $operation = $this->get_last_operation(); 
  248.  
  249. return $this->resource_data->accepted && $operation->qp_status_code == 20000 && $operation->aq_status_code == 20000; 
  250.  
  251. /** 
  252. * get_metadata function 
  253. * 
  254. * Returns the metadata of a transaction 
  255. * @since 4.5.0 
  256. * @return mixed 
  257. * @throws QuickPay_API_Exception 
  258. */ 
  259. public function get_metadata() { 
  260. if( ! is_object( $this->resource_data ) ) 
  261. throw new QuickPay_API_Exception( 'No API payment resource data available.', 0 ); 
  262. return $this->resource_data->metadata; 
  263.  
  264. /** 
  265. * get_state function 
  266. * 
  267. * Returns the current transaction state 
  268. * @since 4.5.0 
  269. * @return mixed 
  270. * @throws QuickPay_API_Exception 
  271. */ 
  272. public function get_state() { 
  273. if( ! is_object( $this->resource_data ) ) 
  274. throw new QuickPay_API_Exception( 'No API payment resource data available.', 0 ); 
  275. return $this->resource_data->state; 
.