AngellEYE_Utility

The PayPal for WooCommerce AngellEYE Utility class.

Defined (1)

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

/angelleye-includes/angelleye-utility.php  
  1. class AngellEYE_Utility { 
  2.  
  3. private $plugin_name; 
  4. private $version; 
  5. private $paypal; 
  6. private $testmode; 
  7. private $api_username; 
  8. private $api_password; 
  9. private $api_signature; 
  10. private $ec_debug; 
  11. private $payment_method; 
  12. private $error_email_notify; 
  13. public $angelleye_woocommerce_order_actions; 
  14. public $total_Order; 
  15. public $total_DoVoid; 
  16. public $total_DoCapture; 
  17. public $total_Pending_DoAuthorization; 
  18. public $total_Completed_DoAuthorization; 
  19. public $total_DoReauthorization; 
  20. public $max_authorize_amount; 
  21. public $remain_authorize_amount; 
  22.  
  23. public function __construct($plugin_name, $version) { 
  24. $this->plugin_name = $plugin_name; 
  25. $this->version = $version; 
  26. $this->load_dependencies(); 
  27.  
  28. public function add_ec_angelleye_paypal_php_library() { 
  29. if (!class_exists('WC_Payment_Gateway')) { 
  30. return; 
  31. if (!class_exists('Angelleye_PayPal')) { 
  32. require_once( PAYPAL_FOR_WOOCOMMERCE_PLUGIN_DIR . '/classes/lib/angelleye/paypal-php-library/includes/paypal.class.php' ); 
  33. if ($this->payment_method == 'paypal_express') { 
  34. $gateway_obj = new WC_Gateway_PayPal_Express_AngellEYE(); 
  35. } else { 
  36. $gateway_obj = new WC_Gateway_PayPal_Pro_AngellEYE(); 
  37. $this->testmode = $gateway_obj->get_option('testmode'); 
  38. if ($this->testmode == true) { 
  39. $this->api_username = $gateway_obj->get_option('sandbox_api_username'); 
  40. $this->api_password = $gateway_obj->get_option('sandbox_api_password'); 
  41. $this->api_signature = $gateway_obj->get_option('sandbox_api_signature'); 
  42. } else { 
  43. $this->api_username = $gateway_obj->get_option('api_username'); 
  44. $this->api_password = $gateway_obj->get_option('api_password'); 
  45. $this->api_signature = $gateway_obj->get_option('api_signature'); 
  46. $this->error_email_notify = $gateway_obj->get_option('error_email_notify'); 
  47. $this->ec_debug = $gateway_obj->get_option('debug'); 
  48. $PayPalConfig = array( 
  49. 'Sandbox' => $this->testmode == 'yes' ? TRUE : FALSE,  
  50. 'APIUsername' => $this->api_username,  
  51. 'APIPassword' => $this->api_password,  
  52. 'APISignature' => $this->api_signature 
  53. ); 
  54. $this->paypal = new Angelleye_PayPal($PayPalConfig); 
  55.  
  56. private function load_dependencies() { 
  57. add_action('init', array($this, 'paypal_for_woocommerce_paypal_transaction_history'), 5); 
  58. if (is_admin() && !defined('DOING_AJAX')) { 
  59. add_action('add_meta_boxes', array($this, 'angelleye_paypal_for_woocommerce_order_action_meta_box'), 10, 2); 
  60. $hook_name = ''; 
  61. $payment_action_with_gateway = array('paypal_express' => array('DoAuthorization', 'DoCapture', 'DoVoid', 'DoReauthorization'), 'paypal_pro' => array('DoAuthorization', 'DoCapture', 'DoVoid')); 
  62. foreach ($payment_action_with_gateway as $payment_method_name => $payment_action_name) { 
  63. foreach ($payment_action_name as $action_name) { 
  64. $hook_name = 'wc_' . $payment_method_name . '_' . strtolower($action_name); 
  65. add_action('woocommerce_order_action_' . $hook_name, array($this, 'angelleye_' . $hook_name)); 
  66. add_filter('woocommerce_payment_gateway_supports', array($this, 'angelleye_woocommerce_payment_gateway_supports'), 10, 3); 
  67. add_action('woocommerce_process_shop_order_meta', array($this, 'save'), 51, 2); 
  68.  
  69. public function angelleye_woocommerce_order_actions($order_actions = array()) { 
  70. global $post; 
  71. $order_id = $post->ID; 
  72. $paypal_payment_action = array(); 
  73. $this->payment_method = get_post_meta($order_id, '_payment_method', true); 
  74. $payment_action = get_post_meta($order_id, '_payment_action', true); 
  75. if ((isset($this->payment_method) && !empty($this->payment_method)) && (isset($payment_action) && !empty($payment_action)) && !$this->has_authorization_expired($post->ID)) { 
  76. switch ($this->payment_method) { 
  77. case 'paypal_express': { 
  78. $paypal_payment_action = array(); 
  79. $this->total_Order = self::get_total('Order', 'Pending', $order_id); 
  80. $this->total_DoVoid = self::get_total('DoVoid', '', $order_id); 
  81. $this->total_DoCapture = self::get_total('DoCapture', 'Completed', $order_id); 
  82. $this->total_Pending_DoAuthorization = self::get_total('DoAuthorization', 'Pending', $order_id); 
  83. $this->total_Completed_DoAuthorization = self::get_total('DoAuthorization', 'Completed', $order_id); 
  84. $this->total_DoReauthorization = self::get_total('DoReauthorization', '', $order_id); 
  85. switch ($payment_action) { 
  86. case ($payment_action == 'Order'): 
  87. $this->angelleye_max_authorize_amount($order_id); 
  88. $this->angelleye_remain_authorize_amount(); 
  89. if ($this->max_authorize_amount == $this->total_DoVoid || $this->max_authorize_amount == $this->total_DoCapture) { 
  90. return $paypal_payment_action; 
  91. } else { 
  92. $paypal_payment_action = array('DoCapture' => 'Capture Authorization', 'DoVoid' => 'Void Authorization', 'DoAuthorization' => 'Authorization'); 
  93. if ($this->total_Completed_DoAuthorization == $this->total_Pending_DoAuthorization || $this->total_Pending_DoAuthorization == 0 || $this->total_Pending_DoAuthorization == $this->total_DoCapture) { 
  94. unset($paypal_payment_action['DoCapture']); 
  95. if ($this->total_Pending_DoAuthorization == 0 && $this->total_Completed_DoAuthorization > 0 || $this->total_Pending_DoAuthorization == $this->total_DoCapture) { 
  96. unset($paypal_payment_action['DoVoid']); 
  97. if ($this->max_authorize_amount == self::round($this->total_Pending_DoAuthorization + $this->total_Completed_DoAuthorization)) { 
  98. unset($paypal_payment_action['DoAuthorization']); 
  99. return $paypal_payment_action; 
  100. break; 
  101. case ($payment_action == 'Authorization'): 
  102. $paypal_payment_action = array('DoCapture' => 'Capture Authorization', 'DoReauthorization' => 'Authorization', 'DoVoid' => 'Void Authorization'); 
  103. $transaction_id = get_post_meta($order_id, '_first_transaction_id', true); 
  104. if (!$this->has_authorization_inside_honor_period($transaction_id)) { 
  105. unset($paypal_payment_action['DoReauthorization']); 
  106. if (!is_object($order_id)) { 
  107. $order = wc_get_order($order_id); 
  108. if ($order->order_total == $this->total_DoVoid || $this->total_Completed_DoAuthorization == $order->order_total || $order->order_total == $this->total_DoCapture) { 
  109. unset($paypal_payment_action['DoCapture']); 
  110. unset($paypal_payment_action['DoVoid']); 
  111. return $paypal_payment_action; 
  112. case 'paypal_pro': { 
  113. switch ($payment_action) { 
  114. case ($payment_action == 'Authorization'): 
  115. $this->total_DoVoid = self::get_total('DoVoid', '', $order_id); 
  116. $this->total_DoCapture = self::get_total('DoCapture', 'Completed', $order_id); 
  117. if ($payment_action == 'Order') { 
  118. $Authorization = 'DoAuthorization'; 
  119. } else { 
  120. $Authorization = 'authorization'; 
  121. $this->total_Pending_DoAuthorization = self::get_total($Authorization, 'Pending', $order_id); 
  122. $this->total_Completed_DoAuthorization = self::get_total($Authorization, 'Completed', $order_id); 
  123. $this->total_DoReauthorization = self::get_total('DoReauthorization', '', $order_id); 
  124. $paypal_payment_action = array('DoCapture' => 'Capture Authorization', 'DoReauthorization' => 'Reauthorization', 'DoVoid' => 'Void Authorization'); 
  125. $this->angelleye_max_authorize_amount($order_id); 
  126. $this->angelleye_remain_authorize_amount(); 
  127. $transaction_id = get_post_meta($order_id, '_first_transaction_id', true); 
  128. if (!$this->has_authorization_inside_honor_period($transaction_id)) { 
  129. unset($paypal_payment_action['DoReauthorization']); 
  130. if (!is_object($order_id)) { 
  131. $order = wc_get_order($order_id); 
  132. if ($order->order_total == $this->total_DoVoid || $this->total_Completed_DoAuthorization == $order->order_total) { 
  133. unset($paypal_payment_action['DoCapture']); 
  134. unset($paypal_payment_action['DoVoid']); 
  135. return $paypal_payment_action; 
  136. if (isset($paypal_payment_action) && !empty($paypal_payment_action)) { 
  137. foreach ($paypal_payment_action as $key => $value) { 
  138. $order_actions['wc_' . $this->payment_method . '_' . strtolower($value)] = _x($value, $value, $this->plugin_name); 
  139. return $order_actions; 
  140.  
  141. /** 
  142. * $_transaction_id, $payment_action, $gateway_name 
  143. * @param type $order_id 
  144. */ 
  145. public static function angelleye_add_order_meta($order_id, $payment_order_meta) { 
  146. foreach ($payment_order_meta as $key => $value) { 
  147. update_post_meta($order_id, $key, $value); 
  148. update_post_meta($order_id, '_trans_date', current_time('mysql')); 
  149.  
  150. /** 
  151. * @param type $order_id 
  152. * @return type 
  153. */ 
  154. public function has_authorization_expired($order_id) { 
  155. $transaction_time = strtotime(get_post_meta($order_id, '_trans_date', true)); 
  156. return floor(( time() - $transaction_time ) / 3600) > 720; 
  157.  
  158. public function has_authorization_inside_honor_period($transaction_id) { 
  159. $transaction_post_is = $this->get_post_by_title($transaction_id); 
  160. $transaction_time = strtotime(get_post_meta($transaction_post_is, '_trans_date', true)); 
  161. return floor(( time() - $transaction_time ) / 3600) > 72; 
  162.  
  163. /** 
  164. * @param type $order 
  165. */ 
  166. public function angelleye_wc_paypal_express_docapture($order) { 
  167. if (!is_object($order)) { 
  168. $order = wc_get_order($order); 
  169. // ensure the authorization is still valid for capture 
  170. if ($this->has_authorization_expired($order->id)) { 
  171. return; 
  172. if (isset($_POST['angelleye_paypal_capture_transaction_dropdown']) && !empty($_POST['angelleye_paypal_capture_transaction_dropdown'])) { 
  173. $transaction_id = $_POST['angelleye_paypal_capture_transaction_dropdown']; 
  174. } else { 
  175. $transaction_id = get_post_meta($order->id, '_transaction_id', true); 
  176. remove_action('woocommerce_order_action_wc_paypal_express_docapture', array($this, 'angelleye_wc_paypal_express_docapture')); 
  177. remove_action('woocommerce_process_shop_order_meta', 'WC_Meta_Box_Order_Data::save', 40, 2); 
  178. $this->pfw_do_capture($order, $transaction_id, $order->order_total); 
  179.  
  180. /** 
  181. * @param type $order 
  182. */ 
  183. public function angelleye_wc_paypal_pro_docapture($order) { 
  184. if (!is_object($order)) { 
  185. $order = wc_get_order($order); 
  186. // ensure the authorization is still valid for capture 
  187. if ($this->has_authorization_expired($order->id)) { 
  188. return; 
  189. $transaction_id = get_post_meta($order->id, '_transaction_id', true); 
  190. remove_action('woocommerce_order_action_wc_paypal_pro_docapture', array($this, 'angelleye_wc_paypal_pro_docapture')); 
  191. remove_action('woocommerce_process_shop_order_meta', 'WC_Meta_Box_Order_Data::save', 40, 2); 
  192. $this->pfw_do_capture($order, $transaction_id, $order->order_total); 
  193.  
  194. public function pfw_do_capture($order, $transaction_id = null, $capture_total = null) { 
  195. $this->add_ec_angelleye_paypal_php_library(); 
  196. $this->ec_add_log('DoCapture API call'); 
  197. $AMT = $this->get_amount_by_transaction_id($transaction_id); 
  198. $DataArray = array( 
  199. 'AUTHORIZATIONID' => $transaction_id,  
  200. 'AMT' => $AMT,  
  201. 'CURRENCYCODE' => get_woocommerce_currency(),  
  202. 'COMPLETETYPE' => 'NotComplete',  
  203. ); 
  204. $PayPalRequest = array( 
  205. 'DCFields' => $DataArray 
  206. ); 
  207. $do_capture_result = $this->paypal->DoCapture($PayPalRequest); 
  208. $this->angelleye_write_request_response_api_log($do_capture_result); 
  209. $ack = strtoupper($do_capture_result["ACK"]); 
  210. if ($ack == "SUCCESS" || $ack == "SUCCESSWITHWARNING") { 
  211. $order->add_order_note(__('PayPal DoCapture', 'paypal-for-woocommerce') . 
  212. ' ( Response Code: ' . $do_capture_result["ACK"] . ", " . 
  213. ' DoCapture TransactionID: ' . $do_capture_result['TRANSACTIONID'] . ' )' . 
  214. ' Authorization ID: ' . $do_capture_result['AUTHORIZATIONID'] . ' )' 
  215. ); 
  216. $order->add_order_note('Payment Action: DoCapture'); 
  217. $payerstatus_note = __('Payment Status: ', 'paypal-for-woocommerce'); 
  218. $payerstatus_note .= ucfirst($do_capture_result['PAYMENTSTATUS']); 
  219. $order->add_order_note($payerstatus_note); 
  220. if ($do_capture_result['PAYMENTSTATUS'] == 'Completed') { 
  221. $AUTHORIZATIONID = $this->get_post_by_title($transaction_id); 
  222. if ($AUTHORIZATIONID != null) { 
  223. update_post_meta($AUTHORIZATIONID, 'PAYMENTSTATUS', $do_capture_result['PAYMENTSTATUS']); 
  224. $payment_order_meta = array('_transaction_id' => $do_capture_result['TRANSACTIONID']); 
  225. self::angelleye_add_order_meta($order->id, $payment_order_meta); 
  226. self::angelleye_paypal_for_woocommerce_add_paypal_transaction($do_capture_result, $order, 'DoCapture'); 
  227. $this->angelleye_paypal_for_woocommerce_order_status_handler($order); 
  228. } else { 
  229. $ErrorCode = urldecode($do_capture_result["L_ERRORCODE0"]); 
  230. $ErrorShortMsg = urldecode($do_capture_result["L_SHORTMESSAGE0"]); 
  231. $ErrorLongMsg = urldecode($do_capture_result["L_LONGMESSAGE0"]); 
  232. $ErrorSeverityCode = urldecode($do_capture_result["L_SEVERITYCODE0"]); 
  233. $this->ec_add_log(__('PayPal DoCapture API call failed. ', 'paypal-for-woocommerce')); 
  234. $this->ec_add_log(__('Detailed Error Message: ', 'paypal-for-woocommerce') . $ErrorLongMsg); 
  235. $this->ec_add_log(__('Short Error Message: ', 'paypal-for-woocommerce') . $ErrorShortMsg); 
  236. $this->ec_add_log(__('Error Code: ', 'paypal-for-woocommerce') . $ErrorCode); 
  237. $this->ec_add_log(__('Error Severity Code: ', 'paypal-for-woocommerce') . $ErrorSeverityCode); 
  238. $order->add_order_note(__('PayPal DoCapture API call failed. ', 'paypal-for-woocommerce') . 
  239. ' ( Detailed Error Message: ' . $ErrorLongMsg . ", " . 
  240. ' Short Error Message: ' . $ErrorShortMsg . ' )' . 
  241. ' Error Code: ' . $ErrorCode . ' )' . 
  242. ' Error Severity Code: ' . $ErrorSeverityCode . ' )' 
  243. ); 
  244. $this->call_error_email_notifications($subject = 'DoCapture failed', $method_name = 'DoCapture', $resArray = $do_capture_result); 
  245.  
  246. /** 
  247. * @param type $order 
  248. */ 
  249. public function angelleye_wc_paypal_express_dovoid($order) { 
  250. $this->add_ec_angelleye_paypal_php_library(); 
  251. if (!is_object($order)) { 
  252. $order = wc_get_order($order); 
  253. // ensure the authorization is still valid for capture 
  254. if ($this->has_authorization_expired($order->id)) { 
  255. return; 
  256. remove_action('woocommerce_order_action_wc_paypal_express_dovoid', array($this, 'angelleye_wc_paypal_express_dovoid')); 
  257. remove_action('woocommerce_process_shop_order_meta', 'WC_Meta_Box_Order_Data::save', 40, 2); 
  258. $this->call_do_void($order); 
  259.  
  260. /** 
  261. * @param type $order 
  262. */ 
  263. public function angelleye_wc_paypal_pro_dovoid($order) { 
  264. $this->add_ec_angelleye_paypal_php_library(); 
  265. if (!is_object($order)) { 
  266. $order = wc_get_order($order); 
  267. // ensure the authorization is still valid for capture 
  268. if ($this->has_authorization_expired($order->id)) { 
  269. return; 
  270. remove_action('woocommerce_order_action_wc_paypal_express_dovoid', array($this, 'angelleye_wc_paypal_pro_dovoid')); 
  271. remove_action('woocommerce_process_shop_order_meta', 'WC_Meta_Box_Order_Data::save', 40, 2); 
  272. $this->call_do_void($order); 
  273.  
  274. public function call_do_void($order) { 
  275. $this->add_ec_angelleye_paypal_php_library(); 
  276. $this->ec_add_log('DoVoid API call'); 
  277. if (isset($_POST['angelleye_paypal_dovoid_transaction_dropdown']) && !empty($_POST['angelleye_paypal_dovoid_transaction_dropdown'])) { 
  278. $transaction_id = $_POST['angelleye_paypal_dovoid_transaction_dropdown']; 
  279. } else { 
  280. $transaction_id = get_post_meta($order->id, '_first_transaction_id', true); 
  281. if (isset($transaction_id) && !empty($transaction_id)) { 
  282. $DVFields = array( 
  283. 'authorizationid' => $transaction_id,  
  284. 'note' => '',  
  285. 'msgsubid' => '' 
  286. ); 
  287. $PayPalRequestData = array('DVFields' => $DVFields); 
  288. $do_void_result = $this->paypal->DoVoid($PayPalRequestData); 
  289. $this->angelleye_write_request_response_api_log($do_void_result); 
  290. $ack = strtoupper($do_void_result["ACK"]); 
  291. if ($ack == "SUCCESS" || $ack == "SUCCESSWITHWARNING") { 
  292. $order->add_order_note(__('PayPal DoVoid', 'paypal-for-woocommerce') . 
  293. ' ( Response Code: ' . $do_void_result["ACK"] . ", " . 
  294. ' DoVoid AUTHORIZATIONID: ' . $do_void_result['AUTHORIZATIONID'] . ' )' 
  295. ); 
  296. $this->angelleye_get_transactionDetails($do_void_result['AUTHORIZATIONID']); 
  297. $payment_order_meta = array('_transaction_id' => $do_void_result['AUTHORIZATIONID']); 
  298. self::angelleye_add_order_meta($order->id, $payment_order_meta); 
  299. self::angelleye_paypal_for_woocommerce_add_paypal_transaction($do_void_result, $order, 'DoVoid'); 
  300. $this->angelleye_paypal_for_woocommerce_order_status_handler($order); 
  301. } else { 
  302. $ErrorCode = urldecode($do_void_result["L_ERRORCODE0"]); 
  303. $ErrorShortMsg = urldecode($do_void_result["L_SHORTMESSAGE0"]); 
  304. $ErrorLongMsg = urldecode($do_void_result["L_LONGMESSAGE0"]); 
  305. $ErrorSeverityCode = urldecode($do_void_result["L_SEVERITYCODE0"]); 
  306. $this->ec_add_log(__('PayPal DoVoid API call failed. ', 'paypal-for-woocommerce')); 
  307. $this->ec_add_log(__('Detailed Error Message: ', 'paypal-for-woocommerce') . $ErrorLongMsg); 
  308. $this->ec_add_log(__('Short Error Message: ', 'paypal-for-woocommerce') . $ErrorShortMsg); 
  309. $this->ec_add_log(__('Error Code: ', 'paypal-for-woocommerce') . $ErrorCode); 
  310. $this->ec_add_log(__('Error Severity Code: ', 'paypal-for-woocommerce') . $ErrorSeverityCode); 
  311. $order->add_order_note(__('PayPal DoVoid API call failed. ', 'paypal-for-woocommerce') . 
  312. ' ( Detailed Error Message: ' . $ErrorLongMsg . ", " . 
  313. ' Short Error Message: ' . $ErrorShortMsg . ' )' . 
  314. ' Error Code: ' . $ErrorCode . ' )' . 
  315. ' Error Severity Code: ' . $ErrorSeverityCode . ' )' 
  316. ); 
  317. $this->call_error_email_notifications($subject = 'DoVoid failed', $method_name = 'DoVoid', $resArray = $do_void_result); 
  318.  
  319. /** 
  320. * @param type $order 
  321. */ 
  322. public function angelleye_wc_paypal_express_doreauthorization($order) { 
  323. if (!is_object($order)) { 
  324. $order = wc_get_order($order); 
  325. $this->payment_method = get_post_meta($order->id, '_payment_method', true); 
  326. remove_action('woocommerce_order_action_wc_paypal_express_doreauthorization', array($this, 'angelleye_wc_paypal_express_doreauthorization')); 
  327. remove_action('woocommerce_process_shop_order_meta', 'WC_Meta_Box_Order_Data::save', 40, 2); 
  328. $this->call_do_reauthorization($order); 
  329.  
  330. public function call_do_reauthorization($order) { 
  331. $this->add_ec_angelleye_paypal_php_library(); 
  332. $this->ec_add_log('DoReauthorization API call'); 
  333. if (isset($_POST['angelleye_paypal_doreauthorization_transaction_dropdown']) && !empty($_POST['angelleye_paypal_doreauthorization_transaction_dropdown'])) { 
  334. $transaction_id = $_POST['angelleye_paypal_doreauthorization_transaction_dropdown']; 
  335. } else { 
  336. $transaction_id = get_post_meta($order->id, '_first_transaction_id', true); 
  337. $AMT = $this->get_amount_by_transaction_id($transaction_id); 
  338. if (isset($transaction_id) && !empty($transaction_id)) { 
  339. $DRFields = array( 
  340. 'authorizationid' => $transaction_id, // Required. The value of a previously authorized transaction ID returned by PayPal. 
  341. 'amt' => $AMT, // Required. Must have two decimal places. Decimal separator must be a period (.) and optional thousands separator must be a comma (, ) 
  342. 'currencycode' => get_woocommerce_currency(), // Three-character currency code. 
  343. 'msgsubid' => '' // A message ID used for idempotence to uniquely identify a message. 
  344. ); 
  345. $PayPalRequestData = array('DRFields' => $DRFields); 
  346. $do_reauthorization_result = $this->paypal->DoReauthorization($PayPalRequestData); 
  347. $this->angelleye_write_request_response_api_log($do_reauthorization_result); 
  348. $ack = strtoupper($do_reauthorization_result["ACK"]); 
  349. if ($ack == "SUCCESS" || $ack == "SUCCESSWITHWARNING") { 
  350. $order->add_order_note(__('PayPal DoReauthorization', 'paypal-for-woocommerce') . 
  351. ' ( Response Code: ' . $do_reauthorization_result["ACK"] . ", " . 
  352. ' DoReauthorization AUTHORIZATIONID: ' . $do_reauthorization_result['AUTHORIZATIONID'] . ' )' 
  353. ); 
  354. $payment_order_meta = array('_transaction_id' => $do_reauthorization_result['AUTHORIZATIONID']); 
  355. self::angelleye_add_order_meta($order->id, $payment_order_meta); 
  356. self::angelleye_paypal_for_woocommerce_add_paypal_transaction($do_reauthorization_result, $order, 'DoReauthorization'); 
  357. } else { 
  358. $ErrorCode = urldecode($do_reauthorization_result["L_ERRORCODE0"]); 
  359. $ErrorShortMsg = urldecode($do_reauthorization_result["L_SHORTMESSAGE0"]); 
  360. $ErrorLongMsg = urldecode($do_reauthorization_result["L_LONGMESSAGE0"]); 
  361. $ErrorSeverityCode = urldecode($do_reauthorization_result["L_SEVERITYCODE0"]); 
  362. $this->ec_add_log(__('PayPal DoReauthorization API call failed. ', 'paypal-for-woocommerce')); 
  363. $this->ec_add_log(__('Detailed Error Message: ', 'paypal-for-woocommerce') . $ErrorLongMsg); 
  364. $this->ec_add_log(__('Short Error Message: ', 'paypal-for-woocommerce') . $ErrorShortMsg); 
  365. $this->ec_add_log(__('Error Code: ', 'paypal-for-woocommerce') . $ErrorCode); 
  366. $this->ec_add_log(__('Error Severity Code: ', 'paypal-for-woocommerce') . $ErrorSeverityCode); 
  367. $order->add_order_note(__('PayPal DoReauthorization API call failed. ', $this->plugin_name) . 
  368. ' ( Detailed Error Message: ' . $ErrorLongMsg . ", " . 
  369. ' Short Error Message: ' . $ErrorShortMsg . ' )' . 
  370. ' Error Code: ' . $ErrorCode . ' )' . 
  371. ' Error Severity Code: ' . $ErrorSeverityCode . ' )' 
  372. ); 
  373. $this->call_error_email_notifications($subject = 'DoReauthorization failed', $method_name = 'DoReauthorization', $resArray = $do_reauthorization_result); 
  374.  
  375. /** 
  376. * @param type $order 
  377. */ 
  378. public function angelleye_wc_paypal_pro_doreauthorization($order) { 
  379. $this->add_ec_angelleye_paypal_php_library(); 
  380. if (!is_object($order)) { 
  381. $order = wc_get_order($order); 
  382. remove_action('woocommerce_order_action_wc_paypal_pro_doreauthorization', array($this, 'angelleye_wc_paypal_pro_doreauthorization')); 
  383. remove_action('woocommerce_process_shop_order_meta', 'WC_Meta_Box_Order_Data::save', 40, 2); 
  384. $this->call_do_reauthorization($order); 
  385.  
  386. /** 
  387. * @param type $order 
  388. */ 
  389. public function angelleye_wc_paypal_express_doauthorization($order) { 
  390. $this->add_ec_angelleye_paypal_php_library(); 
  391. if (!is_object($order)) { 
  392. $order = wc_get_order($order); 
  393. remove_action('woocommerce_order_action_wc_paypal_express_doauthorization', array($this, 'angelleye_wc_paypal_express_doauthorization')); 
  394. remove_action('woocommerce_process_shop_order_meta', 'WC_Meta_Box_Order_Data::save', 40, 2); 
  395. $this->call_do_authorization($order); 
  396.  
  397. public function call_do_authorization($order) { 
  398. $this->add_ec_angelleye_paypal_php_library(); 
  399. $this->ec_add_log('DoAuthorization API call'); 
  400. $transaction_id = get_post_meta($order->id, '_first_transaction_id', true); 
  401. if (isset($transaction_id) && !empty($transaction_id)) { 
  402. $DRFields = array( 
  403. 'TRANSACTIONID' => $transaction_id, // Required. The value of a previously authorized transaction ID returned by PayPal. 
  404. 'AMT' => $_POST['_regular_price'], // Required. Must have two decimal places. Decimal separator must be a period (.) and optional thousands separator must be a comma (, ) 
  405. 'CURRENCYCODE' => get_woocommerce_currency() 
  406. ); 
  407. $PayPalRequestData = array('DAFields' => $DRFields); 
  408. $do_authorization_result = $this->paypal->DoAuthorization($PayPalRequestData); 
  409. $this->angelleye_write_request_response_api_log($do_authorization_result); 
  410. $ack = strtoupper($do_authorization_result["ACK"]); 
  411. if ($ack == "SUCCESS" || $ack == "SUCCESSWITHWARNING") { 
  412. $order->add_order_note(__('PayPal authorization', 'paypal-for-woocommerce') . 
  413. ' ( Response Code: ' . $do_authorization_result["ACK"] . ", " . 
  414. ' DoAuthorization AUTHORIZATIONID: ' . $do_authorization_result['TRANSACTIONID'] . ' )' 
  415. ); 
  416. $payment_order_meta = array('_transaction_id' => $do_authorization_result['TRANSACTIONID']); 
  417. self::angelleye_add_order_meta($order->id, $payment_order_meta); 
  418. self::angelleye_paypal_for_woocommerce_add_paypal_transaction($do_authorization_result, $order, 'DoAuthorization'); 
  419. } else { 
  420. $ErrorCode = urldecode($do_authorization_result["L_ERRORCODE0"]); 
  421. $ErrorShortMsg = urldecode($do_authorization_result["L_SHORTMESSAGE0"]); 
  422. $ErrorLongMsg = urldecode($do_authorization_result["L_LONGMESSAGE0"]); 
  423. $ErrorSeverityCode = urldecode($do_authorization_result["L_SEVERITYCODE0"]); 
  424. $this->ec_add_log(__('PayPal DoAuthorization API call failed. ', 'paypal-for-woocommerce')); 
  425. $this->ec_add_log(__('Detailed Error Message: ', 'paypal-for-woocommerce') . $ErrorLongMsg); 
  426. $this->ec_add_log(__('Short Error Message: ', 'paypal-for-woocommerce') . $ErrorShortMsg); 
  427. $this->ec_add_log(__('Error Code: ', 'paypal-for-woocommerce') . $ErrorCode); 
  428. $this->ec_add_log(__('Error Severity Code: ', 'paypal-for-woocommerce') . $ErrorSeverityCode); 
  429. $order->add_order_note(__('PayPal DoAuthorization API call failed. ', 'paypal-for-woocommerce') . 
  430. ' ( Detailed Error Message: ' . $ErrorLongMsg . ", " . 
  431. ' Short Error Message: ' . $ErrorShortMsg . ' )' . 
  432. ' Error Code: ' . $ErrorCode . ' )' . 
  433. ' Error Severity Code: ' . $ErrorSeverityCode . ' )' 
  434. ); 
  435. $this->call_error_email_notifications($subject = 'DoAuthorization failed', $method_name = 'DoAuthorization', $resArray = $do_authorization_result); 
  436.  
  437. public function ec_add_log($message) { 
  438. if ($this->ec_debug == 'yes') { 
  439. if (empty($this->log)) 
  440. $this->log = new WC_Logger(); 
  441. $this->log->add($this->payment_method, $message); 
  442.  
  443. public function call_error_email_notifications($subject = null, $method_name = null, $resArray = null) { 
  444. if ((isset($resArray["L_ERRORCODE0"]) && !empty($resArray["L_ERRORCODE0"])) && ( isset($resArray["L_SHORTMESSAGE0"]) && !empty($resArray["L_SHORTMESSAGE0"]))) { 
  445. $ErrorCode = urldecode($resArray["L_ERRORCODE0"]); 
  446. $ErrorShortMsg = urldecode($resArray["L_SHORTMESSAGE0"]); 
  447. $ErrorLongMsg = urldecode($resArray["L_LONGMESSAGE0"]); 
  448. $ErrorSeverityCode = urldecode($resArray["L_SEVERITYCODE0"]); 
  449. $this->ec_add_log(__($method_name . ' API call failed. ', 'paypal-for-woocommerce')); 
  450. $this->ec_add_log(__('Detailed Error Message: ', 'paypal-for-woocommerce') . $ErrorLongMsg); 
  451. $this->ec_add_log(__('Short Error Message: ', 'paypal-for-woocommerce') . $ErrorShortMsg); 
  452. $this->ec_add_log(__('Error Code: ', 'paypal-for-woocommerce') . $ErrorCode); 
  453. $this->ec_add_log(__('Error Severity Code: ', 'paypal-for-woocommerce') . $ErrorSeverityCode); 
  454. $message = ''; 
  455. if ($this->error_email_notify) { 
  456. $admin_email = get_option("admin_email"); 
  457. $message .= __($method_name, "paypal-for-woocommerce") . "\n\n"; 
  458. $message .= __('Error Code: ', 'paypal-for-woocommerce') . $ErrorCode . "\n"; 
  459. $message .= __('Error Severity Code: ', 'paypal-for-woocommerce') . $ErrorSeverityCode . "\n"; 
  460. $message .= __('Short Error Message: ', 'paypal-for-woocommerce') . $ErrorShortMsg . "\n"; 
  461. $message .= __('Detailed Error Message: ', 'paypal-for-woocommerce') . $ErrorLongMsg . "\n"; 
  462. $ofw_error_email_notify_mes = apply_filters('angelleye_error_email_notify_message', $message, $ErrorCode, $ErrorSeverityCode, $ErrorShortMsg, $ErrorLongMsg); 
  463. $ofw_error_email_notify_subject = apply_filters('angelleye_error_email_notify_subject', $subject); 
  464. wp_mail($admin_email, $ofw_error_email_notify_subject, $ofw_error_email_notify_mes); 
  465. if ((isset($resArray["Errors"][0]['ErrorID']) && !empty($resArray["Errors"][0]['ErrorID'])) && ( isset($resArray["Errors"][0]['Message']) && !empty($resArray["Errors"][0]['Message']))) { 
  466. $ErrorCode = $resArray["Errors"][0]['ErrorID']; 
  467. $ErrorShortMsg = $resArray["Errors"][0]['Message']; 
  468. $this->ec_add_log(__($method_name . ' API call failed. ', 'paypal-for-woocommerce')); 
  469. $this->ec_add_log(__('Short Error Message: ', 'paypal-for-woocommerce') . $ErrorShortMsg); 
  470. $this->ec_add_log(__('Error Code: ', 'paypal-for-woocommerce') . $ErrorCode); 
  471. $message = ''; 
  472. if ($this->error_email_notify) { 
  473. $admin_email = get_option("admin_email"); 
  474. $message .= __($method_name, "paypal-for-woocommerce") . "\n\n"; 
  475. $message .= __('Error Code: ', 'paypal-for-woocommerce') . $ErrorCode . "\n"; 
  476. $message .= __('Short Error Message: ', 'paypal-for-woocommerce') . $ErrorShortMsg . "\n"; 
  477. $ofw_error_email_notify_mes = apply_filters('angelleye_error_email_notify_message', $message, $ErrorCode, $ErrorShortMsg); 
  478. $ofw_error_email_notify_subject = apply_filters('angelleye_error_email_notify_subject', $subject); 
  479. wp_mail($admin_email, $ofw_error_email_notify_subject, $ofw_error_email_notify_mes); 
  480.  
  481. public function angelleye_woocommerce_payment_gateway_supports($boolean, $feature, $current) { 
  482. global $post; 
  483. $order_id = $post->ID; 
  484. $payment_action = ''; 
  485. if ($current->id == 'paypal_express' || $current->id == 'paypal_pro') { 
  486. $payment_action = get_post_meta($order_id, '_payment_action', true); 
  487. if ($payment_action == 'Sale' || $payment_action == 'DoCapture' || empty($payment_action)) { 
  488. return $boolean; 
  489. } else { 
  490. return false; 
  491. } else { 
  492. return $boolean; 
  493.  
  494. public function angelleye_write_request_response_api_log($PayPalResult) { 
  495. $PayPalRequest = isset($PayPalResult['RAWREQUEST']) ? $PayPalResult['RAWREQUEST'] : ''; 
  496. $PayPalResponse = isset($PayPalResult['RAWRESPONSE']) ? $PayPalResult['RAWRESPONSE'] : ''; 
  497. $this->ec_add_log('Request: ' . print_r($this->paypal->NVPToArray($this->paypal->MaskAPIResult($PayPalRequest)), true)); 
  498. $this->ec_add_log('Response: ' . print_r($this->paypal->NVPToArray($this->paypal->MaskAPIResult($PayPalResponse)), true)); 
  499.  
  500. public static function angelleye_paypal_credit_card_rest_setting_fields() { 
  501. return array( 
  502. 'enabled' => array( 
  503. 'title' => __('Enable/Disable', 'paypal-for-woocommerce'),  
  504. 'type' => 'checkbox',  
  505. 'label' => __('Enable PayPal Credit Card (REST)', 'paypal-for-woocommerce'),  
  506. 'default' => 'no' 
  507. ),  
  508. 'title' => array( 
  509. 'title' => __('Title', 'paypal-for-woocommerce'),  
  510. 'type' => 'text',  
  511. 'description' => __('This controls the title which the user sees during checkout.', 'paypal-for-woocommerce'),  
  512. 'default' => __('PayPal Credit Card (REST)', 'paypal-for-woocommerce') 
  513. ),  
  514. 'description' => array( 
  515. 'title' => __('Description', 'paypal-for-woocommerce'),  
  516. 'type' => 'text',  
  517. 'description' => __('This controls the description which the user sees during checkout.', 'paypal-for-woocommerce'),  
  518. 'default' => __('PayPal Credit Card (REST) description', 'paypal-for-woocommerce') 
  519. ),  
  520. 'testmode' => array( 
  521. 'title' => __('Test Mode', 'paypal-for-woocommerce'),  
  522. 'type' => 'checkbox',  
  523. 'label' => __('Enable PayPal Sandbox/Test Mode', 'paypal-for-woocommerce'),  
  524. 'default' => 'yes',  
  525. 'description' => sprintf(__('Place the payment gateway in development mode. Sign up for a developer account <a href="%s" target="_blank">here</a>', 'paypal-for-woocommerce'), 'https://developer.paypal.com/'),  
  526. ),  
  527. 'rest_client_id_sandbox' => array( 
  528. 'title' => __('Sandbox Client ID', 'paypal-for-woocommerce'),  
  529. 'type' => 'password',  
  530. 'description' => 'Enter your Sandbox PayPal Rest API Client ID',  
  531. 'default' => '' 
  532. ),  
  533. 'rest_secret_id_sandbox' => array( 
  534. 'title' => __('Sandbox Secret ID', 'paypal-for-woocommerce'),  
  535. 'type' => 'password',  
  536. 'description' => __('Enter your Sandbox PayPal Rest API Secret ID.', 'paypal-for-woocommerce'),  
  537. 'default' => '' 
  538. ),  
  539. 'rest_client_id' => array( 
  540. 'title' => __('Live Client ID', 'paypal-for-woocommerce'),  
  541. 'type' => 'password',  
  542. 'description' => 'Enter your PayPal Rest API Client ID',  
  543. 'default' => '' 
  544. ),  
  545. 'rest_secret_id' => array( 
  546. 'title' => __('Live Secret ID', 'paypal-for-woocommerce'),  
  547. 'type' => 'password',  
  548. 'description' => __('Enter your PayPal Rest API Secret ID.', 'paypal-for-woocommerce'),  
  549. 'default' => '' 
  550. ),  
  551. 'invoice_prefix' => array( 
  552. 'title' => __('Invoice Prefix', 'paypal-for-woocommerce'),  
  553. 'type' => 'text',  
  554. 'description' => __('Please enter a prefix for your invoice numbers. If you use your PayPal account for multiple stores ensure this prefix is unique as PayPal will not allow orders with the same invoice number.', 'paypal-for-woocommerce'),  
  555. 'default' => 'WC-PCCR',  
  556. 'desc_tip' => true,  
  557. ),  
  558. 'debug' => array( 
  559. 'title' => __('Debug Log', 'paypal-for-woocommerce'),  
  560. 'type' => 'checkbox',  
  561. 'label' => __('Enable logging', 'paypal-for-woocommerce'),  
  562. 'default' => 'no',  
  563. 'description' => sprintf(__('Log PayPal events, such as Secured Token requests, inside <code>%s</code>', 'paypal-for-woocommerce'), wc_get_log_file_path('paypal_credit_card_rest')),  
  564. ); 
  565.  
  566. public static function card_type_from_account_number($account_number) { 
  567. $types = array( 
  568. 'visa' => '/^4/',  
  569. 'mc' => '/^5[1-5]/',  
  570. 'amex' => '/^3[47]/',  
  571. 'discover' => '/^(6011|65|64[4-9]|622)/',  
  572. 'diners' => '/^(36|38|30[0-5])/',  
  573. 'jcb' => '/^35/',  
  574. 'maestro' => '/^(5018|5020|5038|6304|6759|676[1-3])/',  
  575. 'laser' => '/^(6706|6771|6709)/',  
  576. ); 
  577. foreach ($types as $type => $pattern) { 
  578. if (1 === preg_match($pattern, $account_number)) { 
  579. return $type; 
  580. return null; 
  581.  
  582. public static function is_express_checkout_credentials_is_set() { 
  583. $pp_settings = get_option('woocommerce_paypal_express_settings'); 
  584. $testmode = isset($pp_settings['testmode']) ? $pp_settings['testmode'] : 'yes'; 
  585. $enabled = $pp_settings['enabled']; 
  586. if ($testmode == 'yes') { 
  587. $api_username = $pp_settings['sandbox_api_username']; 
  588. $api_password = $pp_settings['sandbox_api_password']; 
  589. $api_signature = $pp_settings['sandbox_api_signature']; 
  590. } else { 
  591. $api_username = $pp_settings['api_username']; 
  592. $api_password = $pp_settings['api_password']; 
  593. $api_signature = $pp_settings['api_signature']; 
  594. if ('yes' != $enabled) { 
  595. return false; 
  596. if (!$api_username || !$api_password || !$api_signature) { 
  597. return false; 
  598. return true; 
  599.  
  600. public function paypal_for_woocommerce_paypal_transaction_history() { 
  601.  
  602. if (post_type_exists('paypal_transaction')) { 
  603. return; 
  604.  
  605. do_action('paypal_for_woocommerce_register_post_type'); 
  606.  
  607. register_post_type('paypal_transaction', apply_filters('paypal_for_woocommerce_register_post_type_paypal_transaction_history', array( 
  608. 'labels' => array( 
  609. 'name' => __('PayPal Transaction', 'paypal-for-woocommerce'),  
  610. 'singular_name' => __('PayPal Transaction', 'paypal-for-woocommerce'),  
  611. 'menu_name' => _x('PayPal Transaction', 'Admin menu name', 'paypal-for-woocommerce'),  
  612. 'add_new' => __('Add PayPal Transaction', 'paypal-for-woocommerce'),  
  613. 'add_new_item' => __('Add New PayPal Transaction', 'paypal-for-woocommerce'),  
  614. 'edit' => __('Edit', 'paypal-for-woocommerce'),  
  615. 'edit_item' => __('View PayPal Transaction', 'paypal-for-woocommerce'),  
  616. 'new_item' => __('New PayPal Transaction', 'paypal-for-woocommerce'),  
  617. 'view' => __('View PayPal Transaction', 'paypal-for-woocommerce'),  
  618. 'view_item' => __('View PayPal Transaction', 'paypal-for-woocommerce'),  
  619. 'search_items' => __('Search PayPal Transaction', 'paypal-for-woocommerce'),  
  620. 'not_found' => __('No PayPal Transaction found', 'paypal-for-woocommerce'),  
  621. 'not_found_in_trash' => __('No PayPal Transaction found in trash', 'paypal-for-woocommerce'),  
  622. 'parent' => __('Parent PayPal Transaction', 'paypal-for-woocommerce') 
  623. ),  
  624. 'description' => __('This is where you can add new PayPal Transaction to your store.', 'paypal-for-woocommerce'),  
  625. 'public' => false,  
  626. 'show_ui' => false,  
  627. 'capability_type' => 'post',  
  628. 'capabilities' => array( 
  629. 'create_posts' => false, // Removes support for the "Add New" function 
  630. ),  
  631. 'map_meta_cap' => true,  
  632. 'publicly_queryable' => true,  
  633. 'exclude_from_search' => false,  
  634. 'hierarchical' => false, // Hierarchical causes memory issues - WP loads all records! 
  635. 'rewrite' => array('slug' => 'paypal_ipn'),  
  636. 'query_var' => true,  
  637. 'supports' => array('', ''),  
  638. 'has_archive' => true,  
  639. 'show_in_nav_menus' => FALSE 
  640. ); 
  641.  
  642. public function angelleye_paypal_for_woocommerce_order_action_meta_box($post_type, $post) { 
  643. if (isset($post->ID) && !empty($post->ID)) { 
  644. if ($this->angelleye_is_display_paypal_transaction_details($post->ID)) { 
  645. add_meta_box('angelleye-pw-order-action', __('PayPal Transaction History', 'paypal-for-woocommerce'), array($this, 'angelleye_paypal_for_woocommerce_order_action_callback'), 'shop_order', 'normal', 'high', null); 
  646.  
  647. public function angelleye_paypal_for_woocommerce_order_action_callback($post) { 
  648.  
  649. $args = array( 
  650. 'post_type' => 'paypal_transaction',  
  651. 'posts_per_page' => -1,  
  652. 'meta_key' => 'order_id',  
  653. 'meta_value' => $post->ID,  
  654. 'order' => 'ASC',  
  655. 'post_status' => 'any' 
  656. ); 
  657. $posts_array = get_posts($args); 
  658. foreach ($posts_array as $post_data): 
  659. $payment_status = get_post_meta($post_data->ID, 'PAYMENTSTATUS', true); 
  660. if( isset($post->post_title) && !empty($post_data->post_title) && isset($payment_status) && $payment_status == 'Pending' ) { 
  661. $this->angelleye_get_transactionDetails($post_data->post_title); 
  662. endforeach; 
  663. $order = wc_get_order($post->ID); 
  664.  
  665. if (empty($this->angelleye_woocommerce_order_actions)) { 
  666. $this->angelleye_woocommerce_order_actions = $this->angelleye_woocommerce_order_actions(); 
  667. ?> 
  668. <div class='wrap'> 
  669. <?php 
  670. if (isset($this->angelleye_woocommerce_order_actions) && !empty($this->angelleye_woocommerce_order_actions)) { 
  671. ?> 
  672. <select name="angelleye_payment_action" id="angelleye_payment_action">  
  673. <?php 
  674. $i = 0; 
  675. foreach ($this->angelleye_woocommerce_order_actions as $k => $v) : 
  676. if ($i == 0) { 
  677. echo '<option value="" >Select Action</option>'; 
  678. ?> 
  679. <option value="<?php echo esc_attr($k); ?>" ><?php echo esc_html($v); ?></option> 
  680. <?php 
  681. $i = $i + 1; 
  682. endforeach; 
  683. ?> 
  684. </select>  
  685. <div class="angelleye_authorization_box" style="display: none;"> 
  686. <?php 
  687. if (isset($this->remain_authorize_amount)) { 
  688. $remain_authorize_amount_text = 'less than ' . $this->remain_authorize_amount; 
  689. } else { 
  690. $remain_authorize_amount_text = ''; 
  691. ?> 
  692. <input type="text" placeholder="Enter amount <?php echo $remain_authorize_amount_text; ?>" id="_regular_price" name="_regular_price" class="short wc_input_price text-box" style="width: 220px"> 
  693. </div> 
  694. <?php $this->angelleye_express_checkout_transaction_capture_dropdownbox($post->ID); ?> 
  695. <input type="submit" id="angelleye_payment_submit_button" value="Submit" name="save" class="button button-primary" style="display: none"> 
  696. <br/><br/><br/> 
  697. <?php 
  698.  
  699. ?> 
  700. <table class="widefat angelleye_order_action_table" style="width: 190px;float: right;"> 
  701. <tbody> 
  702. <tr> 
  703. <td><?php echo __('Order Total:', 'paypal-for-woocommerce'); ?></td> 
  704. <td><?php echo get_woocommerce_currency_symbol() . $order->order_total; ?></td> 
  705. </tr> 
  706. <tr> 
  707. <td><?php echo __('Total Capture:', 'paypal-for-woocommerce'); ?></td> 
  708. <td><?php echo get_woocommerce_currency_symbol() . $this->total_DoCapture; ?></td> 
  709. </tr> 
  710. </tbody> 
  711. </table> 
  712. <br/><br/> 
  713. <table class="widefat angelleye_order_action_table"> 
  714. <thead> 
  715. <tr> 
  716. <th><?php echo __('Transaction ID', 'paypal-for-woocommerce'); ?></th> 
  717. <th><?php echo __('Date', 'paypal-for-woocommerce'); ?></th>  
  718. <th><?php echo __('Amount', 'paypal-for-woocommerce'); ?></th> 
  719. <th><?php echo __('Payment Status', 'paypal-for-woocommerce'); ?></th> 
  720. <th><?php echo __('Payment Action', 'paypal-for-woocommerce'); ?></th> 
  721. </tr> 
  722. </thead> 
  723. <tfoot> 
  724. <tr> 
  725. <th><?php echo __('Transaction ID', 'paypal-for-woocommerce'); ?></th> 
  726. <th><?php echo __('Date', 'paypal-for-woocommerce'); ?></th>  
  727. <th><?php echo __('Amount', 'paypal-for-woocommerce'); ?></th> 
  728. <th><?php echo __('Payment Status', 'paypal-for-woocommerce'); ?></th> 
  729. <th><?php echo __('Payment Action', 'paypal-for-woocommerce'); ?></th> 
  730. </tr> 
  731. </tfoot> 
  732. <tbody> 
  733. <?php 
  734. foreach ($posts_array as $post): 
  735. ?> 
  736. <tr> 
  737. <td><?php echo $post->post_title; ?></td> 
  738. <td><?php echo esc_attr(get_post_meta($post->ID, 'TIMESTAMP', true)); ?></th>  
  739. <td><?php echo get_woocommerce_currency_symbol() . esc_attr(get_post_meta($post->ID, 'AMT', true)); ?></td> 
  740. <?php $PENDINGREASON = esc_attr(get_post_meta($post->ID, 'PENDINGREASON', true)); ?> 
  741. <td <?php echo ($PENDINGREASON) ? sprintf('title="%s"', $PENDINGREASON) : ""; ?> ><?php echo esc_attr(get_post_meta($post->ID, 'PAYMENTSTATUS', true)); ?></td> 
  742. <td><?php echo esc_attr(get_post_meta($post->ID, 'payment_action', true)); ?> </td> 
  743. </tr> 
  744. <?php 
  745. endforeach; 
  746. ?> 
  747. </tbody> 
  748. </table> 
  749. </div> 
  750. <?php 
  751.  
  752. public static function angelleye_paypal_for_woocommerce_add_paypal_transaction($response, $order, $payment_action) { 
  753. if ($payment_action == 'Authorization') { 
  754. $payment_action = 'DoAuthorization'; 
  755. $TRANSACTIONID = ''; 
  756. if (isset($response['PAYMENTINFO_0_TRANSACTIONID']) && !empty($response['PAYMENTINFO_0_TRANSACTIONID'])) { 
  757. $TRANSACTIONID = $response['PAYMENTINFO_0_TRANSACTIONID']; 
  758. } elseif (isset($response['TRANSACTIONID']) && !empty($response['TRANSACTIONID'])) { 
  759. $TRANSACTIONID = $response['TRANSACTIONID']; 
  760. } elseif (isset($response['AUTHORIZATIONID'])) { 
  761. $TRANSACTIONID = $response['AUTHORIZATIONID']; 
  762. $insert_paypal_transaction = array( 
  763. 'ID' => '',  
  764. 'post_type' => 'paypal_transaction',  
  765. 'post_status' => $payment_action,  
  766. 'post_title' => $TRANSACTIONID,  
  767. 'post_parent' => $order->id 
  768. ); 
  769. unset($response['ERRORS']); 
  770. unset($response['REQUESTDATA']); 
  771. unset($response['RAWREQUEST']); 
  772. unset($response['RAWRESPONSE']); 
  773. unset($response['PAYMENTS']); 
  774. $post_id = wp_insert_post($insert_paypal_transaction); 
  775. $response['order_id'] = $order->id; 
  776. $response['payment_action'] = $payment_action; 
  777. $response['_trans_date'] = current_time('mysql'); 
  778. update_post_meta($post_id, 'paypal_transaction', $response); 
  779. foreach ($response as $metakey => $metavalue) { 
  780. $metakey = str_replace('PAYMENTINFO_0_', '', $metakey); 
  781. update_post_meta($post_id, $metakey, $metavalue); 
  782. if ($payment_action == 'DoVoid') { 
  783. $post_id_value = self::get_post_id_by_meta_key_and_meta_value('TRANSACTIONID', $response['AUTHORIZATIONID']); 
  784. $AMT = get_post_meta($post_id_value, 'AMT', true); 
  785. update_post_meta($post_id, 'AMT', $AMT); 
  786.  
  787. public static function get_post_id_by_meta_key_and_meta_value($key, $value) { 
  788. global $wpdb; 
  789. $post_id_value = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE `meta_key` LIKE '%s' AND `meta_value` LIKE '%s'", $key, $value)); 
  790. return $post_id_value; 
  791.  
  792. public function save($post_id, $post) { 
  793. if (empty($this->payment_method)) { 
  794. $this->payment_method = get_post_meta($post_id, '_payment_method', true); 
  795. $order = wc_get_order($post_id); 
  796. if (!empty($_POST['angelleye_payment_action'])) { 
  797. $action = wc_clean($_POST['angelleye_payment_action']); 
  798. $hook_name = 'wc_' . $this->payment_method . '_' . strtolower($action); 
  799. if (!did_action('woocommerce_order_action_' . sanitize_title($hook_name))) { 
  800. do_action('woocommerce_order_action_' . sanitize_title($hook_name), $order); 
  801.  
  802. public static function get_total($action, $status, $order_id) { 
  803. global $wpdb; 
  804. if ($action == 'DoVoid') { 
  805. $total = $wpdb->get_var($wpdb->prepare(" 
  806. SELECT SUM( postmeta.meta_value ) 
  807. FROM $wpdb->postmeta AS postmeta 
  808. INNER JOIN $wpdb->posts AS posts ON ( posts.post_type = 'paypal_transaction' AND posts.post_status LIKE '%s' AND post_parent = %d ) 
  809. WHERE postmeta.meta_key = 'AMT'  
  810. AND postmeta.post_id = posts.ID LIMIT 0, 99 
  811. ", $action, $order_id)); 
  812. } else { 
  813. if ($action == 'DoCapture') { 
  814. $total = $wpdb->get_var($wpdb->prepare(" 
  815. SELECT SUM( postmeta.meta_value ) 
  816. FROM $wpdb->postmeta AS postmeta 
  817. JOIN $wpdb->postmeta pm2 ON pm2.post_id = postmeta.post_id 
  818. INNER JOIN $wpdb->posts AS posts ON ( posts.post_type = 'paypal_transaction' AND posts.post_status LIKE '%s' AND post_parent = %d ) 
  819. WHERE postmeta.meta_key = 'AMT' AND pm2.meta_key = 'PAYMENTSTATUS' AND (pm2.meta_value LIKE '%s' OR pm2.meta_value LIKE 'Pending') 
  820. AND postmeta.post_id = posts.ID LIMIT 0, 99 
  821. ", $action, $order_id, $status)); 
  822. } else { 
  823. $total = $wpdb->get_var($wpdb->prepare(" 
  824. SELECT SUM( postmeta.meta_value ) 
  825. FROM $wpdb->postmeta AS postmeta 
  826. JOIN $wpdb->postmeta pm2 ON pm2.post_id = postmeta.post_id 
  827. INNER JOIN $wpdb->posts AS posts ON ( posts.post_type = 'paypal_transaction' AND posts.post_status LIKE '%s' AND post_parent = %d ) 
  828. WHERE postmeta.meta_key = 'AMT' AND pm2.meta_key = 'PAYMENTSTATUS' AND pm2.meta_value LIKE '%s' 
  829. AND postmeta.post_id = posts.ID LIMIT 0, 99 
  830. ", $action, $order_id, $status)); 
  831. if ($total == NULL) { 
  832. $total = 0; 
  833. return self::number_format($total); 
  834.  
  835. public function angelleye_paypal_for_woocommerce_order_status_handler($order) { 
  836. $this->angelleye_woocommerce_order_actions = $this->angelleye_woocommerce_order_actions(); 
  837. if (!is_object($order)) { 
  838. $order = wc_get_order($order); 
  839. $_payment_action = get_post_meta($order->id, '_payment_action', true); 
  840. if (isset($_payment_action) && !empty($_payment_action) && $_payment_action == 'Order') { 
  841. if (($this->max_authorize_amount <= $this->total_DoVoid) || ($this->total_Pending_DoAuthorization == 0 && $this->total_Completed_DoAuthorization == 0 && $this->total_DoVoid == $order->order_total)) { 
  842. $_first_transaction_id = get_post_meta($order->id, '_first_transaction_id', true); 
  843. $this->angelleye_get_transactionDetails($_first_transaction_id); 
  844. $order->update_status('cancelled'); 
  845. if ($order->order_total <= $this->total_Completed_DoAuthorization && $this->total_Pending_DoAuthorization == 0) { 
  846. $_first_transaction_id = get_post_meta($order->id, '_first_transaction_id', true); 
  847. $this->angelleye_get_transactionDetails($_first_transaction_id); 
  848. do_action( 'woocommerce_order_status_pending_to_processing', $order->id ); 
  849. $order->payment_complete($_first_transaction_id); 
  850. do_action('woocommerce_checkout_order_processed', $order->id, $posted = array()); 
  851. $order->reduce_order_stock(); 
  852.  
  853. if (isset($_payment_action) && !empty($_payment_action) && $_payment_action == 'Authorization') { 
  854. if ($order->order_total == $this->total_DoVoid) { 
  855. $_first_transaction_id = get_post_meta($order->id, '_first_transaction_id', true); 
  856. $this->angelleye_get_transactionDetails($_first_transaction_id); 
  857. $order->update_status('cancelled'); 
  858. if ($order->order_total == $this->total_Completed_DoAuthorization && $this->total_Pending_DoAuthorization == 0) { 
  859. $_first_transaction_id = get_post_meta($order->id, '_first_transaction_id', true); 
  860. $this->angelleye_get_transactionDetails($_first_transaction_id); 
  861. do_action( 'woocommerce_order_status_pending_to_processing', $order->id ); 
  862. $order->payment_complete($_first_transaction_id); 
  863. do_action('woocommerce_checkout_order_processed', $order->id, $posted = array()); 
  864. $order->reduce_order_stock(); 
  865.  
  866. public function angelleye_express_checkout_transaction_capture_dropdownbox($post_id) { 
  867. global $wpdb; 
  868. $order = wc_get_order($post_id); 
  869. wp_reset_postdata(); 
  870. $payment_action = get_post_meta($order->id, '_payment_action', true); 
  871. if ($this->total_DoCapture == 0 && $this->total_Pending_DoAuthorization == 0) { 
  872. if ('Order' == $payment_action) { 
  873. $post_status = 'Order'; 
  874. } else { 
  875. $post_status = 'DoAuthorization'; 
  876. } else { 
  877. $post_status = 'DoAuthorization'; 
  878. }  
  879. if ($this->total_Completed_DoAuthorization < $this->total_Order || $this->total_Pending_DoAuthorization > 0) { 
  880. $posts = $wpdb->get_results($wpdb->prepare("SELECT $wpdb->posts.ID, $wpdb->posts.post_title FROM $wpdb->posts INNER JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id ) WHERE 1=1 AND $wpdb->posts.post_status LIKE '%s' AND $wpdb->posts.post_parent = %d AND ( ( $wpdb->postmeta.meta_key = 'PAYMENTSTATUS' AND CAST($wpdb->postmeta.meta_value AS CHAR) = 'Pending' ) ) AND $wpdb->posts.post_type = 'paypal_transaction' GROUP BY $wpdb->posts.ID ORDER BY $wpdb->posts.post_date DESC LIMIT 0, 99", $post_status, $order->id), ARRAY_A); 
  881. if (empty($posts)) { 
  882. return false; 
  883. ?> 
  884. <select name="angelleye_paypal_capture_transaction_dropdown" id="angelleye_paypal_capture_transaction_dropdown" style="display: none">  
  885. <?php 
  886. $i = 0; 
  887. foreach ($posts as $post): 
  888. if ($i == 0) { 
  889. echo '<option value="" >Select Transaction ID</option>'; 
  890. ?> 
  891. <option value="<?php echo esc_attr($post['post_title']); ?>" ><?php echo esc_html($post['post_title']); ?></option> 
  892. <?php 
  893. $i = $i + 1; 
  894. endforeach; 
  895. ?> 
  896. </select>  
  897. <?php 
  898. if (($this->total_Completed_DoAuthorization == $this->total_DoCapture && $this->total_DoCapture > 0) || $this->total_Pending_DoAuthorization >= 0) { 
  899. ?> 
  900. <select name="angelleye_paypal_dovoid_transaction_dropdown" id="angelleye_paypal_dovoid_transaction_dropdown" style="display: none">  
  901. <?php 
  902. $i = 0; 
  903. if (empty($posts)) { 
  904. $posts = $wpdb->get_results($wpdb->prepare("SELECT $wpdb->posts.ID, $wpdb->posts.post_title FROM $wpdb->posts INNER JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id ) WHERE 1=1 AND $wpdb->posts.post_status LIKE '%s' AND $wpdb->posts.post_parent = %d AND ( ( $wpdb->postmeta.meta_key = 'PAYMENTSTATUS' AND CAST($wpdb->postmeta.meta_value AS CHAR) = 'Pending' ) ) AND $wpdb->posts.post_type = 'paypal_transaction' GROUP BY $wpdb->posts.ID ORDER BY $wpdb->posts.post_date DESC LIMIT 0, 99", $post_status, $order->id), ARRAY_A); 
  905. foreach ($posts as $post): 
  906. if ($i == 0) { 
  907. echo '<option value="" >Select Transaction ID</option>'; 
  908. ?> 
  909. <option value="<?php echo esc_attr($post['post_title']); ?>" ><?php echo esc_html($post['post_title']); ?></option> 
  910. <?php 
  911. $i = $i + 1; 
  912. endforeach; 
  913. ?> 
  914. </select>  
  915. <?php 
  916. if (($this->total_Completed_DoAuthorization == $this->total_DoCapture && $this->total_DoCapture > 0) || $this->total_Pending_DoAuthorization >= 0) { 
  917. ?> 
  918. <select name="angelleye_paypal_doreauthorization_transaction_dropdown" id="angelleye_paypal_doreauthorization_transaction_dropdown" style="display: none">  
  919. <?php 
  920. $i = 0; 
  921. if (empty($posts)) { 
  922. $posts = $wpdb->get_results($wpdb->prepare("SELECT $wpdb->posts.ID, $wpdb->posts.post_title FROM $wpdb->posts INNER JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id ) WHERE 1=1 AND $wpdb->posts.post_status LIKE '%s' AND $wpdb->posts.post_parent = %d AND ( ( $wpdb->postmeta.meta_key = 'PAYMENTSTATUS' AND CAST($wpdb->postmeta.meta_value AS CHAR) = 'Pending' ) ) AND $wpdb->posts.post_type = 'paypal_transaction' GROUP BY $wpdb->posts.ID ORDER BY $wpdb->posts.post_date DESC LIMIT 0, 99", $post_status, $order->id), ARRAY_A); 
  923. foreach ($posts as $post): 
  924. if ($i == 0) { 
  925. echo '<option value="" >Select Transaction ID</option>'; 
  926. ?> 
  927. <option value="<?php echo esc_attr($post['post_title']); ?>" ><?php echo esc_html($post['post_title']); ?></option> 
  928. <?php 
  929. $i = $i + 1; 
  930. endforeach; 
  931. ?> 
  932. </select>  
  933. <?php 
  934.  
  935. public function angelleye_get_transactionDetails($transaction_id) { 
  936. $this->add_ec_angelleye_paypal_php_library(); 
  937. $GTDFields = array( 
  938. 'transactionid' => $transaction_id 
  939. ); 
  940. $PayPalRequestData = array('GTDFields' => $GTDFields); 
  941. $get_transactionDetails_result = $this->paypal->GetTransactionDetails($PayPalRequestData); 
  942. $this->angelleye_write_request_response_api_log($get_transactionDetails_result); 
  943. $ack = strtoupper($get_transactionDetails_result["ACK"]); 
  944. if ($ack == "SUCCESS" || $ack == "SUCCESSWITHWARNING") { 
  945. $AUTHORIZATIONID = $this->get_post_by_title($transaction_id); 
  946. if ($AUTHORIZATIONID != null) { 
  947. update_post_meta($AUTHORIZATIONID, 'PAYMENTSTATUS', $get_transactionDetails_result['PAYMENTSTATUS']); 
  948.  
  949. function get_post_by_title($page_title) { 
  950. global $wpdb; 
  951. $post = $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type='paypal_transaction'", $page_title)); 
  952. if ($post) { 
  953. return $post; 
  954. return null; 
  955.  
  956. public function get_amount_by_transaction_id($transaction_id) { 
  957. $meta_post_id = self::get_post_id_by_meta_key_and_meta_value('TRANSACTIONID', $transaction_id); 
  958. $AMT = get_post_meta($meta_post_id, 'AMT', true); 
  959. return self::number_format($AMT); 
  960.  
  961. public function angelleye_max_authorize_amount($order_id) { 
  962. if (!is_object($order_id)) { 
  963. $order = wc_get_order($order_id); 
  964. $percentage = 115; 
  965. if (isset($order->order_total) && !empty($order->order_total)) { 
  966. $new_percentage_amount = self::round(($percentage / 100) * $order->order_total); 
  967. $diff_percentage_amount = self::round($new_percentage_amount - $order->order_total); 
  968. if ($diff_percentage_amount > 75) { 
  969. $max_authorize_amount = self::round($order->order_total + 75); 
  970. } else { 
  971. $max_authorize_amount = $new_percentage_amount; 
  972. $this->max_authorize_amount = self::number_format($max_authorize_amount); 
  973.  
  974. public function angelleye_remain_authorize_amount() { 
  975. $this->remain_authorize_amount = self::number_format($this->max_authorize_amount - ( self::round($this->total_Pending_DoAuthorization + $this->total_Completed_DoAuthorization))); 
  976.  
  977. public static function currency_has_decimals($currency) { 
  978. if (in_array($currency, array('HUF', 'JPY', 'TWD'))) { 
  979. return false; 
  980. return true; 
  981.  
  982. public static function round($price) { 
  983. $precision = 2; 
  984. if (!self::currency_has_decimals(get_woocommerce_currency())) { 
  985. $precision = 0; 
  986. return round($price, $precision); 
  987.  
  988. /** 
  989. * @since 1.1.8.1 
  990. * Non-decimal currency bug..?? #384  
  991. * Round prices 
  992. * @param type $price 
  993. * @return type 
  994. */ 
  995. public static function number_format($price) { 
  996. $decimals = 2; 
  997. if (!self::currency_has_decimals(get_woocommerce_currency())) { 
  998. $decimals = 0; 
  999. return number_format($price, $decimals, '.', ''); 
  1000.  
  1001. public function angelleye_is_display_paypal_transaction_details($post_id) { 
  1002. $_payment_method = get_post_meta($post_id, '_payment_method', true); 
  1003. $_payment_action = get_post_meta($post_id, '_payment_action', true); 
  1004.  
  1005. if (isset($_payment_method) && !empty($_payment_method) && isset($_payment_action) && !empty($_payment_action)) { 
  1006. if (($_payment_method == 'paypal_pro' || $_payment_method == 'paypal_express') && $_payment_method != "Sale") { 
  1007. return true; 
  1008. } else { 
  1009. return false; 
  1010. } else { 
  1011. return false; 
  1012. public static function is_valid_for_use() { 
  1013. return in_array( get_woocommerce_currency(), apply_filters( 'paypal_for_woocommerce_supported_currencies', array( 'AUD', 'BRL', 'CAD', 'MXN', 'NZD', 'HKD', 'SGD', 'USD', 'EUR', 'JPY', 'NOK', 'CZK', 'DKK', 'HUF', 'ILS', 'MYR', 'PHP', 'PLN', 'SEK', 'CHF', 'TWD', 'THB', 'GBP' ) ) );