Add GooglePay shipping support.

This commit is contained in:
Pedro Silva 2023-10-13 14:36:11 +01:00
parent 799e06a7dd
commit 8526adbcee
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
6 changed files with 101 additions and 64 deletions

View file

@ -32,9 +32,9 @@ class ApplepayButton {
this.selectedShippingMethod = []
this.nonce = document.getElementById('woocommerce-process-checkout-nonce').value
this.log = (message) => {
this.log = function() {
if ( this.buttonConfig.is_debug ) {
console.log('[ApplePayButton] ' + message, this);
console.log('[ApplePayButton]', ...arguments);
}
}
}

View file

@ -10,6 +10,10 @@ class BaseHandler {
this.externalHandler = externalHandler;
}
shippingAllowed() {
return true;
}
transactionInfo() {
return new Promise((resolve, reject) => {

View file

@ -2,6 +2,10 @@ import BaseHandler from "./BaseHandler";
class CheckoutBlockHandler extends BaseHandler{
shippingAllowed() {
return false;
}
createOrder() {
return this.externalHandler.createOrder();
}

View file

@ -6,6 +6,10 @@ import FormValidator from "../../../../ppcp-button/resources/js/modules/Helper/F
class CheckoutHandler extends BaseHandler {
shippingAllowed() {
return false;
}
transactionInfo() {
return new Promise(async (resolve, reject) => {

View file

@ -23,9 +23,9 @@ class GooglepayButton {
this.externalHandler
);
this.log = (message) => {
this.log = function() {
if ( this.buttonConfig.is_debug ) {
console.log('[GooglePayButton] ' + message, this);
console.log('[GooglePayButton]', ...arguments);
}
}
}
@ -109,7 +109,7 @@ class GooglepayButton {
onPaymentAuthorized: this.onPaymentAuthorized.bind(this)
}
if ( this.buttonConfig.shipping.enabled ) {
if ( this.buttonConfig.shipping.enabled && this.contextHandler.shippingAllowed() ) {
callbacks['onPaymentDataChanged'] = this.onPaymentDataChanged.bind(this);
}
@ -197,7 +197,7 @@ class GooglepayButton {
paymentDataRequest.transactionInfo = await this.contextHandler.transactionInfo();
paymentDataRequest.merchantInfo = googlePayConfig.merchantInfo;
if ( this.buttonConfig.shipping.enabled ) {
if ( this.buttonConfig.shipping.enabled && this.contextHandler.shippingAllowed() ) {
paymentDataRequest.callbackIntents = ["SHIPPING_ADDRESS", "SHIPPING_OPTION", "PAYMENT_AUTHORIZATION"];
paymentDataRequest.shippingAddressRequired = true;
paymentDataRequest.shippingAddressParameters = this.shippingAddressParameters();
@ -227,10 +227,12 @@ class GooglepayButton {
return new Promise(async (resolve, reject) => {
let paymentDataRequestUpdate = {};
const updateData = new UpdatePaymentData(this.buttonConfig.ajax.update_payment_data)
const updatedData = await updateData.update(paymentData);
const updatedData = await (new UpdatePaymentData(this.buttonConfig.ajax.update_payment_data)).update(paymentData);
const transactionInfo = await this.contextHandler.transactionInfo();
this.log('onPaymentDataChanged:updatedData', updatedData);
this.log('onPaymentDataChanged:transactionInfo', transactionInfo);
updatedData.country_code = transactionInfo.countryCode;
updatedData.currency_code = transactionInfo.currencyCode;
updatedData.total_str = transactionInfo.totalPrice;
@ -254,9 +256,6 @@ class GooglepayButton {
}
resolve(paymentDataRequestUpdate);
// Update WooCommerce checkout form.
jQuery(document.body).trigger('update_checkout');
});
}

View file

@ -93,38 +93,8 @@ class UpdatePaymentDataEndpoint {
wc_maybe_define_constant( 'WOOCOMMERCE_CART', true );
}
// Update shipping address.
if ( $payment_data['callbackTrigger'] === 'SHIPPING_ADDRESS' ) {
/**
* The shipping methods.
*
* @var \WC_Customer|null $customer
*/
$customer = WC()->customer;
if ( $customer ) {
$customer->set_billing_postcode( $payment_data['shippingAddress']['postalCode'] ?? '' );
$customer->set_billing_country( $payment_data['shippingAddress']['countryCode'] ?? '' );
$customer->set_billing_state( $payment_data['shippingAddress']['locality'] ?? '' );
$customer->set_shipping_postcode( $payment_data['shippingAddress']['postalCode'] ?? '' );
$customer->set_shipping_country( $payment_data['shippingAddress']['countryCode'] ?? '' );
$customer->set_shipping_state( $payment_data['shippingAddress']['locality'] ?? '' );
// Save the data.
$customer->save();
WC()->session->set( 'customer', WC()->customer->get_data() );
}
}
// Set shipping method.
WC()->shipping->calculate_shipping( WC()->cart->get_shipping_packages() );
$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping_methods[0] = $payment_data['shippingOptionData']['id'];
WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods );
$this->update_addresses( $payment_data );
$this->update_shipping_method( $payment_data );
WC()->cart->calculate_shipping();
WC()->cart->calculate_fees();
@ -162,38 +132,94 @@ class UpdatePaymentDataEndpoint {
* @return array
*/
public function get_shipping_options(): array {
$shipping_methods = array();
$shipping_options = array();
$packages = WC()->cart->get_shipping_packages();
$zone = \WC_Shipping_Zones::get_zone_matching_package( $packages[0] );
$calculated_packages = WC()->shipping->calculate_shipping(
WC()->cart->get_shipping_packages()
);
/**
* The shipping methods.
*
* @var \WC_Shipping_Method[] $methods
*/
$methods = $zone->get_shipping_methods( true );
if ( ! isset( $calculated_packages[0] ) && ! isset( $calculated_packages[0]['rates'] ) ) {
return array();
}
foreach ( $methods as $method ) {
if ( ! $method->is_available( $packages[0] ) ) {
continue;
}
$shipping_methods[] = array(
'id' => $method->get_rate_id(),
'label' => $method->get_title(),
'description' => '',
foreach ( $calculated_packages[0]['rates'] as $rate ) {
/**
* The shipping rate.
*
* @var \WC_Shipping_Rate $rate
*/
$shipping_options[] = array(
'id' => $rate->get_id(),
'label' => $rate->get_label(),
'description' => html_entity_decode(
wp_strip_all_tags(
wc_price( (float) $rate->get_cost(), array( 'currency' => get_woocommerce_currency() ) )
)
),
);
}
if ( ! isset( $shipping_methods[0] ) ) {
if ( ! isset( $shipping_options[0] ) ) {
return array();
}
return array(
'defaultSelectedOptionId' => $shipping_methods[0]['id'],
'shippingOptions' => $shipping_methods,
'defaultSelectedOptionId' => $shipping_options[0]['id'],
'shippingOptions' => $shipping_options,
);
}
/**
* Update addresses.
*
* @param array $payment_data The payment data.
* @return void
*/
private function update_addresses( array $payment_data ): void {
if ( ( $payment_data['callbackTrigger'] ?? '' ) !== 'SHIPPING_ADDRESS' ) {
return;
}
/**
* The shipping methods.
*
* @var \WC_Customer|null $customer
*/
$customer = WC()->customer;
if ( ! $customer ) {
return;
}
$customer->set_billing_postcode( $payment_data['shippingAddress']['postalCode'] ?? '' );
$customer->set_billing_country( $payment_data['shippingAddress']['countryCode'] ?? '' );
$customer->set_billing_state( $payment_data['shippingAddress']['locality'] ?? '' );
$customer->set_shipping_postcode( $payment_data['shippingAddress']['postalCode'] ?? '' );
$customer->set_shipping_country( $payment_data['shippingAddress']['countryCode'] ?? '' );
$customer->set_shipping_state( $payment_data['shippingAddress']['locality'] ?? '' );
// Save the data.
$customer->save();
WC()->session->set( 'customer', WC()->customer->get_data() );
}
/**
* Update shipping method.
*
* @param array $payment_data The payment data.
* @return void
*/
private function update_shipping_method( array $payment_data ): void {
$rate_id = $payment_data['shippingOptionData']['id'];
$calculated_packages = WC()->shipping->calculate_shipping(
WC()->cart->get_shipping_packages()
);
if ( $rate_id && isset( $calculated_packages[0]['rates'][ $rate_id ] ) ) {
WC()->session->set( 'chosen_shipping_methods', array( $rate_id ) );
}
}
}