Merge branch 'trunk' into PCP-2166-extend-apple-pay-google-pay-country-eligibility-to-italy

This commit is contained in:
Pedro Silva 2023-10-31 17:21:54 +00:00
commit ed128a17c3
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
24 changed files with 1023 additions and 416 deletions

View file

@ -23,18 +23,17 @@ class ApplepayButton {
this.ppcpConfig
);
//PRODUCT DETAIL PAGE
this.refreshContextData();
this.updated_contact_info = []
this.selectedShippingMethod = []
this.nonce = document.getElementById('woocommerce-process-checkout-nonce').value
this.nonce = document.getElementById('woocommerce-process-checkout-nonce')?.value
this.log = function() {
if ( this.buttonConfig.is_debug ) {
console.log('[ApplePayButton]', ...arguments);
}
}
this.refreshContextData();
}
init(config) {
@ -260,6 +259,8 @@ class ApplepayButton {
case 'product':
// Refresh product data that makes the price change.
this.productQuantity = document.querySelector('input.qty').value;
this.products = this.contextHandler.products();
this.log('Products updated', this.products);
break;
}
}
@ -389,6 +390,7 @@ class ApplepayButton {
return {
action: 'ppcp_update_shipping_contact',
product_id: product_id,
products: JSON.stringify(this.products),
caller_page: 'productDetail',
product_quantity: this.productQuantity,
simplified_contact: event.shippingContact,
@ -419,6 +421,7 @@ class ApplepayButton {
action: 'ppcp_update_shipping_method',
shipping_method: event.shippingMethod,
product_id: product_id,
products: JSON.stringify(this.products),
caller_page: 'productDetail',
product_quantity: this.productQuantity,
simplified_contact: this.updated_contact_info,
@ -456,6 +459,7 @@ class ApplepayButton {
action: 'ppcp_create_order',
'caller_page': this.context,
'product_id': this.buttonConfig.product.id ?? null,
'products': JSON.stringify(this.products),
'product_quantity': this.productQuantity ?? null,
'shipping_contact': shippingContact,
'billing_contact': billingContact,

View file

@ -49,12 +49,20 @@ class SingleProductHandler extends BaseHandler {
}
createOrder() {
return this.actionHandler().configuration().createOrder();
}
products() {
return this.actionHandler().getProducts();
}
actionHandler() {
const errorHandler = new ErrorHandler(
this.ppcpConfig.labels.error.generic,
document.querySelector('.woocommerce-notices-wrapper')
);
const actionHandler = new SingleProductActionHandler(
return new SingleProductActionHandler(
this.ppcpConfig,
new UpdateCart(
this.ppcpConfig.ajax.change_cart.endpoint,
@ -63,8 +71,6 @@ class SingleProductHandler extends BaseHandler {
document.querySelector('form.cart'),
errorHandler,
);
return actionHandler.configuration().createOrder();
}
}

View file

@ -100,7 +100,6 @@ return array(
return new DataToAppleButtonScripts( $container->get( 'applepay.sdk_script_url' ), $container->get( 'wcgateway.settings' ) );
},
'applepay.button' => static function ( ContainerInterface $container ): ApplePayButton {
return new ApplePayButton(
$container->get( 'wcgateway.settings' ),
$container->get( 'woocommerce.logger.woocommerce' ),
@ -108,7 +107,8 @@ return array(
$container->get( 'applepay.url' ),
$container->get( 'ppcp.asset-version' ),
$container->get( 'applepay.data_to_scripts' ),
$container->get( 'wcgateway.settings.status' )
$container->get( 'wcgateway.settings.status' ),
$container->get( 'button.helper.cart-products' )
);
},
'applepay.blocks-payment-method' => static function ( ContainerInterface $container ): PaymentMethodTypeInterface {

View file

@ -12,6 +12,7 @@ namespace WooCommerce\PayPalCommerce\Applepay;
use Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry;
use WooCommerce\PayPalCommerce\Applepay\Assets\ApplePayButton;
use WooCommerce\PayPalCommerce\Applepay\Assets\AppleProductStatus;
use WooCommerce\PayPalCommerce\Applepay\Assets\PropertiesDictionary;
use WooCommerce\PayPalCommerce\Button\Assets\ButtonInterface;
use WooCommerce\PayPalCommerce\Button\Assets\SmartButtonInterface;
use WooCommerce\PayPalCommerce\Applepay\Helper\AvailabilityNotice;
@ -40,6 +41,7 @@ class ApplepayModule implements ModuleInterface {
* {@inheritDoc}
*/
public function run( ContainerInterface $c ): void {
$module = $this;
// Clears product status when appropriate.
add_action(
@ -51,38 +53,65 @@ class ApplepayModule implements ModuleInterface {
}
);
// Check if the module is applicable, correct country, currency, ... etc.
if ( ! $c->get( 'applepay.eligible' ) ) {
return;
}
add_action(
'init',
static function () use ( $c, $module ) {
// Load the button handler.
$apple_payment_method = $c->get( 'applepay.button' );
// add onboarding and referrals hooks.
assert( $apple_payment_method instanceof ApplepayButton );
$apple_payment_method->initialize();
// Check if the module is applicable, correct country, currency, ... etc.
if ( ! $c->get( 'applepay.eligible' ) ) {
return;
}
// Show notice if there are product availability issues.
$availability_notice = $c->get( 'applepay.availability_notice' );
assert( $availability_notice instanceof AvailabilityNotice );
$availability_notice->execute();
// Load the button handler.
$apple_payment_method = $c->get( 'applepay.button' );
// add onboarding and referrals hooks.
assert( $apple_payment_method instanceof ApplepayButton );
$apple_payment_method->initialize();
// Return if server not supported.
if ( ! $c->get( 'applepay.server_supported' ) ) {
return;
}
// Show notice if there are product availability issues.
$availability_notice = $c->get( 'applepay.availability_notice' );
assert( $availability_notice instanceof AvailabilityNotice );
$availability_notice->execute();
// Check if this merchant can activate / use the buttons.
// We allow non referral merchants as they can potentially still use ApplePay, we just have no way of checking the capability.
if ( ( ! $c->get( 'applepay.available' ) ) && $c->get( 'applepay.is_referral' ) ) {
return;
}
// Return if server not supported.
if ( ! $c->get( 'applepay.server_supported' ) ) {
return;
}
$this->load_assets( $c, $apple_payment_method );
$this->handle_validation_file( $c );
$this->render_buttons( $c, $apple_payment_method );
// Check if this merchant can activate / use the buttons.
// We allow non referral merchants as they can potentially still use ApplePay, we just have no way of checking the capability.
if ( ( ! $c->get( 'applepay.available' ) ) && $c->get( 'applepay.is_referral' ) ) {
return;
}
$apple_payment_method->bootstrap_ajax_request();
$module->load_assets( $c, $apple_payment_method );
$module->handle_validation_file( $c );
$module->render_buttons( $c, $apple_payment_method );
$apple_payment_method->bootstrap_ajax_request();
}
);
add_filter(
'nonce_user_logged_out',
/**
* Prevents nonce from being changed for non logged in users.
*
* @param int $uid The uid.
* @param string|int $action The action.
* @return int
*
* @psalm-suppress MissingClosureParamType
*/
function ( $uid, $action ) {
if ( $action === PropertiesDictionary::NONCE_ACTION ) {
return 0;
}
return $uid;
},
100,
2
);
}
/**

View file

@ -14,6 +14,7 @@ use Psr\Log\LoggerInterface;
use WC_Cart;
use WC_Order;
use WooCommerce\PayPalCommerce\Button\Assets\ButtonInterface;
use WooCommerce\PayPalCommerce\Button\Helper\CartProductsHelper;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
use WooCommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus;
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderProcessor;
@ -25,18 +26,21 @@ use WooCommerce\PayPalCommerce\Webhooks\Handler\RequestHandlerTrait;
*/
class ApplePayButton implements ButtonInterface {
use RequestHandlerTrait;
/**
* The settings.
*
* @var Settings
*/
private $settings;
/**
* The logger.
*
* @var LoggerInterface
*/
private $logger;
/**
* The response templates.
*
@ -58,12 +62,14 @@ class ApplePayButton implements ButtonInterface {
* @var string
*/
protected $id;
/**
* The method title.
*
* @var string
*/
protected $method_title;
/**
* The processor for orders.
*
@ -84,18 +90,21 @@ class ApplePayButton implements ButtonInterface {
* @var string
*/
private $version;
/**
* The module URL.
*
* @var string
*/
private $module_url;
/**
* The data to send to the ApplePay button script.
*
* @var DataToAppleButtonScripts
*/
private $script_data;
/**
* The Settings status helper.
*
@ -103,6 +112,13 @@ class ApplePayButton implements ButtonInterface {
*/
private $settings_status;
/**
* The cart products helper.
*
* @var CartProductsHelper
*/
protected $cart_products;
/**
* PayPalPaymentMethod constructor.
*
@ -113,6 +129,7 @@ class ApplePayButton implements ButtonInterface {
* @param string $version The module version.
* @param DataToAppleButtonScripts $data The data to send to the ApplePay button script.
* @param SettingsStatus $settings_status The settings status helper.
* @param CartProductsHelper $cart_products The cart products helper.
*/
public function __construct(
Settings $settings,
@ -121,7 +138,8 @@ class ApplePayButton implements ButtonInterface {
string $module_url,
string $version,
DataToAppleButtonScripts $data,
SettingsStatus $settings_status
SettingsStatus $settings_status,
CartProductsHelper $cart_products
) {
$this->settings = $settings;
$this->response_templates = new ResponsesToApple();
@ -133,6 +151,7 @@ class ApplePayButton implements ButtonInterface {
$this->version = $version;
$this->script_data = $data;
$this->settings_status = $settings_status;
$this->cart_products = $cart_products;
}
/**
@ -822,15 +841,24 @@ class ApplePayButton implements ButtonInterface {
*
* @param ApplePayDataObjectHttp $applepay_request_data_object The request data object.
* @return bool | string The cart item key after adding to the new cart.
* @throws \Exception If cannot be added to cart.
* @throws \Exception If it cannot be added to cart.
*/
public function prepare_cart( ApplePayDataObjectHttp $applepay_request_data_object ): string {
$this->save_old_cart();
$cart = WC()->cart;
return $cart->add_to_cart(
(int) $applepay_request_data_object->product_id(),
(int) $applepay_request_data_object->product_quantity()
$this->cart_products->set_cart( WC()->cart );
$product = $this->cart_products->product_from_data(
array(
'id' => (int) $applepay_request_data_object->product_id(),
'quantity' => (int) $applepay_request_data_object->product_quantity(),
'variations' => $applepay_request_data_object->product_variations(),
'extra' => $applepay_request_data_object->product_extra(),
'booking' => $applepay_request_data_object->product_booking(),
)
);
$this->cart_products->add_products( array( $product ) );
return $this->cart_products->cart_item_keys()[0];
}
/**

View file

@ -38,13 +38,6 @@ class ApplePayDataObjectHttp {
*/
protected $need_shipping;
/**
* The product id.
*
* @var mixed
*/
protected $product_id = '';
/**
* The caller page.
*
@ -52,6 +45,13 @@ class ApplePayDataObjectHttp {
*/
protected $caller_page;
/**
* The product id.
*
* @var mixed
*/
protected $product_id = '';
/**
* The product quantity.
*
@ -59,6 +59,27 @@ class ApplePayDataObjectHttp {
*/
protected $product_quantity = '';
/**
* The product variations.
*
* @var array
*/
protected $product_variations = array();
/**
* The product extra.
*
* @var array
*/
protected $product_extra = array();
/**
* The product booking.
*
* @var array
*/
protected $product_booking = array();
/**
* The shipping methods.
*
@ -166,6 +187,9 @@ class ApplePayDataObjectHttp {
if ( ! $data ) {
return;
}
$data = $this->preprocess_request_data( $data );
$result = $this->update_required_data(
$data,
PropertiesDictionary::UPDATE_CONTACT_SINGLE_PROD_REQUIRED_FIELDS,
@ -198,6 +222,9 @@ class ApplePayDataObjectHttp {
if ( ! $data ) {
return;
}
$data = $this->preprocess_request_data( $data );
$result = $this->update_required_data(
$data,
PropertiesDictionary::UPDATE_METHOD_SINGLE_PROD_REQUIRED_FIELDS,
@ -226,6 +253,10 @@ class ApplePayDataObjectHttp {
if ( ! $data ) {
return;
}
$data = $this->append_products_to_data( $data, $_POST );
$data = $this->preprocess_request_data( $data );
$data[ PropertiesDictionary::CALLER_PAGE ] = $caller_page;
$result = $this->update_required_data(
$data,
@ -261,6 +292,27 @@ class ApplePayDataObjectHttp {
$this->update_shipping_method( $data );
}
/**
* Pre-processes request data to transform it to a standard format.
*
* @param array $data The data.
* @return array
*/
protected function preprocess_request_data( array $data ): array {
// Fill product variables if a products object is received.
if ( is_array( $data[ PropertiesDictionary::PRODUCTS ] ?? null ) ) {
$product = $data[ PropertiesDictionary::PRODUCTS ][0];
$data[ PropertiesDictionary::PRODUCT_ID ] = $product['id'] ?? 0;
$data[ PropertiesDictionary::PRODUCT_QUANTITY ] = $product['quantity'] ?? array();
$data[ PropertiesDictionary::PRODUCT_VARIATIONS ] = $product['variations'] ?? array();
$data[ PropertiesDictionary::PRODUCT_EXTRA ] = $product['extra'] ?? array();
$data[ PropertiesDictionary::PRODUCT_BOOKING ] = $product['booking'] ?? array();
}
unset( $data[ PropertiesDictionary::PRODUCTS ] );
return $data;
}
/**
* Checks if the array contains all required fields and if those
* are not empty.
@ -300,7 +352,7 @@ class ApplePayDataObjectHttp {
*/
protected function assign_data_object_values( array $data ): void {
foreach ( $data as $key => $value ) {
// Null values may give origin to type errors. If necessary replace condition this with a specialized field filter.
// Null values may give origin to type errors. If necessary replace this condition with a specialized field filter.
if ( null === $value ) {
continue;
}
@ -547,6 +599,33 @@ class ApplePayDataObjectHttp {
return $this->product_quantity;
}
/**
* Returns the product variations.
*
* @return array
*/
public function product_variations(): array {
return $this->product_variations;
}
/**
* Returns the product extra.
*
* @return array
*/
public function product_extra(): array {
return $this->product_extra;
}
/**
* Returns the product booking.
*
* @return array
*/
public function product_booking(): array {
return $this->product_booking;
}
/**
* Returns the nonce.
*
@ -580,7 +659,7 @@ class ApplePayDataObjectHttp {
* @return array|false|null
*/
public function get_filtered_request_data() {
return filter_input_array(
$data = filter_input_array(
INPUT_POST,
array(
PropertiesDictionary::CALLER_PAGE => FILTER_SANITIZE_SPECIAL_CHARS,
@ -604,8 +683,43 @@ class ApplePayDataObjectHttp {
),
PropertiesDictionary::PRODUCT_ID => FILTER_SANITIZE_NUMBER_INT,
PropertiesDictionary::PRODUCT_QUANTITY => FILTER_SANITIZE_NUMBER_INT,
PropertiesDictionary::PRODUCT_VARIATIONS => array(
'filter' => FILTER_SANITIZE_SPECIAL_CHARS,
'flags' => FILTER_REQUIRE_ARRAY,
),
PropertiesDictionary::PRODUCT_EXTRA => array(
'filter' => FILTER_SANITIZE_SPECIAL_CHARS,
'flags' => FILTER_REQUIRE_ARRAY,
),
PropertiesDictionary::PRODUCT_BOOKING => array(
'filter' => FILTER_SANITIZE_SPECIAL_CHARS,
'flags' => FILTER_REQUIRE_ARRAY,
),
)
);
if ( ! $data ) {
return false;
}
return $this->append_products_to_data( $data, $_POST );
}
/**
* Appends product to a data array.
*
* @param array $data The data.
* @param array $request_data The request data.
* @return array
*/
public function append_products_to_data( array $data, array $request_data ): array {
$products = json_decode( wp_unslash( $request_data[ PropertiesDictionary::PRODUCTS ] ?? '' ), true );
if ( $products ) {
$data[ PropertiesDictionary::PRODUCTS ] = $products;
}
return $data;
}
/**

View file

@ -169,37 +169,32 @@ class DataToAppleButtonScripts {
if ( ! $cart ) {
return array();
}
$nonce = wp_nonce_field( 'woocommerce-process_checkout', 'woocommerce-process-checkout-nonce' );
$button_markup =
'<div id="applepay-container">'
. $nonce
. '</div>';
$type = $this->settings->has( 'applepay_button_type' ) ? $this->settings->get( 'applepay_button_type' ) : '';
$color = $this->settings->has( 'applepay_button_color' ) ? $this->settings->get( 'applepay_button_color' ) : '';
$lang = $this->settings->has( 'applepay_button_language' ) ? $this->settings->get( 'applepay_button_language' ) : '';
$lang = apply_filters( 'woocommerce_paypal_payments_applepay_button_language', $lang );
$type = $this->settings->has( 'applepay_button_type' ) ? $this->settings->get( 'applepay_button_type' ) : '';
$color = $this->settings->has( 'applepay_button_color' ) ? $this->settings->get( 'applepay_button_color' ) : '';
$lang = $this->settings->has( 'applepay_button_language' ) ? $this->settings->get( 'applepay_button_language' ) : '';
$lang = apply_filters( 'woocommerce_paypal_payments_applepay_button_language', $lang );
return array(
'sdk_url' => $this->sdk_url,
'is_debug' => defined( 'WP_DEBUG' ) && WP_DEBUG ? true : false,
'button' => array(
'sdk_url' => $this->sdk_url,
'is_debug' => defined( 'WP_DEBUG' ) && WP_DEBUG ? true : false,
'button' => array(
'wrapper' => 'applepay-container',
'mini_cart_wrapper' => 'applepay-container-minicart',
'type' => $type,
'color' => $color,
'lang' => $lang,
),
'product' => array(
'product' => array(
'needShipping' => $cart->needs_shipping(),
'subtotal' => $cart->get_subtotal(),
),
'shop' => array(
'shop' => array(
'countryCode' => $shop_country_code,
'currencyCode' => $currency_code,
'totalLabel' => $total_label,
),
'ajax_url' => admin_url( 'admin-ajax.php' ),
'buttonMarkup' => $button_markup,
'ajax_url' => admin_url( 'admin-ajax.php' ),
);
}
}

View file

@ -14,7 +14,6 @@ namespace WooCommerce\PayPalCommerce\Applepay\Assets;
*/
class PropertiesDictionary {
public const BILLING_CONTACT_INVALID = 'billing Contact Invalid';
public const CREATE_ORDER_SINGLE_PROD_REQUIRED_FIELDS =
@ -62,19 +61,22 @@ class PropertiesDictionary {
self::SIMPLIFIED_CONTACT,
);
public const PRODUCT_ID = 'product_id';
public const SIMPLIFIED_CONTACT = 'simplified_contact';
public const SHIPPING_METHOD = 'shipping_method';
public const SHIPPING_CONTACT = 'shipping_contact';
public const PRODUCTS = 'products';
public const PRODUCT_ID = 'product_id';
public const PRODUCT_QUANTITY = 'product_quantity';
public const PRODUCT_VARIATIONS = 'product_variations';
public const PRODUCT_EXTRA = 'product_extra';
public const PRODUCT_BOOKING = 'product_booking';
public const SIMPLIFIED_CONTACT = 'simplified_contact';
public const SHIPPING_METHOD = 'shipping_method';
public const SHIPPING_CONTACT = 'shipping_contact';
public const SHIPPING_CONTACT_INVALID = 'shipping Contact Invalid';
public const BILLING_CONTACT = 'billing_contact';
public const NONCE = 'nonce';
public const WCNONCE = 'woocommerce-process-checkout-nonce';
public const NONCE = 'nonce';
public const NONCE_ACTION = 'woocommerce-process_checkout';
public const WCNONCE = 'woocommerce-process-checkout-nonce';
public const CREATE_ORDER_CART_REQUIRED_FIELDS =
array(
@ -83,25 +85,16 @@ class PropertiesDictionary {
self::SHIPPING_CONTACT,
);
public const PRODUCT_QUANTITY = 'product_quantity';
public const CALLER_PAGE = 'caller_page';
public const BILLING_CONTACT = 'billing_contact';
public const NEED_SHIPPING = 'need_shipping';
public const UPDATE_SHIPPING_CONTACT = 'ppcp_update_shipping_contact';
public const UPDATE_SHIPPING_METHOD = 'ppcp_update_shipping_method';
public const CREATE_ORDER = 'ppcp_create_order';
public const CREATE_ORDER_CART = 'ppcp_create_order_cart';
public const REDIRECT = 'ppcp_redirect';
public const VALIDATE = 'ppcp_validate';
public const UPDATE_SHIPPING_METHOD = 'ppcp_update_shipping_method';
public const CREATE_ORDER = 'ppcp_create_order';
public const CREATE_ORDER_CART = 'ppcp_create_order_cart';
public const REDIRECT = 'ppcp_redirect';
public const VALIDATE = 'ppcp_validate';
/**
* Returns the possible list of button colors.