Testing shipping fields updating ApplePay addresses on checkout page

This commit is contained in:
Pedro Silva 2023-11-06 10:15:56 +00:00
parent e52acf3c60
commit d49eb60358
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
4 changed files with 98 additions and 21 deletions

View file

@ -67,11 +67,6 @@ class ApplepayButton {
this.onButtonClick();
});
}
// Listen for changes on any input within the WooCommerce checkout form
jQuery('form.checkout').on('change', 'input, select, textarea', () => {
this.fetchTransactionInfo();
});
});
}
}
@ -196,11 +191,14 @@ class ApplepayButton {
const formData = new FormData(document.querySelector(checkoutFormSelector));
this.form_saved = Object.fromEntries(formData.entries());
// This line should be reviewed, the paypal.Applepay().confirmOrder fails if we add it.
//this.update_request_data_with_form(paymentDataRequest);
this.update_request_data_with_form(paymentDataRequest);
} catch (error) {
console.error(error);
}
const session = this.applePaySession(paymentDataRequest)
this.log('=== paymentDataRequest', paymentDataRequest);
const session = this.applePaySession(paymentDataRequest);
const formValidator = PayPalCommerceGateway.early_checkout_validation_enabled ?
new FormValidator(
PayPalCommerceGateway.ajax.validate_checkout.endpoint,
@ -226,11 +224,43 @@ class ApplepayButton {
update_request_data_with_form(paymentDataRequest) {
paymentDataRequest.billingContact = this.fill_billing_contact(this.form_saved);
paymentDataRequest.applicationData = this.fill_application_data(this.form_saved);
// "applicationData" is originating a "PayPalApplePayError: An internal server error has occurred" on paypal.Applepay().confirmOrder().
//paymentDataRequest.applicationData = this.fill_application_data(this.form_saved);
if (!this.buttonConfig.product.needShipping) {
return;
}
paymentDataRequest.shippingContact = this.fill_shipping_contact(this.form_saved);
const rate = this.transactionInfo.chosenShippingMethods[0];
paymentDataRequest.shippingMethods = [];
for (const shippingPackage of this.transactionInfo.shippingPackages) {
if (rate === shippingPackage.id) {
paymentDataRequest.shippingMethods.push({
'label' : shippingPackage.label,
'detail' : '',
'amount' : shippingPackage.cost_str,
'identifier' : shippingPackage.id,
});
break;
}
}
for (const shippingPackage of this.transactionInfo.shippingPackages) {
if (rate !== shippingPackage.id) {
paymentDataRequest.shippingMethods.push({
'label' : shippingPackage.label,
'detail' : '',
'amount' : shippingPackage.cost_str,
'identifier' : shippingPackage.id,
});
break;
}
}
this.log('=== paymentDataRequest.shippingMethods', paymentDataRequest.shippingMethods);
}
paymentDataRequest() {

View file

@ -36,7 +36,9 @@ class BaseHandler {
countryCode: data.country_code,
currencyCode: data.currency_code,
totalPriceStatus: 'FINAL',
totalPrice: data.total_str
totalPrice: data.total_str,
chosenShippingMethods: data.chosen_shipping_methods || null,
shippingPackages: data.shipping_packages || null,
});
});

View file

@ -405,6 +405,10 @@ class ApplePayButton implements ButtonInterface {
public function create_wc_order(): void {
$applepay_request_data_object = $this->applepay_data_object_http();
//phpcs:disable WordPress.Security.NonceVerification
$this->logger->info('== $_POST ==');
$this->logger->info(print_r($_POST, true));
$context = wc_clean( wp_unslash( $_POST['caller_page'] ?? '' ) );
if ( ! is_string( $context ) ) {
$this->response_templates->response_with_data_errors(
@ -418,6 +422,10 @@ class ApplePayButton implements ButtonInterface {
return;
}
$applepay_request_data_object->order_data( $context );
$this->logger->info('== $applepay_request_data_object ==');
$this->logger->info(print_r($applepay_request_data_object, true));
$this->update_posted_data( $applepay_request_data_object );
if ( $context === 'product' ) {
$cart_item_key = $this->prepare_cart( $applepay_request_data_object );
@ -596,10 +604,10 @@ class ApplePayButton implements ButtonInterface {
$address['country'] ?? $shop_country_code
);
WC()->customer->set_shipping_postcode(
$address['postcode'] ?? $shop_country_code
$address['postcode'] ?? ''
);
WC()->customer->set_shipping_city(
$address['city'] ?? $shop_country_code
$address['city'] ?? ''
);
}
@ -776,13 +784,14 @@ class ApplePayButton implements ButtonInterface {
add_action(
'woocommerce_checkout_create_order',
static function ( WC_Order $order, array $data ) use ( $applepay_request_data_object ) {
$this->logger->info('== HOOK woocommerce_checkout_create_order');
if ( ! empty( $applepay_request_data_object->shipping_method() ) ) {
$billing_address = $applepay_request_data_object->billing_address();
$shipping_address = $applepay_request_data_object->shipping_address();
// apple puts email in shipping_address while we get it from WC's billing_address.
$billing_address['email'] = $shipping_address['email'];
$billing_address['phone'] = $shipping_address['phone'];
$this->logger->info(print_r($billing_address, true));
$order->set_address( $billing_address, 'billing' );
$order->set_address( $shipping_address, 'shipping' );
}
@ -867,9 +876,12 @@ class ApplePayButton implements ButtonInterface {
* @param ApplePayDataObjectHttp $applepay_request_data_object The Apple Pay request data.
*/
protected function update_posted_data( $applepay_request_data_object ): void {
// TODO : get checkout form data in here to fill more fields like: ensure billing email and phone are filled.
add_filter(
'woocommerce_checkout_posted_data',
function ( array $data ) use ( $applepay_request_data_object ): array {
$this->logger->info('== HOOK woocommerce_checkout_posted_data');
$data['payment_method'] = 'ppcp-gateway';
$data['shipping_method'] = $applepay_request_data_object->shipping_method();
@ -883,7 +895,7 @@ class ApplePayButton implements ButtonInterface {
$data['billing_state'] = $applepay_request_data_object->billing_address()['state'] ?? '';
$data['billing_postcode'] = $applepay_request_data_object->billing_address()['postcode'] ?? '';
if ( ! empty( $applepay_request_data_object->shipping_method() ) ) {
if ( ! empty( $applepay_request_data_object->need_shipping() ) ) {
$data['billing_email'] = $applepay_request_data_object->shipping_address()['email'] ?? '';
$data['billing_phone'] = $applepay_request_data_object->shipping_address()['phone'] ?? '';
$data['shipping_first_name'] = $applepay_request_data_object->shipping_address()['first_name'] ?? '';
@ -898,6 +910,7 @@ class ApplePayButton implements ButtonInterface {
$data['shipping_email'] = $applepay_request_data_object->shipping_address()['email'] ?? '';
$data['shipping_phone'] = $applepay_request_data_object->shipping_address()['phone'] ?? '';
}
$this->logger->info(print_r($data, true));
return $data;
}