🎨 Minor code improvement

This commit is contained in:
Philipp Stracker 2024-10-21 16:44:59 +02:00
parent 7916d20312
commit 8169a6a70b
No known key found for this signature in database

View file

@ -85,6 +85,13 @@ class ApplePayButton extends PaymentButton {
*/ */
#applePayConfig = null; #applePayConfig = null;
/**
* Details about the product (relevant on product page)
*
* @type {{quantity: ?number, items: []}}
*/
#product = {};
/** /**
* @inheritDoc * @inheritDoc
*/ */
@ -130,6 +137,11 @@ class ApplePayButton extends PaymentButton {
this.onPaymentAuthorized = this.onPaymentAuthorized.bind( this ); this.onPaymentAuthorized = this.onPaymentAuthorized.bind( this );
this.onButtonClick = this.onButtonClick.bind( this ); this.onButtonClick = this.onButtonClick.bind( this );
this.#product = {
quantity: null,
items: [],
};
this.log( 'Create instance' ); this.log( 'Create instance' );
} }
@ -147,7 +159,7 @@ class ApplePayButton extends PaymentButton {
return ( return (
PaymentContext.Checkout !== this.context || PaymentContext.Checkout !== this.context ||
this.shouldUpdateButtonWithFormData() this.shouldUpdateButtonWithFormData
); );
} }
@ -413,7 +425,7 @@ class ApplePayButton extends PaymentButton {
* *
* @return {boolean} True, when Apple Pay data should be submitted to WooCommerce. * @return {boolean} True, when Apple Pay data should be submitted to WooCommerce.
*/ */
shouldUpdateButtonWithFormData() { get shouldUpdateButtonWithFormData() {
if ( PaymentContext.Checkout !== this.context ) { if ( PaymentContext.Checkout !== this.context ) {
return false; return false;
} }
@ -429,7 +441,7 @@ class ApplePayButton extends PaymentButton {
* *
* @return {boolean} True, when the Apple Pay data should be submitted to WooCommerce. * @return {boolean} True, when the Apple Pay data should be submitted to WooCommerce.
*/ */
shouldCompletePaymentWithContextHandler() { get shouldCompletePaymentWithContextHandler() {
// Data already handled, ex: PayNow // Data already handled, ex: PayNow
if ( ! this.contextHandler.shippingAllowed() ) { if ( ! this.contextHandler.shippingAllowed() ) {
return true; return true;
@ -438,7 +450,7 @@ class ApplePayButton extends PaymentButton {
// Use WC form data mode in Checkout. // Use WC form data mode in Checkout.
return ( return (
PaymentContext.Checkout === this.context && PaymentContext.Checkout === this.context &&
! this.shouldUpdateButtonWithFormData() ! this.shouldUpdateButtonWithFormData
); );
} }
@ -448,7 +460,7 @@ class ApplePayButton extends PaymentButton {
* @param {Object} paymentRequest Object to extend with form data. * @param {Object} paymentRequest Object to extend with form data.
*/ */
updateRequestDataWithForm( paymentRequest ) { updateRequestDataWithForm( paymentRequest ) {
if ( ! this.shouldUpdateButtonWithFormData() ) { if ( ! this.shouldUpdateButtonWithFormData ) {
return; return;
} }
@ -521,16 +533,16 @@ class ApplePayButton extends PaymentButton {
'email', 'email',
'phone', 'phone',
], ],
requiredBillingContactFields: [ 'postalAddress' ], // ApplePay does not implement billing // ApplePay does not implement billing email and phone fields.
// email and phone fields. requiredBillingContactFields: [ 'postalAddress' ],
}; };
if ( ! this.requiresShipping ) { if ( ! this.requiresShipping ) {
if ( this.shouldCompletePaymentWithContextHandler() ) { if ( this.shouldCompletePaymentWithContextHandler ) {
// Data needs handled externally. // Data is handled externally.
baseRequest.requiredShippingContactFields = []; baseRequest.requiredShippingContactFields = [];
} else { } else {
// Minimum data required for order creation. // Minimum data required to create order.
baseRequest.requiredShippingContactFields = [ baseRequest.requiredShippingContactFields = [
'email', 'email',
'phone', 'phone',
@ -549,13 +561,18 @@ class ApplePayButton extends PaymentButton {
return paymentRequest; return paymentRequest;
} }
refreshContextData() { refreshProductContextData() {
if ( PaymentContext.Product === this.context ) { if ( PaymentContext.Product !== this.context ) {
// Refresh product data that makes the price change. return;
this.productQuantity = document.querySelector( 'input.qty' )?.value;
this.products = this.contextHandler.products();
this.log( 'Products updated', this.products );
} }
// Refresh product data that makes the price change.
this.#product.quantity = document.querySelector( 'input.qty' )?.value;
// Always an array; grouped products can return multiple items.
this.#product.items = this.contextHandler.products();
this.log( 'Products updated', this.#product );
} }
//------------------------ //------------------------
@ -701,16 +718,16 @@ class ApplePayButton extends PaymentButton {
getShippingContactData( event ) { getShippingContactData( event ) {
const productId = this.buttonConfig.product.id; const productId = this.buttonConfig.product.id;
this.refreshContextData(); this.refreshProductContextData();
switch ( this.context ) { switch ( this.context ) {
case PaymentContext.Product: case PaymentContext.Product:
return { return {
action: 'ppcp_update_shipping_contact', action: 'ppcp_update_shipping_contact',
product_id: productId, product_id: productId,
products: JSON.stringify( this.products ), products: JSON.stringify( this.#product.items ),
caller_page: 'productDetail', caller_page: 'productDetail',
product_quantity: this.productQuantity, product_quantity: this.#product.quantity,
simplified_contact: event.shippingContact, simplified_contact: event.shippingContact,
need_shipping: this.requiresShipping, need_shipping: this.requiresShipping,
'woocommerce-process-checkout-nonce': this.nonce, 'woocommerce-process-checkout-nonce': this.nonce,
@ -734,7 +751,7 @@ class ApplePayButton extends PaymentButton {
getShippingMethodData( event ) { getShippingMethodData( event ) {
const productId = this.buttonConfig.product.id; const productId = this.buttonConfig.product.id;
this.refreshContextData(); this.refreshProductContextData();
switch ( this.context ) { switch ( this.context ) {
case PaymentContext.Product: case PaymentContext.Product:
@ -748,9 +765,9 @@ class ApplePayButton extends PaymentButton {
: this.#initialPaymentRequest?.shippingContact ?? : this.#initialPaymentRequest?.shippingContact ??
this.#initialPaymentRequest?.billingContact, this.#initialPaymentRequest?.billingContact,
product_id: productId, product_id: productId,
products: JSON.stringify( this.products ), products: JSON.stringify( this.#product.items ),
caller_page: 'productDetail', caller_page: 'productDetail',
product_quantity: this.productQuantity, product_quantity: this.#product.quantity,
'woocommerce-process-checkout-nonce': this.nonce, 'woocommerce-process-checkout-nonce': this.nonce,
}; };
@ -797,8 +814,8 @@ class ApplePayButton extends PaymentButton {
action: 'ppcp_create_order', action: 'ppcp_create_order',
caller_page: this.context, caller_page: this.context,
product_id: this.buttonConfig.product.id ?? null, product_id: this.buttonConfig.product.id ?? null,
products: JSON.stringify( this.products ), products: JSON.stringify( this.#product.items ),
product_quantity: this.productQuantity ?? null, product_quantity: this.#product.quantity,
shipping_contact: shippingContact, shipping_contact: shippingContact,
billing_contact: billingContact, billing_contact: billingContact,
token: event.payment.token, token: event.payment.token,
@ -873,7 +890,7 @@ class ApplePayButton extends PaymentButton {
) { ) {
try { try {
if ( if (
this.shouldCompletePaymentWithContextHandler() this.shouldCompletePaymentWithContextHandler
) { ) {
// No shipping, expect immediate capture, ex: PayNow, Checkout with // No shipping, expect immediate capture, ex: PayNow, Checkout with
// form data. // form data.