ez-shp-storefront
Version:
A helper function collection for Shopify storefront.
105 lines (104 loc) • 4.18 kB
JavaScript
import { EventBus } from "./eventBus";
export class Checkout {
constructor() {
}
static getInstance() {
if (!this.instance) {
this.instance = new Checkout();
}
return this.instance;
}
setEventBus(eventBus) {
this.eventBus = eventBus;
return this;
}
getEventBus() {
if (!this.eventBus) {
this.eventBus = EventBus.getInstance();
}
return this.eventBus;
}
getCheckoutPageError(response) {
if (response.url.includes('account/login')) {
return 'You must be signed in to use the discount code';
}
if (response.url.includes('stock_problems')) {
return "Some items don’t have insufficient stock. Please adjust the quantity.";
}
return '';
}
async getCheckoutPage() {
if (!this.getCheckoutPagePromise) {
this.getCheckoutPagePromise = fetch(window.location.origin + '/checkout', { method: 'get' });
}
const response = (await this.getCheckoutPagePromise).clone();
this.getCheckoutPagePromise = null;
const errorMessage = this.getCheckoutPageError(response);
if (errorMessage) {
const error = { errorMessage };
this.getEventBus().emitEventListeners('Ez_GET_CHECKOUT_PAGE_ERROR', error);
return [null, error];
}
try {
const urlParts = response.url.split('/checkouts/');
if (urlParts.length < 2) {
const error = { errorMessage: 'Cannot get "shopify_checkout_token".' };
this.getEventBus().emitEventListeners('Ez_GET_CHECKOUT_PAGE_ERROR', error);
return [null, error];
}
const shopify_checkout_token = urlParts[1].split('?')[0];
this.shopify_checkout_token = shopify_checkout_token;
const text = await response.text();
let parser = new DOMParser();
const doc = parser.parseFromString(text, 'text/html');
const metaEl = doc.querySelector('meta[name="shopify-checkout-authorization-token"]');
if (!metaEl) {
const error = { errorMessage: 'Cannot get "shopify_checkout_authorization_token".' };
this.getEventBus().emitEventListeners('Ez_GET_CHECKOUT_PAGE_ERROR', error);
return [null, error];
}
const shopify_checkout_authorization_token = metaEl.getAttribute('content');
this.shopify_checkout_authorization_token = shopify_checkout_authorization_token;
const data = { shopify_checkout_token, shopify_checkout_authorization_token };
this.getEventBus().emitEventListeners('Ez_GET_CHECKOUT_PAGE_SUCCESS', data);
return [data, null];
}
catch (e) {
console.log(e);
const error = { errorMessage: e };
return [null, error];
}
}
async putDiscountCode(discount_code) {
const res = await fetch(`/wallets/checkouts/${this.shopify_checkout_token}`, {
method: "PUT",
headers: {
'Accept': '*/*',
'Cache-Control': 'max-age=0',
'x-shopify-checkout-authorization-token': this.shopify_checkout_authorization_token,
'Content-Type': 'application/json'
},
body: JSON.stringify({
checkout: {
discount_code
}
})
});
if (res.status !== 200) {
let error = '';
if (res.status === 429) {
error = 'Too many attempts: Please try again in a few minutes';
}
else {
const { errors } = await res.json();
error = JSON.stringify(errors);
}
this.getEventBus().emitEventListeners('Ez_PUT_DISCOUNT_CODE_ERROR', error);
return [null, error];
}
const data = await res.json();
this.getEventBus().emitEventListeners('Ez_PUT_DISCOUNT_CODE_SUCCESS', data);
return [data, null];
}
}
Checkout.instance = null;