Merge branch 'trunk' into PCP-1393-acdc-vault-v3

This commit is contained in:
Emili Castells Guasch 2023-11-30 14:40:04 +01:00
commit cb7612851f
5 changed files with 85 additions and 46 deletions

View file

@ -76,8 +76,8 @@ export const paypalShippingToWc = (shipping) => {
* @returns {Object} * @returns {Object}
*/ */
export const paypalPayerToWc = (payer) => { export const paypalPayerToWc = (payer) => {
const firstName = payer.name.given_name; const firstName = payer?.name?.given_name ?? '';
const lastName = payer.name.surname; const lastName = payer?.name?.surname ?? '';
const address = payer.address ? paypalAddressToWc(payer.address) : {}; const address = payer.address ? paypalAddressToWc(payer.address) : {};
return { return {
...address, ...address,
@ -100,10 +100,12 @@ export const paypalOrderToWcShippingAddress = (order) => {
const res = paypalShippingToWc(shipping); const res = paypalShippingToWc(shipping);
// use the name from billing if the same, to avoid possible mistakes when splitting full_name // use the name from billing if the same, to avoid possible mistakes when splitting full_name
const billingAddress = paypalPayerToWc(order.payer); if (order.payer) {
if (`${res.first_name} ${res.last_name}` === `${billingAddress.first_name} ${billingAddress.last_name}`) { const billingAddress = paypalPayerToWc(order.payer);
res.first_name = billingAddress.first_name; if (`${res.first_name} ${res.last_name}` === `${billingAddress.first_name} ${billingAddress.last_name}`) {
res.last_name = billingAddress.last_name; res.first_name = billingAddress.first_name;
res.last_name = billingAddress.last_name;
}
} }
return res; return res;
@ -116,10 +118,13 @@ export const paypalOrderToWcShippingAddress = (order) => {
*/ */
export const paypalOrderToWcAddresses = (order) => { export const paypalOrderToWcAddresses = (order) => {
const shippingAddress = paypalOrderToWcShippingAddress(order); const shippingAddress = paypalOrderToWcShippingAddress(order);
let billingAddress = paypalPayerToWc(order.payer); let billingAddress = shippingAddress;
// no billing address, such as if billing address retrieval is not allowed in the merchant account if (order.payer) {
if (!billingAddress.address_line_1) { billingAddress = paypalPayerToWc(order.payer);
billingAddress = {...shippingAddress, ...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}; return {billingAddress, shippingAddress};

View file

@ -39,12 +39,17 @@ const PayPalComponent = ({
if (!config.scriptData.continuation || !config.scriptData.continuation.order || window.ppcpContinuationFilled) { if (!config.scriptData.continuation || !config.scriptData.continuation.order || window.ppcpContinuationFilled) {
return; return;
} }
const paypalAddresses = paypalOrderToWcAddresses(config.scriptData.continuation.order); try {
const wcAddresses = wp.data.select('wc/store/cart').getCustomerData(); const paypalAddresses = paypalOrderToWcAddresses(config.scriptData.continuation.order);
const addresses = mergeWcAddress(wcAddresses, paypalAddresses); const wcAddresses = wp.data.select('wc/store/cart').getCustomerData();
wp.data.dispatch('wc/store/cart').setBillingAddress(addresses.billingAddress); const addresses = mergeWcAddress(wcAddresses, paypalAddresses);
if (shippingData.needsShipping) { wp.data.dispatch('wc/store/cart').setBillingAddress(addresses.billingAddress);
wp.data.dispatch('wc/store/cart').setShippingAddress(addresses.shippingAddress); if (shippingData.needsShipping) {
wp.data.dispatch('wc/store/cart').setShippingAddress(addresses.shippingAddress);
}
} catch (err) {
// sometimes the PayPal address is missing, skip in this case.
console.log(err);
} }
// this useEffect should run only once, but adding this in case of some kind of full re-rendering // this useEffect should run only once, but adding this in case of some kind of full re-rendering
window.ppcpContinuationFilled = true; window.ppcpContinuationFilled = true;
@ -321,11 +326,19 @@ const PayPalComponent = ({
const features = ['products']; const features = ['products'];
if ((config.addPlaceOrderMethod || config.usePlaceOrder) && !config.scriptData.continuation) { if ((config.addPlaceOrderMethod || config.usePlaceOrder) && !config.scriptData.continuation) {
let descriptionElement = <div dangerouslySetInnerHTML={{__html: config.description}}></div>;
if (config.placeOrderButtonDescription) {
descriptionElement = <div>
<p dangerouslySetInnerHTML={{__html: config.description}}></p>
<p style={{textAlign: 'center'}} className={'ppcp-place-order-description'} dangerouslySetInnerHTML={{__html: config.placeOrderButtonDescription}}></p>
</div>;
}
registerPaymentMethod({ registerPaymentMethod({
name: config.id, name: config.id,
label: <div dangerouslySetInnerHTML={{__html: config.title}}/>, label: <div dangerouslySetInnerHTML={{__html: config.title}}/>,
content: <div dangerouslySetInnerHTML={{__html: config.description}}/>, content: descriptionElement,
edit: <div dangerouslySetInnerHTML={{__html: config.description}}/>, edit: descriptionElement,
placeOrderButtonLabel: config.placeOrderButtonText, placeOrderButtonLabel: config.placeOrderButtonText,
ariaLabel: config.title, ariaLabel: config.title,
canMakePayment: () => config.enabled, canMakePayment: () => config.enabled,

View file

@ -38,6 +38,7 @@ return array(
$container->get( 'blocks.add-place-order-method' ), $container->get( 'blocks.add-place-order-method' ),
$container->get( 'wcgateway.use-place-order-button' ), $container->get( 'wcgateway.use-place-order-button' ),
$container->get( 'wcgateway.place-order-button-text' ), $container->get( 'wcgateway.place-order-button-text' ),
$container->get( 'wcgateway.place-order-button-description' ),
$container->get( 'wcgateway.all-funding-sources' ) $container->get( 'wcgateway.all-funding-sources' )
); );
}, },

View file

@ -108,6 +108,13 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType {
*/ */
protected $place_order_button_text; protected $place_order_button_text;
/**
* The text for additional "Place order" description.
*
* @var string
*/
protected $place_order_button_description;
/** /**
* All existing funding sources for PayPal buttons. * All existing funding sources for PayPal buttons.
* *
@ -130,6 +137,7 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType {
* @param bool $add_place_order_method Whether to create a non-express method with the standard "Place order" button. * @param bool $add_place_order_method Whether to create a non-express method with the standard "Place order" button.
* @param bool $use_place_order Whether to use the standard "Place order" button instead of PayPal buttons. * @param bool $use_place_order Whether to use the standard "Place order" button instead of PayPal buttons.
* @param string $place_order_button_text The text for the standard "Place order" button. * @param string $place_order_button_text The text for the standard "Place order" button.
* @param string $place_order_button_description The text for additional "Place order" description.
* @param array $all_funding_sources All existing funding sources for PayPal buttons. * @param array $all_funding_sources All existing funding sources for PayPal buttons.
*/ */
public function __construct( public function __construct(
@ -145,22 +153,24 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType {
bool $add_place_order_method, bool $add_place_order_method,
bool $use_place_order, bool $use_place_order,
string $place_order_button_text, string $place_order_button_text,
string $place_order_button_description,
array $all_funding_sources array $all_funding_sources
) { ) {
$this->name = PayPalGateway::ID; $this->name = PayPalGateway::ID;
$this->module_url = $module_url; $this->module_url = $module_url;
$this->version = $version; $this->version = $version;
$this->smart_button = $smart_button; $this->smart_button = $smart_button;
$this->plugin_settings = $plugin_settings; $this->plugin_settings = $plugin_settings;
$this->settings_status = $settings_status; $this->settings_status = $settings_status;
$this->gateway = $gateway; $this->gateway = $gateway;
$this->final_review_enabled = $final_review_enabled; $this->final_review_enabled = $final_review_enabled;
$this->cancellation_view = $cancellation_view; $this->cancellation_view = $cancellation_view;
$this->session_handler = $session_handler; $this->session_handler = $session_handler;
$this->add_place_order_method = $add_place_order_method; $this->add_place_order_method = $add_place_order_method;
$this->use_place_order = $use_place_order; $this->use_place_order = $use_place_order;
$this->place_order_button_text = $place_order_button_text; $this->place_order_button_text = $place_order_button_text;
$this->all_funding_sources = $all_funding_sources; $this->place_order_button_description = $place_order_button_description;
$this->all_funding_sources = $all_funding_sources;
} }
/** /**
@ -214,7 +224,7 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType {
} }
} }
$disabled_funding_sources = explode( ',', $script_data['url_params']['disable-funding'] ) ?: array(); $disabled_funding_sources = explode( ',', $script_data['url_params']['disable-funding'] ?? '' ) ?: array();
$funding_sources = array_values( $funding_sources = array_values(
array_diff( array_diff(
array_keys( $this->all_funding_sources ), array_keys( $this->all_funding_sources ),
@ -223,23 +233,24 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType {
); );
return array( return array(
'id' => $this->gateway->id, 'id' => $this->gateway->id,
'title' => $this->gateway->title, 'title' => $this->gateway->title,
'description' => $this->gateway->description, 'description' => $this->gateway->description,
'enabled' => $this->settings_status->is_smart_button_enabled_for_location( $script_data['context'] ), 'enabled' => $this->settings_status->is_smart_button_enabled_for_location( $script_data['context'] ),
'fundingSource' => $this->session_handler->funding_source(), 'fundingSource' => $this->session_handler->funding_source(),
'finalReviewEnabled' => $this->final_review_enabled, 'finalReviewEnabled' => $this->final_review_enabled,
'addPlaceOrderMethod' => $this->add_place_order_method, 'addPlaceOrderMethod' => $this->add_place_order_method,
'usePlaceOrder' => $this->use_place_order, 'usePlaceOrder' => $this->use_place_order,
'placeOrderButtonText' => $this->place_order_button_text, 'placeOrderButtonText' => $this->place_order_button_text,
'enabledFundingSources' => $funding_sources, 'placeOrderButtonDescription' => $this->place_order_button_description,
'ajax' => array( 'enabledFundingSources' => $funding_sources,
'ajax' => array(
'update_shipping' => array( 'update_shipping' => array(
'endpoint' => WC_AJAX::get_endpoint( UpdateShippingEndpoint::ENDPOINT ), 'endpoint' => WC_AJAX::get_endpoint( UpdateShippingEndpoint::ENDPOINT ),
'nonce' => wp_create_nonce( UpdateShippingEndpoint::nonce() ), 'nonce' => wp_create_nonce( UpdateShippingEndpoint::nonce() ),
), ),
), ),
'scriptData' => $script_data, 'scriptData' => $script_data,
); );
} }
} }

View file

@ -1180,7 +1180,16 @@ return array(
*/ */
return apply_filters( return apply_filters(
'woocommerce_paypal_payments_place_order_button_text', 'woocommerce_paypal_payments_place_order_button_text',
__( 'Pay with PayPal', 'woocommerce-paypal-payments' ) __( 'Proceed to PayPal', 'woocommerce-paypal-payments' )
);
},
'wcgateway.place-order-button-description' => function ( ContainerInterface $container ) : string {
/**
* The text for additional description, when the "Place order" button mode is enabled.
*/
return apply_filters(
'woocommerce_paypal_payments_place_order_button_description',
__( 'Clicking "Proceed to PayPal" will redirect you to PayPal to complete your purchase.', 'woocommerce-paypal-payments' )
); );
}, },