init_quickpay_gateway

The WooCommerce QuickPay init quickpay gateway function.

Description

init_quickpay_gateway(); 

Usage

  1. if ( !function_exists( 'init_quickpay_gateway' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce-quickpay/woocommerce-quickpay.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = init_quickpay_gateway(); 
  7.  

Defined (1)

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

/woocommerce-quickpay.php  
  1. function init_quickpay_gateway() 
  2. /** 
  3. * Required functions 
  4. */ 
  5. if ( ! function_exists( 'is_woocommerce_active' ) ) { 
  6. require_once WCQP_PATH . 'woo-includes/woo-functions.php'; 
  7.  
  8. /** 
  9. * Check if WooCommerce is active, and if it isn't, disable Subscriptions. 
  10. * @since 1.0 
  11. */ 
  12. if ( ! is_woocommerce_active() ) { 
  13. return; 
  14.  
  15. // Import helper classes 
  16. require_once WCQP_PATH . 'classes/woocommerce-quickpay-install.php'; 
  17. require_once WCQP_PATH . 'classes/api/woocommerce-quickpay-api.php'; 
  18. require_once WCQP_PATH . 'classes/api/woocommerce-quickpay-api-transaction.php'; 
  19. require_once WCQP_PATH . 'classes/api/woocommerce-quickpay-api-payment.php'; 
  20. require_once WCQP_PATH . 'classes/api/woocommerce-quickpay-api-subscription.php'; 
  21. require_once WCQP_PATH . 'classes/woocommerce-quickpay-exceptions.php'; 
  22. require_once WCQP_PATH . 'classes/woocommerce-quickpay-log.php'; 
  23. require_once WCQP_PATH . 'classes/woocommerce-quickpay-helper.php'; 
  24. require_once WCQP_PATH . 'classes/woocommerce-quickpay-settings.php'; 
  25. require_once WCQP_PATH . 'classes/woocommerce-quickpay-order.php'; 
  26. require_once WCQP_PATH . 'classes/woocommerce-quickpay-subscription.php'; 
  27. require_once WCQP_PATH . 'classes/woocommerce-quickpay-countries.php'; 
  28. require_once WCQP_PATH . 'classes/woocommerce-quickpay-views.php'; 
  29.  
  30.  
  31. // Main class 
  32. class WC_QuickPay extends WC_Payment_Gateway 
  33.  
  34. /** 
  35. * $_instance 
  36. * @var mixed 
  37. * @access public 
  38. * @static 
  39. */ 
  40. public static $_instance = NULL; 
  41.  
  42. /** 
  43. * @var WC_QuickPay_Log 
  44. */ 
  45. public $log; 
  46.  
  47. /** 
  48. * get_instance 
  49. * Returns a new instance of self, if it does not already exist. 
  50. * @access public 
  51. * @static 
  52. * @return WC_QuickPay 
  53. */ 
  54. public static function get_instance() 
  55. if ( null === self::$_instance ) { 
  56. self::$_instance = new self(); 
  57. return self::$_instance; 
  58.  
  59.  
  60. /** 
  61. * __construct function. 
  62. * The class construct 
  63. * @access public 
  64. * @return void 
  65. */ 
  66. public function __construct() 
  67. $this->id = 'quickpay'; 
  68. $this->method_title = 'QuickPay'; 
  69. $this->icon = ''; 
  70. $this->has_fields = false; 
  71.  
  72. $this->supports = array( 
  73. 'subscriptions',  
  74. 'products',  
  75. 'subscription_cancellation',  
  76. 'subscription_reactivation',  
  77. 'subscription_suspension',  
  78. 'subscription_amount_changes',  
  79. 'subscription_date_changes',  
  80. 'subscription_payment_method_change_customer',  
  81. 'refunds',  
  82. 'multiple_subscriptions',  
  83. 'pre-orders' 
  84. ); 
  85.  
  86. $this->log = new WC_QuickPay_Log(); 
  87.  
  88. // Load the form fields and settings 
  89. $this->init_form_fields(); 
  90. $this->init_settings(); 
  91.  
  92. // Get gateway variables 
  93. $this->title = $this->s('title'); 
  94. $this->description = $this->s('description'); 
  95. $this->instructions = $this->s('instructions'); 
  96. $this->order_button_text = $this->s('checkout_button_text'); 
  97.  
  98.  
  99.  
  100. /** 
  101. * filter_load_instances function. 
  102. * Loads in extra instances of as separate gateways 
  103. * @access public static 
  104. * @return void 
  105. */ 
  106. public static function filter_load_instances($methods) 
  107. require_once WCQP_PATH . 'classes/instances/instance.php'; 
  108. require_once WCQP_PATH . 'classes/instances/mobilepay.php'; 
  109. require_once WCQP_PATH . 'classes/instances/viabill.php'; 
  110. require_once WCQP_PATH . 'classes/instances/klarna.php'; 
  111. require_once WCQP_PATH . 'classes/instances/sofort.php'; 
  112.  
  113. $methods[] = 'WC_QuickPay_MobilePay'; 
  114. $methods[] = 'WC_QuickPay_ViaBill'; 
  115. $methods[] = 'WC_QuickPay_Klarna'; 
  116. $methods[] = 'WC_QuickPay_Sofort'; 
  117.  
  118. return $methods; 
  119.  
  120.  
  121. /** 
  122. * hooks_and_filters function. 
  123. * Applies plugin hooks and filters 
  124. * @access public 
  125. * @return string 
  126. */ 
  127. public function hooks_and_filters() 
  128. add_action('woocommerce_api_wc_' . $this->id, array($this, 'callback_handler')); 
  129. add_action('woocommerce_receipt_' . $this->id, array($this, 'receipt_page')); 
  130. add_action('woocommerce_order_status_completed', array($this, 'woocommerce_order_status_completed')); 
  131. add_action('in_plugin_update_message-woocommerce-quickpay/woocommerce-quickpay.php', array(__CLASS__, 'in_plugin_update_message')); 
  132.  
  133. // WooCommerce Subscriptions hooks/filters 
  134. add_action('woocommerce_scheduled_subscription_payment_' . $this->id, array($this, 'scheduled_subscription_payment'), 10, 2); 
  135. add_action('woocommerce_subscription_cancelled_' . $this->id, array($this, 'subscription_cancellation')); 
  136. add_action('woocommerce_subscription_payment_method_updated_to_' . $this->id, array($this, 'on_subscription_payment_method_updated_to_quickpay'), 10, 2); 
  137. add_filter('wcs_renewal_order_meta_query', array($this, 'remove_failed_quickpay_attempts_meta_query'), 10); 
  138. add_filter('wcs_renewal_order_meta_query', array($this, 'remove_legacy_transaction_id_meta_query'), 10); 
  139.  
  140. // Custom bulk actions 
  141. add_action( 'admin_footer-edit.php', array( $this, 'register_bulk_actions' ) ); 
  142. add_action( 'load-edit.php', array( $this, 'handle_bulk_actions' ) ); 
  143.  
  144. // WooCommerce Pre-Orders 
  145. add_action('wc_pre_orders_process_pre_order_completion_payment_' . $this->id, array($this, 'process_pre_order_payments')); 
  146.  
  147. if (is_admin()) { 
  148. add_action('admin_menu', 'WC_QuickPay_Helper::enqueue_stylesheet'); 
  149. add_action('admin_menu', 'WC_QuickPay_Helper::enqueue_javascript_backend'); 
  150. add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options')); 
  151. add_action('wp_ajax_quickpay_manual_transaction_actions', array($this, 'ajax_quickpay_manual_transaction_actions')); 
  152. add_action('wp_ajax_quickpay_get_transaction_information', array($this, 'ajax_quickpay_get_transaction_information')); 
  153. add_action('wp_ajax_quickpay_empty_logs', array($this, 'ajax_empty_logs')); 
  154. add_action('wp_ajax_quickpay_ping_api', array($this, 'ajax_ping_api')); 
  155. add_action('wp_ajax_quickpay_run_data_upgrader', 'WC_QuickPay_Install::ajax_run_upgrader'); 
  156. add_action('in_plugin_update_message-woocommerce-quickpay/woocommerce-quickpay.php', array(__CLASS__, 'in_plugin_update_message')); 
  157.  
  158. // Make sure not to add these actions multiple times 
  159. if (!has_action('init', 'WC_QuickPay_Helper::load_i18n')) { 
  160. add_action('woocommerce_email_before_order_table', array($this, 'email_instructions'), 10, 2); 
  161. add_action('add_meta_boxes', array($this, 'add_meta_boxes')); 
  162.  
  163. if (WC_QuickPay_Helper::option_is_enabled($this->s('quickpay_orders_transaction_info', 'yes'))) { 
  164. add_filter('manage_shop_order_posts_custom_column', array($this, 'apply_custom_order_data')); 
  165. add_filter('manage_shop_subscription_posts_custom_column', array($this, 'apply_custom_order_data')); 
  166.  
  167. add_action('admin_notices', array($this, 'admin_notices')); 
  168.  
  169. add_action('init', 'WC_QuickPay_Helper::load_i18n'); 
  170.  
  171.  
  172. add_filter('woocommerce_gateway_icon', array($this, 'apply_gateway_icons'), 2, 3); 
  173.  
  174. // Third party plugins 
  175. add_filter( 'qtranslate_language_detect_redirect', 'WC_QuickPay_Helper::qtranslate_prevent_redirect', 10, 3 ); 
  176. add_filter( 'wpss_misc_form_spam_check_bypass', 'WC_QuickPay_Helper::spamshield_bypass_security_check', -10, 1 ); 
  177.  
  178. /** 
  179. * s function. 
  180. * Returns a setting if set. Introduced to prevent undefined key when introducing new settings. 
  181. * @access public 
  182. * @param $key 
  183. * @param null $default 
  184. * @return mixed 
  185. */ 
  186. public function s($key, $default = NULL) 
  187. if (isset($this->settings[$key])) { 
  188. return $this->settings[$key]; 
  189.  
  190. return !is_null($default) ? $default : ''; 
  191.  
  192. /** 
  193. * Hook used to display admin notices 
  194. */ 
  195. public function admin_notices() 
  196. WC_QuickPay_Settings::show_admin_setup_notices(); 
  197. WC_QuickPay_Install::show_update_warning(); 
  198.  
  199.  
  200. /** 
  201. * add_action_links function. 
  202. * Adds action links inside the plugin overview 
  203. * @access public static 
  204. * @return array 
  205. */ 
  206. public static function add_action_links($links) 
  207. $links = array_merge(array( 
  208. '<a href="' . WC_QuickPay_Settings::get_settings_page_url() . '">' . __('Settings', 'woo-quickpay') . '</a>',  
  209. ), $links); 
  210.  
  211. return $links; 
  212.  
  213.  
  214. /** 
  215. * ajax_quickpay_manual_transaction_actions function. 
  216. * Ajax method taking manual transactionrequestsfrom wp-admin. 
  217. * @access public 
  218. * @return void 
  219. */ 
  220. public function ajax_quickpay_manual_transaction_actions() 
  221. if (isset($_REQUEST['quickpay_action']) AND isset($_REQUEST['post'])) { 
  222. $param_action = $_REQUEST['quickpay_action']; 
  223. $param_post = $_REQUEST['post']; 
  224.  
  225. $order = new WC_QuickPay_Order((int) $param_post); 
  226.  
  227. try { 
  228. $transaction_id = $order->get_transaction_id(); 
  229.  
  230. // Subscription 
  231. if ($order->contains_subscription()) { 
  232. $payment = new WC_QuickPay_API_Subscription(); 
  233. $payment->get($transaction_id); 
  234. } // Payment 
  235. else { 
  236. $payment = new WC_QuickPay_API_Payment(); 
  237. $payment->get($transaction_id); 
  238.  
  239. $payment->get($transaction_id); 
  240.  
  241. // Based on the current transaction state, we check if 
  242. // the requested action is allowed 
  243. if ($payment->is_action_allowed($param_action)) { 
  244. // Check if the action method is available in the payment class 
  245. if (method_exists($payment, $param_action)) { 
  246. // Fetch amount if sent. 
  247. $amount = isset($_REQUEST['quickpay_amount']) ? WC_QuickPay_Helper::price_custom_to_multiplied($_REQUEST['quickpay_amount']) : $payment->get_remaining_balance(); 
  248.  
  249. // Call the action method and parse the transaction id and order object 
  250. call_user_func_array(array($payment, $param_action), array($transaction_id, $order, WC_QuickPay_Helper::price_multiplied_to_float($amount))); 
  251. } else { 
  252. throw new QuickPay_API_Exception(sprintf("Unsupported action: %s.", $param_action)); 
  253. } // The action was not allowed. Throw an exception 
  254. else { 
  255. throw new QuickPay_API_Exception(sprintf( 
  256. "Action: \"%s\", is not allowed for order #%d, with type state \"%s\"",  
  257. $param_action,  
  258. $order->get_clean_order_number(),  
  259. $payment->get_current_type() 
  260. )); 
  261. } catch (QuickPay_Exception $e) { 
  262. $e->write_to_logs(); 
  263. } catch (QuickPay_API_Exception $e) { 
  264. $e->write_to_logs(); 
  265.  
  266.  
  267.  
  268. /** 
  269. * ajax_quickpay_get_transaction_information function. 
  270. * Ajax method retrieving status information about a transaction 
  271. * @access public 
  272. * @return json 
  273. */ 
  274. public function ajax_quickpay_get_transaction_information() 
  275. try { 
  276. if (isset($_REQUEST['quickpay-transaction-id']) && isset($_REQUEST['quickpay-post-id'])) { 
  277. $post_id = $_REQUEST['quickpay-post-id']; 
  278. $order = new WC_QuickPay_Order($post_id); 
  279. $transaction_id = $_REQUEST['quickpay-transaction-id']; 
  280.  
  281. $data_transaction_id = $transaction_id; 
  282. $data_test = ''; 
  283. $data_order = $order->get_transaction_order_id(); 
  284.  
  285. // Subscription 
  286. if (WC_QuickPay_Subscription::is_subscription($post_id)) { 
  287. $transaction = new WC_QuickPay_API_Subscription(); 
  288. $transaction->get($transaction_id); 
  289. $status = $transaction->get_current_type() . ' (' . __('subscription', 'woo-quickpay') . ')'; 
  290. } // Renewal failure 
  291. else if ($order->subscription_is_renewal_failure()) { 
  292. $data_transaction_id .= ' ( ' . __('initial order transaction ID', 'woo-quickpay') . ')'; 
  293. $status = __('Failed renewal', 'woo-quickpay'); 
  294. } // Payment 
  295. else { 
  296. $transaction = new WC_QuickPay_API_Payment(); 
  297. $transaction->get($transaction_id); 
  298. $status = $transaction->get_current_type(); 
  299.  
  300. if (isset($transaction) AND is_object($transaction) AND $transaction->is_test()) { 
  301. $data_test = __('Test transaction', 'woo-quickpay'); 
  302.  
  303. $response = array( 
  304. 'id' => array( 
  305. 'value' => sprintf(__('Transaction ID: %s', 'woo-quickpay'), $data_transaction_id) 
  306. ),  
  307. 'order' => array( 
  308. 'value' => empty($data_order) ? '' : sprintf(__('Transaction Order ID: %s', 'woo-quickpay'), $data_order) 
  309. ),  
  310. 'status' => array( 
  311. 'value' => sprintf(__('Transaction state: %s', 'woo-quickpay'), $status),  
  312. 'attr' => array( 
  313. 'class' => 'woocommerce-quickpay-' . $status 
  314. ),  
  315. 'test' => array( 
  316. 'value' => $data_test,  
  317. 'attr' => array( 
  318. 'style' => empty($data_test) ? '' : 'color:red' 
  319. ),  
  320. 'cardtype' => array( 
  321. 'value' => sprintf('<img src="%s" />', WC_QuickPay_Helper::get_payment_type_logo($transaction->get_brand())) 
  322. ); 
  323.  
  324. echo json_encode($response); 
  325. exit; 
  326. } catch (QuickPay_API_Exception $e) { 
  327. $e->write_to_logs(); 
  328.  
  329. $response = array( 
  330. error => array( 
  331. 'value' => $e->getMessage() 
  332. ); 
  333.  
  334. echo json_encode($response); 
  335. exit; 
  336.  
  337.  
  338. /** 
  339. * ajax_empty_logs function. 
  340. * Ajax method to empty the debug logs 
  341. * @access public 
  342. * @return json 
  343. */ 
  344. public function ajax_empty_logs() 
  345. $this->log->clear(); 
  346. echo json_encode(array('status' => 'success', 'message' => 'Logs successfully emptied')); 
  347. exit; 
  348.  
  349. /** 
  350. * Checks if an API key is able to connect to the API 
  351. */ 
  352. public function ajax_ping_api() 
  353. $status = error; 
  354. if (!empty($_POST['apiKey'])) { 
  355. try { 
  356. $api = new WC_QuickPay_API(sanitize_text_field($_POST['apiKey'])); 
  357. $api->get('/payments?page_size=1'); 
  358. $status = 'success'; 
  359. } catch (QuickPay_API_Exception $e) { 
  360. // 
  361. echo json_encode(array('status' => $status)); 
  362. exit; 
  363.  
  364. /** 
  365. * woocommerce_order_status_completed function. 
  366. * Captures one or several transactions when order state changes to complete. 
  367. * @access public 
  368. * @return void 
  369. */ 
  370. public function woocommerce_order_status_completed($post_id) 
  371. // Instantiate new order object 
  372. $order = new WC_QuickPay_Order($post_id); 
  373.  
  374. // Check the gateway settings. 
  375. if ($order->has_quickpay_payment() && WC_QuickPay_Helper::option_is_enabled($this->s('quickpay_captureoncomplete'))) { 
  376. // Capture only orders that are actual payments (regular orders / recurring payments) 
  377. if (!WC_QuickPay_Subscription::is_subscription($order)) { 
  378. $transaction_id = $order->get_transaction_id(); 
  379. $payment = new WC_QuickPay_API_Payment(); 
  380.  
  381. // Check if there is a transaction ID 
  382. if ($transaction_id) { 
  383. // Retrieve resource data about the transaction 
  384. $payment->get($transaction_id); 
  385.  
  386. // Check if the transaction can be captured 
  387. if ($payment->is_action_allowed('capture')) { 
  388. // Capture the payment 
  389. $payment->capture($transaction_id, $order); 
  390.  
  391.  
  392. /** 
  393. * payment_fields function. 
  394. * Prints out the description of the gateway. Also adds two checkboxes for viaBill/creditcard for customers to choose how to pay. 
  395. * @access public 
  396. * @return void 
  397. */ 
  398. public function payment_fields() 
  399. if ($this->description) echo wpautop(wptexturize($this->description)); 
  400.  
  401.  
  402. /** 
  403. * receipt_page function. 
  404. * Shows the recipt. This is the very last step before opening the payment window. 
  405. * @access public 
  406. * @return void 
  407. */ 
  408. public function receipt_page($order) 
  409. echo $this->generate_quickpay_form($order); 
  410.  
  411. /** 
  412. * Processing payments on checkout 
  413. * @param $order_id 
  414. * @return array 
  415. */ 
  416. public function process_payment($order_id) 
  417. try { 
  418. // Instantiate order object 
  419. $order = new WC_QuickPay_Order($order_id); 
  420.  
  421. // Does the order need a new QuickPay payment? 
  422. $needs_payment = true; 
  423.  
  424. // Default redirect to 
  425. $redirect_to = $this->get_return_url($order); 
  426.  
  427. // Instantiate a new transaction 
  428. $api_transaction = new WC_QuickPay_API_Payment(); 
  429.  
  430. // If the order is a subscripion or an attempt of updating the payment method 
  431. if (! WC_QuickPay_Subscription::cart_contains_switches() && ($order->contains_subscription() || $order->is_request_to_change_payment())) { 
  432. // Instantiate a subscription transaction instead of a payment transaction 
  433. $api_transaction = new WC_QuickPay_API_Subscription(); 
  434. // Clean up any legacy data regarding old payment links before creating a new payment. 
  435. $order->delete_payment_id(); 
  436. $order->delete_payment_link(); 
  437. // If the order contains a product switch and does not need a payment, we will skip the QuickPay 
  438. // payment window since we do not need to create a new payment nor modify an existing. 
  439. else if ($order->order_contains_switch() && ! $order->needs_payment()) { 
  440. $needs_payment = false; 
  441.  
  442. if ($needs_payment) { 
  443. // Create a new object 
  444. $payment = new stdClass(); 
  445. // If a payment ID exists, go get it 
  446. $payment->id = $order->get_payment_id(); 
  447. // Create a payment link 
  448. $link = new stdClass(); 
  449. // If a payment link exists, go get it 
  450. $link->url = $order->get_payment_link(); 
  451.  
  452. // If the order does not already have a payment ID,  
  453. // we will create one an attach it to the order 
  454. // We also check if a payment already exists. If a link exists, we don't 
  455. // need to create a payment. 
  456. if (empty($payment->id) && empty($link->url)) { 
  457. $payment = $api_transaction->create($order); 
  458. $order->set_payment_id($payment->id); 
  459.  
  460. // Create or update the payment link. This is necessary to do EVERY TIME 
  461. // to avoid fraud with changing amounts. 
  462. $link = $api_transaction->patch_link($payment->id, $order); 
  463.  
  464. if (WC_QuickPay_Helper::is_url($link->url)) { 
  465. $order->set_payment_link($link->url); 
  466.  
  467. // Overwrite the standard checkout url. Go to the QuickPay payment window. 
  468. if (WC_QuickPay_Helper::is_url($link->url)) { 
  469. $redirect_to = $link->url; 
  470.  
  471. // Perform redirect 
  472. return array( 
  473. 'result' => 'success',  
  474. 'redirect' => $redirect_to 
  475. ); 
  476.  
  477. } catch (QuickPay_Exception $e) { 
  478. $e->write_to_logs(); 
  479. wc_add_notice( $e->getMessage(), error ); 
  480.  
  481. /** 
  482. * HOOK: Handles pre-order payments 
  483. */ 
  484. public function process_pre_order_payments($order) 
  485. // Set order object 
  486. $order = new WC_QuickPay_Order($order); 
  487.  
  488. // Get transaction ID 
  489. $transaction_id = $order->get_transaction_id(); 
  490.  
  491. // Check if there is a transaction ID 
  492. if ($transaction_id) { 
  493. try { 
  494. // Set payment object 
  495. $payment = new WC_QuickPay_API_Payment(); 
  496.  
  497. // Retrieve resource data about the transaction 
  498. $payment->get($transaction_id); 
  499.  
  500. // Check if the transaction can be captured 
  501. if ($payment->is_action_allowed('capture')) { 
  502. try { 
  503. // Capture the payment 
  504. $payment->capture($transaction_id, $order); 
  505. } // Payment failed 
  506. catch (QuickPay_API_Exception $e) { 
  507. $this->log->add( 
  508. sprintf("Could not process pre-order payment for order: #%s with transaction id: %s. Payment failed. Exception: %s",  
  509. $order->get_clean_order_number(), $transaction_id, $e->getMessage()) 
  510. ); 
  511.  
  512. $order->update_status('failed'); 
  513. } catch (QuickPay_API_Exception $e) { 
  514. $this->log->add( 
  515. sprintf("Could not process pre-order payment for order: #%s with transaction id: %s. Transaction not found. Exception: %s",  
  516. $order->get_clean_order_number(), $transaction_id, $e->getMessage()) 
  517. ); 
  518.  
  519.  
  520. /** 
  521. * Process refunds 
  522. * WooCommerce 2.2 or later 
  523. * @param int $order_id 
  524. * @param float $amount 
  525. * @param string $reason 
  526. * @return bool|WP_Error 
  527. */ 
  528. public function process_refund($order_id, $amount = NULL, $reason = '') 
  529. try { 
  530. $order = new WC_QuickPay_Order($order_id); 
  531.  
  532. $transaction_id = $order->get_transaction_id(); 
  533.  
  534. // Check if there is a transaction ID 
  535. if (!$transaction_id) { 
  536. throw new QuickPay_Exception(sprintf(__("No transaction ID for order: %s", 'woo-quickpay'), $order_id)); 
  537.  
  538. // Create a payment instance and retrieve transaction information 
  539. $payment = new WC_QuickPay_API_Payment(); 
  540. $payment->get($transaction_id); 
  541.  
  542. // Check if the transaction can be refunded 
  543. if (!$payment->is_action_allowed('refund')) { 
  544. throw new QuickPay_Exception(__("Transaction state does not allow refunds.", 'woo-quickpay')); 
  545.  
  546. // Perform a refund API request 
  547. $payment->refund($transaction_id, $order, $amount); 
  548.  
  549. return TRUE; 
  550. } catch (QuickPay_Exception $e) { 
  551. $e->write_to_logs(); 
  552. } catch (QuickPay_API_Exception $e) { 
  553. $e->write_to_logs(); 
  554.  
  555. return FALSE; 
  556.  
  557. /** 
  558. * Clear cart in case its not already done. 
  559. * @return [type] [description] 
  560. */ 
  561. public function thankyou_page() 
  562. global $woocommerce; 
  563. $woocommerce->cart->empty_cart(); 
  564.  
  565.  
  566. /** 
  567. * scheduled_subscription_payment function. 
  568. * Runs every time a scheduled renewal of a subscription is required 
  569. * @access public 
  570. * @return The API response 
  571. */ 
  572. public function scheduled_subscription_payment($amount_to_charge, $renewal_order) 
  573. // Create subscription instance 
  574. $transaction = new WC_QuickPay_API_Subscription(); 
  575.  
  576. // Block the callback 
  577. $transaction->block_callback = TRUE; 
  578.  
  579. /** @var WC_Subscription $subscription */ 
  580. // Get the subscription based on the renewal order 
  581. $subscription = WC_QuickPay_Subscription::get_subscriptions_for_renewal_order($renewal_order, $single = TRUE); 
  582.  
  583. $subscription_id = version_compare( WC_VERSION, '3.0', '<' ) ? $subscription->id : $subscription->get_id(); 
  584.  
  585. // Make new instance to properly get the transaction ID with built in fallbacks. 
  586. $subscription_order = new WC_QuickPay_Order($subscription_id); 
  587.  
  588. // Get the transaction ID from the subscription 
  589. $transaction_id = $subscription_order->get_transaction_id(); 
  590.  
  591. // Capture a recurring payment with fixed amount 
  592. $response = $this->process_recurring_payment($transaction, $transaction_id, $amount_to_charge, $renewal_order); 
  593.  
  594. return $response; 
  595.  
  596.  
  597. /** 
  598. * Wrapper to process a recurring payment on an order/subscription 
  599. * @param WC_QuickPay_API_Subscription $transaction 
  600. * @param $subscription_transaction_id 
  601. * @param $amount_to_charge 
  602. * @param $order 
  603. * @return mixed 
  604. */ 
  605. public function process_recurring_payment(WC_QuickPay_API_Subscription $transaction, $subscription_transaction_id, $amount_to_charge, $order) 
  606. if (!$order instanceof WC_QuickPay_Order) { 
  607. $order = new WC_QuickPay_Order($order); 
  608.  
  609. $response = NULL; 
  610. try { 
  611. // Block the callback 
  612. $transaction->block_callback = TRUE; 
  613.  
  614. // Capture a recurring payment with fixed amount 
  615. list($response) = $transaction->recurring($subscription_transaction_id, $order, $amount_to_charge); 
  616.  
  617. if (!$response->accepted) { 
  618. throw new QuickPay_Exception("Recurring payment not accepted by acquirer."); 
  619.  
  620. // If there is a fee added to the transaction. 
  621. if (!empty($response->fee)) { 
  622. $order->add_transaction_fee($response->fee); 
  623. // Process the recurring payment on the orders 
  624. WC_QuickPay_Subscription::process_recurring_response($response, $order); 
  625.  
  626. // Reset failed attempts. 
  627. $order->reset_failed_quickpay_payment_count(); 
  628. } catch (QuickPay_Exception $e) { 
  629. $order->increase_failed_quickpay_payment_count(); 
  630.  
  631. // Set the payment as failed 
  632. $order->update_status('failed', 'Automatic renewal of ' . $order->get_order_number() . ' failed. Message: ' . $e->getMessage()); 
  633.  
  634. // Write debug information to the logs 
  635. $e->write_to_logs(); 
  636. } catch (QuickPay_API_Exception $e) { 
  637. $order->increase_failed_quickpay_payment_count(); 
  638.  
  639. // Set the payment as failed 
  640. $order->update_status('failed', 'Automatic renewal of ' . $order->get_order_number() . ' failed. Message: ' . $e->getMessage()); 
  641.  
  642. // Write debug information to the logs 
  643. $e->write_to_logs(); 
  644.  
  645. return $response; 
  646.  
  647. /** 
  648. * Prevents the failed attempts count to be copied to renewal orders 
  649. * @param $order_meta_query 
  650. * @return string 
  651. */ 
  652. public function remove_failed_quickpay_attempts_meta_query($order_meta_query) 
  653. $order_meta_query .= " AND `meta_key` NOT IN ('" . WC_QuickPay_Order::META_FAILED_PAYMENT_COUNT . "')"; 
  654. $order_meta_query .= " AND `meta_key` NOT IN ('_quickpay_transaction_id')"; 
  655.  
  656. return $order_meta_query; 
  657.  
  658. /** 
  659. * Prevents the legacy transaction ID from being copied to renewal orders 
  660. * @param $order_meta_query 
  661. * @return string 
  662. */ 
  663. public function remove_legacy_transaction_id_meta_query($order_meta_query) 
  664. $order_meta_query .= " AND `meta_key` NOT IN ('TRANSACTION_ID')"; 
  665.  
  666. return $order_meta_query; 
  667.  
  668. /** 
  669. * Triggered when customers are changing payment method to QuickPay. 
  670. * @param $new_payment_method 
  671. * @param $subscription 
  672. * @param $old_payment_method 
  673. */ 
  674. public function on_subscription_payment_method_updated_to_quickpay($subscription, $old_payment_method) 
  675. $subscription_id = version_compare( WC_VERSION, '3.0', '<' ) ? $subscription->id : $subscription->get_id(); 
  676. $order = new WC_QuickPay_Order($subscription_id); 
  677. $order->increase_payment_method_change_count(); 
  678.  
  679.  
  680. /** 
  681. * subscription_cancellation function. 
  682. * Cancels a transaction when the subscription is cancelled 
  683. * @access public 
  684. * @param WC_Order $order - WC_Order object 
  685. * @return void 
  686. */ 
  687. public function subscription_cancellation($order) 
  688. if ('cancelled' !== $order->get_status()) { 
  689. return; 
  690.  
  691. try { 
  692. if (WC_QuickPay_Subscription::is_subscription($order)) { 
  693. $order = new WC_QuickPay_Order($order); 
  694. $transaction_id = $order->get_transaction_id(); 
  695.  
  696. $subscription = new WC_QuickPay_API_Subscription(); 
  697. $subscription->get($transaction_id); 
  698.  
  699. if ($subscription->is_action_allowed('cancel')) { 
  700. $subscription->cancel($transaction_id); 
  701. } catch (QuickPay_Exception $e) { 
  702. $e->write_to_logs(); 
  703. } catch (QuickPay_API_Exception $e) { 
  704. $e->write_to_logs(); 
  705.  
  706. /** 
  707. * on_order_cancellation function. 
  708. * Is called when a customer cancels the payment process from the QuickPay payment window. 
  709. * @access public 
  710. * @return void 
  711. */ 
  712. public function on_order_cancellation($order_id) 
  713. $order = new WC_Order($order_id); 
  714.  
  715. // Redirect the customer to account page if the current order is failed 
  716. if ($order->get_status() === 'failed') { 
  717. $payment_failure_text = sprintf(__('<p><strong>Payment failure</strong> A problem with your payment on order <strong>#%i</strong> occured. Please try again to complete your order.</p>', 'woo-quickpay'), $order_id); 
  718.  
  719. wc_add_notice($payment_failure_text, error); 
  720.  
  721. wp_redirect(get_permalink(get_option('woocommerce_myaccount_page_id'))); 
  722.  
  723. $order->add_order_note(__('QuickPay Payment', 'woo-quickpay') . ': ' . __('Cancelled during process', 'woo-quickpay')); 
  724.  
  725. wc_add_notice(__('<p><strong>%s</strong>: %s</p>',  
  726. __('Payment cancelled', 'woo-quickpay'),  
  727. __('Due to cancellation of your payment, the order process was not completed. Please fulfill the payment to complete your order.', 'woo-quickpay') 
  728. ),  
  729. error); 
  730.  
  731. /** 
  732. * callback_handler function. 
  733. * Is called after a payment has been submitted in the QuickPay payment window. 
  734. * @access public 
  735. * @return void 
  736. */ 
  737. public function callback_handler() 
  738. // Get callback body 
  739. $request_body = file_get_contents("php://input"); 
  740.  
  741. if(empty($request_body)) { 
  742. return; 
  743.  
  744. // Decode the body into JSON 
  745. $json = json_decode($request_body); 
  746.  
  747. // Instantiate payment object 
  748. $payment = new WC_QuickPay_API_Payment($json); 
  749.  
  750. // Fetch order number; 
  751. $order_number = WC_QuickPay_Order::get_order_id_from_callback($json); 
  752.  
  753. // Fetch subscription post ID if present 
  754. $subscription_id = WC_QuickPay_Order::get_subscription_id_from_callback($json); 
  755.  
  756. if (!empty($subscription_id)) { 
  757. $subscription = new WC_QuickPay_Order($subscription_id); 
  758.  
  759. if ($payment->is_authorized_callback($request_body)) { 
  760. // Instantiate order object 
  761. $order = new WC_QuickPay_Order($order_number); 
  762.  
  763. $order_id = version_compare( WC_VERSION, '3.0', '<' ) ? $order->id : $order->get_id(); 
  764.  
  765. // Get last transaction in operation history 
  766. $transaction = end($json->operations); 
  767.  
  768. // Is the transaction accepted and approved by QP / Acquirer? 
  769. if ($json->accepted) { 
  770.  
  771. // Perform action depending on the operation status type 
  772. try { 
  773. switch ($transaction->type) { 
  774. // 
  775. // Cancel callbacks are currently not supported by the QuickPay API 
  776. // 
  777. case 'cancel' : 
  778. // Write a note to the order history 
  779. $order->note(__('Payment cancelled.', 'woo-quickpay')); 
  780. break; 
  781.  
  782. case 'capture' : 
  783. // Write a note to the order history 
  784. $order->note(__('Payment captured.', 'woo-quickpay')); 
  785. break; 
  786.  
  787. case 'refund' : 
  788. $order->note(sprintf(__('Refunded %s %s', 'woo-quickpay'), WC_QuickPay_Helper::price_normalize($transaction->amount), $json->currency)); 
  789. break; 
  790.  
  791. case 'authorize' : 
  792. // Set the transaction order ID 
  793. $order->set_transaction_order_id($json->order_id); 
  794.  
  795. // Remove payment link 
  796. $order->delete_payment_link(); 
  797.  
  798. // Remove payment ID, now we have the transaction ID 
  799. $order->delete_payment_id(); 
  800.  
  801. // Subscription authorization 
  802. if (!empty($subscription_id)) { 
  803. // Write log 
  804. $subscription->note(sprintf(__('Subscription authorized. Transaction ID: %s', 'woo-quickpay'), $json->id)); 
  805. // Activate the subscription 
  806.  
  807. // Check if there is an initial payment on the subscription. 
  808. // We are saving the total before completing the original payment. 
  809. // This gives us the correct payment for the auto initial payment on subscriptions. 
  810. $subscription_initial_payment = $order->get_total(); 
  811.  
  812. // Mark the payment as complete 
  813. //$subscription->set_transaction_id($json->id); 
  814. // Temporarily save the transaction ID on a custom meta row to avoid empty values in 3.0. 
  815. update_post_meta( $subscription_id, '_quickpay_transaction_id', $json->id ); 
  816. //$subscription->payment_complete($json->id); 
  817. $subscription->set_transaction_order_id($json->order_id); 
  818.  
  819. // Only make an instant payment if there is an initial payment 
  820. if ($subscription_initial_payment > 0) { 
  821. // Check if this is an order containing a subscription 
  822. if (!WC_QuickPay_Subscription::is_subscription($order_id) && $order->contains_subscription()) { 
  823. // Process a recurring payment. 
  824. $this->process_recurring_payment(new WC_QuickPay_API_Subscription(), $json->id, $subscription_initial_payment, $order); 
  825. // If there is no initial payment, we will mark the order as complete. 
  826. // This is usually happening if a subscription has a free trial. 
  827. else { 
  828. $order->payment_complete(); 
  829.  
  830. } // Regular payment authorization 
  831. else { 
  832. // Add order transaction fee if available 
  833. if (!empty($json->fee)) { 
  834. $order->add_transaction_fee($json->fee); 
  835.  
  836. // Check for pre-order 
  837. if (WC_QuickPay_Helper::has_preorder_plugin() && WC_Pre_Orders_Order::order_contains_pre_order($order) && WC_Pre_Orders_Order::order_requires_payment_tokenization($order_id)) { 
  838. try { 
  839. // Set transaction ID without marking the payment as complete 
  840. $order->set_transaction_id($json->id); 
  841. } catch (WC_Data_Exception $e) { 
  842. $this->log->add(__( 'Anerroroccured while setting transaction id: %d on order %s. %s', $json->id, $order_id, $e->getMessage())); 
  843. WC_Pre_Orders_Order::mark_order_as_pre_ordered($order); 
  844. } // Regular product 
  845. else { 
  846. // Register the payment on the order 
  847. $order->payment_complete($json->id); 
  848.  
  849. // Write a note to the order history 
  850. $order->note(sprintf(__('Payment authorized. Transaction ID: %s', 'woo-quickpay'), $json->id)); 
  851. break; 
  852.  
  853. do_action('woocommerce_quickpay_accepted_callback_status_' . $transaction->type, $order, $json); 
  854.  
  855. } catch (QuickPay_API_Exception $e) { 
  856. $e->write_to_logs(); 
  857.  
  858. // The transaction was not accepted. 
  859. // Print debug information to logs 
  860. else { 
  861. // Write debug information 
  862. $this->log->separator(); 
  863. $this->log->add(sprintf(__('Transaction failed for #%s.', 'woo-quickpay'), $order_number)); 
  864. $this->log->add(sprintf(__('QuickPay status code: %s.', 'woo-quickpay'), $transaction->qp_status_code)); 
  865. $this->log->add(sprintf(__('QuickPay status message: %s.', 'woo-quickpay'), $transaction->qp_status_msg)); 
  866. $this->log->add(sprintf(__('Acquirer status code: %s', 'woo-quickpay'), $transaction->aq_status_code)); 
  867. $this->log->add(sprintf(__('Acquirer status message: %s', 'woo-quickpay'), $transaction->aq_status_msg)); 
  868. $this->log->separator(); 
  869.  
  870. if ($transaction->type == 'recurring') { 
  871. WC_Subscriptions_Manager::process_subscription_payment_failure_on_order($order); 
  872.  
  873. if ('rejected' != $json->state) { 
  874. // Update the order statuses 
  875. if ($transaction->type == 'subscribe') { 
  876. WC_Subscriptions_Manager::process_subscription_payment_failure_on_order($order); 
  877. } else { 
  878. $order->update_status('failed'); 
  879. } else { 
  880. $this->log->add(sprintf(__('Invalid callback body for order #%s.', 'woo-quickpay'), $order_number)); 
  881.  
  882.  
  883. /** 
  884. * init_form_fields function. 
  885. * Initiates the plugin settings form fields 
  886. * @access public 
  887. * @return array 
  888. */ 
  889. public function init_form_fields() 
  890. $this->form_fields = WC_QuickPay_Settings::get_fields(); 
  891.  
  892.  
  893. /** 
  894. * admin_options function. 
  895. * Prints the admin settings form 
  896. * @access public 
  897. * @return string 
  898. */ 
  899. public function admin_options() 
  900. echo "<h3>QuickPay - {$this->id}, v" . WCQP_VERSION . "</h3>"; 
  901. echo "<p>" . __('Allows you to receive payments via QuickPay.', 'woo-quickpay') . "</p>"; 
  902.  
  903. WC_QuickPay_Settings::clear_logs_section(); 
  904.  
  905.  
  906. echo "<table class=\"form-table\">"; 
  907. $this->generate_settings_html(); 
  908. echo "</table"; 
  909.  
  910.  
  911.  
  912. /** 
  913. * add_meta_boxes function. 
  914. * Adds the action meta box inside the single order view. 
  915. * @access public 
  916. * @return void 
  917. */ 
  918. public function add_meta_boxes() 
  919. global $post; 
  920.  
  921. $screen = get_current_screen(); 
  922. $post_types = array('shop_order', 'shop_subscription'); 
  923.  
  924. if ( in_array($screen->id, $post_types, true) && in_array( $post->post_type, $post_types, true ) ) { 
  925. $order = new WC_QuickPay_Order($post->ID); 
  926. if ($order->has_quickpay_payment()) { 
  927. add_meta_box('quickpay-payment-actions', __('QuickPay Payment', 'woo-quickpay'), array(&$this, 'meta_box_payment'), 'shop_order', 'side', 'high'); 
  928. add_meta_box('quickpay-payment-actions', __('QuickPay Subscription', 'woo-quickpay'), array(&$this, 'meta_box_subscription'), 'shop_subscription', 'side', 'high'); 
  929.  
  930.  
  931. /** 
  932. * meta_box_payment function. 
  933. * Inserts the content of the API actions meta box - Payments 
  934. * @access public 
  935. * @return void 
  936. */ 
  937. public function meta_box_payment() 
  938. global $post; 
  939. $order = new WC_QuickPay_Order($post->ID); 
  940.  
  941. $transaction_id = $order->get_transaction_id(); 
  942. if ($transaction_id && $order->has_quickpay_payment()) { 
  943. try { 
  944. $transaction = new WC_QuickPay_API_Payment(); 
  945. $transaction->get($transaction_id); 
  946. $status = $transaction->get_current_type(); 
  947.  
  948. echo "<p class=\"woocommerce-quickpay-{$status}\"><strong>" . __('Current payment state', 'woo-quickpay') . ": " . $status . "</strong></p>"; 
  949.  
  950. if ($transaction->is_action_allowed('standard_actions')) { 
  951. echo "<h4><strong>" . __('Actions', 'woo-quickpay') . "</strong></h4>"; 
  952. echo "<ul class=\"order_action\">"; 
  953.  
  954. if ($transaction->is_action_allowed('capture')) { 
  955. echo "<li class=\"qp-full-width\"><a class=\"button button-primary\" data-action=\"capture\" data-confirm=\"" . __('You are about to CAPTURE this payment', 'woo-quickpay') . "\">" . sprintf(__('Capture Full Amount (%s)', 'woo-quickpay'), $transaction->get_formatted_remaining_balance()) . "</a></li>"; 
  956.  
  957. printf("<li class=\"qp-balance\"><span class=\"qp-balance__label\">%s:</span><span class=\"qp-balance__amount\"><span class='qp-balance__currency'>%s</span>%s</span></li>", __('Remaining balance', 'woo-quickpay'), $transaction->get_currency(), $transaction->get_formatted_remaining_balance()); 
  958. printf("<li class=\"qp-balance last\"><span class=\"qp-balance__label\">%s:</span><span class=\"qp-balance__amount\"><span class='qp-balance__currency'>%s</span><input id='qp-balance__amount-field' type='text' value='%s' /></span></li>", __('Capture amount', 'woo-quickpay'), $transaction->get_currency(), $transaction->get_formatted_remaining_balance()); 
  959.  
  960. if ($transaction->is_action_allowed('capture')) { 
  961. echo "<li class=\"qp-full-width\"><a class=\"button\" data-action=\"captureAmount\" data-confirm=\"" . __('You are about to CAPTURE this payment', 'woo-quickpay') . "\">" . __('Capture Specified Amount', 'woo-quickpay') . "</a></li>"; 
  962.  
  963.  
  964. if ($transaction->is_action_allowed('cancel')) { 
  965. echo "<li class=\"qp-full-width\"><a class=\"button\" data-action=\"cancel\" data-confirm=\"" . __('You are about to CANCEL this payment', 'woo-quickpay') . "\">" . __('Cancel', 'woo-quickpay') . "</a></li>"; 
  966.  
  967. echo "</ul>"; 
  968.  
  969. printf('<p><small><strong>%s:</strong> %d <span class="qp-meta-card"><img src="%s" /></span></small>',  
  970. __('Transaction ID', 'woo-quickpay'),  
  971. $transaction_id,  
  972. WC_Quickpay_Helperget_payment_type_logo($transaction->get_brand()) 
  973. ); 
  974.  
  975. $transaction_order_id = $order->get_transaction_order_id(); 
  976. if (isset($transaction_order_id) && !empty($transaction_order_id)) { 
  977. printf('<p><small><strong>%s:</strong> %s</small>', __('Transaction Order ID', 'woo-quickpay'), $transaction_order_id); 
  978. } catch (QuickPay_API_Exception $e) { 
  979. $e->write_to_logs(); 
  980. $e->write_standard_warning(); 
  981.  
  982. // Show payment ID and payment link for orders that have not yet 
  983. // been paid. Show this information even if the transaction ID is missing. 
  984. $payment_id = $order->get_payment_id(); 
  985. if (isset($payment_id) && !empty($payment_id)) { 
  986. printf('<p><small><strong>%s:</strong> %d</small>', __('Payment ID', 'woo-quickpay'), $payment_id); 
  987.  
  988. $payment_link = $order->get_payment_link(); 
  989. if (isset($payment_link) && !empty($payment_link)) { 
  990. printf('<p><small><strong>%s:</strong> <br /><input type="text" style="%s"value="%s" readonly /></small></p>', __('Payment Link', 'woo-quickpay'), 'width:100%', $payment_link); 
  991.  
  992.  
  993. /** 
  994. * meta_box_payment function. 
  995. * Inserts the content of the API actions meta box - Subscriptions 
  996. * @access public 
  997. * @return void 
  998. */ 
  999. public function meta_box_subscription() 
  1000. global $post; 
  1001. $order = new WC_QuickPay_Order($post->ID); 
  1002.  
  1003. $transaction_id = $order->get_transaction_id(); 
  1004. if ($transaction_id && $order->has_quickpay_payment()) { 
  1005. try { 
  1006.  
  1007. $transaction = new WC_QuickPay_API_Subscription(); 
  1008. $transaction->get($transaction_id); 
  1009. $status = $transaction->get_current_type() . ' (' . __('subscription', 'woo-quickpay') . ')'; 
  1010.  
  1011. echo "<p class=\"woocommerce-quickpay-{$status}\"><strong>" . __('Current payment state', 'woo-quickpay') . ": " . $status . "</strong></p>"; 
  1012.  
  1013. printf('<p><small><strong>%s:</strong> %d <span class="qp-meta-card"><img src="%s" /></span></small>',  
  1014. __('Transaction ID', 'woo-quickpay'),  
  1015. $transaction_id,  
  1016. WC_Quickpay_Helperget_payment_type_logo($transaction->get_brand()) 
  1017. ); 
  1018.  
  1019. $transaction_order_id = $order->get_transaction_order_id(); 
  1020. if (isset($transaction_order_id) && !empty($transaction_order_id)) { 
  1021. printf('<p><small><strong>%s:</strong> %s</small>', __('Transaction Order ID', 'woo-quickpay'), $transaction_order_id); 
  1022. } catch (QuickPay_API_Exception $e) { 
  1023. $e->write_to_logs(); 
  1024. $e->write_standard_warning(); 
  1025.  
  1026.  
  1027. /** 
  1028. * email_instructions function. 
  1029. * Adds custom text to the order confirmation email. 
  1030. * @access public 
  1031. * @param WC_Order $order 
  1032. * @param boolean $sent_to_admin 
  1033. * @return bool /string/void 
  1034. */ 
  1035. public function email_instructions($order, $sent_to_admin) 
  1036. $payment_method = version_compare( WC_VERSION, '3.0', '<' ) ? $order->payment_method : $order->get_payment_method(); 
  1037.  
  1038. if ($sent_to_admin || ($order->get_status() !== 'processing' && $order->get_status() !== 'completed') || $payment_method !== 'quickpay') { 
  1039. return; 
  1040.  
  1041. if ($this->instructions) { 
  1042. echo wpautop(wptexturize($this->instructions)); 
  1043.  
  1044.  
  1045. /** 
  1046. * apply_custom_order_data function. 
  1047. * Applies transaction ID and state to the order data overview 
  1048. * @access public 
  1049. * @return void 
  1050. */ 
  1051. public function apply_custom_order_data($column) 
  1052. global $post, $woocommerce; 
  1053.  
  1054. $order = new WC_QuickPay_Order($post->ID); 
  1055.  
  1056. // ? ABOVE 2.1 : BELOW 2.1 
  1057. $check_column = version_compare($woocommerce->version, '2.1', '>') ? 'shipping_address' : 'billing_address'; 
  1058.  
  1059. // Show transaction ID on the overview 
  1060. if (($post->post_type == 'shop_order' && $column == $check_column) || ($post->post_type == 'shop_subscription' && $column == 'order_title')) { 
  1061. // Insert transaction id and payment status if any 
  1062. $transaction_id = $order->get_transaction_id(); 
  1063.  
  1064. if ($transaction_id && $order->has_quickpay_payment()) { 
  1065. WC_QuickPay_Views::get_view('html-order-table-transaction-data.php', array( 
  1066. 'transaction_id' => $transaction_id,  
  1067. 'post_id' => $post->ID,  
  1068. )); 
  1069.  
  1070. /** 
  1071. * FILTER: apply_gateway_icons function. 
  1072. * Sets gateway icons on frontend 
  1073. * @access public 
  1074. * @return void 
  1075. */ 
  1076. public function apply_gateway_icons($icon, $id) 
  1077. if ($id == $this->id) { 
  1078. $icon = ''; 
  1079.  
  1080. $icons = $this->s('quickpay_icons'); 
  1081.  
  1082. if (!empty($icons)) { 
  1083. $icons_maxheight = $this->gateway_icon_size(); 
  1084.  
  1085. foreach ($icons as $key => $item) { 
  1086. $icon .= $this->gateway_icon_create($item, $icons_maxheight); 
  1087.  
  1088. return $icon; 
  1089.  
  1090.  
  1091. /** 
  1092. * gateway_icon_create 
  1093. * Helper to get the a gateway icon image tag 
  1094. * @access protected 
  1095. * @return void 
  1096. */ 
  1097. protected function gateway_icon_create($icon, $max_height) 
  1098. $icon_url = WC_HTTPS::force_https_url(plugin_dir_url(__FILE__) . 'assets/images/cards/' . $icon . '.png'); 
  1099. return '<img src="' . $icon_url . '" alt="' . esc_attr($this->get_title()) . '" style="max-height:' . $max_height . '"/>'; 
  1100.  
  1101.  
  1102. /** 
  1103. * gateway_icon_size 
  1104. * Helper to get the a gateway icon image max height 
  1105. * @access protected 
  1106. * @return void 
  1107. */ 
  1108. protected function gateway_icon_size() 
  1109. $settings_icons_maxheight = $this->s('quickpay_icons_maxheight'); 
  1110. return !empty($settings_icons_maxheight) ? $settings_icons_maxheight . 'px' : '20px'; 
  1111.  
  1112.  
  1113. /** 
  1114. * get_gateway_currency 
  1115. * Returns the gateway currency 
  1116. * @access public 
  1117. * @param WC_Order $order 
  1118. * @return void 
  1119. */ 
  1120. public function get_gateway_currency($order) 
  1121. if (WC_QuickPay_Helper::option_is_enabled($this->s('quickpay_currency_auto'))) { 
  1122. $currency = version_compare( WC_VERSION, '3.0', '<' ) ? $order->get_order_currency() : $order->get_currency(); 
  1123. } else { 
  1124. $currency = $this->s('quickpay_currency'); 
  1125.  
  1126. $currency = apply_filters('woocommerce_quickpay_currency', $currency, $order); 
  1127.  
  1128. return $currency; 
  1129.  
  1130.  
  1131. /** 
  1132. * get_gateway_language 
  1133. * Returns the gateway language 
  1134. * @access public 
  1135. * @return string 
  1136. */ 
  1137. public function get_gateway_language() 
  1138. $language = apply_filters('woocommerce_quickpay_language', $this->s('quickpay_language')); 
  1139. return $language; 
  1140.  
  1141. /** 
  1142. * Registers custom bulk actions 
  1143. */ 
  1144. public function register_bulk_actions() { 
  1145. global $post_type; 
  1146.  
  1147. if ( $post_type === 'shop_order' && WC_QuickPay_Subscription::plugin_is_active()) { 
  1148. WC_QuickPay_Views::get_view('bulk-actions.php'); 
  1149.  
  1150. /** 
  1151. * Handles custom bulk actions 
  1152. */ 
  1153. public function handle_bulk_actions() { 
  1154. $wp_list_table = _get_list_table( 'WP_Posts_List_Table' ); 
  1155.  
  1156. $action = $wp_list_table->current_action(); 
  1157.  
  1158. // Check for posts 
  1159. if ( ! empty( $_GET['post'] ) ) { 
  1160. $order_ids = $_GET['post']; 
  1161.  
  1162. // Make sure the $posts variable is an array 
  1163. if ( ! is_array( $order_ids ) ) { 
  1164. $order_ids = array( $order_ids ); 
  1165.  
  1166. if ( current_user_can( 'manage_woocommerce' ) ) { 
  1167. switch ( $action ) { 
  1168. // 3. Perform the action 
  1169. case 'quickpay_capture_recurring': 
  1170. // Security check 
  1171. $this->bulk_action_quickpay_capture_recurring( $order_ids ); 
  1172. break; 
  1173. default: 
  1174. return; 
  1175.  
  1176. // 4. Redirect client 
  1177. wp_redirect( $_SERVER['HTTP_REFERER'] ); 
  1178. exit; 
  1179.  
  1180. /** 
  1181. * @param array $order_ids 
  1182. */ 
  1183. public function bulk_action_quickpay_capture_recurring( $order_ids = array() ) { 
  1184. if (!empty($order_ids)) { 
  1185. foreach ( $order_ids as $order_id ) { 
  1186. $order = new WC_QuickPay_Order($order_id); 
  1187. $payment_method = version_compare( WC_VERSION, '3.0', '<' ) ? $order->payment_method : $order->get_payment_method(); 
  1188. if (WC_QuickPay_Subscription::is_renewal($order) && $order->needs_payment() && $payment_method === $this->id) { 
  1189. $this->scheduled_subscription_payment($order->get_total(), $order); 
  1190.  
  1191.  
  1192.  
  1193. /** 
  1194. * in_plugin_update_message 
  1195. * Show plugin changes. Code adapted from W3 Total Cache. 
  1196. * @access public 
  1197. * @static 
  1198. * @return void 
  1199. */ 
  1200. public static function in_plugin_update_message($args) 
  1201. $transient_name = 'wcqp_upgrade_notice_' . $args['Version']; 
  1202. if (false === ($upgrade_notice = get_transient($transient_name))) { 
  1203. $response = wp_remote_get('https://plugins.svn.wordpress.org/woocommerce-quickpay/trunk/README.txt'); 
  1204.  
  1205. if (!is_wp_error($response) && !empty($response['body'])) { 
  1206. $upgrade_notice = self::parse_update_notice($response['body']); 
  1207. set_transient($transient_name, $upgrade_notice, DAY_IN_SECONDS); 
  1208.  
  1209. echo wp_kses_post($upgrade_notice); 
  1210.  
  1211. /** 
  1212. * parse_update_notice 
  1213. * Parse update notice from readme file. 
  1214. * @param string $content 
  1215. * @return string 
  1216. */ 
  1217. private static function parse_update_notice($content) 
  1218. // Output Upgrade Notice 
  1219. $matches = null; 
  1220. $regexp = '~==\s*Upgrade Notice\s*==\s*=\s*(.*)\s*=(.*)(=\s*' . preg_quote(WCQP_VERSION, '/' ) . '\s*=|$)~Uis'; 
  1221. $upgrade_notice = ''; 
  1222.  
  1223. if (preg_match($regexp, $content, $matches)) { 
  1224. $version = trim($matches[1]); 
  1225. $notices = (array)preg_split('~[\r\n]+~', trim($matches[2])); 
  1226.  
  1227. if (version_compare(WCQP_VERSION, $version, '<')) { 
  1228.  
  1229. $upgrade_notice .= '<div class="wc_plugin_upgrade_notice">'; 
  1230.  
  1231. foreach ($notices as $index => $line) { 
  1232. $upgrade_notice .= wp_kses_post(preg_replace('~\[([^\]]*)\]\(([^\)]*)\)~', '<a href="${2}">${1}</a>', $line)); 
  1233.  
  1234. $upgrade_notice .= '</div> '; 
  1235.  
  1236. return wp_kses_post($upgrade_notice); 
  1237.  
  1238. /** 
  1239. * path 
  1240. * Returns a plugin URL path 
  1241. * @param $path 
  1242. * @return mixed 
  1243. */ 
  1244. public function plugin_url($path) 
  1245. return plugins_url($path, __FILE__); 
  1246.  
  1247. /** 
  1248. * Make the object available for later use 
  1249. * @return WC_QuickPay 
  1250. */ 
  1251. function WC_QP() 
  1252. return WC_QuickPay::get_instance(); 
  1253.  
  1254. // Instantiate 
  1255. WC_QP(); 
  1256. WC_QP()->hooks_and_filters(); 
  1257.  
  1258. // Add the gateway to WooCommerce 
  1259. function add_quickpay_gateway($methods) 
  1260. $methods[] = 'WC_QuickPay'; 
  1261.  
  1262. return apply_filters('woocommerce_quickpay_load_instances', $methods); 
  1263.  
  1264. add_filter('woocommerce_payment_gateways', 'add_quickpay_gateway'); 
  1265. add_filter('woocommerce_quickpay_load_instances', 'WC_QuickPay::filter_load_instances'); 
  1266. add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'WC_QuickPay::add_action_links');