/includes/classes/Pay/Helper/Transaction.php

  1. <?php 
  2.  
  3. class Pay_Helper_Transaction { 
  4.  
  5. public static function newTransaction($transactionId, $opionId, $amount, $orderId, $startData, $optionSubId = null) { 
  6. global $wpdb; 
  7.  
  8. $table_name_transactions = $wpdb->prefix . "pay_transactions"; 
  9.  
  10. $wpdb->insert( 
  11. $table_name_transactions, array( 
  12. 'transaction_id' => $transactionId,  
  13. 'option_id' => $opionId,  
  14. 'option_sub_id' => $optionSubId,  
  15. 'amount' => $amount,  
  16. 'order_id' => $orderId,  
  17. 'status' => Pay_Gateways::STATUS_PENDING,  
  18. 'start_data' => $startData,  
  19. ), array( 
  20. '%s', '%d', '%d', '%d', '%d', '%s', '%s' 
  21. ); 
  22. $insertId = $wpdb->insert_id; 
  23. return $insertId; 
  24.  
  25. public static function getPaidTransactionIdForOrderId($orderId) { 
  26. global $wpdb; 
  27. $table_name_transactions = $wpdb->prefix . "pay_transactions"; 
  28. $result = $wpdb->get_results( 
  29. $wpdb->prepare("SELECT transaction_id FROM $table_name_transactions WHERE order_id = %s AND status = 'SUCCESS'", $orderId), ARRAY_A 
  30. ); 
  31. if(!empty($result)) { 
  32. return $result[0]['transaction_id']; 
  33. } else { 
  34. return false; 
  35.  
  36. private static function updateStatus($transactionId, $status) { 
  37. global $wpdb; 
  38. $table_name_transactions = $wpdb->prefix . "pay_transactions"; 
  39. $wpdb->query( 
  40. $wpdb->prepare(" 
  41. UPDATE $table_name_transactions SET status = %s WHERE transaction_id = %s 
  42. ", $status, $transactionId) 
  43. ); 
  44.  
  45. private static function getTransaction($transactionId) { 
  46. global $wpdb; 
  47.  
  48. $table_name_transactions = $wpdb->prefix . "pay_transactions"; 
  49. $result = $wpdb->get_results( 
  50. $wpdb->prepare("SELECT * FROM $table_name_transactions WHERE transaction_id = %s", $transactionId), ARRAY_A 
  51. ); 
  52.  
  53. return $result[0]; 
  54.  
  55. public static function processTransaction($transactionId, $status = null) { 
  56. global $woocommerce; 
  57.  
  58. // we gaan eerst kijken naar de status 
  59. $transaction = self::getTransaction($transactionId); 
  60.  
  61. if (empty($transaction)) { 
  62. throw new Pay_Exception(__('Transaction not found', '')); 
  63.  
  64. //order ophalen 
  65. $order = new WC_Order($transaction['order_id']); 
  66. if ($status == $transaction['status']) { 
  67. if($status == Pay_Gateways::STATUS_CANCELED) { 
  68. return add_query_arg('paynl_status', Pay_Gateways::STATUS_CANCELED, $woocommerce->cart->get_checkout_url()); 
  69. //update hoeft niet te worden doorgevoerd 
  70. return self::getOrderReturnUrl($order); 
  71.  
  72.  
  73. // huidige status ophalen bij pay 
  74. Pay_Gateway_Abstract::loginSDK(); 
  75. $transaction = \Paynl\Transaction::get($transactionId); 
  76.  
  77.  
  78. $paidCurrencyAmount = $transaction->getPaidCurrencyAmount(); 
  79.  
  80. $data = $transaction->getData(); 
  81. $apiStatus = Pay_Gateways::getStatusFromStatusId($data['paymentDetails']['state']); 
  82.  
  83. self::updateStatus($transactionId, $apiStatus); 
  84.  
  85. if($order->status == 'complete' || $order->status == 'processing') { 
  86. throw new Pay_Exception('Order is already completed'); 
  87.  
  88. // aan de hand van apistatus gaan we hem updaten 
  89. switch ($apiStatus) { 
  90. case Pay_Gateways::STATUS_SUCCESS: 
  91. $woocommerce->cart->empty_cart(); 
  92.  
  93. //checken of het bedrag klopt 
  94. if($order->get_total() != $paidCurrencyAmount) { 
  95. $order->update_status('on-hold', sprintf(__("Validation error: Paid amount does not match order amount. \npaidAmount: %s, \norderAmount: %s\n", PAYNL_WOOCOMMERCE_TEXTDOMAIN), $paidCurrencyAmount, $order->get_total())); 
  96. } else { 
  97. $order->payment_complete($transactionId); 
  98.  
  99. update_post_meta($order->get_id(), 'CustomerName', esc_attr($transaction->getAccountHolderName())); 
  100. update_post_meta($order->get_id(), 'CustomerKey', esc_attr($transaction->getAccountNumber())); 
  101.  
  102. $order->add_order_note(sprintf(__('Pay.nl: Payment complete. customerkey: %s', PAYNL_WOOCOMMERCE_TEXTDOMAIN), $transaction->getAccountNumber())); 
  103.  
  104. $url = self::getOrderReturnUrl($order); 
  105. break; 
  106. case Pay_Gateways::STATUS_CANCELED: 
  107. $order->add_order_note(__('Pay.nl: Payment canceled', PAYNL_WOOCOMMERCE_TEXTDOMAIN)); 
  108.  
  109. $url = $woocommerce->cart->get_checkout_url(); 
  110.  
  111. $url = add_query_arg('paynl_status', Pay_Gateways::STATUS_CANCELED, $url); 
  112.  
  113. break; 
  114. default: 
  115. // Pending doen we niks mee 
  116. $url = self::getOrderReturnUrl($order); 
  117. break; 
  118.  
  119. return $url; 
  120.  
  121.  
  122. public static function getOrderReturnUrl(WC_Order $order) { 
  123. // return url returnen 
  124. $return_url = $order->get_checkout_order_received_url(); 
  125. if (is_ssl() || get_option('woocommerce_force_ssl_checkout') == 'yes') { 
  126. $return_url = str_replace('http:', 'https:', $return_url); 
  127.  
  128. return apply_filters('woocommerce_get_return_url', $return_url); 
.