Handle final review option

This commit is contained in:
Alex P 2023-04-24 08:12:48 +03:00
parent 57414cff9f
commit 37959ad2d8
No known key found for this signature in database
GPG key ID: 54487A734A204D71
6 changed files with 90 additions and 42 deletions

View file

@ -41,7 +41,7 @@ return array(
$fields,
'smart_button_enable_styling_per_location',
array(
'blocks_no_final_review' => array(
'blocks_final_review_enabled' => array(
'title' => __( 'Block Express payments Final Review', 'woocommerce-paypal-payments' ),
'type' => 'checkbox',
'label' => __( 'Require customers to confirm Block Express payments on the Checkout page. If disabled, the Checkout page is skipped for Block Express payments.', 'woocommerce-paypal-payments' ),

View file

@ -100,3 +100,19 @@ export const paypalOrderToWcShippingAddress = (order) => {
return res;
}
/**
*
* @param order
* @returns {{shippingAddress: Object, billingAddress: Object}}
*/
export const paypalOrderToWcAddresses = (order) => {
const shippingAddress = paypalOrderToWcShippingAddress(order);
let billingAddress = paypalPayerToWc(order.payer);
// no billing address, such as if billing address retrieval is not allowed in the merchant account
if (!billingAddress.address_line_1) {
billingAddress = {...shippingAddress, ...paypalPayerToWc(order.payer)};
}
return {billingAddress, shippingAddress};
}

View file

@ -1,6 +1,6 @@
import {useEffect, useState} from '@wordpress/element';
import {registerExpressPaymentMethod, registerPaymentMethod} from '@woocommerce/blocks-registry';
import {paypalOrderToWcShippingAddress, paypalPayerToWc} from "./Helper/Address";
import {paypalOrderToWcAddresses} from "./Helper/Address";
import {loadPaypalScript} from '../../../ppcp-button/resources/js/modules/Helper/ScriptLoading'
const config = wc.wcSettings.getSetting('ppcp-gateway_data');
@ -96,24 +96,22 @@ const PayPalComponent = ({
setPaypalOrder(order);
const shippingAddress = paypalOrderToWcShippingAddress(order);
let billingAddress = paypalPayerToWc(order.payer);
// no billing address, such as if billing address retrieval is not allowed in the merchant account
if (!billingAddress.address_line_1) {
billingAddress = {...shippingAddress, ...paypalPayerToWc(order.payer)};
if (config.finalReviewEnabled) {
const addresses = paypalOrderToWcAddresses(order);
await wp.data.dispatch('wc/store/cart').updateCustomerData({
billing_address: addresses.billingAddress,
shipping_address: addresses.shippingAddress,
});
const checkoutUrl = new URL(config.scriptData.redirect);
// sometimes some browsers may load some kind of cached version of the page,
// so adding a parameter to avoid that
checkoutUrl.searchParams.append('ppcp-continuation-redirect', (new Date()).getTime().toString());
location.href = checkoutUrl.toString();
} else {
onSubmit();
}
await wp.data.dispatch('wc/store/cart').updateCustomerData({
billing_address: billingAddress,
shipping_address: shippingAddress,
});
const checkoutUrl = new URL(config.scriptData.redirect);
// sometimes some browsers may load some kind of cached version of the page,
// so adding a parameter to avoid that
checkoutUrl.searchParams.append('ppcp-continuation-redirect', (new Date()).getTime().toString());
location.href = checkoutUrl.toString();
} catch (err) {
console.error(err);
@ -144,10 +142,23 @@ const PayPalComponent = ({
paymentMethodData: {
'paypal_order_id': config.scriptData.continuation.order_id,
'funding_source': window.ppcpFundingSource ?? 'paypal',
},
}
},
};
}
const addresses = paypalOrderToWcAddresses(paypalOrder);
return {
type: responseTypes.SUCCESS,
meta: {
paymentMethodData: {
'paypal_order_id': paypalOrder.id,
'funding_source': window.ppcpFundingSource ?? 'paypal',
},
...addresses,
},
};
});
return () => {
unsubscribeProcessing();

View file

@ -12,7 +12,7 @@ namespace WooCommerce\PayPalCommerce\Blocks;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
return array(
'blocks.url' => static function ( ContainerInterface $container ): string {
'blocks.url' => static function ( ContainerInterface $container ): string {
/**
* The path cannot be false.
*
@ -23,7 +23,7 @@ return array(
dirname( realpath( __FILE__ ), 3 ) . '/woocommerce-paypal-payments.php'
);
},
'blocks.method' => static function ( ContainerInterface $container ): PayPalPaymentMethod {
'blocks.method' => static function ( ContainerInterface $container ): PayPalPaymentMethod {
return new PayPalPaymentMethod(
$container->get( 'blocks.url' ),
$container->get( 'ppcp.asset-version' ),
@ -31,8 +31,17 @@ return array(
$container->get( 'wcgateway.settings' ),
$container->get( 'wcgateway.settings.status' ),
$container->get( 'wcgateway.paypal-gateway' ),
$container->get( 'blocks.settings.final_review_enabled' ),
$container->get( 'session.cancellation.view' ),
$container->get( 'session.handler' )
);
},
'blocks.settings.final_review_enabled' => static function ( ContainerInterface $container ): bool {
$settings = $container->get( 'wcgateway.settings' );
assert( $settings instanceof ContainerInterface );
return $settings->has( 'blocks_final_review_enabled' ) ?
(bool) $settings->get( 'blocks_final_review_enabled' ) :
true;
},
);

View file

@ -64,6 +64,13 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType {
*/
private $gateway;
/**
* Whether the final review is enabled.
*
* @var bool
*/
private $final_review_enabled;
/**
* The cancellation view.
*
@ -87,6 +94,7 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType {
* @param Settings $plugin_settings The settings.
* @param SettingsStatus $settings_status The Settings status helper.
* @param PayPalGateway $gateway The WC gateway.
* @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.
*/
@ -97,18 +105,20 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType {
Settings $plugin_settings,
SettingsStatus $settings_status,
PayPalGateway $gateway,
bool $final_review_enabled,
CancelView $cancellation_view,
SessionHandler $session_handler
) {
$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->cancellation_view = $cancellation_view;
$this->session_handler = $session_handler;
$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;
}
/**
@ -158,12 +168,13 @@ 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(),
'scriptData' => $script_data,
'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,
'scriptData' => $script_data,
);
}
}

View file

@ -19,12 +19,13 @@ import {setVisibleByClass, isVisible} from "../../../ppcp-button/resources/js/mo
const form = jQuery('#mainform');
const noReviewChk = document.querySelector('#ppcp-blocks_no_final_review');
if (noReviewChk) {
noReviewChk.addEventListener('click', (e) => {
if (!noReviewChk.checked) {
// TODO: maybe move to a separate JS file in blocks module, when we need more JS for block settings
const finalReviewCheckbox = document.querySelector('#ppcp-blocks_final_review_enabled');
if (finalReviewCheckbox) {
finalReviewCheckbox.addEventListener('click', () => {
if (!finalReviewCheckbox.checked) {
if (!window.confirm('Are you sure you want to disable the final review on the Checkout page for Block Express payments?')) {
noReviewChk.checked = true;
finalReviewCheckbox.checked = true;
}
}
});