From 17a06a61a323f39919246154416fb43d8566a348 Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Mon, 14 Aug 2023 15:43:56 +0200 Subject: [PATCH 01/10] Do not process renew if order was created with PayPal Subscription API --- modules/ppcp-subscription/src/RenewalHandler.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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(); From 55f5e62dd80726740f8c0e1ed18c09e1e2477261 Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Mon, 14 Aug 2023 17:02:27 +0200 Subject: [PATCH 02/10] Create subscription renewal order on payment sale completed webhook handler --- .../ppcp-webhooks/src/Handler/PaymentSaleCompleted.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/modules/ppcp-webhooks/src/Handler/PaymentSaleCompleted.php b/modules/ppcp-webhooks/src/Handler/PaymentSaleCompleted.php index b606e2bd9..6cbd804dd 100644 --- a/modules/ppcp-webhooks/src/Handler/PaymentSaleCompleted.php +++ b/modules/ppcp-webhooks/src/Handler/PaymentSaleCompleted.php @@ -89,10 +89,12 @@ 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 ) ) { + $renewal_order = wcs_create_renewal_order( $subscription ); + if ( is_a( $renewal_order, WC_Order::class ) ) { + $this->update_transaction_id( $transaction_id, $renewal_order, $this->logger ); + } } } From ae0f56e30bc5ce8312d8ab5ac7766e09fa231c40 Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Wed, 16 Aug 2023 15:48:45 +0200 Subject: [PATCH 03/10] Update renewal order status to completed --- modules/ppcp-webhooks/src/Handler/PaymentSaleCompleted.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modules/ppcp-webhooks/src/Handler/PaymentSaleCompleted.php b/modules/ppcp-webhooks/src/Handler/PaymentSaleCompleted.php index 7442c7a55..0596ed154 100644 --- a/modules/ppcp-webhooks/src/Handler/PaymentSaleCompleted.php +++ b/modules/ppcp-webhooks/src/Handler/PaymentSaleCompleted.php @@ -94,9 +94,12 @@ class PaymentSaleCompleted implements RequestHandler { foreach ( $subscriptions as $subscription ) { $transaction_id = wc_clean( wp_unslash( $request['resource']['id'] ?? '' ) ); if ( $transaction_id && is_string( $transaction_id ) ) { + $this->logger->info( 'Creating renewal order from PAYMENT.SALE.COMPLETED webhook handler' ); $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 ); + $this->logger->info( 'Updating status completed and transaction id from PAYMENT.SALE.COMPLETED webhook handler' ); } } } From f33f603a2346a7594ca49f8cc0a243abbfc2f39d Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Wed, 16 Aug 2023 16:53:15 +0200 Subject: [PATCH 04/10] Revert to updating parent order transcation id on each renewal from PayPal --- .../src/Handler/PaymentSaleCompleted.php | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/modules/ppcp-webhooks/src/Handler/PaymentSaleCompleted.php b/modules/ppcp-webhooks/src/Handler/PaymentSaleCompleted.php index 0596ed154..442dabd97 100644 --- a/modules/ppcp-webhooks/src/Handler/PaymentSaleCompleted.php +++ b/modules/ppcp-webhooks/src/Handler/PaymentSaleCompleted.php @@ -92,15 +92,10 @@ 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 ) ) { - $this->logger->info( 'Creating renewal order from PAYMENT.SALE.COMPLETED webhook handler' ); - $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 ); - $this->logger->info( 'Updating status completed and transaction id from PAYMENT.SALE.COMPLETED webhook handler' ); - } + if ( $transaction_id && is_string( $transaction_id ) && is_a( $parent_order, WC_Order::class ) ) { + $this->update_transaction_id( $transaction_id, $parent_order, $this->logger ); } } From 4d0a4676ecf44d6eec2631715298f45c49eecf6f Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Fri, 18 Aug 2023 12:47:05 +0200 Subject: [PATCH 05/10] Do not add payment token if subscription use PayPal Subscriptions API --- modules/ppcp-subscription/src/SubscriptionModule.php | 5 +++++ 1 file changed, 5 insertions(+) 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' ); From 1cddb3d1327a488b76d9c292aa8dfb897720c263 Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Fri, 18 Aug 2023 16:17:37 +0200 Subject: [PATCH 06/10] Update transaction id on parent order or otherwsie create renewal order --- .../src/Handler/PaymentSaleCompleted.php | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) 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 ); + } } } From 3d5a44ba7d41b3ae2e1aa7b9176fe6fd3e074edb Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Fri, 18 Aug 2023 16:36:32 +0200 Subject: [PATCH 07/10] Fix phpunit --- tests/PHPUnit/Subscription/RenewalHandlerTest.php | 3 +++ tests/PHPUnit/TestCase.php | 1 + 2 files changed, 4 insertions(+) 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(); } From 8cb9c026e20b9f958e85cb2d90bbcef62f5d75af Mon Sep 17 00:00:00 2001 From: carmenmaymo Date: Thu, 24 Aug 2023 10:57:25 +0200 Subject: [PATCH 08/10] add onboarding options filter --- .../src/Render/OnboardingOptionsRenderer.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/ppcp-onboarding/src/Render/OnboardingOptionsRenderer.php b/modules/ppcp-onboarding/src/Render/OnboardingOptionsRenderer.php index 844e78875..b82e054c9 100644 --- a/modules/ppcp-onboarding/src/Render/OnboardingOptionsRenderer.php +++ b/modules/ppcp-onboarding/src/Render/OnboardingOptionsRenderer.php @@ -56,10 +56,8 @@ class OnboardingOptionsRenderer { * @param bool $is_shop_supports_dcc Whether the shop can use DCC (country, currency). */ public function render( bool $is_shop_supports_dcc ): string { - $checked = $is_shop_supports_dcc ? '' : 'checked'; - return ' -
    -
  • + $checked = $is_shop_supports_dcc ? '' : 'checked'; + $on_boarding_options = '
  • @@ -68,8 +66,10 @@ class OnboardingOptionsRenderer {
  • ' . $this->render_dcc( $is_shop_supports_dcc ) . '
  • ' . - $this->render_pui_option() - . '
'; + $this->render_pui_option(); + return '
    ' . + apply_filters( 'ppcp_onboarding_options', $on_boarding_options ); + '
'; } /** From 2563d41d836f4ef8dfcb94d2cf5b6a435e67c14a Mon Sep 17 00:00:00 2001 From: carmenmaymo Date: Thu, 24 Aug 2023 10:58:42 +0200 Subject: [PATCH 09/10] add sdk components filter --- modules/ppcp-button/src/Assets/SmartButton.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/modules/ppcp-button/src/Assets/SmartButton.php b/modules/ppcp-button/src/Assets/SmartButton.php index 763038e8e..f8e3bff10 100644 --- a/modules/ppcp-button/src/Assets/SmartButton.php +++ b/modules/ppcp-button/src/Assets/SmartButton.php @@ -1168,6 +1168,16 @@ class SmartButton implements SmartButtonInterface { if ( $this->dcc_is_enabled() ) { $components[] = 'hosted-fields'; } + /** + * Filter to add further components from the extensions. + * + * @internal Matches filter name in APM extension. + * @since TODO + * + * @param array $components The array of components already registered. + */ + return apply_filters( 'woocommerce_paypal_payments_sdk_components_hook', $components ); + return $components; } From e66289b3a1208105875f26af06cac1acd87c118f Mon Sep 17 00:00:00 2001 From: carmenmaymo Date: Thu, 24 Aug 2023 11:01:19 +0200 Subject: [PATCH 10/10] return filter --- modules/ppcp-button/src/Assets/SmartButton.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/modules/ppcp-button/src/Assets/SmartButton.php b/modules/ppcp-button/src/Assets/SmartButton.php index f8e3bff10..f1592c734 100644 --- a/modules/ppcp-button/src/Assets/SmartButton.php +++ b/modules/ppcp-button/src/Assets/SmartButton.php @@ -1177,8 +1177,6 @@ class SmartButton implements SmartButtonInterface { * @param array $components The array of components already registered. */ return apply_filters( 'woocommerce_paypal_payments_sdk_components_hook', $components ); - - return $components; } /**