mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-01 07:02:48 +08:00
Testing shipping fields updating ApplePay addresses on checkout page
This commit is contained in:
parent
e52acf3c60
commit
d49eb60358
4 changed files with 98 additions and 21 deletions
|
@ -67,11 +67,6 @@ class ApplepayButton {
|
||||||
this.onButtonClick();
|
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));
|
const formData = new FormData(document.querySelector(checkoutFormSelector));
|
||||||
this.form_saved = Object.fromEntries(formData.entries());
|
this.form_saved = Object.fromEntries(formData.entries());
|
||||||
// This line should be reviewed, the paypal.Applepay().confirmOrder fails if we add it.
|
// 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) {
|
} catch (error) {
|
||||||
console.error(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 ?
|
const formValidator = PayPalCommerceGateway.early_checkout_validation_enabled ?
|
||||||
new FormValidator(
|
new FormValidator(
|
||||||
PayPalCommerceGateway.ajax.validate_checkout.endpoint,
|
PayPalCommerceGateway.ajax.validate_checkout.endpoint,
|
||||||
|
@ -226,11 +224,43 @@ class ApplepayButton {
|
||||||
|
|
||||||
update_request_data_with_form(paymentDataRequest) {
|
update_request_data_with_form(paymentDataRequest) {
|
||||||
paymentDataRequest.billingContact = this.fill_billing_contact(this.form_saved);
|
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) {
|
if (!this.buttonConfig.product.needShipping) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
paymentDataRequest.shippingContact = this.fill_shipping_contact(this.form_saved);
|
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() {
|
paymentDataRequest() {
|
||||||
|
|
|
@ -36,7 +36,9 @@ class BaseHandler {
|
||||||
countryCode: data.country_code,
|
countryCode: data.country_code,
|
||||||
currencyCode: data.currency_code,
|
currencyCode: data.currency_code,
|
||||||
totalPriceStatus: 'FINAL',
|
totalPriceStatus: 'FINAL',
|
||||||
totalPrice: data.total_str
|
totalPrice: data.total_str,
|
||||||
|
chosenShippingMethods: data.chosen_shipping_methods || null,
|
||||||
|
shippingPackages: data.shipping_packages || null,
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -405,6 +405,10 @@ class ApplePayButton implements ButtonInterface {
|
||||||
public function create_wc_order(): void {
|
public function create_wc_order(): void {
|
||||||
$applepay_request_data_object = $this->applepay_data_object_http();
|
$applepay_request_data_object = $this->applepay_data_object_http();
|
||||||
//phpcs:disable WordPress.Security.NonceVerification
|
//phpcs:disable WordPress.Security.NonceVerification
|
||||||
|
|
||||||
|
$this->logger->info('== $_POST ==');
|
||||||
|
$this->logger->info(print_r($_POST, true));
|
||||||
|
|
||||||
$context = wc_clean( wp_unslash( $_POST['caller_page'] ?? '' ) );
|
$context = wc_clean( wp_unslash( $_POST['caller_page'] ?? '' ) );
|
||||||
if ( ! is_string( $context ) ) {
|
if ( ! is_string( $context ) ) {
|
||||||
$this->response_templates->response_with_data_errors(
|
$this->response_templates->response_with_data_errors(
|
||||||
|
@ -418,6 +422,10 @@ class ApplePayButton implements ButtonInterface {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$applepay_request_data_object->order_data( $context );
|
$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 );
|
$this->update_posted_data( $applepay_request_data_object );
|
||||||
if ( $context === 'product' ) {
|
if ( $context === 'product' ) {
|
||||||
$cart_item_key = $this->prepare_cart( $applepay_request_data_object );
|
$cart_item_key = $this->prepare_cart( $applepay_request_data_object );
|
||||||
|
@ -596,10 +604,10 @@ class ApplePayButton implements ButtonInterface {
|
||||||
$address['country'] ?? $shop_country_code
|
$address['country'] ?? $shop_country_code
|
||||||
);
|
);
|
||||||
WC()->customer->set_shipping_postcode(
|
WC()->customer->set_shipping_postcode(
|
||||||
$address['postcode'] ?? $shop_country_code
|
$address['postcode'] ?? ''
|
||||||
);
|
);
|
||||||
WC()->customer->set_shipping_city(
|
WC()->customer->set_shipping_city(
|
||||||
$address['city'] ?? $shop_country_code
|
$address['city'] ?? ''
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -776,13 +784,14 @@ class ApplePayButton implements ButtonInterface {
|
||||||
add_action(
|
add_action(
|
||||||
'woocommerce_checkout_create_order',
|
'woocommerce_checkout_create_order',
|
||||||
static function ( WC_Order $order, array $data ) use ( $applepay_request_data_object ) {
|
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() ) ) {
|
if ( ! empty( $applepay_request_data_object->shipping_method() ) ) {
|
||||||
$billing_address = $applepay_request_data_object->billing_address();
|
$billing_address = $applepay_request_data_object->billing_address();
|
||||||
$shipping_address = $applepay_request_data_object->shipping_address();
|
$shipping_address = $applepay_request_data_object->shipping_address();
|
||||||
// apple puts email in shipping_address while we get it from WC's billing_address.
|
// apple puts email in shipping_address while we get it from WC's billing_address.
|
||||||
$billing_address['email'] = $shipping_address['email'];
|
$billing_address['email'] = $shipping_address['email'];
|
||||||
$billing_address['phone'] = $shipping_address['phone'];
|
$billing_address['phone'] = $shipping_address['phone'];
|
||||||
|
$this->logger->info(print_r($billing_address, true));
|
||||||
$order->set_address( $billing_address, 'billing' );
|
$order->set_address( $billing_address, 'billing' );
|
||||||
$order->set_address( $shipping_address, 'shipping' );
|
$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.
|
* @param ApplePayDataObjectHttp $applepay_request_data_object The Apple Pay request data.
|
||||||
*/
|
*/
|
||||||
protected function update_posted_data( $applepay_request_data_object ): void {
|
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(
|
add_filter(
|
||||||
'woocommerce_checkout_posted_data',
|
'woocommerce_checkout_posted_data',
|
||||||
function ( array $data ) use ( $applepay_request_data_object ): array {
|
function ( array $data ) use ( $applepay_request_data_object ): array {
|
||||||
|
$this->logger->info('== HOOK woocommerce_checkout_posted_data');
|
||||||
|
|
||||||
$data['payment_method'] = 'ppcp-gateway';
|
$data['payment_method'] = 'ppcp-gateway';
|
||||||
$data['shipping_method'] = $applepay_request_data_object->shipping_method();
|
$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_state'] = $applepay_request_data_object->billing_address()['state'] ?? '';
|
||||||
$data['billing_postcode'] = $applepay_request_data_object->billing_address()['postcode'] ?? '';
|
$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_email'] = $applepay_request_data_object->shipping_address()['email'] ?? '';
|
||||||
$data['billing_phone'] = $applepay_request_data_object->shipping_address()['phone'] ?? '';
|
$data['billing_phone'] = $applepay_request_data_object->shipping_address()['phone'] ?? '';
|
||||||
$data['shipping_first_name'] = $applepay_request_data_object->shipping_address()['first_name'] ?? '';
|
$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_email'] = $applepay_request_data_object->shipping_address()['email'] ?? '';
|
||||||
$data['shipping_phone'] = $applepay_request_data_object->shipping_address()['phone'] ?? '';
|
$data['shipping_phone'] = $applepay_request_data_object->shipping_address()['phone'] ?? '';
|
||||||
}
|
}
|
||||||
|
$this->logger->info(print_r($data, true));
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
|
@ -79,6 +79,35 @@ class CartScriptParamsEndpoint implements EndpointInterface {
|
||||||
$shop_country_code = $base_location['country'] ?? '';
|
$shop_country_code = $base_location['country'] ?? '';
|
||||||
$currency_code = get_woocommerce_currency();
|
$currency_code = get_woocommerce_currency();
|
||||||
|
|
||||||
|
|
||||||
|
$calculated_packages = WC()->shipping->calculate_shipping(
|
||||||
|
WC()->cart->get_shipping_packages()
|
||||||
|
);
|
||||||
|
|
||||||
|
$shipping_packages = array();
|
||||||
|
|
||||||
|
foreach ( $calculated_packages[0]['rates'] as $rate ) {
|
||||||
|
$rate_cost = $rate->get_cost();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The shipping rate.
|
||||||
|
*
|
||||||
|
* @var \WC_Shipping_Rate $rate
|
||||||
|
*/
|
||||||
|
$shipping_packages[] = array(
|
||||||
|
'id' => $rate->get_id(),
|
||||||
|
'label' => $rate->get_label(),
|
||||||
|
'cost' => (float) $rate_cost,
|
||||||
|
'cost_str' => ( new Money( (float) $rate_cost, $currency_code ) )->value_str(),
|
||||||
|
'description' => html_entity_decode(
|
||||||
|
wp_strip_all_tags(
|
||||||
|
wc_price( (float) $rate->get_cost(), array( 'currency' => get_woocommerce_currency() ) )
|
||||||
|
)
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
wp_send_json_success(
|
wp_send_json_success(
|
||||||
array(
|
array(
|
||||||
'url_params' => $script_data['url_params'],
|
'url_params' => $script_data['url_params'],
|
||||||
|
@ -90,6 +119,9 @@ class CartScriptParamsEndpoint implements EndpointInterface {
|
||||||
'total_str' => ( new Money( $total, $currency_code ) )->value_str(),
|
'total_str' => ( new Money( $total, $currency_code ) )->value_str(),
|
||||||
'currency_code' => $currency_code,
|
'currency_code' => $currency_code,
|
||||||
'country_code' => $shop_country_code,
|
'country_code' => $shop_country_code,
|
||||||
|
|
||||||
|
'chosen_shipping_methods' => WC()->session->get( 'chosen_shipping_methods' ),
|
||||||
|
'shipping_packages' => $shipping_packages
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue