PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/www/wp-content/plugins/ithemes-exchange/core-addons/transaction-methods/offline-payments/init.php

https://github.com/ArzuA/gitwordpress
PHP | 620 lines | 320 code | 86 blank | 214 comment | 48 complexity | 4509cc6fe6ea37e0edada9a69ceb9678 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Offline Transaction Method
  4. *
  5. * @since 0.3.0
  6. * @package IT_Exchange
  7. */
  8. /**
  9. * Mark this transaction method as okay to manually change transactions
  10. *
  11. * @since 0.4.11
  12. */
  13. add_filter( 'it_exchange_offline-payments_transaction_status_can_be_manually_changed', '__return_true' );
  14. /**
  15. * Returns status options
  16. *
  17. * @since 0.3.6
  18. * @return void
  19. */
  20. function it_exchange_offline_payments_get_default_status_options() {
  21. $options = array(
  22. 'pending' => _x( 'Pending', 'Transaction Status', 'it-l10n-ithemes-exchange' ),
  23. 'paid' => _x( 'Paid', 'Transaction Status', 'it-l10n-ithemes-exchange' ),
  24. 'refunded' => _x( 'Refunded', 'Transaction Status', 'it-l10n-ithemes-exchange' ),
  25. 'voided' => _x( 'Voided', 'Transaction Status', 'it-l10n-ithemes-exchange' ),
  26. );
  27. return $options;
  28. }
  29. add_filter( 'it_exchange_get_status_options_for_offline-payments_transaction', 'it_exchange_offline_payments_get_default_status_options' );
  30. /**
  31. * Call back for settings page
  32. *
  33. * This is set in options array when registering the add-on and called from it_exchange_enable_addon()
  34. *
  35. * @since 0.3.6
  36. * @return void
  37. */
  38. function it_exchange_offline_payments_settings_callback() {
  39. $IT_Exchange_Offline_Payments_Add_On = new IT_Exchange_Offline_Payments_Add_On();
  40. $IT_Exchange_Offline_Payments_Add_On->print_settings_page();
  41. }
  42. /**
  43. * Outputs wizard settings for Offline Payments
  44. *
  45. * @since 0.4.0
  46. * @todo make this better, probably
  47. * @param object $form Current IT Form object
  48. * @return void
  49. */
  50. function it_exchange_print_offline_payments_wizard_settings( $form ) {
  51. $IT_Exchange_Offline_Payments_Add_On = new IT_Exchange_Offline_Payments_Add_On();
  52. $settings = it_exchange_get_option( 'addon_offline_payments', true );
  53. $form_values = ITUtility::merge_defaults( ITForm::get_post_data(), $settings );
  54. $hide_if_js = it_exchange_is_addon_enabled( 'offline-payments' ) ? '' : 'hide-if-js';
  55. ?>
  56. <div class="field offline-payments-wizard <?php echo $hide_if_js; ?>">
  57. <?php if ( empty( $hide_if_js ) ) { ?>
  58. <input class="enable-offline-payments" type="hidden" name="it-exchange-transaction-methods[]" value="offline-payments" />
  59. <?php } ?>
  60. <?php $IT_Exchange_Offline_Payments_Add_On->get_offline_payment_form_table( $form, $form_values ); ?>
  61. </div>
  62. <?php
  63. }
  64. add_action( 'it_exchange_print_offline-payments_wizard_settings', 'it_exchange_print_offline_payments_wizard_settings' );
  65. /**
  66. * Saves offline payments settings when the Wizard is saved
  67. *
  68. * @since 0.4.0
  69. *
  70. * @return void
  71. */
  72. function it_exchange_save_offline_payments_wizard_settings( $errors ) {
  73. if ( ! empty( $errors ) )
  74. return $errors;
  75. $IT_Exchange_Offline_Payments_Add_On = new IT_Exchange_Offline_Payments_Add_On();
  76. return $IT_Exchange_Offline_Payments_Add_On->offline_payments_save_wizard_settings();
  77. }
  78. add_action( 'it_exchange_save_offline-payments_wizard_settings', 'it_exchange_save_offline_payments_wizard_settings' );
  79. /**
  80. * This proccesses an offline transaction.
  81. *
  82. * @since 0.4.0
  83. *
  84. * @param string $status passed by WP filter.
  85. * @param object $transaction_object The transaction object
  86. */
  87. function it_exchange_offline_payments_addon_process_transaction( $status, $transaction_object ) {
  88. // If this has been modified as true already, return.
  89. if ( $status )
  90. return $status;
  91. // Verify nonce
  92. if ( ! empty( $_REQUEST['_offline_payments_nonce'] ) && ! wp_verify_nonce( $_REQUEST['_offline_payments_nonce'], 'offline-payments-checkout' ) ) {
  93. it_exchange_add_message( 'error', __( 'Transaction Failed, unable to verify security token.', 'it-l10n-ithemes-exchange' ) );
  94. return false;
  95. } else {
  96. $settings = it_exchange_get_option( 'addon_offline_payments' );
  97. $uniqid = it_exchange_get_offline_transaction_uniqid();
  98. // Get customer ID data
  99. $it_exchange_customer = it_exchange_get_current_customer();
  100. return it_exchange_add_transaction( 'offline-payments', $uniqid, $settings['offline-payments-default-status'], $it_exchange_customer->ID, $transaction_object );
  101. }
  102. return false;
  103. }
  104. add_action( 'it_exchange_do_transaction_offline-payments', 'it_exchange_offline_payments_addon_process_transaction', 10, 2 );
  105. /**
  106. * Generates a unique ID to stand in for the payment gateway ID that doesn't exist for this method
  107. *
  108. * @since 0.4.0
  109. *
  110. * @return string
  111. */
  112. function it_exchange_get_offline_transaction_uniqid() {
  113. $uniqid = uniqid( '', true );
  114. if( !it_exchange_verify_offline_transaction_unique_uniqid( $uniqid ) )
  115. $uniqid = it_exchange_get_offline_transaction_uniqid();
  116. return $uniqid;
  117. }
  118. /**
  119. * Verifies that the psassed string is unique since we're generating it ourselves
  120. *
  121. * @since 0.4.0
  122. *
  123. * @param string $uniqid The id we're checking
  124. * @return boolean
  125. */
  126. function it_exchange_verify_offline_transaction_unique_uniqid( $uniqid ) {
  127. if ( ! empty( $uniqid ) ) { //verify we get a valid 32 character md5 hash
  128. $args = array(
  129. 'post_type' => 'it_exchange_tran',
  130. 'meta_query' => array(
  131. array(
  132. 'key' => '_it_exchange_transaction_method',
  133. 'value' => 'offline-payments',
  134. ),
  135. array(
  136. 'key' => '_it_exchange_transaction_method_id',
  137. 'value' => $uniqid ,
  138. ),
  139. ),
  140. );
  141. $query = new WP_Query( $args );
  142. return ( !empty( $query ) );
  143. }
  144. return false;
  145. }
  146. /**
  147. * Returns the button for making the payment
  148. *
  149. * @since 0.4.0
  150. *
  151. * @param array $options
  152. * @return string
  153. */
  154. function it_exchange_offline_payments_addon_make_payment_button( $options ) {
  155. if ( 0 >= it_exchange_get_cart_total( false ) )
  156. return;
  157. $general_settings = it_exchange_get_option( 'settings_general' );
  158. $stripe_settings = it_exchange_get_option( 'addon_offline_payments' );
  159. $products = it_exchange_get_cart_data( 'products' );
  160. $payment_form = '<form id="offline_payment_form" action="' . it_exchange_get_page_url( 'transaction' ) . '" method="post">';
  161. $payment_form .= '<input type="hidden" name="it-exchange-transaction-method" value="offline-payments" />';
  162. $payment_form .= wp_nonce_field( 'offline-payments-checkout', '_offline_payments_nonce', true, false );
  163. $payment_form .= '<input type="submit" id="offline-payments-button" name="offline_payments_purchase" value="' . it_exchange_get_transaction_method_name_from_slug( 'offline-payments' ) .'" />';
  164. $payment_form .= '</form>';
  165. /*
  166. * Going to remove this for now. It should be
  167. * the responsibility of the site owner to
  168. * notify if Javascript is disabled, but I will
  169. * revisit this in case we want to a notifications.
  170. *
  171. $payment_form .= '<div class="hide-if-js">';
  172. $payment_form .= '<h3>' . __( 'JavaScript disabled: Stripe Payment Gateway cannot be loaded!', 'it-l10n-ithemes-exchange' ) . '</h3>';
  173. $payment_form .= '</div>';
  174. */
  175. return $payment_form;
  176. }
  177. add_filter( 'it_exchange_get_offline-payments_make_payment_button', 'it_exchange_offline_payments_addon_make_payment_button', 10, 2 );
  178. /**
  179. * Replace Offline name with what is set in admin settings
  180. *
  181. * @since 0.3.7
  182. * @param string $name the name passed in from the WP filter API
  183. * @return string
  184. */
  185. function it_exchange_get_offline_payments_name( $name ) {
  186. $options = it_exchange_get_option( 'addon_offline_payments' );
  187. if ( ! empty( $options['offline-payments-title'] ) )
  188. $name = $options['offline-payments-title'];
  189. return $name;
  190. }
  191. add_filter( 'it_exchange_get_transaction_method_name_offline-payments', 'it_exchange_get_offline_payments_name', 9 );
  192. /**
  193. * Adds manual transactions template path inf on confirmation page
  194. *
  195. * @since 0.3.8
  196. * @return array of possible template paths + offline-payments template path
  197. */
  198. function it_exchange_offline_payments_add_template_path( $paths ) {
  199. if ( it_exchange_is_page( 'confirmation' ) )
  200. $paths[] = dirname( __FILE__ ) . '/templates/';
  201. return $paths;
  202. }
  203. add_filter( 'it_exchange_possible_template_paths', 'it_exchange_offline_payments_add_template_path' );
  204. /**
  205. * Return instructions to be displayed when someone makes a purchase with this payment method
  206. *
  207. * @since 0.4.0
  208. *
  209. * @param string $instructions passed in via filter. Ignored here.
  210. * @return string
  211. */
  212. function it_exchange_transaction_instructions_offline_payments( $instructions ) {
  213. $options = it_exchange_get_option( 'addon_offline_payments' );
  214. if ( ! empty( $options['offline-payments-instructions'] ) )
  215. $instructions = $options['offline-payments-instructions'];
  216. return $instructions;
  217. }
  218. add_filter( 'it_exchange_transaction_instructions_offline-payments', 'it_exchange_transaction_instructions_offline_payments' );
  219. /**
  220. * Gets the interpretted transaction status from valid transaction statuses
  221. *
  222. * @since 0.4.0
  223. *
  224. * @param string $status the string of the stripe transaction
  225. * @return string translaction transaction status
  226. */
  227. function it_exchange_offline_payments_addon_transaction_status_label( $status ) {
  228. switch ( $status ) {
  229. case 'succeeded':
  230. case 'paid':
  231. return __( 'Paid', 'it-l10n-ithemes-exchange' );
  232. break;
  233. case 'refunded':
  234. return __( 'Refunded', 'it-l10n-ithemes-exchange' );
  235. break;
  236. case 'pending':
  237. return __( 'Pending', 'it-l10n-ithemes-exchange' );
  238. break;
  239. case 'voided':
  240. return __( 'Voided', 'it-l10n-ithemes-exchange' );
  241. break;
  242. default:
  243. return __( 'Unknown', 'it-l10n-ithemes-exchange' );
  244. }
  245. }
  246. add_filter( 'it_exchange_transaction_status_label_offline-payments', 'it_exchange_offline_payments_addon_transaction_status_label' );
  247. /**
  248. * Returns a boolean. Is this transaction a status that warrants delivery of any products attached to it?
  249. *
  250. * @since 0.4.2
  251. *
  252. * @param boolean $cleared passed in through WP filter. Ignored here.
  253. * @param object $transaction
  254. * @return boolean
  255. */
  256. function it_exchange_offline_payments_transaction_is_cleared_for_delivery( $cleared, $transaction ) {
  257. $valid_stati = array( 'succeeded', 'paid' );
  258. return in_array( it_exchange_get_transaction_status( $transaction ), $valid_stati );
  259. }
  260. add_filter( 'it_exchange_offline-payments_transaction_is_cleared_for_delivery', 'it_exchange_offline_payments_transaction_is_cleared_for_delivery', 10, 2 );
  261. /**
  262. * Class for Offline
  263. * @since 0.3.6
  264. */
  265. class IT_Exchange_Offline_Payments_Add_On {
  266. /**
  267. * @var boolean $_is_admin true or false
  268. * @since 0.3.6
  269. */
  270. var $_is_admin;
  271. /**
  272. * @var string $_current_page Current $_GET['page'] value
  273. * @since 0.3.6
  274. */
  275. var $_current_page;
  276. /**
  277. * @var string $_current_add_on Current $_GET['add-on-settings'] value
  278. * @since 0.3.6
  279. */
  280. var $_current_add_on;
  281. /**
  282. * @var string $status_message will be displayed if not empty
  283. * @since 0.3.6
  284. */
  285. var $status_message;
  286. /**
  287. * @var string $error_message will be displayed if not empty
  288. * @since 0.3.6
  289. */
  290. var $error_message;
  291. /**
  292. * Class constructor
  293. *
  294. * Sets up the class.
  295. * @since 0.3.6
  296. * @return void
  297. */
  298. function IT_Exchange_Offline_Payments_Add_On() {
  299. $this->_is_admin = is_admin();
  300. $this->_current_page = empty( $_GET['page'] ) ? false : $_GET['page'];
  301. $this->_current_add_on = empty( $_GET['add-on-settings'] ) ? false : $_GET['add-on-settings'];
  302. if ( ! empty( $_POST ) && $this->_is_admin && 'it-exchange-addons' == $this->_current_page && 'offline-payments' == $this->_current_add_on ) {
  303. add_action( 'it_exchange_save_add_on_settings_offline-payments', array( $this, 'save_settings' ) );
  304. do_action( 'it_exchange_save_add_on_settings_offline-payments' );
  305. }
  306. add_filter( 'it_storage_get_defaults_exchange_addon_offline_payments', array( $this, 'set_default_settings' ) );
  307. }
  308. function print_settings_page() {
  309. $settings = it_exchange_get_option( 'addon_offline_payments', true );
  310. $form_values = empty( $this->error_message ) ? $settings : ITForm::get_post_data();
  311. $form_options = array(
  312. 'id' => apply_filters( 'it_exchange_add_on_offline_payments', 'it-exchange-add-on-offline-payments-settings' ),
  313. 'enctype' => apply_filters( 'it_exchange_add_on_offline_payments_settings_form_enctype', false ),
  314. 'action' => 'admin.php?page=it-exchange-addons&add-on-settings=offline-payments',
  315. );
  316. $form = new ITForm( $form_values, array( 'prefix' => 'it-exchange-add-on-offline-payments' ) );
  317. if ( ! empty ( $this->status_message ) )
  318. ITUtility::show_status_message( $this->status_message );
  319. if ( ! empty( $this->error_message ) )
  320. ITUtility::show_error_message( $this->error_message );
  321. include( 'view-add-on-settings.php' );
  322. }
  323. function get_offline_payment_form_table( $form, $settings = array() ) {
  324. $default_status_options = it_exchange_offline_payments_get_default_status_options();
  325. if ( !empty( $settings ) )
  326. foreach ( $settings as $key => $var )
  327. $form->set_option( $key, $var );
  328. if ( ! empty( $_GET['page'] ) && 'it-exchange-setup' == $_GET['page'] ) : ?>
  329. <h3><?php _e( 'Offline Payments', 'it-l10n-ithemes-exchange' ); ?></h3>
  330. <?php endif; ?>
  331. <p><?php _e( 'Offline payments allow customers to purchase products from your site using check or cash. Transactions can be set as pending until you receive payment.', 'it-l10n-ithemes-exchange' ); ?></p>
  332. <p><?php _e( 'Video:', 'it-l10n-ithemes-exchange' ); ?>&nbsp;<a href="http://ithemes.com/tutorials/using-offline-payments-in-exchange/" target="_blank"><?php _e( 'Setting Up Offline Payments in Exchange', 'it-l10n-ithemes-exchange' ); ?></a></p>
  333. <p><?php _e( 'To process payments offline, complete the settings below.', 'it-l10n-ithemes-exchange' ); ?></p>
  334. <table class="form-table">
  335. <?php do_action( 'it_exchange_offline_payments_settings_table_top' ); ?>
  336. <tr valign="top">
  337. <th scope="row"><label for="offline-payments-title"><?php _e( 'Title', 'it-l10n-ithemes-exchange' ) ?> <span class="tip" title="<?php _e( 'What would you like to title this payment option? eg: Check', 'it-l10n-ithemes-exchange' ); ?>">i</span></label></th>
  338. <td>
  339. <?php $form->add_text_box( 'offline-payments-title', array( 'class' => 'normal-text' ) ); ?> </td>
  340. </tr>
  341. <tr valign="top">
  342. <th scope="row"><label for="offline-payments-instructions"><?php _e( 'Instructions after purchase', 'it-l10n-ithemes-exchange' ) ?> <span class="tip" title="<?php _e( 'This will be the notification customers see after using this method of payment.', 'it-l10n-ithemes-exchange' ); ?>">i</span></label></th>
  343. <td>
  344. <?php $form->add_text_area( 'offline-payments-instructions', array( 'cols' => 50, 'rows' => 5, 'class' => 'normal-text' ) ); ?>
  345. </td>
  346. </tr>
  347. <tr valign="top">
  348. <th scope="row"><label for="offline-payments-default-status"><?php _e( 'Default Payment Status', 'it-l10n-ithemes-exchange' ) ?> <span class="tip" title="<?php _e( 'This is the default payment status applied to all offline payment transactions.', 'it-l10n-ithemes-exchange' ); ?>">i</span></label></th>
  349. <td>
  350. <?php $form->add_drop_down( 'offline-payments-default-status', $default_status_options ); ?>
  351. </td>
  352. </tr>
  353. <?php do_action( 'it_exchange_offline_payments_settings_table_bottom' ); ?>
  354. <?php do_action( 'it_exchange_addon_settings_page_bottom' ); ?>
  355. </table>
  356. <?php
  357. }
  358. /**
  359. * Save settings
  360. *
  361. * @since 0.3.6
  362. * @return void
  363. */
  364. function save_settings() {
  365. $defaults = it_exchange_get_option( 'addon_offline_payments' );
  366. $new_values = wp_parse_args( ITForm::get_post_data(), $defaults );
  367. // Check nonce
  368. if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'it-exchange-offline-payments-settings' ) ) {
  369. $this->error_message = __( 'Error. Please try again', 'it-l10n-ithemes-exchange' );
  370. return;
  371. }
  372. $errors = apply_filters( 'it_exchange_add_on_manual_transaction_validate_settings', $this->get_form_errors( $new_values ), $new_values );
  373. if ( ! $errors && it_exchange_save_option( 'addon_offline_payments', $new_values ) ) {
  374. ITUtility::show_status_message( __( 'Settings saved.', 'it-l10n-ithemes-exchange' ) );
  375. } else if ( $errors ) {
  376. $errors = implode( '<br />', $errors );
  377. $this->error_message = $errors;
  378. } else {
  379. $this->status_message = __( 'Settings not saved.', 'it-l10n-ithemes-exchange' );
  380. }
  381. }
  382. function offline_payments_save_wizard_settings() {
  383. if ( !isset( $_REQUEST['it_exchange_settings-wizard-submitted'] ) )
  384. return;
  385. $offline_payments_settings = array();
  386. $default_wizard_offline_payments_settings = apply_filters( 'default_wizard_offline_payments_settings', array( 'offline-payments-title', 'offline-payments-instructions', 'offline-payments-default-status' ) );
  387. foreach( $default_wizard_offline_payments_settings as $var ) {
  388. if ( isset( $_REQUEST['it_exchange_settings-' . $var] ) ) {
  389. $offline_payments_settings[$var] = $_REQUEST['it_exchange_settings-' . $var];
  390. }
  391. }
  392. $settings = wp_parse_args( $offline_payments_settings, it_exchange_get_option( 'addon_offline_payments' ) );
  393. if ( $error_msg = $this->get_form_errors( $settings ) ) {
  394. return $error_msg;
  395. } else {
  396. it_exchange_save_option( 'addon_offline_payments', $settings );
  397. $this->status_message = __( 'Settings Saved.', 'it-l10n-ithemes-exchange' );
  398. }
  399. return;
  400. }
  401. /**
  402. * Validates for values
  403. *
  404. * Returns string of errors if anything is invalid
  405. *
  406. * @since 0.3.6
  407. * @return void
  408. */
  409. function get_form_errors( $values ) {
  410. $errors = array();
  411. if ( empty( $values['offline-payments-title'] ) )
  412. $errors[] = __( 'The Title field cannot be left blank', 'it-l10n-ithemes-exchange' );
  413. if ( empty( $values['offline-payments-instructions'] ) )
  414. $errors[] = __( 'Please leave some instructions for customers checking out with this transaction method', 'it-l10n-ithemes-exchange' );
  415. $valid_status_options = it_exchange_offline_payments_get_default_status_options();
  416. if ( empty( $values['offline-payments-default-status'] ) || empty( $valid_status_options[$values['offline-payments-default-status']] ) )
  417. $errors[] = __( 'Please select a valid default transaction status.', 'it-l10n-ithemes-exchange' );
  418. return $errors;
  419. }
  420. /**
  421. * Sets the default options for manual payment settings
  422. *
  423. * @since 0.3.6
  424. * @return array settings
  425. */
  426. function set_default_settings( $defaults ) {
  427. $defaults['offline-payments-title'] = __( 'Pay with check', 'it-l10n-ithemes-exchange' );
  428. $defaults['offline-payments-instructions'] = __( 'Thank you for your order. We will contact you shortly for payment.', 'it-l10n-ithemes-exchange' );
  429. $defaults['offline-payments-default-status'] = 'pending';
  430. return $defaults;
  431. }
  432. }
  433. /*
  434. * Handles expired transactions that are offline payments
  435. * If this product autorenews and is an offline payment, it should auto-renew
  436. * unless the susbcriber status has been deactivated already
  437. * If it autorenews, it creates an offline payment child transaction
  438. *
  439. * @since 1.3.1
  440. * @param bool $true Default True bool, passed from recurring payments expire schedule
  441. * @param int $product_id iThemes Exchange Product ID
  442. * @param object $transaction iThemes Exchange Transaction Object
  443. * @return bool True if expired, False if not Expired
  444. */
  445. function it_exchange_offline_payments_handle_expired( $true, $product_id, $transaction ) {
  446. $transaction_method = it_exchange_get_transaction_method( $transaction->ID );
  447. if ( 'offline-payments' === $transaction_method ) {
  448. $autorenews = $transaction->get_transaction_meta( 'subscription_autorenew_' . $product_id, true );
  449. $status = $transaction->get_transaction_meta( 'subscriber_status', true );
  450. if ( $autorenews && empty( $status ) ) { //if the subscriber status is empty, it hasn't been set, which really means it's active for offline payments
  451. //if the transaction autorenews and is an offline payment, we want to create a new child transaction until deactivated
  452. it_exchange_offline_payments_add_child_transaction( $transaction );
  453. return false;
  454. }
  455. }
  456. return $true;
  457. }
  458. add_filter( 'it_exchange_recurring_payments_handle_expired', 'it_exchange_offline_payments_handle_expired', 10, 3 );
  459. /**
  460. * Add a new transaction, really only used for subscription payments.
  461. * If a subscription pays again, we want to create another transaction in Exchange
  462. * This transaction needs to be linked to the parent transaction.
  463. *
  464. * @since 1.3.1
  465. *
  466. * @param string $parent_txn_id Parent Transaction ID
  467. * @return bool
  468. */
  469. function it_exchange_offline_payments_add_child_transaction( $parent_txn ) {
  470. $settings = it_exchange_get_option( 'addon_offline_payments' );
  471. $customer_id = get_post_meta( $parent_txn->ID, '_it_exchange_customer_id', true );
  472. if ( $customer_id ) {
  473. $uniqid = it_exchange_get_offline_transaction_uniqid();
  474. $transaction_object = new stdClass;
  475. $transaction_object->total = $parent_txn->cart_details->total;
  476. it_exchange_add_child_transaction( 'offline-payments', $uniqid, $settings['offline-payments-default-status'], $customer_id, $parent_txn->ID, $transaction_object );
  477. return true;
  478. }
  479. return false;
  480. }
  481. /**
  482. * Output the Cancel URL for the Payments screen
  483. *
  484. * @since 1.3.1
  485. *
  486. * @param object $transaction iThemes Transaction object
  487. * @return void
  488. */
  489. function it_exchange_offline_payments_checkout_after_payment_details_cancel_url( $transaction ) {
  490. $cart_object = get_post_meta( $transaction->ID, '_it_exchange_cart_object', true );
  491. foreach ( $cart_object->products as $product ) {
  492. $autorenews = $transaction->get_transaction_meta( 'subscription_autorenew_' . $product['product_id'], true );
  493. if ( $autorenews ) {
  494. $status = $transaction->get_transaction_meta( 'subscriber_status', true );
  495. switch( $status ) {
  496. case false: //active
  497. case '':
  498. $output = '<a href="' . add_query_arg( 'offline-payments-recurring-payment', 'cancel' ) . '">' . __( 'Cancel Recurring Payment', 'it-l10n-ithemes-exchange' ) . '</a>';
  499. break;
  500. case 'deactivated':
  501. default:
  502. $output = __( 'Recurring payment has been deactivated', 'it-l10n-ithemes-exchange' );
  503. break;
  504. }
  505. ?>
  506. <div class="transaction-autorenews clearfix spacing-wrapper">
  507. <div class="recurring-payment-cancel-options left">
  508. <div class="recurring-payment-status-name"><?php echo $output; ?></div>
  509. </div>
  510. </div>
  511. <?php
  512. continue;
  513. }
  514. }
  515. }
  516. add_action( 'it_exchange_after_payment_details_cancel_url_for_offline-payments', 'it_exchange_offline_payments_checkout_after_payment_details_cancel_url' );
  517. /**
  518. * Process Offline Payments Recurring Payments cancellations
  519. *
  520. * @since 1.3.1
  521. *
  522. * @return void
  523. */
  524. function it_exchange_process_offline_payments_recurring_payment_cancel() {
  525. if ( !empty( $_REQUEST['offline-payments-recurring-payment'] ) && 'cancel' === $_REQUEST['offline-payments-recurring-payment'] ) {
  526. if ( !empty( $_REQUEST['post'] ) && $post_id = $_REQUEST['post'] ) {
  527. $transaction = it_exchange_get_transaction( $post_id );
  528. $status = $transaction->update_transaction_meta( 'subscriber_status', 'cancel' );
  529. }
  530. }
  531. }
  532. add_action( 'admin_init', 'it_exchange_process_offline_payments_recurring_payment_cancel' );