From 319d34c105812c9051c0e7cac0a71abd4da67e11 Mon Sep 17 00:00:00 2001 From: Zvonimir Maglica Date: Wed, 26 Feb 2025 10:12:23 +0100 Subject: [PATCH] Add order note if payment tokens are missing or invalid on subscription orders (#10438) --- ...x-8894-add-order-note-if-payment-tokens-are-missing | 4 ++++ .../trait-wc-payment-gateway-wcpay-subscriptions.php | 2 ++ ...st-class-wc-payment-gateway-wcpay-subscriptions.php | 10 +++++++++- 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 changelog/fix-8894-add-order-note-if-payment-tokens-are-missing diff --git a/changelog/fix-8894-add-order-note-if-payment-tokens-are-missing b/changelog/fix-8894-add-order-note-if-payment-tokens-are-missing new file mode 100644 index 00000000000..269b4c126eb --- /dev/null +++ b/changelog/fix-8894-add-order-note-if-payment-tokens-are-missing @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Add an order note when a recurring payment fails or when updating the payment method fails due to a missing or invalid payment token. diff --git a/includes/compat/subscriptions/trait-wc-payment-gateway-wcpay-subscriptions.php b/includes/compat/subscriptions/trait-wc-payment-gateway-wcpay-subscriptions.php index 53d6cb43200..4ac83a586f4 100644 --- a/includes/compat/subscriptions/trait-wc-payment-gateway-wcpay-subscriptions.php +++ b/includes/compat/subscriptions/trait-wc-payment-gateway-wcpay-subscriptions.php @@ -326,6 +326,7 @@ public function scheduled_subscription_payment( $amount, $renewal_order ) { $token = $this->get_payment_token( $renewal_order ); if ( is_null( $token ) && ! WC_Payments::is_network_saved_cards_enabled() ) { + $renewal_order->add_order_note( 'Subscription renewal failed: No saved payment method found.' ); Logger::error( 'There is no saved payment token for order #' . $renewal_order->get_id() ); // TODO: Update to use Order_Service->mark_payment_failed. $renewal_order->update_status( 'failed' ); @@ -380,6 +381,7 @@ public function scheduled_subscription_payment( $amount, $renewal_order ) { public function update_failing_payment_method( $subscription, $renewal_order ) { $renewal_token = $this->get_payment_token( $renewal_order ); if ( is_null( $renewal_token ) ) { + $renewal_order->add_order_note( 'Unable to update subscription payment method: No valid payment token or method found.' ); Logger::error( 'Failing subscription could not be updated: there is no saved payment token for order #' . $renewal_order->get_id() ); return; } diff --git a/tests/unit/test-class-wc-payment-gateway-wcpay-subscriptions.php b/tests/unit/test-class-wc-payment-gateway-wcpay-subscriptions.php index fe96d9d9834..c2b4b661150 100644 --- a/tests/unit/test-class-wc-payment-gateway-wcpay-subscriptions.php +++ b/tests/unit/test-class-wc-payment-gateway-wcpay-subscriptions.php @@ -265,7 +265,15 @@ public function test_update_failing_payment_method_copies_last_method_from_renew public function test_update_failing_payment_method_does_not_copy_method_if_renewal_has_no_method() { $subscription = WC_Helper_Order::create_order( self::USER_ID ); - $renewal_order = WC_Helper_Order::create_order( self::USER_ID ); + $renewal_order = $this->createMock( WC_Order::class ); + + $renewal_order->expects( $this->once() ) + ->method( 'get_payment_tokens' ) + ->willReturn( [] ); + + $renewal_order->expects( $this->once() ) + ->method( 'add_order_note' ) + ->with( 'Unable to update subscription payment method: No valid payment token or method found.' ); $this->wcpay_gateway->update_failing_payment_method( $subscription, $renewal_order );