Merge pull request #1435 from woocommerce/PCP-860-apm

Improve handling of APMs when popup is not used or not redirected back to WC
This commit is contained in:
Emili Castells 2023-07-04 16:22:39 +02:00 committed by GitHub
commit 1d75e73b56
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
45 changed files with 827 additions and 617 deletions

View file

@ -28,6 +28,8 @@ const cardsSpinner = new Spinner('#ppcp-hosted-fields');
const bootstrap = () => {
const checkoutFormSelector = 'form.woocommerce-checkout';
const context = PayPalCommerceGateway.context;
const errorHandler = new ErrorHandler(
PayPalCommerceGateway.labels.error.generic,
document.querySelector(checkoutFormSelector) ?? document.querySelector('.woocommerce-notices-wrapper')
@ -58,7 +60,7 @@ const bootstrap = () => {
}
});
const onSmartButtonClick = (data, actions) => {
const onSmartButtonClick = async (data, actions) => {
window.ppcpFundingSource = data.fundingSource;
const requiredFields = jQuery('form.woocommerce-checkout .validate-required:visible :input');
requiredFields.each((i, input) => {
@ -120,6 +122,14 @@ const bootstrap = () => {
freeTrialHandler.handle();
return actions.reject();
}
if (context === 'checkout' && !PayPalCommerceGateway.funding_sources_without_redirect.includes(data.fundingSource)) {
try {
await formSaver.save(form);
} catch (error) {
console.error(error);
}
}
};
let smartButtonsOptions = {
@ -138,7 +148,6 @@ const bootstrap = () => {
};
const renderer = new Renderer(creditCardRenderer, PayPalCommerceGateway, onSmartButtonClick, onSmartButtonsInit, smartButtonsOptions);
const messageRenderer = new MessageRenderer(PayPalCommerceGateway.messages);
const context = PayPalCommerceGateway.context;
if (context === 'mini-cart' || context === 'product') {
if (PayPalCommerceGateway.mini_cart_buttons_enabled === '1') {
const miniCartBootstrap = new MiniCartBootstap(

View file

@ -67,23 +67,6 @@ return array(
return $dummy_ids[ $shop_country ] ?? $container->get( 'button.client_id' );
},
'button.is_paypal_continuation' => static function( ContainerInterface $container ): bool {
$session_handler = $container->get( 'session.handler' );
$order = $session_handler->order();
if ( ! $order ) {
return false;
}
$source = $order->payment_source();
if ( $source && $source->card() ) {
return false; // Ignore for DCC.
}
if ( 'card' === $session_handler->funding_source() ) {
return false; // Ignore for card buttons.
}
return true;
},
'button.smart-button' => static function ( ContainerInterface $container ): SmartButtonInterface {
$state = $container->get( 'onboarding.state' );
if ( $state->current_state() !== State::STATE_ONBOARDED ) {
@ -125,6 +108,7 @@ return array(
$container->get( 'button.basic-checkout-validation-enabled' ),
$container->get( 'button.early-wc-checkout-validation-enabled' ),
$container->get( 'button.pay-now-contexts' ),
$container->get( 'wcgateway.funding-sources-without-redirect' ),
$container->get( 'woocommerce.logger.woocommerce' )
);
},
@ -176,6 +160,7 @@ return array(
$container->get( 'button.early-wc-checkout-validation-enabled' ),
$container->get( 'button.pay-now-contexts' ),
$container->get( 'button.handle-shipping-in-paypal' ),
$container->get( 'wcgateway.funding-sources-without-redirect' ),
$logger
);
},
@ -184,8 +169,7 @@ return array(
$state = $container->get( 'onboarding.state' );
$order_processor = $container->get( 'wcgateway.order-processor' );
$session_handler = $container->get( 'session.handler' );
$prefix = $container->get( 'api.prefix' );
return new EarlyOrderHandler( $state, $order_processor, $session_handler, $prefix );
return new EarlyOrderHandler( $state, $order_processor, $session_handler );
},
'button.endpoint.approve-order' => static function ( ContainerInterface $container ): ApproveOrderEndpoint {
$request_data = $container->get( 'button.request-data' );
@ -215,7 +199,9 @@ return array(
);
},
'button.checkout-form-saver' => static function ( ContainerInterface $container ): CheckoutFormSaver {
return new CheckoutFormSaver();
return new CheckoutFormSaver(
$container->get( 'session.handler' )
);
},
'button.endpoint.save-checkout-form' => static function ( ContainerInterface $container ): SaveCheckoutFormEndpoint {
return new SaveCheckoutFormEndpoint(

View file

@ -174,6 +174,13 @@ class SmartButton implements SmartButtonInterface {
*/
private $pay_now_contexts;
/**
* The sources that do not cause issues about redirecting (on mobile, ...) and sometimes not returning back.
*
* @var string[]
*/
private $funding_sources_without_redirect;
/**
* The logger.
*
@ -209,6 +216,7 @@ class SmartButton implements SmartButtonInterface {
* @param bool $basic_checkout_validation_enabled Whether the basic JS validation of the form iss enabled.
* @param bool $early_validation_enabled Whether to execute WC validation of the checkout form.
* @param array $pay_now_contexts The contexts that should have the Pay Now button.
* @param string[] $funding_sources_without_redirect The sources that do not cause issues about redirecting (on mobile, ...) and sometimes not returning back.
* @param LoggerInterface $logger The logger.
*/
public function __construct(
@ -230,6 +238,7 @@ class SmartButton implements SmartButtonInterface {
bool $basic_checkout_validation_enabled,
bool $early_validation_enabled,
array $pay_now_contexts,
array $funding_sources_without_redirect,
LoggerInterface $logger
) {
@ -251,6 +260,7 @@ class SmartButton implements SmartButtonInterface {
$this->basic_checkout_validation_enabled = $basic_checkout_validation_enabled;
$this->early_validation_enabled = $early_validation_enabled;
$this->pay_now_contexts = $pay_now_contexts;
$this->funding_sources_without_redirect = $funding_sources_without_redirect;
$this->logger = $logger;
}
@ -942,6 +952,7 @@ class SmartButton implements SmartButtonInterface {
'mini_cart_buttons_enabled' => $this->settings_status->is_smart_button_enabled_for_location( 'mini-cart' ),
'basic_checkout_validation_enabled' => $this->basic_checkout_validation_enabled,
'early_checkout_validation_enabled' => $this->early_validation_enabled,
'funding_sources_without_redirect' => $this->funding_sources_without_redirect,
);
if ( $this->style_for_context( 'layout', 'mini-cart' ) !== 'horizontal' ) {
@ -1003,7 +1014,7 @@ class SmartButton implements SmartButtonInterface {
);
if (
$this->environment->current_environment_is( Environment::SANDBOX )
&& defined( 'WP_DEBUG' ) && \WP_DEBUG && is_user_logged_in()
&& defined( 'WP_DEBUG' ) && \WP_DEBUG
&& WC()->customer instanceof \WC_Customer && WC()->customer->get_billing_country()
&& 2 === strlen( WC()->customer->get_billing_country() )
) {

View file

@ -152,6 +152,13 @@ class CreateOrderEndpoint implements EndpointInterface {
*/
private $handle_shipping_in_paypal;
/**
* The sources that do not cause issues about redirecting (on mobile, ...) and sometimes not returning back.
*
* @var string[]
*/
private $funding_sources_without_redirect;
/**
* The logger.
*
@ -175,6 +182,7 @@ class CreateOrderEndpoint implements EndpointInterface {
* @param bool $early_validation_enabled Whether to execute WC validation of the checkout form.
* @param string[] $pay_now_contexts The contexts that should have the Pay Now button.
* @param bool $handle_shipping_in_paypal If true, the shipping methods are sent to PayPal allowing the customer to select it inside the popup.
* @param string[] $funding_sources_without_redirect The sources that do not cause issues about redirecting (on mobile, ...) and sometimes not returning back.
* @param LoggerInterface $logger The logger.
*/
public function __construct(
@ -191,23 +199,25 @@ class CreateOrderEndpoint implements EndpointInterface {
bool $early_validation_enabled,
array $pay_now_contexts,
bool $handle_shipping_in_paypal,
array $funding_sources_without_redirect,
LoggerInterface $logger
) {
$this->request_data = $request_data;
$this->purchase_unit_factory = $purchase_unit_factory;
$this->shipping_preference_factory = $shipping_preference_factory;
$this->api_endpoint = $order_endpoint;
$this->payer_factory = $payer_factory;
$this->session_handler = $session_handler;
$this->settings = $settings;
$this->early_order_handler = $early_order_handler;
$this->registration_needed = $registration_needed;
$this->card_billing_data_mode = $card_billing_data_mode;
$this->early_validation_enabled = $early_validation_enabled;
$this->pay_now_contexts = $pay_now_contexts;
$this->handle_shipping_in_paypal = $handle_shipping_in_paypal;
$this->logger = $logger;
$this->request_data = $request_data;
$this->purchase_unit_factory = $purchase_unit_factory;
$this->shipping_preference_factory = $shipping_preference_factory;
$this->api_endpoint = $order_endpoint;
$this->payer_factory = $payer_factory;
$this->session_handler = $session_handler;
$this->settings = $settings;
$this->early_order_handler = $early_order_handler;
$this->registration_needed = $registration_needed;
$this->card_billing_data_mode = $card_billing_data_mode;
$this->early_validation_enabled = $early_validation_enabled;
$this->pay_now_contexts = $pay_now_contexts;
$this->handle_shipping_in_paypal = $handle_shipping_in_paypal;
$this->funding_sources_without_redirect = $funding_sources_without_redirect;
$this->logger = $logger;
}
/**
@ -288,6 +298,11 @@ class CreateOrderEndpoint implements EndpointInterface {
}
if ( 'checkout' === $data['context'] ) {
if ( $payment_method === PayPalGateway::ID && ! in_array( $funding_source, $this->funding_sources_without_redirect, true ) ) {
$this->session_handler->replace_order( $order );
$this->session_handler->replace_funding_source( $funding_source );
}
if (
! $this->early_order_handler->should_create_early_order()
|| $this->registration_needed

View file

@ -10,11 +10,30 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Button\Helper;
use WC_Checkout;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
/**
* Class CheckoutFormSaver
*/
class CheckoutFormSaver extends WC_Checkout {
/**
* The Session handler.
*
* @var SessionHandler
*/
private $session_handler;
/**
* CheckoutFormSaver constructor.
*
* @param SessionHandler $session_handler The session handler.
*/
public function __construct(
SessionHandler $session_handler
) {
$this->session_handler = $session_handler;
}
/**
* Saves the form data to the WC customer and session.
*
@ -28,5 +47,7 @@ class CheckoutFormSaver extends WC_Checkout {
$data = $this->get_posted_data();
$this->update_session( $data );
$this->session_handler->replace_checkout_form( $data );
}
}

View file

@ -9,6 +9,8 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Button\Helper;
use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus;
trait ContextTrait {
/**
@ -63,6 +65,12 @@ trait ContextTrait {
return false;
}
if ( ! $order->status()->is( OrderStatus::APPROVED )
&& ! $order->status()->is( OrderStatus::COMPLETED )
) {
return false;
}
$source = $order->payment_source();
if ( $source && $source->card() ) {
return false; // Ignore for DCC.

View file

@ -15,15 +15,12 @@ use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderProcessor;
use WooCommerce\PayPalCommerce\Webhooks\Handler\PrefixTrait;
/**
* Class EarlyOrderHandler
*/
class EarlyOrderHandler {
use PrefixTrait;
/**
* The State.
*
@ -51,19 +48,16 @@ class EarlyOrderHandler {
* @param State $state The State.
* @param OrderProcessor $order_processor The Order Processor.
* @param SessionHandler $session_handler The Session Handler.
* @param string $prefix The Prefix.
*/
public function __construct(
State $state,
OrderProcessor $order_processor,
SessionHandler $session_handler,
string $prefix
SessionHandler $session_handler
) {
$this->state = $state;
$this->order_processor = $order_processor;
$this->session_handler = $session_handler;
$this->prefix = $prefix;
}
/**
@ -101,7 +95,7 @@ class EarlyOrderHandler {
$order_id = false;
foreach ( $order->purchase_units() as $purchase_unit ) {
if ( $purchase_unit->custom_id() === sanitize_text_field( wp_unslash( $_REQUEST['ppcp-resume-order'] ) ) ) {
$order_id = (int) $this->sanitize_custom_id( $purchase_unit->custom_id() );
$order_id = (int) $purchase_unit->custom_id();
}
}
if ( $order_id === $resume_order_id ) {