Allow handling the shipping in PayPal

This commit is contained in:
Narek Zakarian 2024-05-06 18:44:24 +04:00
parent f1a114588a
commit b7498149ad
No known key found for this signature in database
GPG key ID: 07AFD7E7A9C164A7
3 changed files with 48 additions and 4 deletions

View file

@ -3,6 +3,10 @@ import {loadScript} from "@paypal/paypal-js";
import {keysToCamelCase} from "../Helper/Utils";
import widgetBuilder from "./WidgetBuilder";
import {normalizeStyleForFundingSource} from "../Helper/Style";
import {
handleShippingOptionsChange,
handleShippingAddressChange,
} from "../Helper/ShippingHandler.js";
class Renderer {
constructor(creditCardRenderer, defaultSettings, onSmartButtonClick, onSmartButtonsInit) {
@ -64,6 +68,10 @@ class Renderer {
}
}
shouldHandleShippingInPaypal = () => {
return this.defaultSettings.should_handle_shipping_in_paypal;
}
renderButtons(wrapper, style, contextConfig, hasEnabledSeparateGateways, fundingSource = null) {
if (! document.querySelector(wrapper) || this.isAlreadyRendered(wrapper, fundingSource, hasEnabledSeparateGateways) ) {
// Try to render registered buttons again in case they were removed from the DOM by an external source.
@ -86,6 +94,8 @@ class Renderer {
}
this.handleOnButtonsInit(wrapper, data, actions);
},
onShippingOptionsChange: (data, actions) => this.shouldHandleShippingInPaypal() ? handleShippingOptionsChange(data, actions, this.defaultSettings) : null,
onShippingAddressChange: (data, actions) => this.shouldHandleShippingInPaypal() ? handleShippingAddressChange(data, actions, this.defaultSettings) : null,
}
}

View file

@ -147,7 +147,8 @@ return array(
$container->get( 'wcgateway.funding-sources-without-redirect' ),
$container->get( 'vaulting.vault-v3-enabled' ),
$container->get( 'api.endpoint.payment-tokens' ),
$container->get( 'woocommerce.logger.woocommerce' )
$container->get( 'woocommerce.logger.woocommerce' ),
$container->get( 'button.handle-shipping-in-paypal' )
);
},
'button.url' => static function ( ContainerInterface $container ): string {
@ -157,7 +158,13 @@ return array(
);
},
'button.pay-now-contexts' => static function ( ContainerInterface $container ): array {
return array( 'checkout', 'pay-now' );
$defaults = array( 'checkout', 'pay-now' );
if ( $container->get( 'button.handle-shipping-in-paypal' ) ) {
return array_merge( $defaults, array( 'cart', 'product', 'mini-cart' ) );
}
return $defaults;
},
'button.request-data' => static function ( ContainerInterface $container ): RequestData {
return new RequestData();
@ -342,6 +349,6 @@ return array(
* May result in slower popup performance, additional loading.
*/
'button.handle-shipping-in-paypal' => static function ( ContainerInterface $container ): bool {
return false;
return ! $container->get( 'blocks.settings.final_review_enabled' );
},
);

View file

@ -19,6 +19,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\Money;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory;
use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies;
use WooCommerce\PayPalCommerce\Blocks\Endpoint\UpdateShippingEndpoint;
use WooCommerce\PayPalCommerce\Button\Endpoint\ApproveOrderEndpoint;
use WooCommerce\PayPalCommerce\Button\Endpoint\ApproveSubscriptionEndpoint;
use WooCommerce\PayPalCommerce\Button\Endpoint\CartScriptParamsEndpoint;
@ -217,6 +218,13 @@ class SmartButton implements SmartButtonInterface {
*/
private $logger;
/**
* Whether the shipping should be handled in PayPal.
*
* @var bool
*/
private $should_handle_shipping_in_paypal;
/**
* SmartButton constructor.
*
@ -242,6 +250,7 @@ class SmartButton implements SmartButtonInterface {
* @param bool $vault_v3_enabled Whether Vault v3 module is enabled.
* @param PaymentTokensEndpoint $payment_tokens_endpoint Payment tokens endpoint.
* @param LoggerInterface $logger The logger.
* @param bool $should_handle_shipping_in_paypal Whether the shipping should be handled in PayPal.
*/
public function __construct(
string $module_url,
@ -265,7 +274,8 @@ class SmartButton implements SmartButtonInterface {
array $funding_sources_without_redirect,
bool $vault_v3_enabled,
PaymentTokensEndpoint $payment_tokens_endpoint,
LoggerInterface $logger
LoggerInterface $logger,
bool $should_handle_shipping_in_paypal
) {
$this->module_url = $module_url;
@ -290,6 +300,7 @@ class SmartButton implements SmartButtonInterface {
$this->vault_v3_enabled = $vault_v3_enabled;
$this->logger = $logger;
$this->payment_tokens_endpoint = $payment_tokens_endpoint;
$this->should_handle_shipping_in_paypal = $should_handle_shipping_in_paypal;
}
/**
@ -1133,6 +1144,21 @@ document.querySelector("#payment").before(document.querySelector(".ppcp-messages
'endpoint' => \WC_AJAX::get_endpoint( CreatePaymentTokenForGuest::ENDPOINT ),
'nonce' => wp_create_nonce( CreatePaymentTokenForGuest::nonce() ),
),
'update_shipping' => array(
'endpoint' => \WC_AJAX::get_endpoint( UpdateShippingEndpoint::ENDPOINT ),
'nonce' => wp_create_nonce( UpdateShippingEndpoint::nonce() ),
),
'update_customer_shipping' => array(
'shipping_options' => array(
'endpoint' => '/wp-json/wc/store/cart/select-shipping-rate',
),
'shipping_address' => array(
'cart_endpoint' => '/wp-json/wc/store/cart/',
'update_customer_endpoint' => '/wp-json/wc/store/v1/cart/update-customer/',
),
'wp_rest_nonce' => wp_create_nonce( 'wc_store_api' ),
'update_shipping_method' => \WC_AJAX::get_endpoint( 'update_shipping_method' ),
),
),
'cart_contains_subscription' => $this->subscription_helper->cart_contains_subscription(),
'subscription_plan_id' => $this->subscription_helper->paypal_subscription_id(),
@ -1253,6 +1279,7 @@ document.querySelector("#payment").before(document.querySelector(".ppcp-messages
'user' => array(
'is_logged' => is_user_logged_in(),
),
'should_handle_shipping_in_paypal' => $this->should_handle_shipping_in_paypal
);
if ( 'pay-now' === $this->context() ) {