Refactor requests for cart, checkout and blocks

This commit is contained in:
carmenmaymo 2023-09-08 11:04:24 +02:00
parent c2f99abe6a
commit 904bfe79d1
No known key found for this signature in database
GPG key ID: 6023F686B0F3102E
2 changed files with 97 additions and 80 deletions

View file

@ -23,7 +23,7 @@ class ApplepayButton {
this.productQuantity = document.querySelector('input.qty').value
}
this.updatedContactInfo = []
this.updated_contact_info = []
this.selectedShippingMethod = []
this.nonce = document.getElementById('woocommerce-process-checkout-nonce').value
}
@ -58,17 +58,15 @@ class ApplepayButton {
});
}
applePaySession(paymentRequest) {
console.log('apple session', paymentRequest)
const session = new ApplePaySession(4, paymentRequest)
session.begin()
const ajaxUrl = this.buttonConfig.ajax_url
const productId = this.buttonConfig.product.id
if (this.buttonConfig.product.needShipping) {
session.onshippingmethodselected = this.onshippingmethodselected(ajaxUrl, productId, session)
session.onshippingcontactselected = this.onshippingcontactselected(ajaxUrl, productId, session)
session.onshippingmethodselected = this.onshippingmethodselected(session)
session.onshippingcontactselected = this.onshippingcontactselected(session)
}
session.onvalidatemerchant = this.onvalidatemerchant(session);
session.onpaymentauthorized = this.onpaymentauthorized(ajaxUrl, productId, session);
session.onpaymentauthorized = this.onpaymentauthorized(session);
}
@ -176,37 +174,22 @@ class ApplepayButton {
});
};
}
onshippingmethodselected(ajaxUrl, productId, session) {
function getData(event) {
switch (this.context) {
case 'product': return {
action: 'ppcp_update_shipping_method',
shipping_method: event.shippingMethod,
product_id: productId,
caller_page: 'productDetail',
product_quantity: this.productQuantity,
simplified_contact: this.updatedContactInfo,
'woocommerce-process-checkout-nonce': this.nonce,
}
case 'cart':
case 'checkout':
return {
action: 'ppcp_update_shipping_method',
shipping_method: event.shippingMethod,
caller_page: 'cart',
simplified_contact: this.updatedContactInfo,
'woocommerce-process-checkout-nonce': this.nonce,
}
}
}
onshippingmethodselected(session) {
const ajax_url = this.buttonConfig.ajax_url
console.log('[ApplePayButton] onshippingmethodselected');
return function (event) {
return (event) => {
const data = this.getShippingMethodData(event);
jQuery.ajax({
url: this.buttonConfig.ajax_url,
url: ajax_url,
method: 'POST',
data: getData.call(this, event),
data: data,
success: (applePayShippingMethodUpdate, textStatus, jqXHR) => {
let response = applePayShippingMethodUpdate.data
if (applePayShippingMethodUpdate.success === false) {
response.errors = createAppleErrors(response.errors)
}
console.log('shipping method update response', response, applePayShippingMethodUpdate)
this.selectedShippingMethod = event.shippingMethod
//order the response shipping methods, so that the selected shipping method is the first one
let orderedShippingMethods = response.newShippingMethods.sort((a, b) => {
@ -220,7 +203,7 @@ class ApplepayButton {
if (applePayShippingMethodUpdate.success === false) {
response.errors = createAppleErrors(response.errors)
}
this.completeShippingMethodSelection(response)
session.completeShippingMethodSelection(response)
},
error: (jqXHR, textStatus, errorThrown) => {
console.warn(textStatus, errorThrown)
@ -229,46 +212,27 @@ class ApplepayButton {
})
};
}
onshippingcontactselected(ajax_url, productId, session) {
function getData(event) {
switch (this.context) {
case 'product': return {
action: 'ppcp_update_shipping_contact',
product_id: productId,
caller_page: 'productDetail',
product_quantity: this.productQuantity,
simplified_contact: event.shippingContact,
need_shipping: this.needShipping,
'woocommerce-process-checkout-nonce': this.nonce,
}
case 'cart':
case 'checkout':
return {
action: 'ppcp_update_shipping_contact',
simplified_contact: event.shippingContact,
caller_page: 'cart',
need_shipping: this.needShipping,
'woocommerce-process-checkout-nonce': this.nonce,
}
}
}
onshippingcontactselected(session) {
const ajax_url = this.buttonConfig.ajax_url
return function (event) {
return (event) => {
const data = this.getShippingContactData(event);
console.log('shipping contact selected', data, event)
jQuery.ajax({
url: this.buttonConfig.ajax_url,
url: ajax_url,
method: 'POST',
data: getData.call(this, event),
data: data,
success: (applePayShippingContactUpdate, textStatus, jqXHR) => {
let response = applePayShippingContactUpdate.data
this.updatedContactInfo = event.shippingContact
this.updated_contact_info = event.shippingContact
console.log('shipping contact update response', response, applePayShippingContactUpdate, this.updated_contact_info)
if (applePayShippingContactUpdate.success === false) {
response.errors = createAppleErrors(response.errors)
}
if (response.newShippingMethods) {
this.selectedShippingMethod = response.newShippingMethods[0]
}
this.completeShippingContactSelection(response)
session.completeShippingContactSelection(response)
},
error: (jqXHR, textStatus, errorThrown) => {
console.warn(textStatus, errorThrown)
@ -277,8 +241,60 @@ class ApplepayButton {
})
};
}
getShippingContactData(event) {
const product_id = this.buttonConfig.product.id;
onpaymentauthorized(ajaxUrl, productId, session) {
switch (this.context) {
case 'product':
return {
action: 'ppcp_update_shipping_contact',
product_id: product_id,
caller_page: 'productDetail',
product_quantity: this.productQuantity,
simplified_contact: event.shippingContact,
need_shipping: this.buttonConfig.product.needShipping,
'woocommerce-process-checkout-nonce': this.nonce,
};
case 'cart':
case 'checkout':
case 'cart-block':
case 'checkout-block':
return {
action: 'ppcp_update_shipping_contact',
simplified_contact: event.shippingContact,
caller_page: 'cart',
need_shipping: this.buttonConfig.product.needShipping,
'woocommerce-process-checkout-nonce': this.nonce,
};
}
}
getShippingMethodData(event) {
const product_id = this.buttonConfig.product.id;
switch (this.context) {
case 'product': return {
action: 'ppcp_update_shipping_method',
shipping_method: event.shippingMethod,
product_id: product_id,
caller_page: 'productDetail',
product_quantity: this.productQuantity,
simplified_contact: this.updated_contact_info,
'woocommerce-process-checkout-nonce': this.nonce,
}
case 'cart':
case 'checkout':
case 'cart-block':
case 'checkout-block':
return {
action: 'ppcp_update_shipping_method',
shipping_method: event.shippingMethod,
caller_page: 'cart',
simplified_contact: this.updated_contact_info,
'woocommerce-process-checkout-nonce': this.nonce,
}
}
}
onpaymentauthorized(session) {
/*return (event) => {
function form() {
return document.querySelector('form.cart');

View file

@ -302,11 +302,7 @@ class ApplePayButton implements ButtonInterface {
);
return;
}
$cart_item_key = $this->prepare_cart($applepay_request_data_object);
$cart = WC()->cart;
$payment_details = $this->which_calculate_totals( $cart, $applepay_request_data_object );
$this->clear_current_cart($cart, $cart_item_key);
$this->reload_cart( $cart );
$payment_details = $this->which_calculate_totals( $applepay_request_data_object );
$response = $this->response_templates->apple_formatted_response( $payment_details );
$this->response_templates->response_success( $response );
}
@ -327,11 +323,7 @@ class ApplePayButton implements ButtonInterface {
if ( $applepay_request_data_object->has_errors() ) {
$this->response_templates->response_with_data_errors( $applepay_request_data_object->errors() );
}
$cart_item_key = $this->prepare_cart($applepay_request_data_object);
$cart = WC()->cart;
$payment_details = $this->which_calculate_totals( $cart, $applepay_request_data_object );
$this->clear_current_cart($cart, $cart_item_key);
$this->reload_cart( $cart );
$payment_details = $this->which_calculate_totals( $applepay_request_data_object );
$response = $this->response_templates->apple_formatted_response( $payment_details );
$this->response_templates->response_success( $response );
}
@ -349,7 +341,12 @@ class ApplePayButton implements ButtonInterface {
$this->update_posted_data($applepay_request_data_object);
$cart_item_key = $this->prepare_cart($applepay_request_data_object);
$cart = WC()->cart;
$this->which_calculate_totals($cart, $applepay_request_data_object );
$address = $applepay_request_data_object->shipping_address();
$this->calculate_totals_single_product(
$cart,
$address,
$applepay_request_data_object->shipping_method()
);
if (! $cart_item_key) {
$this->response_templates->response_with_data_errors(
array(
@ -421,19 +418,23 @@ class ApplePayButton implements ButtonInterface {
* @return array|bool
*/
protected function which_calculate_totals(
$cart,
$applepay_request_data_object
) {
$address = $applepay_request_data_object->shipping_address() ?? $applepay_request_data_object->simplified_contact();
$address = empty($applepay_request_data_object->shipping_address()) ? $applepay_request_data_object->simplified_contact() : $applepay_request_data_object->shipping_address();
if ( $applepay_request_data_object->caller_page === 'productDetail' ) {
$cart_item_key = $this->prepare_cart($applepay_request_data_object);
$cart = WC()->cart;
if (! assert($cart instanceof WC_Cart)) {
return false;
}
return $this->calculate_totals_single_product(
$totals = $this->calculate_totals_single_product(
$cart,
$address,
$applepay_request_data_object->shipping_method()
);
$this->clear_current_cart($cart, $cart_item_key);
$this->reload_cart( $cart );
return $totals;
}
if ( $applepay_request_data_object->caller_page === 'cart' ) {
return $this->calculate_totals_cart_page(
@ -907,7 +908,7 @@ class ApplePayButton implements ButtonInterface {
);
}
if ( $button_enabled_minicart ) {
/*if ( $button_enabled_minicart ) {
$default_hook_name = 'woocommerce_paypal_payments_minicart_button_render';
$render_placeholder = apply_filters( 'woocommerce_paypal_payments_googlepay_minicart_button_render_hook', $default_hook_name );
$render_placeholder = is_string( $render_placeholder ) ? $render_placeholder : $default_hook_name;
@ -918,7 +919,7 @@ class ApplePayButton implements ButtonInterface {
},
21
);
}
}*/
return true;
}
/**