diff --git a/modules/ppcp-button/src/Assets/SmartButton.php b/modules/ppcp-button/src/Assets/SmartButton.php index d63404421..f1592c734 100644 --- a/modules/ppcp-button/src/Assets/SmartButton.php +++ b/modules/ppcp-button/src/Assets/SmartButton.php @@ -1171,7 +1171,7 @@ class SmartButton implements SmartButtonInterface { /** * Filter to add further components from the extensions. * - * @internal Matches filter name in GooglePay extension. + * @internal Matches filter name in APM extension. * @since TODO * * @param array $components The array of components already registered. diff --git a/modules/ppcp-subscription/src/RenewalHandler.php b/modules/ppcp-subscription/src/RenewalHandler.php index cd2385499..a45eb638c 100644 --- a/modules/ppcp-subscription/src/RenewalHandler.php +++ b/modules/ppcp-subscription/src/RenewalHandler.php @@ -9,6 +9,7 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\Subscription; +use WC_Subscription; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Entity\Order; use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken; @@ -139,8 +140,16 @@ class RenewalHandler { * * @param \WC_Order $wc_order The WooCommerce order. */ - public function renew( \WC_Order $wc_order ) { + public function renew( \WC_Order $wc_order ): void { try { + $subscription = wcs_get_subscription( $wc_order->get_id() ); + if ( is_a( $subscription, WC_Subscription::class ) ) { + $subscription_id = $subscription->get_meta( 'ppcp_subscription' ) ?? ''; + if ( $subscription_id ) { + return; + } + } + $this->process_order( $wc_order ); } catch ( \Exception $exception ) { $error = $exception->getMessage(); diff --git a/modules/ppcp-subscription/src/SubscriptionModule.php b/modules/ppcp-subscription/src/SubscriptionModule.php index e9e3d1123..d49480467 100644 --- a/modules/ppcp-subscription/src/SubscriptionModule.php +++ b/modules/ppcp-subscription/src/SubscriptionModule.php @@ -74,6 +74,11 @@ class SubscriptionModule implements ModuleInterface { add_action( 'woocommerce_subscription_payment_complete', function ( $subscription ) use ( $c ) { + $paypal_subscription_id = $subscription->get_meta( 'ppcp_subscription' ) ?? ''; + if ( $paypal_subscription_id ) { + return; + } + $payment_token_repository = $c->get( 'vaulting.repository.payment-token' ); $logger = $c->get( 'woocommerce.logger.woocommerce' ); diff --git a/modules/ppcp-webhooks/src/Handler/PaymentSaleCompleted.php b/modules/ppcp-webhooks/src/Handler/PaymentSaleCompleted.php index 442dabd97..4c6b26782 100644 --- a/modules/ppcp-webhooks/src/Handler/PaymentSaleCompleted.php +++ b/modules/ppcp-webhooks/src/Handler/PaymentSaleCompleted.php @@ -92,10 +92,24 @@ class PaymentSaleCompleted implements RequestHandler { ); $subscriptions = wcs_get_subscriptions( $args ); foreach ( $subscriptions as $subscription ) { - $parent_order = wc_get_order( $subscription->get_parent() ); $transaction_id = wc_clean( wp_unslash( $request['resource']['id'] ?? '' ) ); - if ( $transaction_id && is_string( $transaction_id ) && is_a( $parent_order, WC_Order::class ) ) { - $this->update_transaction_id( $transaction_id, $parent_order, $this->logger ); + if ( $transaction_id && is_string( $transaction_id ) ) { + $is_renewal = $subscription->get_meta( '_ppcp_is_subscription_renewal' ) ?? ''; + if ( $is_renewal ) { + $renewal_order = wcs_create_renewal_order( $subscription ); + if ( is_a( $renewal_order, WC_Order::class ) ) { + $renewal_order->payment_complete(); + $this->update_transaction_id( $transaction_id, $renewal_order, $this->logger ); + break; + } + } + + $parent_order = wc_get_order( $subscription->get_parent() ); + if ( is_a( $parent_order, WC_Order::class ) ) { + $subscription->update_meta_data( '_ppcp_is_subscription_renewal', 'true' ); + $subscription->save_meta_data(); + $this->update_transaction_id( $transaction_id, $parent_order, $this->logger ); + } } } diff --git a/tests/PHPUnit/Subscription/RenewalHandlerTest.php b/tests/PHPUnit/Subscription/RenewalHandlerTest.php index d079012c3..70484956b 100644 --- a/tests/PHPUnit/Subscription/RenewalHandlerTest.php +++ b/tests/PHPUnit/Subscription/RenewalHandlerTest.php @@ -115,6 +115,9 @@ class RenewalHandlerTest extends TestCase ->shouldReceive('payment_source') ->andReturn(null); + $wcOrder + ->shouldReceive('get_meta') + ->andReturn(''); $wcOrder ->shouldReceive('get_id') ->andReturn(1); diff --git a/tests/PHPUnit/TestCase.php b/tests/PHPUnit/TestCase.php index 2289dbc20..4e6c859a1 100644 --- a/tests/PHPUnit/TestCase.php +++ b/tests/PHPUnit/TestCase.php @@ -38,6 +38,7 @@ class TestCase extends \PHPUnit\Framework\TestCase when('wc_clean')->returnArg(); when('get_transient')->returnArg(); when('delete_transient')->returnArg(); + when('wcs_get_subscription')->returnArg(); setUp(); }