mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-01 07:02:48 +08:00
Merge pull request #2269 from woocommerce/PCP-3028-disable-shipping-callback-for-venmo-when-vaulting-is-active
Disable the shipping callback for "venmo" when vaulting is active (3028)
This commit is contained in:
commit
336492d63d
4 changed files with 65 additions and 31 deletions
|
@ -23,6 +23,9 @@ import {
|
||||||
import buttonModuleWatcher from "../../../ppcp-button/resources/js/modules/ButtonModuleWatcher";
|
import buttonModuleWatcher from "../../../ppcp-button/resources/js/modules/ButtonModuleWatcher";
|
||||||
import BlockCheckoutMessagesBootstrap from "./Bootstrap/BlockCheckoutMessagesBootstrap";
|
import BlockCheckoutMessagesBootstrap from "./Bootstrap/BlockCheckoutMessagesBootstrap";
|
||||||
import {keysToCamelCase} from "../../../ppcp-button/resources/js/modules/Helper/Utils";
|
import {keysToCamelCase} from "../../../ppcp-button/resources/js/modules/Helper/Utils";
|
||||||
|
import {
|
||||||
|
handleShippingOptionsChange
|
||||||
|
} from "../../../ppcp-button/resources/js/modules/Helper/ShippingHandler";
|
||||||
const config = wc.wcSettings.getSetting('ppcp-gateway_data');
|
const config = wc.wcSettings.getSetting('ppcp-gateway_data');
|
||||||
|
|
||||||
window.ppcpFundingSource = config.fundingSource;
|
window.ppcpFundingSource = config.fundingSource;
|
||||||
|
@ -146,7 +149,7 @@ const PayPalComponent = ({
|
||||||
shipping_address: addresses.shippingAddress,
|
shipping_address: addresses.shippingAddress,
|
||||||
}),
|
}),
|
||||||
];
|
];
|
||||||
if (!config.finalReviewEnabled) {
|
if (shouldHandleShippingInPayPal()) {
|
||||||
// set address in UI
|
// set address in UI
|
||||||
promises.push(wp.data.dispatch('wc/store/cart').setBillingAddress(addresses.billingAddress));
|
promises.push(wp.data.dispatch('wc/store/cart').setBillingAddress(addresses.billingAddress));
|
||||||
if (shippingData.needsShipping) {
|
if (shippingData.needsShipping) {
|
||||||
|
@ -181,7 +184,7 @@ const PayPalComponent = ({
|
||||||
throw new Error(config.scriptData.labels.error.generic)
|
throw new Error(config.scriptData.labels.error.generic)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.finalReviewEnabled) {
|
if (!shouldHandleShippingInPayPal()) {
|
||||||
location.href = getCheckoutRedirectUrl();
|
location.href = getCheckoutRedirectUrl();
|
||||||
} else {
|
} else {
|
||||||
setGotoContinuationOnError(true);
|
setGotoContinuationOnError(true);
|
||||||
|
@ -220,7 +223,7 @@ const PayPalComponent = ({
|
||||||
shipping_address: addresses.shippingAddress,
|
shipping_address: addresses.shippingAddress,
|
||||||
}),
|
}),
|
||||||
];
|
];
|
||||||
if (!config.finalReviewEnabled) {
|
if (shouldHandleShippingInPayPal()) {
|
||||||
// set address in UI
|
// set address in UI
|
||||||
promises.push(wp.data.dispatch('wc/store/cart').setBillingAddress(addresses.billingAddress));
|
promises.push(wp.data.dispatch('wc/store/cart').setBillingAddress(addresses.billingAddress));
|
||||||
if (shippingData.needsShipping) {
|
if (shippingData.needsShipping) {
|
||||||
|
@ -255,7 +258,7 @@ const PayPalComponent = ({
|
||||||
throw new Error(config.scriptData.labels.error.generic)
|
throw new Error(config.scriptData.labels.error.generic)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.finalReviewEnabled) {
|
if (!shouldHandleShippingInPayPal()) {
|
||||||
location.href = getCheckoutRedirectUrl();
|
location.href = getCheckoutRedirectUrl();
|
||||||
} else {
|
} else {
|
||||||
setGotoContinuationOnError(true);
|
setGotoContinuationOnError(true);
|
||||||
|
@ -297,8 +300,12 @@ const PayPalComponent = ({
|
||||||
onClick();
|
onClick();
|
||||||
};
|
};
|
||||||
|
|
||||||
const isVenmoAndVaultingEnabled = () => {
|
const shouldHandleShippingInPayPal = () => {
|
||||||
return window.ppcpFundingSource === 'venmo' && config.scriptData.vaultingEnabled;
|
if (config.finalReviewEnabled) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return window.ppcpFundingSource !== 'venmo' || !config.scriptData.vaultingEnabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
let handleShippingOptionsChange = null;
|
let handleShippingOptionsChange = null;
|
||||||
|
@ -306,7 +313,7 @@ const PayPalComponent = ({
|
||||||
let handleSubscriptionShippingOptionsChange = null;
|
let handleSubscriptionShippingOptionsChange = null;
|
||||||
let handleSubscriptionShippingAddressChange = null;
|
let handleSubscriptionShippingAddressChange = null;
|
||||||
|
|
||||||
if (shippingData.needsShipping && !config.finalReviewEnabled) {
|
if (shippingData.needsShipping && shouldHandleShippingInPayPal()) {
|
||||||
handleShippingOptionsChange = async (data, actions) => {
|
handleShippingOptionsChange = async (data, actions) => {
|
||||||
try {
|
try {
|
||||||
const shippingOptionId = data.selectedShippingOption?.id;
|
const shippingOptionId = data.selectedShippingOption?.id;
|
||||||
|
@ -447,7 +454,7 @@ const PayPalComponent = ({
|
||||||
if (config.scriptData.continuation) {
|
if (config.scriptData.continuation) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (!config.finalReviewEnabled) {
|
if (shouldHandleShippingInPayPal()) {
|
||||||
location.href = getCheckoutRedirectUrl();
|
location.href = getCheckoutRedirectUrl();
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@ -493,8 +500,16 @@ const PayPalComponent = ({
|
||||||
onError={onClose}
|
onError={onClose}
|
||||||
createSubscription={createSubscription}
|
createSubscription={createSubscription}
|
||||||
onApprove={handleApproveSubscription}
|
onApprove={handleApproveSubscription}
|
||||||
onShippingOptionsChange={handleSubscriptionShippingOptionsChange}
|
onShippingOptionsChange={(data, actions) => {
|
||||||
onShippingAddressChange={handleSubscriptionShippingAddressChange}
|
shouldHandleShippingInPayPal()
|
||||||
|
? handleSubscriptionShippingOptionsChange(data, actions)
|
||||||
|
: null;
|
||||||
|
}}
|
||||||
|
onShippingAddressChange={(data, actions) => {
|
||||||
|
shouldHandleShippingInPayPal()
|
||||||
|
? handleSubscriptionShippingAddressChange(data, actions)
|
||||||
|
: null;
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -508,8 +523,16 @@ const PayPalComponent = ({
|
||||||
onError={onClose}
|
onError={onClose}
|
||||||
createOrder={createOrder}
|
createOrder={createOrder}
|
||||||
onApprove={handleApprove}
|
onApprove={handleApprove}
|
||||||
onShippingOptionsChange={handleShippingOptionsChange}
|
onShippingOptionsChange={(data, actions) => {
|
||||||
onShippingAddressChange={handleShippingAddressChange}
|
shouldHandleShippingInPayPal()
|
||||||
|
? handleShippingOptionsChange(data, actions)
|
||||||
|
: null;
|
||||||
|
}}
|
||||||
|
onShippingAddressChange={(data, actions) => {
|
||||||
|
shouldHandleShippingInPayPal()
|
||||||
|
? handleShippingAddressChange(data, actions)
|
||||||
|
: null;
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,8 +60,7 @@ class CartActionHandler {
|
||||||
funding_source: window.ppcpFundingSource,
|
funding_source: window.ppcpFundingSource,
|
||||||
bn_code:bnCode,
|
bn_code:bnCode,
|
||||||
payer,
|
payer,
|
||||||
context:this.config.context,
|
context:this.config.context
|
||||||
payment_source: data.paymentSource
|
|
||||||
}),
|
}),
|
||||||
}).then(function(res) {
|
}).then(function(res) {
|
||||||
return res.json();
|
return res.json();
|
||||||
|
|
|
@ -68,14 +68,6 @@ class Renderer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
shouldHandleShippingInPaypal = (venmoButtonClicked) => {
|
|
||||||
if (!this.defaultSettings.should_handle_shipping_in_paypal) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return !venmoButtonClicked || !this.defaultSettings.vaultingEnabled;
|
|
||||||
}
|
|
||||||
|
|
||||||
renderButtons(wrapper, style, contextConfig, hasEnabledSeparateGateways, fundingSource = null) {
|
renderButtons(wrapper, style, contextConfig, hasEnabledSeparateGateways, fundingSource = null) {
|
||||||
if (! document.querySelector(wrapper) || this.isAlreadyRendered(wrapper, fundingSource, hasEnabledSeparateGateways) ) {
|
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.
|
// Try to render registered buttons again in case they were removed from the DOM by an external source.
|
||||||
|
@ -93,7 +85,16 @@ class Renderer {
|
||||||
const options = {
|
const options = {
|
||||||
style,
|
style,
|
||||||
...contextConfig,
|
...contextConfig,
|
||||||
onClick: this.onSmartButtonClick,
|
onClick: (data, actions) => {
|
||||||
|
if (this.onSmartButtonClick) {
|
||||||
|
this.onSmartButtonClick(data, actions);
|
||||||
|
}
|
||||||
|
|
||||||
|
venmoButtonClicked = false;
|
||||||
|
if (data.fundingSource === 'venmo') {
|
||||||
|
venmoButtonClicked = true;
|
||||||
|
}
|
||||||
|
},
|
||||||
onInit: (data, actions) => {
|
onInit: (data, actions) => {
|
||||||
if (this.onSmartButtonsInit) {
|
if (this.onSmartButtonsInit) {
|
||||||
this.onSmartButtonsInit(data, actions);
|
this.onSmartButtonsInit(data, actions);
|
||||||
|
@ -103,9 +104,17 @@ class Renderer {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Check the condition and add the handler if needed
|
// Check the condition and add the handler if needed
|
||||||
if (this.shouldHandleShippingInPaypal(venmoButtonClicked)) {
|
if (this.defaultSettings.should_handle_shipping_in_paypal) {
|
||||||
options.onShippingOptionsChange = (data, actions) => handleShippingOptionsChange(data, actions, this.defaultSettings);
|
options.onShippingOptionsChange = (data, actions) => {
|
||||||
options.onShippingAddressChange = (data, actions) => handleShippingAddressChange(data, actions, this.defaultSettings);
|
!this.isVenmoButtonClickedWhenVaultingIsEnabled(venmoButtonClicked)
|
||||||
|
? handleShippingOptionsChange(data, actions, this.defaultSettings)
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
options.onShippingAddressChange = (data, actions) => {
|
||||||
|
!this.isVenmoButtonClickedWhenVaultingIsEnabled(venmoButtonClicked)
|
||||||
|
? handleShippingAddressChange(data, actions, this.defaultSettings)
|
||||||
|
: null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return options;
|
return options;
|
||||||
|
@ -139,6 +148,10 @@ class Renderer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isVenmoButtonClickedWhenVaultingIsEnabled = (venmoButtonClicked) => {
|
||||||
|
return venmoButtonClicked && this.defaultSettings.vaultingEnabled;
|
||||||
|
}
|
||||||
|
|
||||||
isAlreadyRendered(wrapper, fundingSource) {
|
isAlreadyRendered(wrapper, fundingSource) {
|
||||||
return this.renderedSources.has(wrapper + (fundingSource ?? ''));
|
return this.renderedSources.has(wrapper + (fundingSource ?? ''));
|
||||||
}
|
}
|
||||||
|
|
|
@ -246,7 +246,6 @@ class CreateOrderEndpoint implements EndpointInterface {
|
||||||
$this->parsed_request_data = $data;
|
$this->parsed_request_data = $data;
|
||||||
$payment_method = $data['payment_method'] ?? '';
|
$payment_method = $data['payment_method'] ?? '';
|
||||||
$funding_source = $data['funding_source'] ?? '';
|
$funding_source = $data['funding_source'] ?? '';
|
||||||
$payment_source = $data['payment_source'] ?? '';
|
|
||||||
$wc_order = null;
|
$wc_order = null;
|
||||||
if ( 'pay-now' === $data['context'] ) {
|
if ( 'pay-now' === $data['context'] ) {
|
||||||
$wc_order = wc_get_order( (int) $data['order_id'] );
|
$wc_order = wc_get_order( (int) $data['order_id'] );
|
||||||
|
@ -262,7 +261,7 @@ class CreateOrderEndpoint implements EndpointInterface {
|
||||||
}
|
}
|
||||||
$this->purchase_unit = $this->purchase_unit_factory->from_wc_order( $wc_order );
|
$this->purchase_unit = $this->purchase_unit_factory->from_wc_order( $wc_order );
|
||||||
} else {
|
} else {
|
||||||
$this->purchase_unit = $this->purchase_unit_factory->from_wc_cart( null, $this->should_handle_shipping_in_paypal( $payment_source ) );
|
$this->purchase_unit = $this->purchase_unit_factory->from_wc_cart( null, $this->should_handle_shipping_in_paypal( $funding_source ) );
|
||||||
|
|
||||||
// Do not allow completion by webhooks when started via non-checkout buttons,
|
// Do not allow completion by webhooks when started via non-checkout buttons,
|
||||||
// it is needed only for some APMs in checkout.
|
// it is needed only for some APMs in checkout.
|
||||||
|
@ -615,16 +614,16 @@ class CreateOrderEndpoint implements EndpointInterface {
|
||||||
/**
|
/**
|
||||||
* Checks if the shipping should be handled in PayPal popup.
|
* Checks if the shipping should be handled in PayPal popup.
|
||||||
*
|
*
|
||||||
* @param string $payment_source The payment source.
|
* @param string $funding_source The funding source.
|
||||||
* @return bool true if the shipping should be handled in PayPal popup, otherwise false.
|
* @return bool true if the shipping should be handled in PayPal popup, otherwise false.
|
||||||
*/
|
*/
|
||||||
protected function should_handle_shipping_in_paypal( string $payment_source ): bool {
|
protected function should_handle_shipping_in_paypal( string $funding_source ): bool {
|
||||||
$is_vaulting_enabled = $this->settings->has( 'vault_enabled' ) && $this->settings->get( 'vault_enabled' );
|
$is_vaulting_enabled = $this->settings->has( 'vault_enabled' ) && $this->settings->get( 'vault_enabled' );
|
||||||
|
|
||||||
if ( ! $this->handle_shipping_in_paypal ) {
|
if ( ! $this->handle_shipping_in_paypal ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ! $is_vaulting_enabled || $payment_source !== 'venmo';
|
return ! $is_vaulting_enabled || $funding_source !== 'venmo';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue