mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
✨ New CheckoutBootstrap for GooglePay
This new module uses previously stored payer details to populate the checkout form on the classic checkout page.
This commit is contained in:
parent
a51748f805
commit
c48c94e09d
3 changed files with 101 additions and 24 deletions
|
@ -0,0 +1,64 @@
|
||||||
|
import { GooglePayStorage } from '../Helper/GooglePayStorage';
|
||||||
|
import { setPayerData } from '../../../../ppcp-button/resources/js/modules/Helper/PayerData';
|
||||||
|
|
||||||
|
const CHECKOUT_FORM_SELECTOR = 'form.woocommerce-checkout';
|
||||||
|
|
||||||
|
export class CheckoutBootstrap {
|
||||||
|
/**
|
||||||
|
* @type {GooglePayStorage}
|
||||||
|
*/
|
||||||
|
#storage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {null|HTMLFormElement}
|
||||||
|
*/
|
||||||
|
#checkoutForm = null;
|
||||||
|
|
||||||
|
constructor( storage ) {
|
||||||
|
this.#storage = storage;
|
||||||
|
|
||||||
|
this.onFormSubmit = this.onFormSubmit.bind( this );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the WooCommerce checkout form element.
|
||||||
|
*
|
||||||
|
* @return {HTMLFormElement|null} The form, or null if not a checkout page.
|
||||||
|
*/
|
||||||
|
get checkoutForm() {
|
||||||
|
if ( null === this.#checkoutForm ) {
|
||||||
|
this.#checkoutForm = document.querySelector(
|
||||||
|
CHECKOUT_FORM_SELECTOR
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.#checkoutForm;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates, if the current page contains a checkout form.
|
||||||
|
*
|
||||||
|
* @return {boolean} True, if a checkout form is present.
|
||||||
|
*/
|
||||||
|
get isPageWithCheckoutForm() {
|
||||||
|
return this.checkoutForm instanceof HTMLElement;
|
||||||
|
}
|
||||||
|
|
||||||
|
init() {
|
||||||
|
if ( ! this.isPageWithCheckoutForm ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const billingData = this.#storage.getPayer();
|
||||||
|
|
||||||
|
if ( billingData ) {
|
||||||
|
setPayerData( billingData );
|
||||||
|
|
||||||
|
this.checkoutForm.addEventListener( 'submit', this.onFormSubmit );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onFormSubmit() {
|
||||||
|
this.#storage.clearPayer();
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,6 +7,7 @@ import widgetBuilder from '../../../ppcp-button/resources/js/modules/Renderer/Wi
|
||||||
import UpdatePaymentData from './Helper/UpdatePaymentData';
|
import UpdatePaymentData from './Helper/UpdatePaymentData';
|
||||||
import { PaymentMethods } from '../../../ppcp-button/resources/js/modules/Helper/CheckoutMethodState';
|
import { PaymentMethods } from '../../../ppcp-button/resources/js/modules/Helper/CheckoutMethodState';
|
||||||
import { setPayerData } from '../../../ppcp-button/resources/js/modules/Helper/PayerData';
|
import { setPayerData } from '../../../ppcp-button/resources/js/modules/Helper/PayerData';
|
||||||
|
import moduleStorage from './Helper/GooglePayStorage';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Plugin-specific styling.
|
* Plugin-specific styling.
|
||||||
|
@ -74,6 +75,26 @@ import { setPayerData } from '../../../ppcp-button/resources/js/modules/Helper/P
|
||||||
* Google Pay payment sheet.
|
* Google Pay payment sheet.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
function payerDataFromPaymentResponse( response ) {
|
||||||
|
const raw = response?.paymentMethodData?.info?.billingAddress;
|
||||||
|
|
||||||
|
return {
|
||||||
|
email_address: response?.email,
|
||||||
|
name: {
|
||||||
|
given_name: raw.name.split( ' ' )[ 0 ], // Assuming first name is the first part
|
||||||
|
surname: raw.name.split( ' ' ).slice( 1 ).join( ' ' ), // Assuming last name is the rest
|
||||||
|
},
|
||||||
|
address: {
|
||||||
|
country_code: raw.countryCode,
|
||||||
|
address_line_1: raw.address1,
|
||||||
|
address_line_2: raw.address2,
|
||||||
|
admin_area_1: raw.administrativeArea,
|
||||||
|
admin_area_2: raw.locality,
|
||||||
|
postal_code: raw.postalCode,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
class GooglepayButton extends PaymentButton {
|
class GooglepayButton extends PaymentButton {
|
||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
|
@ -643,30 +664,16 @@ class GooglepayButton extends PaymentButton {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const propagatePayerDataToForm = () => {
|
const addBillingDataToSession = () => {
|
||||||
const raw = paymentData?.paymentMethodData?.info?.billingAddress;
|
const payer = payerDataFromPaymentResponse( paymentData );
|
||||||
const payer = {
|
|
||||||
email_address: paymentData?.email,
|
|
||||||
name: {
|
|
||||||
given_name: raw.name.split( ' ' )[ 0 ], // Assuming first name is the first part
|
|
||||||
surname: raw.name.split( ' ' ).slice( 1 ).join( ' ' ), // Assuming last name is the rest
|
|
||||||
},
|
|
||||||
address: {
|
|
||||||
country_code: raw.countryCode,
|
|
||||||
address_line_1: raw.address1,
|
|
||||||
address_line_2: raw.address2,
|
|
||||||
admin_area_1: raw.administrativeArea,
|
|
||||||
admin_area_2: raw.locality,
|
|
||||||
postal_code: raw.postalCode,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
|
moduleStorage.setPayer( payer );
|
||||||
setPayerData( payer );
|
setPayerData( payer );
|
||||||
};
|
};
|
||||||
|
|
||||||
return new Promise( async ( resolve ) => {
|
return new Promise( async ( resolve ) => {
|
||||||
try {
|
try {
|
||||||
propagatePayerDataToForm();
|
addBillingDataToSession();
|
||||||
await processPaymentPromise( resolve );
|
await processPaymentPromise( resolve );
|
||||||
} catch ( err ) {
|
} catch ( err ) {
|
||||||
resolve( paymentError( err.message ) );
|
resolve( paymentError( err.message ) );
|
||||||
|
|
|
@ -2,13 +2,23 @@ import { loadCustomScript } from '@paypal/paypal-js';
|
||||||
import { loadPaypalScript } from '../../../ppcp-button/resources/js/modules/Helper/ScriptLoading';
|
import { loadPaypalScript } from '../../../ppcp-button/resources/js/modules/Helper/ScriptLoading';
|
||||||
import GooglepayManager from './GooglepayManager';
|
import GooglepayManager from './GooglepayManager';
|
||||||
import { setupButtonEvents } from '../../../ppcp-button/resources/js/modules/Helper/ButtonRefreshHelper';
|
import { setupButtonEvents } from '../../../ppcp-button/resources/js/modules/Helper/ButtonRefreshHelper';
|
||||||
|
import { CheckoutBootstrap } from './ContextBootstrap/CheckoutBootstrap';
|
||||||
|
import moduleStorage from './Helper/GooglePayStorage';
|
||||||
|
|
||||||
|
( function ( { buttonConfig, ppcpConfig } ) {
|
||||||
|
const context = ppcpConfig.context;
|
||||||
|
|
||||||
( function ( { buttonConfig, ppcpConfig, jQuery } ) {
|
|
||||||
let manager;
|
let manager;
|
||||||
|
|
||||||
const bootstrap = function () {
|
const bootstrap = function () {
|
||||||
manager = new GooglepayManager( buttonConfig, ppcpConfig );
|
manager = new GooglepayManager( buttonConfig, ppcpConfig );
|
||||||
manager.init();
|
manager.init();
|
||||||
|
|
||||||
|
if ( 'continuation' === context || 'checkout' === context ) {
|
||||||
|
const checkoutBootstap = new CheckoutBootstrap( moduleStorage );
|
||||||
|
|
||||||
|
checkoutBootstap.init();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
setupButtonEvents( function () {
|
setupButtonEvents( function () {
|
||||||
|
@ -18,10 +28,7 @@ import { setupButtonEvents } from '../../../ppcp-button/resources/js/modules/Hel
|
||||||
} );
|
} );
|
||||||
|
|
||||||
document.addEventListener( 'DOMContentLoaded', () => {
|
document.addEventListener( 'DOMContentLoaded', () => {
|
||||||
if (
|
if ( ! buttonConfig || ! ppcpConfig ) {
|
||||||
typeof buttonConfig === 'undefined' ||
|
|
||||||
typeof ppcpConfig === 'undefined'
|
|
||||||
) {
|
|
||||||
// No PayPal buttons present on this page.
|
// No PayPal buttons present on this page.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -52,5 +59,4 @@ import { setupButtonEvents } from '../../../ppcp-button/resources/js/modules/Hel
|
||||||
} )( {
|
} )( {
|
||||||
buttonConfig: window.wc_ppcp_googlepay,
|
buttonConfig: window.wc_ppcp_googlepay,
|
||||||
ppcpConfig: window.PayPalCommerceGateway,
|
ppcpConfig: window.PayPalCommerceGateway,
|
||||||
jQuery: window.jQuery,
|
|
||||||
} );
|
} );
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue