From 694929b0bf2612bdaa24fc9fa2f8cd3de950b521 Mon Sep 17 00:00:00 2001 From: Brent Shepherd Date: Tue, 21 Jul 2015 18:00:00 -0700 Subject: [PATCH] Simplify: Update change failling payment method For Subscriptions v2.0. * Use new 'woocommerce_subscription_failing_payment_method_updated_' hook * Use new wcs_is_subscription() method to run process_subscription() when the transaction is for a subscription object. * Save the card and customer IDs on the 'shop_subscription' post from the renewal order rather than saving it on the order which purchased the subscription. --- ...ddons-gateway-simplify-commerce-deprecated.php | 15 +++++++++++++++ .../class-wc-addons-gateway-simplify-commerce.php | 13 +++++-------- .../class-wc-gateway-simplify-commerce.php | 3 ++- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/includes/gateways/simplify-commerce-deprecated/class-wc-addons-gateway-simplify-commerce-deprecated.php b/includes/gateways/simplify-commerce-deprecated/class-wc-addons-gateway-simplify-commerce-deprecated.php index 41e7c1494da..874d962429d 100644 --- a/includes/gateways/simplify-commerce-deprecated/class-wc-addons-gateway-simplify-commerce-deprecated.php +++ b/includes/gateways/simplify-commerce-deprecated/class-wc-addons-gateway-simplify-commerce-deprecated.php @@ -25,6 +25,7 @@ class WC_Addons_Gateway_Simplify_Commerce_Deprecated extends WC_Addons_Gateway_S if ( class_exists( 'WC_Subscriptions_Order' ) ) { add_action( 'scheduled_subscription_payment_' . $this->id, array( $this, 'process_scheduled_subscription_payment' ), 10, 3 ); add_filter( 'woocommerce_subscriptions_renewal_order_meta_query', array( $this, 'remove_renewal_order_meta' ), 10, 4 ); + add_action( 'woocommerce_subscriptions_changed_failing_payment_method_' . $this->id, array( $this, 'change_failing_payment_method' ), 10, 3 ); } } @@ -159,4 +160,18 @@ class WC_Addons_Gateway_Simplify_Commerce_Deprecated extends WC_Addons_Gateway_S protected function order_contains_subscription( $order_id ) { return class_exists( 'WC_Subscriptions_Order' ) && ( WC_Subscriptions_Order::order_contains_subscription( $order_id ) || WC_Subscriptions_Renewal_Order::is_renewal( $order_id ) ); } + + /** + * Update the customer_id for a subscription after using Simplify to complete a payment to make up for + * an automatic renewal payment which previously failed. + * + * @param WC_Order $original_order The original order in which the subscription was purchased. + * @param WC_Order $renewal_order The order which recorded the successful payment (to make up for the failed automatic payment). + * @param string $subscription_key A subscription key of the form created by @see WC_Subscriptions_Manager::get_subscription_key() + */ + public function change_failing_payment_method( $original_order, $renewal_order, $subscription_key ) { + $new_customer_id = get_post_meta( $renewal_order->id, '_simplify_customer_id', true ); + + update_post_meta( $original_order->id, '_simplify_customer_id', $new_customer_id ); + } } diff --git a/includes/gateways/simplify-commerce/class-wc-addons-gateway-simplify-commerce.php b/includes/gateways/simplify-commerce/class-wc-addons-gateway-simplify-commerce.php index d395da16bf0..09a24b13a80 100644 --- a/includes/gateways/simplify-commerce/class-wc-addons-gateway-simplify-commerce.php +++ b/includes/gateways/simplify-commerce/class-wc-addons-gateway-simplify-commerce.php @@ -24,7 +24,7 @@ class WC_Addons_Gateway_Simplify_Commerce extends WC_Gateway_Simplify_Commerce { if ( class_exists( 'WC_Subscriptions_Order' ) ) { add_action( 'woocommerce_scheduled_subscription_payment_' . $this->id, array( $this, 'scheduled_subscription_payment' ), 10, 2 ); - add_action( 'woocommerce_subscriptions_changed_failing_payment_method_' . $this->id, array( $this, 'update_failing_payment_method' ), 10, 3 ); + add_action( 'woocommerce_subscription_failing_payment_method_updated_' . $this->id, array( $this, 'update_failing_payment_method' ), 10, 2 ); add_action( 'wcs_resubscribe_order_created', array( $this, 'delete_resubscribe_meta' ), 10 ); @@ -258,7 +258,7 @@ class WC_Addons_Gateway_Simplify_Commerce extends WC_Gateway_Simplify_Commerce { $order = wc_get_order( $order_id ); // Processing subscription - if ( 'standard' == $this->mode && $this->order_contains_subscription( $order->id ) ) { + if ( 'standard' == $this->mode && ( $this->order_contains_subscription( $order->id ) || ( function_exists( 'wcs_is_subscription' ) && wcs_is_subscription( $order_id ) ) ) ) { return $this->process_subscription( $order, $cart_token ); // Processing pre-order @@ -362,14 +362,11 @@ class WC_Addons_Gateway_Simplify_Commerce extends WC_Gateway_Simplify_Commerce { * Update the customer_id for a subscription after using Simplify to complete a payment to make up for * an automatic renewal payment which previously failed. * - * @param WC_Order $original_order The original order in which the subscription was purchased. + * @param WC_Subscription $subscription The subscription for which the failing payment method relates. * @param WC_Order $renewal_order The order which recorded the successful payment (to make up for the failed automatic payment). - * @param string $subscription_key A subscription key of the form created by @see WC_Subscriptions_Manager::get_subscription_key() */ - public function update_failing_payment_method( $original_order, $renewal_order, $subscription_key ) { - $new_customer_id = get_post_meta( $renewal_order->id, '_simplify_customer_id', true ); - - update_post_meta( $original_order->id, '_simplify_customer_id', $new_customer_id ); + public function update_failing_payment_method( $subscription, $renewal_order ) { + update_post_meta( $original_order->id, '_simplify_customer_id', get_post_meta( $renewal_order->id, '_simplify_customer_id', true ) ); } /** diff --git a/includes/gateways/simplify-commerce/class-wc-gateway-simplify-commerce.php b/includes/gateways/simplify-commerce/class-wc-gateway-simplify-commerce.php index 6ef85ed28e0..b01741e49ee 100644 --- a/includes/gateways/simplify-commerce/class-wc-gateway-simplify-commerce.php +++ b/includes/gateways/simplify-commerce/class-wc-gateway-simplify-commerce.php @@ -31,7 +31,8 @@ class WC_Gateway_Simplify_Commerce extends WC_Payment_Gateway { 'subscription_reactivation', 'subscription_suspension', 'subscription_amount_changes', - 'subscription_payment_method_change', + 'subscription_payment_method_change', // Subscriptions 1.n compatibility + 'subscription_payment_method_change_customer', 'subscription_payment_method_change_admin', 'subscription_date_changes', 'default_credit_card_form',