From c3de0c28ece149e1ec4d6d170fc883ea07a430e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=BCsken?= Date: Wed, 12 Feb 2025 14:05:44 +0100 Subject: [PATCH 1/5] add som logs on card renewals when subscription not found --- modules/ppcp-wc-subscriptions/src/RenewalHandler.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/modules/ppcp-wc-subscriptions/src/RenewalHandler.php b/modules/ppcp-wc-subscriptions/src/RenewalHandler.php index f0900881d..0b75702f7 100644 --- a/modules/ppcp-wc-subscriptions/src/RenewalHandler.php +++ b/modules/ppcp-wc-subscriptions/src/RenewalHandler.php @@ -556,7 +556,11 @@ private function card_payment_source( string $token, WC_Order $wc_order ): Payme 'usage' => 'SUBSEQUENT', 'previous_transaction_reference' => $transaction, ); + } else { + $this->logger->warning( sprintf( 'Previous transaction not found for subscription %s', $subscription->get_id() ) ); } + } else { + $this->logger->warning( sprintf( 'Subscription not found for renewal order %s', $wc_order->get_id() ) ); } return new PaymentSource( From b72a6232be2d0bb1724aaab7a4b6e435e88759c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=BCsken?= Date: Thu, 13 Feb 2025 08:16:13 +0100 Subject: [PATCH 2/5] check payment method of current oder to resolve PAYMENT_SOURCE_MISMATCH --- .../src/Helper/SubscriptionHelper.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/modules/ppcp-wc-subscriptions/src/Helper/SubscriptionHelper.php b/modules/ppcp-wc-subscriptions/src/Helper/SubscriptionHelper.php index 45e481728..3e6d9283e 100644 --- a/modules/ppcp-wc-subscriptions/src/Helper/SubscriptionHelper.php +++ b/modules/ppcp-wc-subscriptions/src/Helper/SubscriptionHelper.php @@ -19,8 +19,6 @@ use WC_Subscriptions_Product; use WCS_Manual_Renewal_Manager; use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException; -use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway; -use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway; /** * Class SubscriptionHelper @@ -295,18 +293,16 @@ public function previous_transaction( WC_Subscription $subscription ): string { return ''; } - // Sort orders by key descending. - krsort( $orders ); - - // Removes first order (the current processing order). - unset( $orders[ array_key_first( $orders ) ] ); + // Sort orders by oder ID descending. + rsort( $orders ); + $current_order = wc_get_order( array_shift( $orders ) ); foreach ( $orders as $order_id ) { $order = wc_get_order( $order_id ); if ( is_a( $order, WC_Order::class ) && in_array( $order->get_status(), array( 'processing', 'completed' ), true ) - && in_array( $order->get_payment_method(), array( PayPalGateway::ID, CreditCardGateway::ID ), true ) + && $current_order->get_payment_method() === $order->get_payment_method() ) { $transaction_id = $order->get_transaction_id(); if ( $transaction_id ) { From bb9aab400714598ee06f5272f2d2f6354433859e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=BCsken?= Date: Thu, 13 Feb 2025 08:46:14 +0100 Subject: [PATCH 3/5] change log message from warning to debug --- modules/ppcp-wc-subscriptions/src/RenewalHandler.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/ppcp-wc-subscriptions/src/RenewalHandler.php b/modules/ppcp-wc-subscriptions/src/RenewalHandler.php index 0b75702f7..82341fc8d 100644 --- a/modules/ppcp-wc-subscriptions/src/RenewalHandler.php +++ b/modules/ppcp-wc-subscriptions/src/RenewalHandler.php @@ -557,10 +557,10 @@ private function card_payment_source( string $token, WC_Order $wc_order ): Payme 'previous_transaction_reference' => $transaction, ); } else { - $this->logger->warning( sprintf( 'Previous transaction not found for subscription %s', $subscription->get_id() ) ); + $this->logger->debug( sprintf( 'Previous transaction not found for subscription %s', $subscription->get_id() ) ); } } else { - $this->logger->warning( sprintf( 'Subscription not found for renewal order %s', $wc_order->get_id() ) ); + $this->logger->debug( sprintf( 'Subscription not found for renewal order %s', $wc_order->get_id() ) ); } return new PaymentSource( From b9b6af888efdaa287ee6efd4c03a01ab0cbcb3cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=BCsken?= Date: Thu, 13 Feb 2025 09:39:28 +0100 Subject: [PATCH 4/5] add change when no order is found --- .../ppcp-wc-subscriptions/src/Helper/SubscriptionHelper.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modules/ppcp-wc-subscriptions/src/Helper/SubscriptionHelper.php b/modules/ppcp-wc-subscriptions/src/Helper/SubscriptionHelper.php index 3e6d9283e..6500d03b0 100644 --- a/modules/ppcp-wc-subscriptions/src/Helper/SubscriptionHelper.php +++ b/modules/ppcp-wc-subscriptions/src/Helper/SubscriptionHelper.php @@ -296,6 +296,9 @@ public function previous_transaction( WC_Subscription $subscription ): string { // Sort orders by oder ID descending. rsort( $orders ); $current_order = wc_get_order( array_shift( $orders ) ); + if ( ! $current_order ) { + return ''; + } foreach ( $orders as $order_id ) { $order = wc_get_order( $order_id ); From c21bbf38f13e9420965e318411e01fb03f1e2051 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=BCsken?= Date: Thu, 13 Feb 2025 10:01:14 +0100 Subject: [PATCH 5/5] check if we have an WC-Order --- modules/ppcp-wc-subscriptions/src/Helper/SubscriptionHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ppcp-wc-subscriptions/src/Helper/SubscriptionHelper.php b/modules/ppcp-wc-subscriptions/src/Helper/SubscriptionHelper.php index 6500d03b0..1853bc69c 100644 --- a/modules/ppcp-wc-subscriptions/src/Helper/SubscriptionHelper.php +++ b/modules/ppcp-wc-subscriptions/src/Helper/SubscriptionHelper.php @@ -296,7 +296,7 @@ public function previous_transaction( WC_Subscription $subscription ): string { // Sort orders by oder ID descending. rsort( $orders ); $current_order = wc_get_order( array_shift( $orders ) ); - if ( ! $current_order ) { + if ( ! $current_order instanceof WC_Order ) { return ''; }