From 5c699c24a52e267fb23a7d8e2876271d736aa29f Mon Sep 17 00:00:00 2001 From: Alex P Date: Tue, 14 Nov 2023 10:50:56 +0200 Subject: [PATCH 01/15] Refactor checkoutnow url generation --- .psalm/stubs.php | 6 ++ modules/ppcp-api-client/services.php | 8 ++ modules/ppcp-onboarding/services.php | 15 ++++ modules/ppcp-wc-gateway/services.php | 3 +- .../src/Gateway/PayPalGateway.php | 79 ++++++++++--------- tests/PHPUnit/ModularTestCase.php | 2 + .../WcGateway/Gateway/WcGatewayTest.php | 5 +- woocommerce-paypal-payments.php | 2 + 8 files changed, 82 insertions(+), 38 deletions(-) diff --git a/.psalm/stubs.php b/.psalm/stubs.php index 7282b6907..6d897c723 100644 --- a/.psalm/stubs.php +++ b/.psalm/stubs.php @@ -2,6 +2,12 @@ if (!defined('PAYPAL_INTEGRATION_DATE')) { define('PAYPAL_INTEGRATION_DATE', '2023-06-02'); } +if (!defined('PAYPAL_URL')) { + define( 'PAYPAL_URL', 'https://www.paypal.com' ); +} +if (!defined('PAYPAL_SANDBOX_URL')) { + define( 'PAYPAL_SANDBOX_URL', 'https://www.sandbox.paypal.com' ); +} if (!defined('EP_PAGES')) { define('EP_PAGES', 4096); } diff --git a/modules/ppcp-api-client/services.php b/modules/ppcp-api-client/services.php index c5478554f..076782081 100644 --- a/modules/ppcp-api-client/services.php +++ b/modules/ppcp-api-client/services.php @@ -80,6 +80,14 @@ return array( 'api.paypal-host' => function( ContainerInterface $container ) : string { return PAYPAL_API_URL; }, + 'api.paypal-website-url' => function( ContainerInterface $container ) : string { + return PAYPAL_URL; + }, + 'api.factory.paypal-checkout-url' => function( ContainerInterface $container ) : callable { + return function ( string $id ) use ( $container ): string { + return $container->get( 'api.paypal-website-url' ) . '/checkoutnow?token=' . $id; + }; + }, 'api.partner_merchant_id' => static function () : string { return ''; }, diff --git a/modules/ppcp-onboarding/services.php b/modules/ppcp-onboarding/services.php index 27ab1ad25..b76519f62 100644 --- a/modules/ppcp-onboarding/services.php +++ b/modules/ppcp-onboarding/services.php @@ -71,6 +71,12 @@ return array( 'api.paypal-host-sandbox' => static function( ContainerInterface $container ) : string { return PAYPAL_SANDBOX_API_URL; }, + 'api.paypal-website-url-production' => static function( ContainerInterface $container ) : string { + return PAYPAL_URL; + }, + 'api.paypal-website-url-sandbox' => static function( ContainerInterface $container ) : string { + return PAYPAL_SANDBOX_URL; + }, 'api.partner_merchant_id-production' => static function( ContainerInterface $container ) : string { return CONNECT_WOO_MERCHANT_ID; }, @@ -89,6 +95,15 @@ return array( } return $container->get( 'api.paypal-host-production' ); + }, + 'api.paypal-website-url' => function( ContainerInterface $container ) : string { + $environment = $container->get( 'onboarding.environment' ); + assert( $environment instanceof Environment ); + if ( $environment->current_environment_is( Environment::SANDBOX ) ) { + return $container->get( 'api.paypal-website-url-sandbox' ); + } + return $container->get( 'api.paypal-website-url-production' ); + }, 'api.bearer' => static function ( ContainerInterface $container ): Bearer { diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 063afa9a3..9f48f56bb 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -97,7 +97,8 @@ return array( $payment_token_repository, $logger, $api_shop_country, - $container->get( 'api.endpoint.order' ) + $container->get( 'api.endpoint.order' ), + $container->get( 'api.factory.paypal-checkout-url' ) ); }, 'wcgateway.credit-card-gateway' => static function ( ContainerInterface $container ): CreditCardGateway { diff --git a/modules/ppcp-wc-gateway/src/Gateway/PayPalGateway.php b/modules/ppcp-wc-gateway/src/Gateway/PayPalGateway.php index b17e48a7a..c256de226 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/PayPalGateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/PayPalGateway.php @@ -163,24 +163,32 @@ class PayPalGateway extends \WC_Payment_Gateway { */ private $order_endpoint; + /** + * The function return the PayPal checkout URL for the given order ID. + * + * @var callable(string):string + */ + private $paypal_checkout_url_factory; + /** * PayPalGateway constructor. * - * @param SettingsRenderer $settings_renderer The Settings Renderer. - * @param FundingSourceRenderer $funding_source_renderer The funding source renderer. - * @param OrderProcessor $order_processor The Order Processor. - * @param ContainerInterface $config The settings. - * @param SessionHandler $session_handler The Session Handler. - * @param RefundProcessor $refund_processor The Refund Processor. - * @param State $state The state. - * @param TransactionUrlProvider $transaction_url_provider Service providing transaction view URL based on order. - * @param SubscriptionHelper $subscription_helper The subscription helper. - * @param string $page_id ID of the current PPCP gateway settings page, or empty if it is not such page. - * @param Environment $environment The environment. - * @param PaymentTokenRepository $payment_token_repository The payment token repository. - * @param LoggerInterface $logger The logger. - * @param string $api_shop_country The api shop country. - * @param OrderEndpoint $order_endpoint The order endpoint. + * @param SettingsRenderer $settings_renderer The Settings Renderer. + * @param FundingSourceRenderer $funding_source_renderer The funding source renderer. + * @param OrderProcessor $order_processor The Order Processor. + * @param ContainerInterface $config The settings. + * @param SessionHandler $session_handler The Session Handler. + * @param RefundProcessor $refund_processor The Refund Processor. + * @param State $state The state. + * @param TransactionUrlProvider $transaction_url_provider Service providing transaction view URL based on order. + * @param SubscriptionHelper $subscription_helper The subscription helper. + * @param string $page_id ID of the current PPCP gateway settings page, or empty if it is not such page. + * @param Environment $environment The environment. + * @param PaymentTokenRepository $payment_token_repository The payment token repository. + * @param LoggerInterface $logger The logger. + * @param string $api_shop_country The api shop country. + * @param OrderEndpoint $order_endpoint The order endpoint. + * @param callable(string):string $paypal_checkout_url_factory The function return the PayPal checkout URL for the given order ID. */ public function __construct( SettingsRenderer $settings_renderer, @@ -197,24 +205,26 @@ class PayPalGateway extends \WC_Payment_Gateway { PaymentTokenRepository $payment_token_repository, LoggerInterface $logger, string $api_shop_country, - OrderEndpoint $order_endpoint + OrderEndpoint $order_endpoint, + callable $paypal_checkout_url_factory ) { - $this->id = self::ID; - $this->settings_renderer = $settings_renderer; - $this->funding_source_renderer = $funding_source_renderer; - $this->order_processor = $order_processor; - $this->config = $config; - $this->session_handler = $session_handler; - $this->refund_processor = $refund_processor; - $this->state = $state; - $this->transaction_url_provider = $transaction_url_provider; - $this->subscription_helper = $subscription_helper; - $this->page_id = $page_id; - $this->environment = $environment; - $this->onboarded = $state->current_state() === State::STATE_ONBOARDED; - $this->payment_token_repository = $payment_token_repository; - $this->logger = $logger; - $this->api_shop_country = $api_shop_country; + $this->id = self::ID; + $this->settings_renderer = $settings_renderer; + $this->funding_source_renderer = $funding_source_renderer; + $this->order_processor = $order_processor; + $this->config = $config; + $this->session_handler = $session_handler; + $this->refund_processor = $refund_processor; + $this->state = $state; + $this->transaction_url_provider = $transaction_url_provider; + $this->subscription_helper = $subscription_helper; + $this->page_id = $page_id; + $this->environment = $environment; + $this->onboarded = $state->current_state() === State::STATE_ONBOARDED; + $this->payment_token_repository = $payment_token_repository; + $this->logger = $logger; + $this->api_shop_country = $api_shop_country; + $this->paypal_checkout_url_factory = $paypal_checkout_url_factory; if ( $this->onboarded ) { $this->supports = array( 'refunds', 'tokenization' ); @@ -593,12 +603,9 @@ class PayPalGateway extends \WC_Payment_Gateway { ); } - $host = $this->config->has( 'sandbox_on' ) && $this->config->get( 'sandbox_on' ) ? - 'https://www.sandbox.paypal.com/' : 'https://www.paypal.com/'; - $url = $host . 'checkoutnow?token=' . $this->session_handler->order()->id(); return array( 'result' => 'success', - 'redirect' => $url, + 'redirect' => ( $this->paypal_checkout_url_factory )( $this->session_handler->order()->id() ), ); } diff --git a/tests/PHPUnit/ModularTestCase.php b/tests/PHPUnit/ModularTestCase.php index 61583c4e1..a880acfbe 100644 --- a/tests/PHPUnit/ModularTestCase.php +++ b/tests/PHPUnit/ModularTestCase.php @@ -41,7 +41,9 @@ class ModularTestCase extends TestCase $wpdb->postmeta = ''; !defined('PAYPAL_API_URL') && define('PAYPAL_API_URL', 'https://api-m.paypal.com'); + !defined('PAYPAL_URL') && define( 'PAYPAL_URL', 'https://www.paypal.com' ); !defined('PAYPAL_SANDBOX_API_URL') && define('PAYPAL_SANDBOX_API_URL', 'https://api-m.sandbox.paypal.com'); + !defined('PAYPAL_SANDBOX_URL') && define( 'PAYPAL_SANDBOX_URL', 'https://www.sandbox.paypal.com' ); !defined('PAYPAL_INTEGRATION_DATE') && define('PAYPAL_INTEGRATION_DATE', '2020-10-15'); !defined('PPCP_FLAG_SUBSCRIPTION') && define('PPCP_FLAG_SUBSCRIPTION', true); diff --git a/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php b/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php index aa9057c67..041f12618 100644 --- a/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php +++ b/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php @@ -106,7 +106,10 @@ class WcGatewayTest extends TestCase $this->paymentTokenRepository, $this->logger, $this->apiShopCountry, - $this->orderEndpoint + $this->orderEndpoint, + function ($id) { + return 'checkoutnow=' . $id; + } ); } diff --git a/woocommerce-paypal-payments.php b/woocommerce-paypal-payments.php index 5e89cbae4..d70e57580 100644 --- a/woocommerce-paypal-payments.php +++ b/woocommerce-paypal-payments.php @@ -22,7 +22,9 @@ namespace WooCommerce\PayPalCommerce; use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; define( 'PAYPAL_API_URL', 'https://api-m.paypal.com' ); +define( 'PAYPAL_URL', 'https://www.paypal.com' ); define( 'PAYPAL_SANDBOX_API_URL', 'https://api-m.sandbox.paypal.com' ); +define( 'PAYPAL_SANDBOX_URL', 'https://www.sandbox.paypal.com' ); define( 'PAYPAL_INTEGRATION_DATE', '2023-11-06' ); ! defined( 'CONNECT_WOO_CLIENT_ID' ) && define( 'CONNECT_WOO_CLIENT_ID', 'AcCAsWta_JTL__OfpjspNyH7c1GGHH332fLwonA5CwX4Y10mhybRZmHLA0GdRbwKwjQIhpDQy0pluX_P' ); From a61e9303e9521234103738a1ddcb159dc75b705c Mon Sep 17 00:00:00 2001 From: Alex P Date: Tue, 14 Nov 2023 10:55:30 +0200 Subject: [PATCH 02/15] Create order and redirect when no order --- modules/ppcp-wc-gateway/services.php | 8 +- .../src/Endpoint/ReturnUrlEndpoint.php | 4 +- .../Exception/PayPalOrderMissingException.php | 18 +++ .../src/Gateway/CardButtonGateway.php | 89 ++++++----- .../src/Gateway/CreditCardGateway.php | 11 +- .../src/Gateway/PayPalGateway.php | 24 +-- .../src/Processor/OrderProcessor.php | 143 +++++++++++------- .../src/Handler/CheckoutOrderApproved.php | 7 +- .../WcGateway/Gateway/WcGatewayTest.php | 10 +- .../Processor/OrderProcessorTest.php | 31 +++- 10 files changed, 214 insertions(+), 131 deletions(-) create mode 100644 modules/ppcp-wc-gateway/src/Exception/PayPalOrderMissingException.php diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 9f48f56bb..bbf9629d4 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -142,7 +142,8 @@ return array( $container->get( 'wcgateway.settings.allow_card_button_gateway.default' ), $container->get( 'onboarding.environment' ), $container->get( 'vaulting.repository.payment-token' ), - $container->get( 'woocommerce.logger.woocommerce' ) + $container->get( 'woocommerce.logger.woocommerce' ), + $container->get( 'api.factory.paypal-checkout-url' ) ); }, 'wcgateway.disabler' => static function ( ContainerInterface $container ): DisableGateways { @@ -346,7 +347,10 @@ return array( $logger, $environment, $subscription_helper, - $order_helper + $order_helper, + $container->get( 'api.factory.purchase-unit' ), + $container->get( 'api.factory.payer' ), + $container->get( 'api.factory.shipping-preference' ) ); }, 'wcgateway.processor.refunds' => static function ( ContainerInterface $container ): RefundProcessor { diff --git a/modules/ppcp-wc-gateway/src/Endpoint/ReturnUrlEndpoint.php b/modules/ppcp-wc-gateway/src/Endpoint/ReturnUrlEndpoint.php index 3efe16542..23442a59d 100644 --- a/modules/ppcp-wc-gateway/src/Endpoint/ReturnUrlEndpoint.php +++ b/modules/ppcp-wc-gateway/src/Endpoint/ReturnUrlEndpoint.php @@ -85,14 +85,14 @@ class ReturnUrlEndpoint { // phpcs:enable WordPress.Security.NonceVerification.Recommended $order = $this->order_endpoint->order( $token ); + $this->session_handler->replace_order( $order ); + $wc_order_id = (int) $order->purchase_units()[0]->custom_id(); if ( ! $wc_order_id ) { // We cannot finish processing here without WC order, but at least go into the continuation mode. if ( $order->status()->is( OrderStatus::APPROVED ) || $order->status()->is( OrderStatus::COMPLETED ) ) { - $this->session_handler->replace_order( $order ); - wp_safe_redirect( wc_get_checkout_url() ); exit(); } diff --git a/modules/ppcp-wc-gateway/src/Exception/PayPalOrderMissingException.php b/modules/ppcp-wc-gateway/src/Exception/PayPalOrderMissingException.php new file mode 100644 index 000000000..6a14e13dd --- /dev/null +++ b/modules/ppcp-wc-gateway/src/Exception/PayPalOrderMissingException.php @@ -0,0 +1,18 @@ +id = self::ID; - $this->settings_renderer = $settings_renderer; - $this->order_processor = $order_processor; - $this->config = $config; - $this->session_handler = $session_handler; - $this->refund_processor = $refund_processor; - $this->state = $state; - $this->transaction_url_provider = $transaction_url_provider; - $this->subscription_helper = $subscription_helper; - $this->default_enabled = $default_enabled; - $this->environment = $environment; - $this->onboarded = $state->current_state() === State::STATE_ONBOARDED; - $this->payment_token_repository = $payment_token_repository; - $this->logger = $logger; + $this->id = self::ID; + $this->settings_renderer = $settings_renderer; + $this->order_processor = $order_processor; + $this->config = $config; + $this->session_handler = $session_handler; + $this->refund_processor = $refund_processor; + $this->state = $state; + $this->transaction_url_provider = $transaction_url_provider; + $this->subscription_helper = $subscription_helper; + $this->default_enabled = $default_enabled; + $this->environment = $environment; + $this->onboarded = $state->current_state() === State::STATE_ONBOARDED; + $this->payment_token_repository = $payment_token_repository; + $this->logger = $logger; + $this->paypal_checkout_url_factory = $paypal_checkout_url_factory; $this->supports = array( 'refunds', @@ -294,18 +305,20 @@ class CardButtonGateway extends \WC_Payment_Gateway { //phpcs:enable WordPress.Security.NonceVerification.Recommended try { - if ( ! $this->order_processor->process( $wc_order ) ) { - return $this->handle_payment_failure( - $wc_order, - new Exception( - $this->order_processor->last_error() - ) + try { + $this->order_processor->process( $wc_order ); + + do_action( 'woocommerce_paypal_payments_before_handle_payment_success', $wc_order ); + + return $this->handle_payment_success( $wc_order ); + } catch ( PayPalOrderMissingException $exc ) { + $order = $this->order_processor->create_order( $wc_order ); + + return array( + 'result' => 'success', + 'redirect' => ( $this->paypal_checkout_url_factory )( $order->id() ), ); } - - do_action( 'woocommerce_paypal_payments_before_handle_payment_success', $wc_order ); - - return $this->handle_payment_success( $wc_order ); } catch ( PayPalApiException $error ) { return $this->handle_payment_failure( $wc_order, @@ -315,7 +328,7 @@ class CardButtonGateway extends \WC_Payment_Gateway { $error ) ); - } catch ( RuntimeException $error ) { + } catch ( Exception $error ) { return $this->handle_payment_failure( $wc_order, $error ); } } diff --git a/modules/ppcp-wc-gateway/src/Gateway/CreditCardGateway.php b/modules/ppcp-wc-gateway/src/Gateway/CreditCardGateway.php index 1b99b9d05..78012b2d7 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/CreditCardGateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/CreditCardGateway.php @@ -387,14 +387,7 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC { //phpcs:enable WordPress.Security.NonceVerification.Recommended try { - if ( ! $this->order_processor->process( $wc_order ) ) { - return $this->handle_payment_failure( - $wc_order, - new Exception( - $this->order_processor->last_error() - ) - ); - } + $this->order_processor->process( $wc_order ); do_action( 'woocommerce_paypal_payments_before_handle_payment_success', $wc_order ); @@ -408,7 +401,7 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC { $error ) ); - } catch ( RuntimeException $error ) { + } catch ( Exception $error ) { return $this->handle_payment_failure( $wc_order, $error ); } } diff --git a/modules/ppcp-wc-gateway/src/Gateway/PayPalGateway.php b/modules/ppcp-wc-gateway/src/Gateway/PayPalGateway.php index c256de226..456c7e09c 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/PayPalGateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/PayPalGateway.php @@ -24,6 +24,7 @@ use WooCommerce\PayPalCommerce\Subscription\FreeTrialHandlerTrait; use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper; use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository; use WooCommerce\PayPalCommerce\WcGateway\Exception\GatewayGenericException; +use WooCommerce\PayPalCommerce\WcGateway\Exception\PayPalOrderMissingException; use WooCommerce\PayPalCommerce\WcGateway\FundingSource\FundingSourceRenderer; use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\PayUponInvoiceGateway; use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderMetaTrait; @@ -556,19 +557,20 @@ class PayPalGateway extends \WC_Payment_Gateway { return $this->handle_payment_success( $wc_order ); } + try { + $this->order_processor->process( $wc_order ); - if ( ! $this->order_processor->process( $wc_order ) ) { - return $this->handle_payment_failure( - $wc_order, - new Exception( - $this->order_processor->last_error() - ) + do_action( 'woocommerce_paypal_payments_before_handle_payment_success', $wc_order ); + + return $this->handle_payment_success( $wc_order ); + } catch ( PayPalOrderMissingException $exc ) { + $order = $this->order_processor->create_order( $wc_order ); + + return array( + 'result' => 'success', + 'redirect' => ( $this->paypal_checkout_url_factory )( $order->id() ), ); } - - do_action( 'woocommerce_paypal_payments_before_handle_payment_success', $wc_order ); - - return $this->handle_payment_success( $wc_order ); } catch ( PayPalApiException $error ) { $retry_keys_messages = array( 'INSTRUMENT_DECLINED' => __( 'Instrument declined.', 'woocommerce-paypal-payments' ), @@ -617,7 +619,7 @@ class PayPalGateway extends \WC_Payment_Gateway { $error ) ); - } catch ( RuntimeException $error ) { + } catch ( Exception $error ) { return $this->handle_payment_failure( $wc_order, $error ); } } diff --git a/modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php b/modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php index 1fb10ed8c..6e8f8253e 100644 --- a/modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php +++ b/modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php @@ -9,19 +9,25 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\WcGateway\Processor; +use Exception; use Psr\Log\LoggerInterface; use WC_Order; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; +use WooCommerce\PayPalCommerce\ApiClient\Entity\ApplicationContext; use WooCommerce\PayPalCommerce\ApiClient\Entity\Order; use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus; use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; use WooCommerce\PayPalCommerce\ApiClient\Factory\OrderFactory; +use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory; +use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory; +use WooCommerce\PayPalCommerce\ApiClient\Factory\ShippingPreferenceFactory; use WooCommerce\PayPalCommerce\ApiClient\Helper\OrderHelper; use WooCommerce\PayPalCommerce\Button\Helper\ThreeDSecure; use WooCommerce\PayPalCommerce\Onboarding\Environment; use WooCommerce\PayPalCommerce\Session\SessionHandler; use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper; use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository; +use WooCommerce\PayPalCommerce\WcGateway\Exception\PayPalOrderMissingException; use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway; use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; @@ -88,13 +94,6 @@ class OrderProcessor { */ private $settings; - /** - * The last error. - * - * @var string - */ - private $last_error = ''; - /** * A logger. * @@ -116,6 +115,27 @@ class OrderProcessor { */ private $order_helper; + /** + * The PurchaseUnit factory. + * + * @var PurchaseUnitFactory + */ + private $purchase_unit_factory; + + /** + * The payer factory. + * + * @var PayerFactory + */ + private $payer_factory; + + /** + * The shipping_preference factory. + * + * @var ShippingPreferenceFactory + */ + private $shipping_preference_factory; + /** * Array to store temporary order data changes to restore after processing. * @@ -136,6 +156,9 @@ class OrderProcessor { * @param Environment $environment The environment. * @param SubscriptionHelper $subscription_helper The subscription helper. * @param OrderHelper $order_helper The order helper. + * @param PurchaseUnitFactory $purchase_unit_factory The PurchaseUnit factory. + * @param PayerFactory $payer_factory The payer factory. + * @param ShippingPreferenceFactory $shipping_preference_factory The shipping_preference factory. */ public function __construct( SessionHandler $session_handler, @@ -147,7 +170,10 @@ class OrderProcessor { LoggerInterface $logger, Environment $environment, SubscriptionHelper $subscription_helper, - OrderHelper $order_helper + OrderHelper $order_helper, + PurchaseUnitFactory $purchase_unit_factory, + PayerFactory $payer_factory, + ShippingPreferenceFactory $shipping_preference_factory ) { $this->session_handler = $session_handler; @@ -160,60 +186,57 @@ class OrderProcessor { $this->logger = $logger; $this->subscription_helper = $subscription_helper; $this->order_helper = $order_helper; + $this->purchase_unit_factory = $purchase_unit_factory; + $this->payer_factory = $payer_factory; + $this->shipping_preference_factory = $shipping_preference_factory; } /** * Processes a given WooCommerce order and captured/authorizes the connected PayPal orders. * - * @param \WC_Order $wc_order The WooCommerce order. + * @param WC_Order $wc_order The WooCommerce order. * - * @return bool + * @throws PayPalOrderMissingException If no PayPal order. + * @throws Exception If processing fails. */ - public function process( \WC_Order $wc_order ): bool { - // phpcs:ignore WordPress.Security.NonceVerification - $order_id = $wc_order->get_meta( PayPalGateway::ORDER_ID_META_KEY ) ?: wc_clean( wp_unslash( $_POST['paypal_order_id'] ?? '' ) ); - $order = $this->session_handler->order(); - if ( ! $order && is_string( $order_id ) && $order_id ) { - $order = $this->order_endpoint->order( $order_id ); - } + public function process( WC_Order $wc_order ): void { + $order = $this->session_handler->order(); if ( ! $order ) { - $order_id = $wc_order->get_meta( PayPalGateway::ORDER_ID_META_KEY ); - if ( ! $order_id ) { + // phpcs:ignore WordPress.Security.NonceVerification + $order_id = $wc_order->get_meta( PayPalGateway::ORDER_ID_META_KEY ) ?: wc_clean( wp_unslash( $_POST['paypal_order_id'] ?? '' ) ); + if ( is_string( $order_id ) && $order_id ) { + try { + $order = $this->order_endpoint->order( $order_id ); + } catch ( RuntimeException $exception ) { + throw new Exception( __( 'Could not retrieve PayPal order.', 'woocommerce-paypal-payments' ) ); + } + } else { $this->logger->warning( sprintf( 'No PayPal order ID found in order #%d meta.', $wc_order->get_id() ) ); - $this->last_error = __( 'Could not retrieve order. Maybe it was already completed or this browser is not supported. Please check your email or try again with a different browser.', 'woocommerce-paypal-payments' ); - return false; - } - try { - $order = $this->order_endpoint->order( $order_id ); - } catch ( RuntimeException $exception ) { - $this->last_error = __( 'Could not retrieve PayPal order.', 'woocommerce-paypal-payments' ); - return false; + throw new PayPalOrderMissingException( + __( + 'Could not retrieve order. Maybe it was already completed or this browser is not supported. Please check your email or try again with a different browser.', + 'woocommerce-paypal-payments' + ) + ); } } $this->add_paypal_meta( $wc_order, $order, $this->environment ); - $error_message = null; if ( $this->order_helper->contains_physical_goods( $order ) && ! $this->order_is_ready_for_process( $order ) ) { - $error_message = __( - 'The payment is not ready for processing yet.', - 'woocommerce-paypal-payments' + throw new Exception( + __( + 'The payment is not ready for processing yet.', + 'woocommerce-paypal-payments' + ) ); } - if ( $error_message ) { - $this->last_error = sprintf( - // translators: %s is the message of the error. - __( 'Payment error: %s', 'woocommerce-paypal-payments' ), - $error_message - ); - return false; - } $order = $this->patch_order( $wc_order, $order ); @@ -242,8 +265,30 @@ class OrderProcessor { if ( $this->capture_authorized_downloads( $order ) ) { $this->authorized_payments_processor->capture_authorized_payment( $wc_order ); } - $this->last_error = ''; - return true; + } + + /** + * Creates a PayPal order for the given WC order. + * + * @param WC_Order $wc_order The WC order. + * @return Order + * @throws RuntimeException If order creation fails. + */ + public function create_order( WC_Order $wc_order ): Order { + $pu = $this->purchase_unit_factory->from_wc_order( $wc_order ); + $shipping_preference = $this->shipping_preference_factory->from_state( $pu, 'checkout' ); + $order = $this->order_endpoint->create( + array( $pu ), + $shipping_preference, + $this->payer_factory->from_wc_order( $wc_order ), + null, + '', + ApplicationContext::USER_ACTION_PAY_NOW + ); + + $this->add_paypal_meta( $wc_order, $order, $this->environment ); + + return $order; } /** @@ -280,25 +325,15 @@ class OrderProcessor { return true; } - /** - * Returns the last error. - * - * @return string - */ - public function last_error(): string { - - return $this->last_error; - } - /** * Patches a given PayPal order with a WooCommerce order. * - * @param \WC_Order $wc_order The WooCommerce order. - * @param Order $order The PayPal order. + * @param WC_Order $wc_order The WooCommerce order. + * @param Order $order The PayPal order. * * @return Order */ - public function patch_order( \WC_Order $wc_order, Order $order ): Order { + public function patch_order( WC_Order $wc_order, Order $order ): Order { $this->apply_outbound_order_filters( $wc_order ); $updated_order = $this->order_factory->from_wc_order( $wc_order, $order ); $this->restore_order_from_filters( $wc_order ); diff --git a/modules/ppcp-webhooks/src/Handler/CheckoutOrderApproved.php b/modules/ppcp-webhooks/src/Handler/CheckoutOrderApproved.php index 177bda0ad..d93ca5b4c 100644 --- a/modules/ppcp-webhooks/src/Handler/CheckoutOrderApproved.php +++ b/modules/ppcp-webhooks/src/Handler/CheckoutOrderApproved.php @@ -15,6 +15,7 @@ use WC_Session_Handler; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus; use Psr\Log\LoggerInterface; +use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; use WooCommerce\PayPalCommerce\Session\MemoryWcSession; use WooCommerce\PayPalCommerce\Session\SessionHandler; use WooCommerce\PayPalCommerce\WcGateway\FundingSource\FundingSourceRenderer; @@ -228,12 +229,14 @@ class CheckoutOrderApproved implements RequestHandler { continue; } - if ( ! $this->order_processor->process( $wc_order ) ) { + try { + $this->order_processor->process( $wc_order ); + } catch ( RuntimeException $exception ) { return $this->failure_response( sprintf( 'Failed to process WC order %s: %s.', (string) $wc_order->get_id(), - $this->order_processor->last_error() + $exception->getMessage() ) ); } diff --git a/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php b/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php index 041f12618..44f729377 100644 --- a/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php +++ b/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php @@ -3,6 +3,7 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\WcGateway\Gateway; +use Exception; use Psr\Log\LoggerInterface; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Entity\Order; @@ -207,13 +208,10 @@ class WcGatewayTest extends TestCase public function testProcessPaymentFails() { $orderId = 1; $wcOrder = Mockery::mock(\WC_Order::class); - $lastError = 'some-error'; + $error = 'some-error'; $this->orderProcessor ->expects('process') - ->andReturnFalse(); - $this->orderProcessor - ->expects('last_error') - ->andReturn($lastError); + ->andThrow(new Exception($error)); $this->subscriptionHelper->shouldReceive('has_subscription')->with($orderId)->andReturn(true); $this->subscriptionHelper->shouldReceive('is_subscription_change_payment')->andReturn(true); $wcOrder->shouldReceive('update_status')->andReturn(true); @@ -226,7 +224,7 @@ class WcGatewayTest extends TestCase $this->sessionHandler ->shouldReceive('destroy_session_data'); expect('wc_add_notice') - ->with($lastError, 'error'); + ->with($error, 'error'); $redirectUrl = 'http://example.com/checkout'; diff --git a/tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php b/tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php index 97ceca9b5..d6754fdee 100644 --- a/tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php +++ b/tests/PHPUnit/WcGateway/Processor/OrderProcessorTest.php @@ -4,6 +4,10 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\WcGateway\Processor; +use Exception; +use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory; +use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory; +use WooCommerce\PayPalCommerce\ApiClient\Factory\ShippingPreferenceFactory; use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\Dictionary; use Psr\Log\LoggerInterface; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; @@ -143,7 +147,10 @@ class OrderProcessorTest extends TestCase $logger, $this->environment, $subscription_helper, - $order_helper + $order_helper, + Mockery::mock(PurchaseUnitFactory::class), + Mockery::mock(PayerFactory::class), + Mockery::mock(ShippingPreferenceFactory::class) ); $wcOrder @@ -173,7 +180,9 @@ class OrderProcessorTest extends TestCase $order_helper->shouldReceive('contains_physical_goods')->andReturn(true); - $this->assertTrue($testee->process($wcOrder)); + $testee->process($wcOrder); + + $this->expectNotToPerformAssertions(); } public function testCapture() { @@ -268,7 +277,10 @@ class OrderProcessorTest extends TestCase $logger, $this->environment, $subscription_helper, - $order_helper + $order_helper, + Mockery::mock(PurchaseUnitFactory::class), + Mockery::mock(PayerFactory::class), + Mockery::mock(ShippingPreferenceFactory::class) ); $wcOrder @@ -293,7 +305,9 @@ class OrderProcessorTest extends TestCase $order_helper->shouldReceive('contains_physical_goods')->andReturn(true); - $this->assertTrue($testee->process($wcOrder)); + $testee->process($wcOrder); + + $this->expectNotToPerformAssertions(); } public function testError() { @@ -375,7 +389,10 @@ class OrderProcessorTest extends TestCase $logger, $this->environment, $subscription_helper, - $order_helper + $order_helper, + Mockery::mock(PurchaseUnitFactory::class), + Mockery::mock(PayerFactory::class), + Mockery::mock(ShippingPreferenceFactory::class) ); $wcOrder @@ -394,8 +411,8 @@ class OrderProcessorTest extends TestCase $order_helper->shouldReceive('contains_physical_goods')->andReturn(true); - $this->assertFalse($testee->process($wcOrder)); - $this->assertNotEmpty($testee->last_error()); + $this->expectException(Exception::class); + $testee->process($wcOrder); } From b2ba72c06c8907eab77d5327d14ba6f4a301faed Mon Sep 17 00:00:00 2001 From: Alex P Date: Tue, 14 Nov 2023 10:56:15 +0200 Subject: [PATCH 03/15] Add ppcp_create_paypal_order_for_wc_order --- api/order-functions.php | 14 +++++ .../PHPUnit/Api/CreateOrderForWcOrderTest.php | 52 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 tests/PHPUnit/Api/CreateOrderForWcOrderTest.php diff --git a/api/order-functions.php b/api/order-functions.php index 701b064fb..340be8585 100644 --- a/api/order-functions.php +++ b/api/order-functions.php @@ -21,6 +21,7 @@ use WooCommerce\PayPalCommerce\PPCP; use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway; use WooCommerce\PayPalCommerce\WcGateway\Helper\RefundFeesUpdater; use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor; +use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderProcessor; use WooCommerce\PayPalCommerce\WcGateway\Processor\RefundProcessor; /** @@ -47,6 +48,19 @@ function ppcp_get_paypal_order( $paypal_id_or_wc_order ): Order { return $order_endpoint->order( $paypal_id_or_wc_order ); } +/** + * Creates a PayPal order for the given WC order. + * + * @param WC_Order $wc_order The WC order. + * @throws Exception When the operation fails. + */ +function ppcp_create_paypal_order_for_wc_order( WC_Order $wc_order ): Order { + $order_processor = PPCP::container()->get( 'wcgateway.order-processor' ); + assert( $order_processor instanceof OrderProcessor ); + + return $order_processor->create_order( $wc_order ); +} + /** * Captures the PayPal order. * diff --git a/tests/PHPUnit/Api/CreateOrderForWcOrderTest.php b/tests/PHPUnit/Api/CreateOrderForWcOrderTest.php new file mode 100644 index 000000000..b2d16caf7 --- /dev/null +++ b/tests/PHPUnit/Api/CreateOrderForWcOrderTest.php @@ -0,0 +1,52 @@ +orderProcesor = Mockery::mock(OrderProcessor::class); + + $this->bootstrapModule([ + 'wcgateway.order-processor' => function () { + return $this->orderProcesor; + }, + ]); + } + + public function testSuccess(): void { + $wcOrder = Mockery::mock(WC_Order::class); + $ret = Mockery::mock(Order::class); + $this->orderProcesor + ->expects('create_order') + ->with($wcOrder) + ->andReturn($ret) + ->once(); + + self::assertEquals($ret, ppcp_create_paypal_order_for_wc_order($wcOrder)); + } + + public function testFailure(): void { + $wcOrder = Mockery::mock(WC_Order::class); + $this->orderProcesor + ->expects('create_order') + ->with($wcOrder) + ->andThrow(new RuntimeException()) + ->once(); + + $this->expectException(RuntimeException::class); + + ppcp_create_paypal_order_for_wc_order($wcOrder); + } +} From c71e312f187144483a3caf171b6acbf93a1764e0 Mon Sep 17 00:00:00 2001 From: Alex P Date: Wed, 15 Nov 2023 09:44:26 +0200 Subject: [PATCH 04/15] Add filters for place order button mode and text replacement script --- .../PlaceOrderButtonBootstrap.js | 45 +++++++ .../resources/js/place-order-button.js | 8 ++ modules/ppcp-button/services.php | 45 +++++++ .../src/Assets/PlaceOrderButtonAssets.php | 116 ++++++++++++++++++ modules/ppcp-button/src/ButtonModule.php | 6 + modules/ppcp-button/webpack.config.js | 1 + modules/ppcp-wc-gateway/services.php | 19 +++ 7 files changed, 240 insertions(+) create mode 100644 modules/ppcp-button/resources/js/modules/ContextBootstrap/PlaceOrderButtonBootstrap.js create mode 100644 modules/ppcp-button/resources/js/place-order-button.js create mode 100644 modules/ppcp-button/src/Assets/PlaceOrderButtonAssets.php diff --git a/modules/ppcp-button/resources/js/modules/ContextBootstrap/PlaceOrderButtonBootstrap.js b/modules/ppcp-button/resources/js/modules/ContextBootstrap/PlaceOrderButtonBootstrap.js new file mode 100644 index 000000000..ee4d12070 --- /dev/null +++ b/modules/ppcp-button/resources/js/modules/ContextBootstrap/PlaceOrderButtonBootstrap.js @@ -0,0 +1,45 @@ +import { + getCurrentPaymentMethod, + ORDER_BUTTON_SELECTOR, + PaymentMethods +} from "../Helper/CheckoutMethodState"; + +class PlaceOrderButtonBootstrap { + constructor(config) { + this.config = config; + this.defaultButtonText = null; + } + + init() { + jQuery(document.body).on('updated_checkout payment_method_selected', () => { + this.updateUi(); + }); + + this.updateUi(); + } + + updateUi() { + const button = document.querySelector(ORDER_BUTTON_SELECTOR); + if (!button) { + return; + } + + if (!this.defaultButtonText) { + this.defaultButtonText = button.innerText; + + if (!this.defaultButtonText) { + return; + } + } + + const currentPaymentMethod = getCurrentPaymentMethod(); + + if ([PaymentMethods.PAYPAL, PaymentMethods.CARD_BUTTON].includes(currentPaymentMethod)) { + button.innerText = this.config.buttonText; + } else { + button.innerText = this.defaultButtonText; + } + } +} + +export default PlaceOrderButtonBootstrap diff --git a/modules/ppcp-button/resources/js/place-order-button.js b/modules/ppcp-button/resources/js/place-order-button.js new file mode 100644 index 000000000..b1d0969f7 --- /dev/null +++ b/modules/ppcp-button/resources/js/place-order-button.js @@ -0,0 +1,8 @@ +import PlaceOrderButtonBootstrap from "./modules/ContextBootstrap/PlaceOrderButtonBootstrap"; + +document.addEventListener( + 'DOMContentLoaded', + () => { + const placeOrderButtonBootstrap = new PlaceOrderButtonBootstrap(PpcpPlaceOrderButton); + placeOrderButtonBootstrap.init(); + }); diff --git a/modules/ppcp-button/services.php b/modules/ppcp-button/services.php index 9267307d8..a0b5fac63 100644 --- a/modules/ppcp-button/services.php +++ b/modules/ppcp-button/services.php @@ -9,14 +9,17 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\Button; +use WooCommerce\PayPalCommerce\Button\Assets\PlaceOrderButtonAssets; use WooCommerce\PayPalCommerce\Button\Endpoint\ApproveSubscriptionEndpoint; use WooCommerce\PayPalCommerce\Button\Endpoint\CartScriptParamsEndpoint; use WooCommerce\PayPalCommerce\Button\Endpoint\SimulateCartEndpoint; use WooCommerce\PayPalCommerce\Button\Helper\CartProductsHelper; use WooCommerce\PayPalCommerce\Button\Helper\CheckoutFormSaver; use WooCommerce\PayPalCommerce\Button\Endpoint\SaveCheckoutFormEndpoint; +use WooCommerce\PayPalCommerce\Button\Helper\ContextTrait; use WooCommerce\PayPalCommerce\Button\Validation\CheckoutFormValidator; use WooCommerce\PayPalCommerce\Button\Endpoint\ValidateCheckoutEndpoint; +use WooCommerce\PayPalCommerce\Session\SessionHandler; use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; use WooCommerce\PayPalCommerce\Button\Assets\DisabledSmartButton; use WooCommerce\PayPalCommerce\Button\Assets\SmartButton; @@ -68,8 +71,41 @@ return array( return $dummy_ids[ $shop_country ] ?? $container->get( 'button.client_id' ); }, + // This service may not work correctly when called too early. + 'button.context' => static function ( ContainerInterface $container ): string { + $obj = new class() { + use ContextTrait; + + /** + * Session handler. + * + * @var SessionHandler + */ + protected $session_handler; + + /** Constructor. */ + public function __construct() { + // phpcs:ignore PHPCompatibility.FunctionDeclarations.NewClosure.ThisFoundInStatic + $this->session_handler = new SessionHandler(); + } + + /** + * Wrapper for a non-public function. + */ + public function get_context(): string { + // phpcs:ignore PHPCompatibility.FunctionDeclarations.NewClosure.ThisFoundInStatic + return $this->context(); + } + }; + return $obj->get_context(); + }, 'button.smart-button' => static function ( ContainerInterface $container ): SmartButtonInterface { $state = $container->get( 'onboarding.state' ); + if ( $container->get( 'wcgateway.use-place-order-button' ) + && in_array( $container->get( 'button.context' ), array( 'checkout', 'pay-now' ), true ) + ) { + return new DisabledSmartButton(); + } if ( $state->current_state() !== State::STATE_ONBOARDED ) { return new DisabledSmartButton(); } @@ -113,6 +149,15 @@ return array( $container->get( 'woocommerce.logger.woocommerce' ) ); }, + 'button.place-order-assets' => static function ( ContainerInterface $container ): PlaceOrderButtonAssets { + return new PlaceOrderButtonAssets( + $container->get( 'button.url' ), + $container->get( 'ppcp.asset-version' ), + $container->get( 'session.handler' ), + $container->get( 'wcgateway.use-place-order-button' ), + $container->get( 'wcgateway.place-order-button-text' ) + ); + }, 'button.url' => static function ( ContainerInterface $container ): string { return plugins_url( '/modules/ppcp-button/', diff --git a/modules/ppcp-button/src/Assets/PlaceOrderButtonAssets.php b/modules/ppcp-button/src/Assets/PlaceOrderButtonAssets.php new file mode 100644 index 000000000..ab4b6dd8d --- /dev/null +++ b/modules/ppcp-button/src/Assets/PlaceOrderButtonAssets.php @@ -0,0 +1,116 @@ +module_url = $module_url; + $this->version = $version; + $this->session_handler = $session_handler; + $this->use_place_order = $use_place_order; + $this->button_text = $button_text; + } + + /** + * Registers the assets. + */ + public function register_assets(): void { + if ( $this->should_load() ) { + wp_enqueue_script( + 'ppcp-place-order-button', + trailingslashit( $this->module_url ) . 'assets/js/place-order-button.js', + array(), + $this->version, + true + ); + + wp_localize_script( + 'ppcp-place-order-button', + 'PpcpPlaceOrderButton', + array( + 'buttonText' => $this->button_text, + ) + ); + } + } + + /** + * Checks if the assets should be loaded. + */ + protected function should_load(): bool { + return $this->use_place_order && in_array( $this->context(), array( 'checkout', 'pay-now' ), true ); + } +} diff --git a/modules/ppcp-button/src/ButtonModule.php b/modules/ppcp-button/src/ButtonModule.php index 67dec01f3..b7ce6c36e 100644 --- a/modules/ppcp-button/src/ButtonModule.php +++ b/modules/ppcp-button/src/ButtonModule.php @@ -9,6 +9,7 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\Button; +use WooCommerce\PayPalCommerce\Button\Assets\PlaceOrderButtonAssets; use WooCommerce\PayPalCommerce\Button\Endpoint\ApproveSubscriptionEndpoint; use WooCommerce\PayPalCommerce\Button\Endpoint\CartScriptParamsEndpoint; use WooCommerce\PayPalCommerce\Button\Endpoint\SaveCheckoutFormEndpoint; @@ -71,6 +72,11 @@ class ButtonModule implements ModuleInterface { if ( $smart_button->should_load_ppcp_script() ) { $smart_button->enqueue(); } + + $place_order_assets = $c->get( 'button.place-order-assets' ); + assert( $place_order_assets instanceof PlaceOrderButtonAssets ); + + $place_order_assets->register_assets(); } ); diff --git a/modules/ppcp-button/webpack.config.js b/modules/ppcp-button/webpack.config.js index 8e02ccaa5..678a7d458 100644 --- a/modules/ppcp-button/webpack.config.js +++ b/modules/ppcp-button/webpack.config.js @@ -7,6 +7,7 @@ module.exports = { target: 'web', entry: { button: path.resolve('./resources/js/button.js'), + 'place-order-button': path.resolve('./resources/js/place-order-button.js'), "hosted-fields": path.resolve('./resources/css/hosted-fields.scss'), "gateway": path.resolve('./resources/css/gateway.scss') }, diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index bbf9629d4..5f84694b4 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -1156,6 +1156,25 @@ return array( ); }, + 'wcgateway.use-place-order-button' => function ( ContainerInterface $container ) : bool { + /** + * Whether to use the standard "Place order" button with redirect to PayPal instead of the PayPal smart buttons. + */ + return apply_filters( + 'woocommerce_paypal_payments_use_place_order_button', + false + ); + }, + 'wcgateway.place-order-button-text' => function ( ContainerInterface $container ) : string { + /** + * The text for the standard "Place order" button, when the "Place order" button mode is enabled. + */ + return apply_filters( + 'woocommerce_paypal_payments_place_order_button_text', + __( 'Pay with PayPal', 'woocommerce-paypal-payments' ) + ); + }, + 'wcgateway.helper.vaulting-scope' => static function ( ContainerInterface $container ): bool { try { $token = $container->get( 'api.bearer' )->bearer(); From 33ed474ae1070b2b21951d7b13c8101063187331 Mon Sep 17 00:00:00 2001 From: Alex P Date: Wed, 15 Nov 2023 09:45:15 +0200 Subject: [PATCH 05/15] Support place order button mode in block --- .../resources/js/checkout-block.js | 47 ++++++++++++------- modules/ppcp-blocks/services.php | 3 +- .../ppcp-blocks/src/PayPalPaymentMethod.php | 13 ++++- 3 files changed, 45 insertions(+), 18 deletions(-) diff --git a/modules/ppcp-blocks/resources/js/checkout-block.js b/modules/ppcp-blocks/resources/js/checkout-block.js index 582fdede3..3645782e4 100644 --- a/modules/ppcp-blocks/resources/js/checkout-block.js +++ b/modules/ppcp-blocks/resources/js/checkout-block.js @@ -316,20 +316,35 @@ const PayPalComponent = ({ } const features = ['products']; -let registerMethod = registerExpressPaymentMethod; -if (config.scriptData.continuation) { - features.push('ppcp_continuation'); - registerMethod = registerPaymentMethod; -} -registerMethod({ - name: config.id, - label:
, - content: , - edit: , - ariaLabel: config.title, - canMakePayment: () => config.enabled, - supports: { - features: features, - }, -}); +if (config.usePlaceOrder && !config.scriptData.continuation) { + registerPaymentMethod({ + name: config.id, + label:
, + content:
, + edit:
, + ariaLabel: config.title, + canMakePayment: () => config.enabled, + supports: { + features: features, + }, + }); +} else { + let registerMethod = registerExpressPaymentMethod; + if (config.scriptData.continuation) { + features.push('ppcp_continuation'); + registerMethod = registerPaymentMethod; + } + + registerMethod({ + name: config.id, + label:
, + content: , + edit: , + ariaLabel: config.title, + canMakePayment: () => config.enabled, + supports: { + features: features, + }, + }); +} diff --git a/modules/ppcp-blocks/services.php b/modules/ppcp-blocks/services.php index 5b5a5159b..7aa47bb64 100644 --- a/modules/ppcp-blocks/services.php +++ b/modules/ppcp-blocks/services.php @@ -34,7 +34,8 @@ return array( $container->get( 'wcgateway.paypal-gateway' ), $container->get( 'blocks.settings.final_review_enabled' ), $container->get( 'session.cancellation.view' ), - $container->get( 'session.handler' ) + $container->get( 'session.handler' ), + $container->get( 'wcgateway.use-place-order-button' ) ); }, 'blocks.settings.final_review_enabled' => static function ( ContainerInterface $container ): bool { diff --git a/modules/ppcp-blocks/src/PayPalPaymentMethod.php b/modules/ppcp-blocks/src/PayPalPaymentMethod.php index 3c407f2a2..61ac66140 100644 --- a/modules/ppcp-blocks/src/PayPalPaymentMethod.php +++ b/modules/ppcp-blocks/src/PayPalPaymentMethod.php @@ -87,6 +87,13 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType { */ private $session_handler; + /** + * Whether to use the standard "Place order" button. + * + * @var bool + */ + protected $use_place_order; + /** * Assets constructor. * @@ -99,6 +106,7 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType { * @param bool $final_review_enabled Whether the final review is enabled. * @param CancelView $cancellation_view The cancellation view. * @param SessionHandler $session_handler The Session handler. + * @param bool $use_place_order Whether to use the standard "Place order" button. */ public function __construct( string $module_url, @@ -109,7 +117,8 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType { PayPalGateway $gateway, bool $final_review_enabled, CancelView $cancellation_view, - SessionHandler $session_handler + SessionHandler $session_handler, + bool $use_place_order ) { $this->name = PayPalGateway::ID; $this->module_url = $module_url; @@ -121,6 +130,7 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType { $this->final_review_enabled = $final_review_enabled; $this->cancellation_view = $cancellation_view; $this->session_handler = $session_handler; + $this->use_place_order = $use_place_order; } /** @@ -181,6 +191,7 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType { 'enabled' => $this->settings_status->is_smart_button_enabled_for_location( $script_data['context'] ), 'fundingSource' => $this->session_handler->funding_source(), 'finalReviewEnabled' => $this->final_review_enabled, + 'usePlaceOrder' => $this->use_place_order, 'ajax' => array( 'update_shipping' => array( 'endpoint' => WC_AJAX::get_endpoint( UpdateShippingEndpoint::ENDPOINT ), From 3672c5a651bf35685827c48e9da0c9ea479acd49 Mon Sep 17 00:00:00 2001 From: Alex P Date: Wed, 15 Nov 2023 09:46:15 +0200 Subject: [PATCH 06/15] Do not load paypal sdk for continuation in block --- modules/ppcp-blocks/resources/js/checkout-block.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ppcp-blocks/resources/js/checkout-block.js b/modules/ppcp-blocks/resources/js/checkout-block.js index 3645782e4..50d7b9311 100644 --- a/modules/ppcp-blocks/resources/js/checkout-block.js +++ b/modules/ppcp-blocks/resources/js/checkout-block.js @@ -42,7 +42,7 @@ const PayPalComponent = ({ const [loaded, setLoaded] = useState(false); useEffect(() => { - if (!loaded) { + if (!loaded && !config.scriptData.continuation) { loadPaypalScript(config.scriptData, () => { setLoaded(true); From 91c6e55f3d5372abd8511244d18298f4336e1b1c Mon Sep 17 00:00:00 2001 From: Alex P Date: Thu, 16 Nov 2023 09:38:35 +0200 Subject: [PATCH 07/15] Replace place order text via WC_Gateway --- .../PlaceOrderButtonBootstrap.js | 45 ------- .../resources/js/place-order-button.js | 8 -- modules/ppcp-button/services.php | 10 -- .../src/Assets/PlaceOrderButtonAssets.php | 116 ------------------ modules/ppcp-button/src/ButtonModule.php | 6 - modules/ppcp-button/webpack.config.js | 1 - modules/ppcp-wc-gateway/services.php | 6 +- .../src/Gateway/CardButtonGateway.php | 5 +- .../src/Gateway/PayPalGateway.php | 5 +- .../WcGateway/Gateway/WcGatewayTest.php | 3 +- 10 files changed, 14 insertions(+), 191 deletions(-) delete mode 100644 modules/ppcp-button/resources/js/modules/ContextBootstrap/PlaceOrderButtonBootstrap.js delete mode 100644 modules/ppcp-button/resources/js/place-order-button.js delete mode 100644 modules/ppcp-button/src/Assets/PlaceOrderButtonAssets.php diff --git a/modules/ppcp-button/resources/js/modules/ContextBootstrap/PlaceOrderButtonBootstrap.js b/modules/ppcp-button/resources/js/modules/ContextBootstrap/PlaceOrderButtonBootstrap.js deleted file mode 100644 index ee4d12070..000000000 --- a/modules/ppcp-button/resources/js/modules/ContextBootstrap/PlaceOrderButtonBootstrap.js +++ /dev/null @@ -1,45 +0,0 @@ -import { - getCurrentPaymentMethod, - ORDER_BUTTON_SELECTOR, - PaymentMethods -} from "../Helper/CheckoutMethodState"; - -class PlaceOrderButtonBootstrap { - constructor(config) { - this.config = config; - this.defaultButtonText = null; - } - - init() { - jQuery(document.body).on('updated_checkout payment_method_selected', () => { - this.updateUi(); - }); - - this.updateUi(); - } - - updateUi() { - const button = document.querySelector(ORDER_BUTTON_SELECTOR); - if (!button) { - return; - } - - if (!this.defaultButtonText) { - this.defaultButtonText = button.innerText; - - if (!this.defaultButtonText) { - return; - } - } - - const currentPaymentMethod = getCurrentPaymentMethod(); - - if ([PaymentMethods.PAYPAL, PaymentMethods.CARD_BUTTON].includes(currentPaymentMethod)) { - button.innerText = this.config.buttonText; - } else { - button.innerText = this.defaultButtonText; - } - } -} - -export default PlaceOrderButtonBootstrap diff --git a/modules/ppcp-button/resources/js/place-order-button.js b/modules/ppcp-button/resources/js/place-order-button.js deleted file mode 100644 index b1d0969f7..000000000 --- a/modules/ppcp-button/resources/js/place-order-button.js +++ /dev/null @@ -1,8 +0,0 @@ -import PlaceOrderButtonBootstrap from "./modules/ContextBootstrap/PlaceOrderButtonBootstrap"; - -document.addEventListener( - 'DOMContentLoaded', - () => { - const placeOrderButtonBootstrap = new PlaceOrderButtonBootstrap(PpcpPlaceOrderButton); - placeOrderButtonBootstrap.init(); - }); diff --git a/modules/ppcp-button/services.php b/modules/ppcp-button/services.php index a0b5fac63..cae843039 100644 --- a/modules/ppcp-button/services.php +++ b/modules/ppcp-button/services.php @@ -9,7 +9,6 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\Button; -use WooCommerce\PayPalCommerce\Button\Assets\PlaceOrderButtonAssets; use WooCommerce\PayPalCommerce\Button\Endpoint\ApproveSubscriptionEndpoint; use WooCommerce\PayPalCommerce\Button\Endpoint\CartScriptParamsEndpoint; use WooCommerce\PayPalCommerce\Button\Endpoint\SimulateCartEndpoint; @@ -149,15 +148,6 @@ return array( $container->get( 'woocommerce.logger.woocommerce' ) ); }, - 'button.place-order-assets' => static function ( ContainerInterface $container ): PlaceOrderButtonAssets { - return new PlaceOrderButtonAssets( - $container->get( 'button.url' ), - $container->get( 'ppcp.asset-version' ), - $container->get( 'session.handler' ), - $container->get( 'wcgateway.use-place-order-button' ), - $container->get( 'wcgateway.place-order-button-text' ) - ); - }, 'button.url' => static function ( ContainerInterface $container ): string { return plugins_url( '/modules/ppcp-button/', diff --git a/modules/ppcp-button/src/Assets/PlaceOrderButtonAssets.php b/modules/ppcp-button/src/Assets/PlaceOrderButtonAssets.php deleted file mode 100644 index ab4b6dd8d..000000000 --- a/modules/ppcp-button/src/Assets/PlaceOrderButtonAssets.php +++ /dev/null @@ -1,116 +0,0 @@ -module_url = $module_url; - $this->version = $version; - $this->session_handler = $session_handler; - $this->use_place_order = $use_place_order; - $this->button_text = $button_text; - } - - /** - * Registers the assets. - */ - public function register_assets(): void { - if ( $this->should_load() ) { - wp_enqueue_script( - 'ppcp-place-order-button', - trailingslashit( $this->module_url ) . 'assets/js/place-order-button.js', - array(), - $this->version, - true - ); - - wp_localize_script( - 'ppcp-place-order-button', - 'PpcpPlaceOrderButton', - array( - 'buttonText' => $this->button_text, - ) - ); - } - } - - /** - * Checks if the assets should be loaded. - */ - protected function should_load(): bool { - return $this->use_place_order && in_array( $this->context(), array( 'checkout', 'pay-now' ), true ); - } -} diff --git a/modules/ppcp-button/src/ButtonModule.php b/modules/ppcp-button/src/ButtonModule.php index b7ce6c36e..67dec01f3 100644 --- a/modules/ppcp-button/src/ButtonModule.php +++ b/modules/ppcp-button/src/ButtonModule.php @@ -9,7 +9,6 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\Button; -use WooCommerce\PayPalCommerce\Button\Assets\PlaceOrderButtonAssets; use WooCommerce\PayPalCommerce\Button\Endpoint\ApproveSubscriptionEndpoint; use WooCommerce\PayPalCommerce\Button\Endpoint\CartScriptParamsEndpoint; use WooCommerce\PayPalCommerce\Button\Endpoint\SaveCheckoutFormEndpoint; @@ -72,11 +71,6 @@ class ButtonModule implements ModuleInterface { if ( $smart_button->should_load_ppcp_script() ) { $smart_button->enqueue(); } - - $place_order_assets = $c->get( 'button.place-order-assets' ); - assert( $place_order_assets instanceof PlaceOrderButtonAssets ); - - $place_order_assets->register_assets(); } ); diff --git a/modules/ppcp-button/webpack.config.js b/modules/ppcp-button/webpack.config.js index 678a7d458..8e02ccaa5 100644 --- a/modules/ppcp-button/webpack.config.js +++ b/modules/ppcp-button/webpack.config.js @@ -7,7 +7,6 @@ module.exports = { target: 'web', entry: { button: path.resolve('./resources/js/button.js'), - 'place-order-button': path.resolve('./resources/js/place-order-button.js'), "hosted-fields": path.resolve('./resources/css/hosted-fields.scss'), "gateway": path.resolve('./resources/css/gateway.scss') }, diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 5f84694b4..fb4fc68d1 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -98,7 +98,8 @@ return array( $logger, $api_shop_country, $container->get( 'api.endpoint.order' ), - $container->get( 'api.factory.paypal-checkout-url' ) + $container->get( 'api.factory.paypal-checkout-url' ), + $container->get( 'wcgateway.place-order-button-text' ) ); }, 'wcgateway.credit-card-gateway' => static function ( ContainerInterface $container ): CreditCardGateway { @@ -143,7 +144,8 @@ return array( $container->get( 'onboarding.environment' ), $container->get( 'vaulting.repository.payment-token' ), $container->get( 'woocommerce.logger.woocommerce' ), - $container->get( 'api.factory.paypal-checkout-url' ) + $container->get( 'api.factory.paypal-checkout-url' ), + $container->get( 'wcgateway.place-order-button-text' ) ); }, 'wcgateway.disabler' => static function ( ContainerInterface $container ): DisableGateways { diff --git a/modules/ppcp-wc-gateway/src/Gateway/CardButtonGateway.php b/modules/ppcp-wc-gateway/src/Gateway/CardButtonGateway.php index bc2e62db9..2f2f17680 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/CardButtonGateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/CardButtonGateway.php @@ -150,6 +150,7 @@ class CardButtonGateway extends \WC_Payment_Gateway { * @param PaymentTokenRepository $payment_token_repository The payment token repository. * @param LoggerInterface $logger The logger. * @param callable(string):string $paypal_checkout_url_factory The function return the PayPal checkout URL for the given order ID. + * @param string $place_order_button_text The text for the standard "Place order" button. */ public function __construct( SettingsRenderer $settings_renderer, @@ -164,7 +165,8 @@ class CardButtonGateway extends \WC_Payment_Gateway { Environment $environment, PaymentTokenRepository $payment_token_repository, LoggerInterface $logger, - callable $paypal_checkout_url_factory + callable $paypal_checkout_url_factory, + string $place_order_button_text ) { $this->id = self::ID; $this->settings_renderer = $settings_renderer; @@ -181,6 +183,7 @@ class CardButtonGateway extends \WC_Payment_Gateway { $this->payment_token_repository = $payment_token_repository; $this->logger = $logger; $this->paypal_checkout_url_factory = $paypal_checkout_url_factory; + $this->order_button_text = $place_order_button_text; $this->supports = array( 'refunds', diff --git a/modules/ppcp-wc-gateway/src/Gateway/PayPalGateway.php b/modules/ppcp-wc-gateway/src/Gateway/PayPalGateway.php index 456c7e09c..7d8ae3d04 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/PayPalGateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/PayPalGateway.php @@ -190,6 +190,7 @@ class PayPalGateway extends \WC_Payment_Gateway { * @param string $api_shop_country The api shop country. * @param OrderEndpoint $order_endpoint The order endpoint. * @param callable(string):string $paypal_checkout_url_factory The function return the PayPal checkout URL for the given order ID. + * @param string $place_order_button_text The text for the standard "Place order" button. */ public function __construct( SettingsRenderer $settings_renderer, @@ -207,7 +208,8 @@ class PayPalGateway extends \WC_Payment_Gateway { LoggerInterface $logger, string $api_shop_country, OrderEndpoint $order_endpoint, - callable $paypal_checkout_url_factory + callable $paypal_checkout_url_factory, + string $place_order_button_text ) { $this->id = self::ID; $this->settings_renderer = $settings_renderer; @@ -226,6 +228,7 @@ class PayPalGateway extends \WC_Payment_Gateway { $this->logger = $logger; $this->api_shop_country = $api_shop_country; $this->paypal_checkout_url_factory = $paypal_checkout_url_factory; + $this->order_button_text = $place_order_button_text; if ( $this->onboarded ) { $this->supports = array( 'refunds', 'tokenization' ); diff --git a/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php b/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php index 44f729377..962dbbdd4 100644 --- a/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php +++ b/tests/PHPUnit/WcGateway/Gateway/WcGatewayTest.php @@ -110,7 +110,8 @@ class WcGatewayTest extends TestCase $this->orderEndpoint, function ($id) { return 'checkoutnow=' . $id; - } + }, + 'Pay via PayPal' ); } From 2dcfd115a995b7da48a6c0eb1f969bcb636050bc Mon Sep 17 00:00:00 2001 From: Alex P Date: Thu, 16 Nov 2023 10:24:56 +0200 Subject: [PATCH 08/15] Fix not approved order processing --- modules/ppcp-wc-gateway/src/Endpoint/ReturnUrlEndpoint.php | 6 +++++- modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php | 2 -- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/modules/ppcp-wc-gateway/src/Endpoint/ReturnUrlEndpoint.php b/modules/ppcp-wc-gateway/src/Endpoint/ReturnUrlEndpoint.php index 23442a59d..9d5f98ec7 100644 --- a/modules/ppcp-wc-gateway/src/Endpoint/ReturnUrlEndpoint.php +++ b/modules/ppcp-wc-gateway/src/Endpoint/ReturnUrlEndpoint.php @@ -85,7 +85,11 @@ class ReturnUrlEndpoint { // phpcs:enable WordPress.Security.NonceVerification.Recommended $order = $this->order_endpoint->order( $token ); - $this->session_handler->replace_order( $order ); + if ( $order->status()->is( OrderStatus::APPROVED ) + || $order->status()->is( OrderStatus::COMPLETED ) + ) { + $this->session_handler->replace_order( $order ); + } $wc_order_id = (int) $order->purchase_units()[0]->custom_id(); if ( ! $wc_order_id ) { diff --git a/modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php b/modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php index 6e8f8253e..2497f8f91 100644 --- a/modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php +++ b/modules/ppcp-wc-gateway/src/Processor/OrderProcessor.php @@ -286,8 +286,6 @@ class OrderProcessor { ApplicationContext::USER_ACTION_PAY_NOW ); - $this->add_paypal_meta( $wc_order, $order, $this->environment ); - return $order; } From 59d6c969233c7a9d09f3cc416f1a4a072f5f72d0 Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Thu, 16 Nov 2023 16:06:07 +0400 Subject: [PATCH 09/15] Fix action callback arguments count --- .../src/Integration/ShipStationIntegration.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ppcp-order-tracking/src/Integration/ShipStationIntegration.php b/modules/ppcp-order-tracking/src/Integration/ShipStationIntegration.php index 8d294f724..08a476e4b 100644 --- a/modules/ppcp-order-tracking/src/Integration/ShipStationIntegration.php +++ b/modules/ppcp-order-tracking/src/Integration/ShipStationIntegration.php @@ -111,7 +111,7 @@ class ShipStationIntegration implements Integration { } }, 500, - 1 + 2 ); } } From f232946afac0b3d38aa8bcb2c9b2b6078a624876 Mon Sep 17 00:00:00 2001 From: Alex P Date: Thu, 16 Nov 2023 16:01:32 +0200 Subject: [PATCH 10/15] Replace place order text in block --- .../resources/js/checkout-block.js | 1 + modules/ppcp-blocks/services.php | 3 +- .../ppcp-blocks/src/PayPalPaymentMethod.php | 53 +++++++++++-------- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/modules/ppcp-blocks/resources/js/checkout-block.js b/modules/ppcp-blocks/resources/js/checkout-block.js index 50d7b9311..8631d8b05 100644 --- a/modules/ppcp-blocks/resources/js/checkout-block.js +++ b/modules/ppcp-blocks/resources/js/checkout-block.js @@ -323,6 +323,7 @@ if (config.usePlaceOrder && !config.scriptData.continuation) { label:
, content:
, edit:
, + placeOrderButtonLabel: config.placeOrderButtonText, ariaLabel: config.title, canMakePayment: () => config.enabled, supports: { diff --git a/modules/ppcp-blocks/services.php b/modules/ppcp-blocks/services.php index 7aa47bb64..8b4ac5475 100644 --- a/modules/ppcp-blocks/services.php +++ b/modules/ppcp-blocks/services.php @@ -35,7 +35,8 @@ return array( $container->get( 'blocks.settings.final_review_enabled' ), $container->get( 'session.cancellation.view' ), $container->get( 'session.handler' ), - $container->get( 'wcgateway.use-place-order-button' ) + $container->get( 'wcgateway.use-place-order-button' ), + $container->get( 'wcgateway.place-order-button-text' ) ); }, 'blocks.settings.final_review_enabled' => static function ( ContainerInterface $container ): bool { diff --git a/modules/ppcp-blocks/src/PayPalPaymentMethod.php b/modules/ppcp-blocks/src/PayPalPaymentMethod.php index 61ac66140..2fc2cbca1 100644 --- a/modules/ppcp-blocks/src/PayPalPaymentMethod.php +++ b/modules/ppcp-blocks/src/PayPalPaymentMethod.php @@ -94,6 +94,13 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType { */ protected $use_place_order; + /** + * The text for the standard "Place order" button. + * + * @var string + */ + protected $place_order_button_text; + /** * Assets constructor. * @@ -107,6 +114,7 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType { * @param CancelView $cancellation_view The cancellation view. * @param SessionHandler $session_handler The Session handler. * @param bool $use_place_order Whether to use the standard "Place order" button. + * @param string $place_order_button_text The text for the standard "Place order" button. */ public function __construct( string $module_url, @@ -118,19 +126,21 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType { bool $final_review_enabled, CancelView $cancellation_view, SessionHandler $session_handler, - bool $use_place_order + bool $use_place_order, + string $place_order_button_text ) { - $this->name = PayPalGateway::ID; - $this->module_url = $module_url; - $this->version = $version; - $this->smart_button = $smart_button; - $this->plugin_settings = $plugin_settings; - $this->settings_status = $settings_status; - $this->gateway = $gateway; - $this->final_review_enabled = $final_review_enabled; - $this->cancellation_view = $cancellation_view; - $this->session_handler = $session_handler; - $this->use_place_order = $use_place_order; + $this->name = PayPalGateway::ID; + $this->module_url = $module_url; + $this->version = $version; + $this->smart_button = $smart_button; + $this->plugin_settings = $plugin_settings; + $this->settings_status = $settings_status; + $this->gateway = $gateway; + $this->final_review_enabled = $final_review_enabled; + $this->cancellation_view = $cancellation_view; + $this->session_handler = $session_handler; + $this->use_place_order = $use_place_order; + $this->place_order_button_text = $place_order_button_text; } /** @@ -185,20 +195,21 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType { } return array( - 'id' => $this->gateway->id, - 'title' => $this->gateway->title, - 'description' => $this->gateway->description, - 'enabled' => $this->settings_status->is_smart_button_enabled_for_location( $script_data['context'] ), - 'fundingSource' => $this->session_handler->funding_source(), - 'finalReviewEnabled' => $this->final_review_enabled, - 'usePlaceOrder' => $this->use_place_order, - 'ajax' => array( + 'id' => $this->gateway->id, + 'title' => $this->gateway->title, + 'description' => $this->gateway->description, + 'enabled' => $this->settings_status->is_smart_button_enabled_for_location( $script_data['context'] ), + 'fundingSource' => $this->session_handler->funding_source(), + 'finalReviewEnabled' => $this->final_review_enabled, + 'usePlaceOrder' => $this->use_place_order, + 'placeOrderButtonText' => $this->place_order_button_text, + 'ajax' => array( 'update_shipping' => array( 'endpoint' => WC_AJAX::get_endpoint( UpdateShippingEndpoint::ENDPOINT ), 'nonce' => wp_create_nonce( UpdateShippingEndpoint::nonce() ), ), ), - 'scriptData' => $script_data, + 'scriptData' => $script_data, ); } } From f50a071a11a5abcc6eb5b96eb7a7217d84af813b Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Fri, 17 Nov 2023 17:45:21 +0400 Subject: [PATCH 11/15] Add priority "high" for tracking metabox --- modules/ppcp-order-tracking/src/OrderTrackingModule.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/ppcp-order-tracking/src/OrderTrackingModule.php b/modules/ppcp-order-tracking/src/OrderTrackingModule.php index ae6bfecc2..5d5905cef 100644 --- a/modules/ppcp-order-tracking/src/OrderTrackingModule.php +++ b/modules/ppcp-order-tracking/src/OrderTrackingModule.php @@ -102,7 +102,8 @@ class OrderTrackingModule implements ModuleInterface { __( 'PayPal Package Tracking', 'woocommerce-paypal-payments' ), array( $meta_box_renderer, 'render' ), $screen, - 'side' + 'side', + 'high' ); }, 10, From e7e5f10f83d7759e75d5430273460aac7acbea09 Mon Sep 17 00:00:00 2001 From: Pedro Silva Date: Fri, 17 Nov 2023 16:31:47 +0000 Subject: [PATCH 12/15] Add block cart and block express checkout to default buttons locations. --- modules/ppcp-wc-gateway/services.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 8c0f4474a..68d4ed172 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -1372,7 +1372,9 @@ return array( ); }, 'wcgateway.button.default-locations' => static function( ContainerInterface $container ): array { - return array_keys( $container->get( 'wcgateway.settings.pay-later.messaging-locations' ) ); + $button_locations = $container->get( 'wcgateway.button.locations' ); + unset( $button_locations['mini-cart'] ); + return array_keys( $button_locations ); }, 'wcgateway.settings.pay-later.button-locations' => static function( ContainerInterface $container ): array { $settings = $container->get( 'wcgateway.settings' ); From 40bd3f1d5ca3f74d4ebd76b011f10191ea7f9b99 Mon Sep 17 00:00:00 2001 From: Pedro Silva Date: Mon, 20 Nov 2023 10:43:09 +0000 Subject: [PATCH 13/15] Refactor button and paylater messaging default locations. --- modules/ppcp-wc-gateway/services.php | 18 +++++++--- .../Settings/Fields/pay-later-tab-fields.php | 4 +-- .../ppcp-wc-gateway/src/Settings/Settings.php | 33 ++++++++++++++++--- 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 68d4ed172..8a0348a80 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -209,7 +209,9 @@ return array( static function ( ContainerInterface $container ): Settings { return new Settings( $container->get( 'wcgateway.button.default-locations' ), - $container->get( 'wcgateway.settings.dcc-gateway-title.default' ) + $container->get( 'wcgateway.settings.dcc-gateway-title.default' ), + $container->get( 'wcgateway.settings.pay-later.default-button-locations' ), + $container->get( 'wcgateway.settings.pay-later.default-messaging-locations' ) ); } ), @@ -1360,6 +1362,11 @@ return array( 'mini-cart' => 'Mini Cart', ); }, + 'wcgateway.button.default-locations' => static function( ContainerInterface $container ): array { + $button_locations = $container->get( 'wcgateway.button.locations' ); + unset( $button_locations['mini-cart'] ); + return array_keys( $button_locations ); + }, 'wcgateway.settings.pay-later.messaging-locations' => static function( ContainerInterface $container ): array { $button_locations = $container->get( 'wcgateway.button.locations' ); unset( $button_locations['mini-cart'] ); @@ -1371,10 +1378,8 @@ return array( ) ); }, - 'wcgateway.button.default-locations' => static function( ContainerInterface $container ): array { - $button_locations = $container->get( 'wcgateway.button.locations' ); - unset( $button_locations['mini-cart'] ); - return array_keys( $button_locations ); + 'wcgateway.settings.pay-later.default-messaging-locations' => static function( ContainerInterface $container ): array { + return array_keys( $container->get( 'wcgateway.settings.pay-later.messaging-locations' ) ); }, 'wcgateway.settings.pay-later.button-locations' => static function( ContainerInterface $container ): array { $settings = $container->get( 'wcgateway.settings' ); @@ -1386,6 +1391,9 @@ return array( return array_intersect_key( $button_locations, array_flip( $smart_button_selected_locations ) ); }, + 'wcgateway.settings.pay-later.default-button-locations' => static function( ContainerInterface $container ): array { + return $container->get( 'wcgateway.button.default-locations' ); + }, 'wcgateway.ppcp-gateways' => static function ( ContainerInterface $container ): array { return array( PayPalGateway::ID, diff --git a/modules/ppcp-wc-gateway/src/Settings/Fields/pay-later-tab-fields.php b/modules/ppcp-wc-gateway/src/Settings/Fields/pay-later-tab-fields.php index 99a3fd3f0..4fc6635f7 100644 --- a/modules/ppcp-wc-gateway/src/Settings/Fields/pay-later-tab-fields.php +++ b/modules/ppcp-wc-gateway/src/Settings/Fields/pay-later-tab-fields.php @@ -73,7 +73,7 @@ return function ( ContainerInterface $container, array $fields ): array { 'type' => 'ppcp-multiselect', 'class' => array(), 'input_class' => array( 'wc-enhanced-select' ), - 'default' => $container->get( 'wcgateway.button.default-locations' ), + 'default' => $container->get( 'wcgateway.settings.pay-later.default-button-locations' ), 'desc_tip' => false, 'description' => __( 'Select where the Pay Later button should be displayed.', 'woocommerce-paypal-payments' ), 'options' => $container->get( 'wcgateway.settings.pay-later.button-locations' ), @@ -119,7 +119,7 @@ return function ( ContainerInterface $container, array $fields ): array { 'type' => 'ppcp-multiselect', 'class' => array(), 'input_class' => array( 'wc-enhanced-select' ), - 'default' => $container->get( 'wcgateway.button.default-locations' ), + 'default' => $container->get( 'wcgateway.settings.pay-later.default-messaging-locations' ), 'desc_tip' => false, 'description' => __( 'Select where the Pay Later messaging should be displayed.', 'woocommerce-paypal-payments' ), 'options' => $container->get( 'wcgateway.settings.pay-later.messaging-locations' ), diff --git a/modules/ppcp-wc-gateway/src/Settings/Settings.php b/modules/ppcp-wc-gateway/src/Settings/Settings.php index 9abca804d..9ba1529c2 100644 --- a/modules/ppcp-wc-gateway/src/Settings/Settings.php +++ b/modules/ppcp-wc-gateway/src/Settings/Settings.php @@ -35,6 +35,20 @@ class Settings implements ContainerInterface { */ protected $default_button_locations; + /** + * The list of selected default pay later button locations. + * + * @var string[] + */ + protected $default_pay_later_button_locations; + + /** + * The list of selected default pay later messaging locations. + * + * @var string[] + */ + protected $default_pay_later_messaging_locations; + /** * The default ACDC gateway title. * @@ -47,10 +61,19 @@ class Settings implements ContainerInterface { * * @param string[] $default_button_locations The list of selected default button locations. * @param string $default_dcc_gateway_title The default ACDC gateway title. + * @param string[] $default_pay_later_button_locations The list of selected default pay later button locations. + * @param string[] $default_pay_later_messaging_locations The list of selected default pay later messaging locations. */ - public function __construct( array $default_button_locations, string $default_dcc_gateway_title ) { - $this->default_button_locations = $default_button_locations; - $this->default_dcc_gateway_title = $default_dcc_gateway_title; + public function __construct( + array $default_button_locations, + string $default_dcc_gateway_title, + array $default_pay_later_button_locations, + array $default_pay_later_messaging_locations + ) { + $this->default_button_locations = $default_button_locations; + $this->default_dcc_gateway_title = $default_dcc_gateway_title; + $this->default_pay_later_button_locations = $default_pay_later_button_locations; + $this->default_pay_later_messaging_locations = $default_pay_later_messaging_locations; } /** @@ -122,8 +145,8 @@ class Settings implements ContainerInterface { 'smart_button_enable_styling_per_location' => true, 'pay_later_messaging_enabled' => true, 'pay_later_button_enabled' => true, - 'pay_later_button_locations' => $this->default_button_locations, - 'pay_later_messaging_locations' => $this->default_button_locations, + 'pay_later_button_locations' => $this->default_pay_later_button_locations, + 'pay_later_messaging_locations' => $this->default_pay_later_messaging_locations, 'brand_name' => get_bloginfo( 'name' ), 'dcc_gateway_title' => $this->default_dcc_gateway_title, 'dcc_gateway_description' => __( From e3276cda691d34f870233dbdd19810090b6fe913 Mon Sep 17 00:00:00 2001 From: Pedro Silva Date: Tue, 21 Nov 2023 11:55:50 +0000 Subject: [PATCH 14/15] Fix message rendering on block themes. --- .../ppcp-button/src/Assets/SmartButton.php | 116 ++++++++++++++++-- 1 file changed, 105 insertions(+), 11 deletions(-) diff --git a/modules/ppcp-button/src/Assets/SmartButton.php b/modules/ppcp-button/src/Assets/SmartButton.php index e8e0796ff..583121a2e 100644 --- a/modules/ppcp-button/src/Assets/SmartButton.php +++ b/modules/ppcp-button/src/Assets/SmartButton.php @@ -397,7 +397,15 @@ class SmartButton implements SmartButtonInterface { $default_pay_order_hook = 'woocommerce_pay_order_before_submit'; - $get_hook = function ( string $location ) use ( $default_pay_order_hook ): ?array { + /** + * The filter returning if the current theme is a block theme or not. + */ + $is_block_theme = (bool) apply_filters( + 'woocommerce_paypal_payments_messages_renderer_is_block', + wp_is_block_theme() + ); + + $get_hook = function ( string $location ) use ( $default_pay_order_hook, $is_block_theme ): ?array { switch ( $location ) { case 'checkout': return $this->messages_renderer_hook( $location, 'woocommerce_review_order_before_payment', 10 ); @@ -408,9 +416,13 @@ class SmartButton implements SmartButtonInterface { case 'product': return $this->messages_renderer_hook( $location, $this->single_product_renderer_hook(), 30 ); case 'shop': - return $this->messages_renderer_hook( $location, 'woocommerce_archive_description', 10 ); + return $is_block_theme + ? $this->messages_renderer_block( $location, 'core/query-title', 10 ) + : $this->messages_renderer_hook( $location, 'woocommerce_archive_description', 10 ); case 'home': - return $this->messages_renderer_hook( $location, 'loop_start', 20 ); + return $is_block_theme + ? $this->messages_renderer_block( $location, 'core/navigation', 10 ) + : $this->messages_renderer_hook( $location, 'loop_start', 20 ); default: return null; } @@ -421,11 +433,15 @@ class SmartButton implements SmartButtonInterface { return false; } - add_action( - $hook['name'], - array( $this, 'message_renderer' ), - $hook['priority'] - ); + if ( $hook['blockName'] ?? false ) { + $this->message_renderer( $hook ); + } else { + add_action( + $hook['name'], + array( $this, 'message_renderer' ), + $hook['priority'] + ); + } // Looks like there are no hooks like woocommerce_review_order_before_payment on the pay for order page, so have to move using JS. if ( $location === 'pay-now' && $hook['name'] === $default_pay_order_hook && @@ -704,9 +720,11 @@ document.querySelector("#payment").before(document.querySelector("#ppcp-messages /** * Renders the HTML for the credit messaging. + * + * @param array|null $block_params If it's to be rendered after a block, contains the block params. + * @return void */ - public function message_renderer(): void { - + public function message_renderer( $block_params = array() ): void { $product = wc_get_product(); $location = $this->location(); @@ -727,7 +745,18 @@ document.querySelector("#payment").before(document.querySelector("#ppcp-messages */ do_action( "ppcp_before_{$location_hook}_message_wrapper" ); - echo '
'; + $messages_placeholder = '
'; + + if ( is_array( $block_params ) && ( $block_params['blockName'] ?? false ) ) { + $this->render_after_block( + $block_params['blockName'], + '
' . $messages_placeholder . '
', + $block_params['priority'] ?? 10 + ); + } else { + // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + echo $messages_placeholder; + } /** * A hook executed after rendering of the PCP Pay Later messages wrapper. @@ -735,6 +764,40 @@ document.querySelector("#payment").before(document.querySelector("#ppcp-messages do_action( "ppcp_after_{$location_hook}_message_wrapper" ); } + /** + * Renders content after a given block. + * + * @param string $name The name of the block to render after. + * @param string $content The content to be rendered. + * @param int $priority The 'render_block' hook priority. + * @return void + */ + private function render_after_block( string $name, string $content, int $priority = 10 ): void { + add_filter( + 'render_block', + /** + * Adds content after a given block. + * + * @param string $block_content The block content. + * @param array|mixed $block_params The block params. + * @return string + * + * @psalm-suppress MissingClosureParamType + */ + function ( $block_content, $block_params ) use ( $name, $content, $priority ) { + if ( + is_array( $block_params ) + && ( $block_params['blockName'] ?? null ) === $name + ) { + $block_content .= $content; + } + return $block_content; + }, + $priority, + 2 + ); + } + /** * The values for the credit messaging. * @@ -1464,6 +1527,37 @@ document.querySelector("#payment").before(document.querySelector("#ppcp-messages ); } + /** + * Returns the block name that will be used for rendering Pay Later messages after. + * + * @param string $location The location name like 'checkout', 'shop'. See render_message_wrapper_registrar. + * @param string $default_block The default name of the block. + * @param int $default_priority The default priority of the 'render_block' hook. + * @return array An array with 'blockName' and 'priority' keys. + */ + private function messages_renderer_block( string $location, string $default_block, int $default_priority ): array { + $location_hook = $this->location_to_hook( $location ); + + /** + * The filter returning the action name that will be used for rendering Pay Later messages. + */ + $block_name = (string) apply_filters( + "woocommerce_paypal_payments_${location_hook}_messages_renderer_block", + $default_block + ); + /** + * The filter returning the action priority that will be used for rendering Pay Later messages. + */ + $priority = (int) apply_filters( + "woocommerce_paypal_payments_${location_hook}_messages_renderer_block_priority", + $default_priority + ); + return array( + 'blockName' => $block_name, + 'priority' => $priority, + ); + } + /** * Returns action name that PayPal button will use for rendering next to Proceed to checkout button (normally displayed in cart). * From 020be490a6ad8fb2f266e7dcdd71c19b5f3dc490 Mon Sep 17 00:00:00 2001 From: Pedro Silva Date: Tue, 21 Nov 2023 16:37:45 +0000 Subject: [PATCH 15/15] Fix lint --- modules/ppcp-wc-gateway/services.php | 4 +++- modules/ppcp-wc-gateway/src/Assets/SettingsPageAssets.php | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 8a0348a80..80af8ce8d 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -1379,7 +1379,9 @@ return array( ); }, 'wcgateway.settings.pay-later.default-messaging-locations' => static function( ContainerInterface $container ): array { - return array_keys( $container->get( 'wcgateway.settings.pay-later.messaging-locations' ) ); + $locations = $container->get( 'wcgateway.settings.pay-later.messaging-locations' ); + unset( $locations['home'] ); + return array_keys( $locations ); }, 'wcgateway.settings.pay-later.button-locations' => static function( ContainerInterface $container ): array { $settings = $container->get( 'wcgateway.settings' ); diff --git a/modules/ppcp-wc-gateway/src/Assets/SettingsPageAssets.php b/modules/ppcp-wc-gateway/src/Assets/SettingsPageAssets.php index 37571c184..db2bdd68a 100644 --- a/modules/ppcp-wc-gateway/src/Assets/SettingsPageAssets.php +++ b/modules/ppcp-wc-gateway/src/Assets/SettingsPageAssets.php @@ -184,7 +184,7 @@ class SettingsPageAssets { } $screen = get_current_screen(); - if ( $screen->id !== 'woocommerce_page_wc-settings' ) { + if ( ! $screen || $screen->id !== 'woocommerce_page_wc-settings' ) { return false; }