2021-11-22 17:51:50 +00:00
|
|
|
import { CartBaseController } from "./cart_base_controller";
|
2021-06-01 21:33:49 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Retrieves payment methods and redirect to external checkouts
|
|
|
|
*/
|
|
|
|
export default class extends CartBaseController {
|
2021-11-22 17:51:50 +00:00
|
|
|
static targets = ["form", "submit", "specialInstructions"];
|
2021-06-01 21:33:49 +00:00
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
async connect() {
|
|
|
|
const orderToken = this.token;
|
|
|
|
const response = await this.spree.checkout.paymentMethods({ orderToken });
|
2021-06-01 21:33:49 +00:00
|
|
|
|
|
|
|
if (response.isFail()) {
|
2021-11-22 17:51:50 +00:00
|
|
|
this.handleFailure(response);
|
|
|
|
return;
|
2021-06-01 21:33:49 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
const payment_methods = response.success().data;
|
|
|
|
const site = window.site;
|
|
|
|
const cart = this.cart;
|
|
|
|
const next = { url: this.data.get("nextUrl") };
|
|
|
|
const back = { url: this.data.get("backUrl") };
|
2021-06-01 21:33:49 +00:00
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
this.render({ payment_methods, site, cart, next, back });
|
2021-06-01 21:33:49 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
async render(data = {}) {
|
|
|
|
const template = window.templates[this.data.get("template")];
|
2021-06-01 21:33:49 +00:00
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
this.element.innerHTML = await this.engine.parseAndRender(template, data);
|
2021-06-01 21:33:49 +00:00
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
if (!this.hasSubmitTarget) return;
|
|
|
|
this.formTarget.elements.forEach((p) =>
|
|
|
|
p.addEventListener("change", (e) => (this.submitTarget.disabled = false))
|
|
|
|
);
|
2021-06-01 21:33:49 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
async pay(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
2021-06-01 21:33:49 +00:00
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
this.formDisabled = true;
|
2021-06-01 21:33:49 +00:00
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
const payment_method_id = this.formTarget.elements.payment_method_id.value;
|
|
|
|
const orderToken = this.token;
|
|
|
|
const special_instructions = this.specialInstructionsTarget.value.trim();
|
2021-06-01 21:33:49 +00:00
|
|
|
|
|
|
|
// XXX: Currently SpreeClient expects us to send payment source
|
|
|
|
// attributes as if it were a credit card.
|
2021-11-22 17:51:50 +00:00
|
|
|
let response = await this.spree.checkout.orderUpdate(
|
|
|
|
{ orderToken },
|
2021-06-01 21:33:49 +00:00
|
|
|
{
|
2021-10-27 18:59:32 +00:00
|
|
|
order: {
|
|
|
|
special_instructions,
|
2021-11-22 17:51:50 +00:00
|
|
|
payments_attributes: [{ payment_method_id }],
|
2021-10-27 18:59:32 +00:00
|
|
|
payment_source: {
|
|
|
|
[payment_method_id]: {
|
2021-11-22 17:51:50 +00:00
|
|
|
name: "Pepitx",
|
2021-10-27 18:59:32 +00:00
|
|
|
month: 12,
|
2021-11-22 17:51:50 +00:00
|
|
|
year: 2020,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2021-06-01 21:33:49 +00:00
|
|
|
|
|
|
|
if (response.isFail()) {
|
2021-11-22 17:51:50 +00:00
|
|
|
this.handleFailure(response);
|
|
|
|
this.formDisabled = false;
|
|
|
|
return;
|
2021-06-01 21:33:49 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
this.cart = response;
|
2021-06-01 21:33:49 +00:00
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
response = await this.spree.checkout.complete({ orderToken });
|
2021-06-01 21:33:49 +00:00
|
|
|
|
|
|
|
if (response.isFail()) {
|
2021-11-22 17:51:50 +00:00
|
|
|
this.handleFailure(response);
|
|
|
|
this.formDisabled = false;
|
|
|
|
return;
|
2021-06-01 21:33:49 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
this.cart = response;
|
2021-06-01 21:33:49 +00:00
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
const checkoutUrls = await this.spree.sutty.getCheckoutURL({ orderToken });
|
|
|
|
let redirectUrl = this.data.get("nextUrl");
|
2021-06-01 21:33:49 +00:00
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
if (checkoutUrls.data.length > 0) redirectUrl = checkoutUrls.data[0];
|
2021-06-01 21:33:49 +00:00
|
|
|
|
2021-11-22 17:51:50 +00:00
|
|
|
window.location = redirectUrl;
|
2021-06-01 21:33:49 +00:00
|
|
|
}
|
|
|
|
}
|