From 8efa0e6da38769c3403243e14755b209a8044d6c Mon Sep 17 00:00:00 2001 From: Alex P Date: Mon, 24 Apr 2023 08:15:22 +0300 Subject: [PATCH] Fix pay now button not sure if the action parameter in order creation request affects anything, but the docs say that it should match the js sdk commit parameter --- modules/ppcp-blocks/extensions.php | 10 ++++++++++ modules/ppcp-button/services.php | 5 +++++ modules/ppcp-button/src/Assets/SmartButton.php | 12 +++++++++++- .../ppcp-button/src/Endpoint/CreateOrderEndpoint.php | 12 +++++++++++- .../Button/Endpoint/CreateOrderEndpointTest.php | 1 + 5 files changed, 38 insertions(+), 2 deletions(-) diff --git a/modules/ppcp-blocks/extensions.php b/modules/ppcp-blocks/extensions.php index 522248023..9a48a57fb 100644 --- a/modules/ppcp-blocks/extensions.php +++ b/modules/ppcp-blocks/extensions.php @@ -53,4 +53,14 @@ return array( ) ); }, + + 'button.pay-now-contexts' => function ( ContainerInterface $container, array $contexts ): array { + $contexts[] = 'checkout-block'; + + if ( ! $container->get( 'blocks.settings.final_review_enabled' ) ) { + $contexts[] = 'cart-block'; + } + + return $contexts; + }, ); diff --git a/modules/ppcp-button/services.php b/modules/ppcp-button/services.php index 53e01f193..ee6d0033c 100644 --- a/modules/ppcp-button/services.php +++ b/modules/ppcp-button/services.php @@ -110,6 +110,7 @@ return array( $container->get( 'wcgateway.all-funding-sources' ), $container->get( 'button.basic-checkout-validation-enabled' ), $container->get( 'button.early-wc-checkout-validation-enabled' ), + $container->get( 'button.pay-now-contexts' ), $container->get( 'woocommerce.logger.woocommerce' ) ); }, @@ -119,6 +120,9 @@ return array( dirname( realpath( __FILE__ ), 3 ) . '/woocommerce-paypal-payments.php' ); }, + 'button.pay-now-contexts' => static function ( ContainerInterface $container ): array { + return array( 'checkout', 'pay-now' ); + }, 'button.request-data' => static function ( ContainerInterface $container ): RequestData { return new RequestData(); }, @@ -156,6 +160,7 @@ return array( $registration_needed, $container->get( 'wcgateway.settings.card_billing_data_mode' ), $container->get( 'button.early-wc-checkout-validation-enabled' ), + $container->get( 'button.pay-now-contexts' ), $logger ); }, diff --git a/modules/ppcp-button/src/Assets/SmartButton.php b/modules/ppcp-button/src/Assets/SmartButton.php index 45d021c67..3ea10a1bb 100644 --- a/modules/ppcp-button/src/Assets/SmartButton.php +++ b/modules/ppcp-button/src/Assets/SmartButton.php @@ -165,6 +165,13 @@ class SmartButton implements SmartButtonInterface { */ protected $early_validation_enabled; + /** + * The contexts that should have the Pay Now button. + * + * @var string[] + */ + private $pay_now_contexts; + /** * The logger. * @@ -199,6 +206,7 @@ class SmartButton implements SmartButtonInterface { * @param array $all_funding_sources All existing funding sources. * @param bool $basic_checkout_validation_enabled Whether the basic JS validation of the form iss enabled. * @param bool $early_validation_enabled Whether to execute WC validation of the checkout form. + * @param string[] $pay_now_contexts The contexts that should have the Pay Now button. * @param LoggerInterface $logger The logger. */ public function __construct( @@ -219,6 +227,7 @@ class SmartButton implements SmartButtonInterface { array $all_funding_sources, bool $basic_checkout_validation_enabled, bool $early_validation_enabled, + array $pay_now_contexts, LoggerInterface $logger ) { @@ -239,6 +248,7 @@ class SmartButton implements SmartButtonInterface { $this->all_funding_sources = $all_funding_sources; $this->basic_checkout_validation_enabled = $basic_checkout_validation_enabled; $this->early_validation_enabled = $early_validation_enabled; + $this->pay_now_contexts = $pay_now_contexts; $this->logger = $logger; } @@ -971,7 +981,7 @@ class SmartButton implements SmartButtonInterface { 'integration-date' => PAYPAL_INTEGRATION_DATE, 'components' => implode( ',', $this->components() ), 'vault' => $this->can_save_vault_token() ? 'true' : 'false', - 'commit' => in_array( $context, array( 'checkout', 'pay-now' ), true ) ? 'true' : 'false', + 'commit' => in_array( $context, $this->pay_now_contexts, true ) ? 'true' : 'false', 'intent' => $context === 'product' ? $product_intent : $other_context_intent, ); if ( diff --git a/modules/ppcp-button/src/Endpoint/CreateOrderEndpoint.php b/modules/ppcp-button/src/Endpoint/CreateOrderEndpoint.php index 4abd7dad6..930ccd59c 100644 --- a/modules/ppcp-button/src/Endpoint/CreateOrderEndpoint.php +++ b/modules/ppcp-button/src/Endpoint/CreateOrderEndpoint.php @@ -138,6 +138,13 @@ class CreateOrderEndpoint implements EndpointInterface { */ protected $early_validation_enabled; + /** + * The contexts that should have the Pay Now button. + * + * @var string[] + */ + private $pay_now_contexts; + /** * The logger. * @@ -159,6 +166,7 @@ class CreateOrderEndpoint implements EndpointInterface { * @param bool $registration_needed Whether a new user must be registered during checkout. * @param string $card_billing_data_mode The value of card_billing_data_mode from the settings. * @param bool $early_validation_enabled Whether to execute WC validation of the checkout form. + * @param string[] $pay_now_contexts The contexts that should have the Pay Now button. * @param LoggerInterface $logger The logger. */ public function __construct( @@ -173,6 +181,7 @@ class CreateOrderEndpoint implements EndpointInterface { bool $registration_needed, string $card_billing_data_mode, bool $early_validation_enabled, + array $pay_now_contexts, LoggerInterface $logger ) { @@ -187,6 +196,7 @@ class CreateOrderEndpoint implements EndpointInterface { $this->registration_needed = $registration_needed; $this->card_billing_data_mode = $card_billing_data_mode; $this->early_validation_enabled = $early_validation_enabled; + $this->pay_now_contexts = $pay_now_contexts; $this->logger = $logger; } @@ -385,7 +395,7 @@ class CreateOrderEndpoint implements EndpointInterface { $funding_source ); - $action = in_array( $this->parsed_request_data['context'], array( 'checkout' ), true ) ? + $action = in_array( $this->parsed_request_data['context'], $this->pay_now_contexts, true ) ? ApplicationContext::USER_ACTION_PAY_NOW : ApplicationContext::USER_ACTION_CONTINUE; if ( 'card' === $funding_source ) { diff --git a/tests/PHPUnit/Button/Endpoint/CreateOrderEndpointTest.php b/tests/PHPUnit/Button/Endpoint/CreateOrderEndpointTest.php index 4e6d82a6c..d884ee59f 100644 --- a/tests/PHPUnit/Button/Endpoint/CreateOrderEndpointTest.php +++ b/tests/PHPUnit/Button/Endpoint/CreateOrderEndpointTest.php @@ -167,6 +167,7 @@ class CreateOrderEndpointTest extends TestCase false, CardBillingMode::MINIMAL_INPUT, false, + ['checkout'], new NullLogger() ); return array($payer_factory, $testee);