From 75cccf077825ec0d6f57c99dc6dc9813c726ce13 Mon Sep 17 00:00:00 2001 From: Kcppdevelopers Date: Sat, 19 Aug 2017 22:10:30 +0530 Subject: [PATCH] PayFlow : Woo subscription issue with renewal, ref #823 --- ...eway-braintree-subscriptions-angelleye.php | 2 +- ...aypal-advanced-subscriptions-angelleye.php | 52 +++++++++++++++--- ...dit-cards-rest-subscriptions-angelleye.php | 2 +- ...paypal-express-subscriptions-angelleye.php | 2 +- ...al-pro-payflow-subscriptions-angelleye.php | 53 ++++++++++++++++--- ...way-paypal-pro-subscriptions-angelleye.php | 52 +++++++++++++++--- 6 files changed, 138 insertions(+), 25 deletions(-) diff --git a/classes/subscriptions/wc-gateway-braintree-subscriptions-angelleye.php b/classes/subscriptions/wc-gateway-braintree-subscriptions-angelleye.php index b21cbab82..ad3c77e82 100644 --- a/classes/subscriptions/wc-gateway-braintree-subscriptions-angelleye.php +++ b/classes/subscriptions/wc-gateway-braintree-subscriptions-angelleye.php @@ -54,7 +54,7 @@ public function add_subscription_payment_meta($payment_meta, $subscription) { public function validate_subscription_payment_meta($payment_method_id, $payment_meta) { if ($this->id === $payment_method_id) { - if (!empty($payment_meta['post_meta']['_payment_tokens_id']['value']) && empty($payment_meta['post_meta']['_payment_tokens_id']['value'])) { + if (empty($payment_meta['post_meta']['_payment_tokens_id']['value'])) { throw new Exception('A "_payment_tokens_id" value is required.'); } } diff --git a/classes/subscriptions/wc-gateway-paypal-advanced-subscriptions-angelleye.php b/classes/subscriptions/wc-gateway-paypal-advanced-subscriptions-angelleye.php index 9607f2798..9ff6876ec 100644 --- a/classes/subscriptions/wc-gateway-paypal-advanced-subscriptions-angelleye.php +++ b/classes/subscriptions/wc-gateway-paypal-advanced-subscriptions-angelleye.php @@ -5,17 +5,19 @@ } class WC_Gateway_PayPal_Advanced_Subscriptions_AngellEYE extends WC_Gateway_PayPal_Advanced_AngellEYE { + public $wc_pre_30; + public function __construct() { parent::__construct(); if (class_exists('WC_Subscriptions_Order')) { add_action('woocommerce_scheduled_subscription_payment_' . $this->id, array($this, 'scheduled_subscription_payment'), 10, 2); add_filter('woocommerce_subscription_payment_meta', array($this, 'add_subscription_payment_meta'), 10, 2); - add_filter('woocommerce_subscription_validate_payment_meta', array($this, 'validate_subscription_payment_meta'), 10, 2); + add_filter('woocommerce_subscription_validate_payment_meta', array($this, 'validate_subscription_payment_meta'), 10, 3); add_action('wcs_resubscribe_order_created', array($this, 'delete_resubscribe_meta'), 10); add_action('woocommerce_subscription_failing_payment_method_updated_' . $this->id, array($this, 'update_failing_payment_method'), 10, 2); } - $this->wc_pre_30 = version_compare( WC_VERSION, '3.0.0', '<' ); + $this->wc_pre_30 = version_compare(WC_VERSION, '3.0.0', '<'); } public function is_subscription($order_id) { @@ -31,6 +33,7 @@ public function process_payment($order_id) { } public function scheduled_subscription_payment($amount_to_charge, $renewal_order) { + $this->angelleye_scheduled_subscription_payment_retry_compability($renewal_order); parent::process_payment($renewal_order); } @@ -47,10 +50,17 @@ public function add_subscription_payment_meta($payment_meta, $subscription) { return $payment_meta; } - public function validate_subscription_payment_meta($payment_method_id, $payment_meta) { + public function validate_subscription_payment_meta($payment_method_id, $payment_meta, $subscription) { if ($this->id === $payment_method_id) { - if (!empty($payment_meta['post_meta']['_payment_tokens_id']['value']) && empty($payment_meta['post_meta']['_payment_tokens_id']['value'])) { - throw new Exception('A "_payment_tokens_id" value is required.'); + if (empty($payment_meta['post_meta']['_payment_tokens_id']['value'])) { + $subscription_id = $this->wc_pre_30 ? $subscription->id : $subscription->get_id(); + $subscription_parent_id = $this->wc_pre_30 ? $subscription->parent_id : $subscription->get_parent_id(); + $payment_tokens_id = get_post_meta($subscription_parent_id, '_transaction_id', true); + if (!empty($payment_tokens_id)) { + update_post_meta($subscription_id, '_payment_tokens_id', $payment_tokens_id); + } else { + throw new Exception('A "_payment_tokens_id" value is required.'); + } } } } @@ -80,8 +90,36 @@ public function delete_resubscribe_meta($resubscribe_order) { } public function update_failing_payment_method($subscription, $renewal_order) { - $subscription_id = $this->wc_pre_30 ? $subscription->id : $subscription->get_id(); - update_post_meta($subscription_id, '_payment_tokens_id', $renewal_order->payment_tokens_id); + if ($this->wc_pre_30) { + update_post_meta($subscription->id, '_payment_tokens_id', $renewal_order->payment_tokens_id); + } else { + $subscription->update_meta_data('_payment_tokens_id', $renewal_order->get_meta('_payment_tokens_id', true)); + } + } + + public function angelleye_scheduled_subscription_payment_retry_compability($renewal_order) { + $renewal_order_id = $this->wc_pre_30 ? $renewal_order->id : $renewal_order->get_id(); + $payment_tokens_id = get_post_meta($renewal_order_id, '_payment_tokens_id', true); + if (empty($payment_tokens_id) || $payment_tokens_id == false) { + if (function_exists('wcs_order_contains_subscription') && wcs_order_contains_subscription($renewal_order_id)) { + $subscriptions = wcs_get_subscriptions_for_order($renewal_order_id); + } elseif (function_exists('wcs_order_contains_renewal') && wcs_order_contains_renewal($renewal_order_id)) { + $subscriptions = wcs_get_subscriptions_for_renewal_order($renewal_order_id); + } else { + $subscriptions = array(); + } + if (!empty($subscriptions)) { + foreach ($subscriptions as $subscription) { + $subscription_id = $this->wc_pre_30 ? $subscription->id : $subscription->get_id(); + $subscription_parent_id = $this->wc_pre_30 ? $subscription->parent_id : $subscription->get_parent_id(); + $payment_tokens_id = get_post_meta($subscription_parent_id, '_transaction_id', true); + if (!empty($payment_tokens_id)) { + update_post_meta($subscription_id, '_payment_tokens_id', $payment_tokens_id); + update_post_meta($renewal_order_id, '_payment_tokens_id', $payment_tokens_id); + } + } + } + } } } diff --git a/classes/subscriptions/wc-gateway-paypal-credit-cards-rest-subscriptions-angelleye.php b/classes/subscriptions/wc-gateway-paypal-credit-cards-rest-subscriptions-angelleye.php index 24d456c5c..89c4e4057 100644 --- a/classes/subscriptions/wc-gateway-paypal-credit-cards-rest-subscriptions-angelleye.php +++ b/classes/subscriptions/wc-gateway-paypal-credit-cards-rest-subscriptions-angelleye.php @@ -59,7 +59,7 @@ public function add_subscription_payment_meta($payment_meta, $subscription) { public function validate_subscription_payment_meta($payment_method_id, $payment_meta) { if ($this->id === $payment_method_id) { - if (!empty($payment_meta['post_meta']['_payment_tokens_id']['value']) && empty($payment_meta['post_meta']['_payment_tokens_id']['value'])) { + if (empty($payment_meta['post_meta']['_payment_tokens_id']['value'])) { throw new Exception('A "_payment_tokens_id" value is required.'); } } diff --git a/classes/subscriptions/wc-gateway-paypal-express-subscriptions-angelleye.php b/classes/subscriptions/wc-gateway-paypal-express-subscriptions-angelleye.php index cb40df5af..24db8049f 100644 --- a/classes/subscriptions/wc-gateway-paypal-express-subscriptions-angelleye.php +++ b/classes/subscriptions/wc-gateway-paypal-express-subscriptions-angelleye.php @@ -59,7 +59,7 @@ public function add_subscription_payment_meta($payment_meta, $subscription) { public function validate_subscription_payment_meta($payment_method_id, $payment_meta) { if ($this->id === $payment_method_id) { - if (!empty($payment_meta['post_meta']['_payment_tokens_id']['value']) && empty($payment_meta['post_meta']['_payment_tokens_id']['value'])) { + if (empty($payment_meta['post_meta']['_payment_tokens_id']['value'])) { throw new Exception('A "_payment_tokens_id" value is required.'); } } diff --git a/classes/subscriptions/wc-gateway-paypal-pro-payflow-subscriptions-angelleye.php b/classes/subscriptions/wc-gateway-paypal-pro-payflow-subscriptions-angelleye.php index 26f791b8e..5c17ff0f4 100644 --- a/classes/subscriptions/wc-gateway-paypal-pro-payflow-subscriptions-angelleye.php +++ b/classes/subscriptions/wc-gateway-paypal-pro-payflow-subscriptions-angelleye.php @@ -5,17 +5,19 @@ } class WC_Gateway_PayPal_Pro_PayFlow_Subscriptions_AngellEYE extends WC_Gateway_PayPal_Pro_PayFlow_AngellEYE { + public $wc_pre_30; + public function __construct() { parent::__construct(); if (class_exists('WC_Subscriptions_Order')) { add_action('woocommerce_scheduled_subscription_payment_' . $this->id, array($this, 'scheduled_subscription_payment'), 10, 2); add_filter('woocommerce_subscription_payment_meta', array($this, 'add_subscription_payment_meta'), 10, 2); - add_filter('woocommerce_subscription_validate_payment_meta', array($this, 'validate_subscription_payment_meta'), 10, 2); + add_filter('woocommerce_subscription_validate_payment_meta', array($this, 'validate_subscription_payment_meta'), 10, 3); add_action('wcs_resubscribe_order_created', array($this, 'delete_resubscribe_meta'), 10); add_action('woocommerce_subscription_failing_payment_method_updated_' . $this->id, array($this, 'update_failing_payment_method'), 10, 2); } - $this->wc_pre_30 = version_compare( WC_VERSION, '3.0.0', '<' ); + $this->wc_pre_30 = version_compare(WC_VERSION, '3.0.0', '<'); } public function is_subscription($order_id) { @@ -31,6 +33,7 @@ public function process_payment($order_id) { } public function scheduled_subscription_payment($amount_to_charge, $renewal_order) { + $this->angelleye_scheduled_subscription_payment_retry_compability($renewal_order); parent::process_subscription_payment($renewal_order, $amount_to_charge); } @@ -47,10 +50,17 @@ public function add_subscription_payment_meta($payment_meta, $subscription) { return $payment_meta; } - public function validate_subscription_payment_meta($payment_method_id, $payment_meta) { + public function validate_subscription_payment_meta($payment_method_id, $payment_meta, $subscription) { if ($this->id === $payment_method_id) { - if (!empty($payment_meta['post_meta']['_payment_tokens_id']['value']) && empty($payment_meta['post_meta']['_payment_tokens_id']['value'])) { - throw new Exception('A "_payment_tokens_id" value is required.'); + if (empty($payment_meta['post_meta']['_payment_tokens_id']['value'])) { + $subscription_id = $this->wc_pre_30 ? $subscription->id : $subscription->get_id(); + $subscription_parent_id = $this->wc_pre_30 ? $subscription->parent_id : $subscription->get_parent_id(); + $payment_tokens_id = get_post_meta($subscription_parent_id, '_transaction_id', true); + if (!empty($payment_tokens_id)) { + update_post_meta($subscription_id, '_payment_tokens_id', $payment_tokens_id); + } else { + throw new Exception('A "_payment_tokens_id" value is required.'); + } } } } @@ -80,8 +90,35 @@ public function delete_resubscribe_meta($resubscribe_order) { } public function update_failing_payment_method($subscription, $renewal_order) { - $subscription_id = $this->wc_pre_30 ? $subscription->id : $subscription->get_id(); - update_post_meta($subscription_id, '_payment_tokens_id', $renewal_order->payment_tokens_id); + if ($this->wc_pre_30) { + update_post_meta($subscription->id, '_payment_tokens_id', $renewal_order->payment_tokens_id); + } else { + $subscription->update_meta_data('_payment_tokens_id', $renewal_order->get_meta('_payment_tokens_id', true)); + } } -} + public function angelleye_scheduled_subscription_payment_retry_compability($renewal_order) { + $renewal_order_id = $this->wc_pre_30 ? $renewal_order->id : $renewal_order->get_id(); + $payment_tokens_id = get_post_meta($renewal_order_id, '_payment_tokens_id', true); + if (empty($payment_tokens_id) || $payment_tokens_id == false) { + if (function_exists('wcs_order_contains_subscription') && wcs_order_contains_subscription($renewal_order_id)) { + $subscriptions = wcs_get_subscriptions_for_order($renewal_order_id); + } elseif (function_exists('wcs_order_contains_renewal') && wcs_order_contains_renewal($renewal_order_id)) { + $subscriptions = wcs_get_subscriptions_for_renewal_order($renewal_order_id); + } else { + $subscriptions = array(); + } + if (!empty($subscriptions)) { + foreach ($subscriptions as $subscription) { + $subscription_id = $this->wc_pre_30 ? $subscription->id : $subscription->get_id(); + $subscription_parent_id = $this->wc_pre_30 ? $subscription->parent_id : $subscription->get_parent_id(); + $payment_tokens_id = get_post_meta($subscription_parent_id, '_transaction_id', true); + if (!empty($payment_tokens_id)) { + update_post_meta($subscription_id, '_payment_tokens_id', $payment_tokens_id); + update_post_meta($renewal_order_id, '_payment_tokens_id', $payment_tokens_id); + } + } + } + } + } +} \ No newline at end of file diff --git a/classes/subscriptions/wc-gateway-paypal-pro-subscriptions-angelleye.php b/classes/subscriptions/wc-gateway-paypal-pro-subscriptions-angelleye.php index a3fe9c156..9a94e5b90 100644 --- a/classes/subscriptions/wc-gateway-paypal-pro-subscriptions-angelleye.php +++ b/classes/subscriptions/wc-gateway-paypal-pro-subscriptions-angelleye.php @@ -5,17 +5,19 @@ } class WC_Gateway_PayPal_Pro_Subscriptions_AngellEYE extends WC_Gateway_PayPal_Pro_AngellEYE { + public $wc_pre_30; + public function __construct() { parent::__construct(); if (class_exists('WC_Subscriptions_Order')) { add_action('woocommerce_scheduled_subscription_payment_' . $this->id, array($this, 'scheduled_subscription_payment'), 10, 2); add_filter('woocommerce_subscription_payment_meta', array($this, 'add_subscription_payment_meta'), 10, 2); - add_filter('woocommerce_subscription_validate_payment_meta', array($this, 'validate_subscription_payment_meta'), 10, 2); + add_filter('woocommerce_subscription_validate_payment_meta', array($this, 'validate_subscription_payment_meta'), 10, 3); add_action('wcs_resubscribe_order_created', array($this, 'delete_resubscribe_meta'), 10); add_action('woocommerce_subscription_failing_payment_method_updated_' . $this->id, array($this, 'update_failing_payment_method'), 10, 2); } - $this->wc_pre_30 = version_compare( WC_VERSION, '3.0.0', '<' ); + $this->wc_pre_30 = version_compare(WC_VERSION, '3.0.0', '<'); } public function is_subscription($order_id) { @@ -35,6 +37,7 @@ public function process_payment($order_id) { } public function scheduled_subscription_payment($amount_to_charge, $renewal_order) { + $this->angelleye_scheduled_subscription_payment_retry_compability($renewal_order); parent::process_subscription_payment($renewal_order, $amount_to_charge); } @@ -51,10 +54,17 @@ public function add_subscription_payment_meta($payment_meta, $subscription) { return $payment_meta; } - public function validate_subscription_payment_meta($payment_method_id, $payment_meta) { + public function validate_subscription_payment_meta($payment_method_id, $payment_meta, $subscription) { if ($this->id === $payment_method_id) { - if (!empty($payment_meta['post_meta']['_payment_tokens_id']['value']) && empty($payment_meta['post_meta']['_payment_tokens_id']['value'])) { - throw new Exception('A "_payment_tokens_id" value is required.'); + if (empty($payment_meta['post_meta']['_payment_tokens_id']['value'])) { + $subscription_id = $this->wc_pre_30 ? $subscription->id : $subscription->get_id(); + $subscription_parent_id = $this->wc_pre_30 ? $subscription->parent_id : $subscription->get_parent_id(); + $payment_tokens_id = get_post_meta($subscription_parent_id, '_transaction_id', true); + if (!empty($payment_tokens_id)) { + update_post_meta($subscription_id, '_payment_tokens_id', $payment_tokens_id); + } else { + throw new Exception('A "_payment_tokens_id" value is required.'); + } } } } @@ -84,8 +94,11 @@ public function delete_resubscribe_meta($resubscribe_order) { } public function update_failing_payment_method($subscription, $renewal_order) { - $subscription_id = $this->wc_pre_30 ? $subscription->id : $subscription->get_id(); - update_post_meta($subscription_id, '_payment_tokens_id', $renewal_order->payment_tokens_id); + if ($this->wc_pre_30) { + update_post_meta($subscription->id, '_payment_tokens_id', $renewal_order->payment_tokens_id); + } else { + $subscription->update_meta_data('_payment_tokens_id', $renewal_order->get_meta('_payment_tokens_id', true)); + } } public function free_signup_with_token_payment_tokenization($order_id) { @@ -98,4 +111,29 @@ public function free_signup_with_token_payment_tokenization($order_id) { return false; } + public function angelleye_scheduled_subscription_payment_retry_compability($renewal_order) { + $renewal_order_id = $this->wc_pre_30 ? $renewal_order->id : $renewal_order->get_id(); + $payment_tokens_id = get_post_meta($renewal_order_id, '_payment_tokens_id', true); + if (empty($payment_tokens_id) || $payment_tokens_id == false) { + if (function_exists('wcs_order_contains_subscription') && wcs_order_contains_subscription($renewal_order_id)) { + $subscriptions = wcs_get_subscriptions_for_order($renewal_order_id); + } elseif (function_exists('wcs_order_contains_renewal') && wcs_order_contains_renewal($renewal_order_id)) { + $subscriptions = wcs_get_subscriptions_for_renewal_order($renewal_order_id); + } else { + $subscriptions = array(); + } + if (!empty($subscriptions)) { + foreach ($subscriptions as $subscription) { + $subscription_id = $this->wc_pre_30 ? $subscription->id : $subscription->get_id(); + $subscription_parent_id = $this->wc_pre_30 ? $subscription->parent_id : $subscription->get_parent_id(); + $payment_tokens_id = get_post_meta($subscription_parent_id, '_transaction_id', true); + if (!empty($payment_tokens_id)) { + update_post_meta($subscription_id, '_payment_tokens_id', $payment_tokens_id); + update_post_meta($renewal_order_id, '_payment_tokens_id', $payment_tokens_id); + } + } + } + } + } + }