2024-08-20 14:19:41 +02:00
|
|
|
import { GooglePayStorage } from '../Helper/GooglePayStorage';
|
2024-08-21 13:52:11 +02:00
|
|
|
import {
|
|
|
|
getWooCommerceCustomerDetails,
|
|
|
|
setPayerData,
|
|
|
|
} from '../../../../ppcp-button/resources/js/modules/Helper/PayerData';
|
2024-08-20 14:19:41 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-08-21 13:52:11 +02:00
|
|
|
this.#populateCheckoutFields();
|
|
|
|
}
|
|
|
|
|
|
|
|
#populateCheckoutFields() {
|
|
|
|
const loggedInData = getWooCommerceCustomerDetails();
|
|
|
|
|
|
|
|
// If customer is logged in, we use the details from the customer profile.
|
|
|
|
if ( loggedInData ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-20 14:19:41 +02:00
|
|
|
const billingData = this.#storage.getPayer();
|
|
|
|
|
|
|
|
if ( billingData ) {
|
|
|
|
setPayerData( billingData );
|
|
|
|
|
2024-08-21 13:52:11 +02:00
|
|
|
this.checkoutForm.addEventListener( 'submit', () =>
|
|
|
|
this.#onFormSubmit()
|
|
|
|
);
|
2024-08-20 14:19:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-21 13:52:11 +02:00
|
|
|
#onFormSubmit() {
|
2024-08-20 14:19:41 +02:00
|
|
|
this.#storage.clearPayer();
|
|
|
|
}
|
|
|
|
}
|