sutty-base-jekyll-theme/_packs/controllers/cart_paypal_confirmation_controller.js

61 lines
1.4 KiB
JavaScript
Raw Normal View History

2021-11-22 17:51:50 +00:00
import { CartBaseController } from "./cart_base_controller";
2021-06-01 21:33:49 +00:00
/*
* Replaces checkout.js.
*
* When the customer is redirected back from the approval URL, there's
* three params attached to the URL. We need paymentId and PayerID to
* execute the payment and later capture it via IPN. The token can be
* discarded.
*/
export default class extends CartBaseController {
2021-11-22 17:51:50 +00:00
async connect() {
if (this.params.PayerID === undefined) return;
2021-06-01 21:33:49 +00:00
2021-11-22 17:51:50 +00:00
this.site = window.site;
this.element.innerHTML = this.site.i18n.cart.layouts.paypal.confirming;
2021-06-01 21:33:49 +00:00
fetch(this.executeURL)
2021-11-22 17:51:50 +00:00
.then(
(r) =>
(this.element.innerHTML =
this.site.i18n.cart.layouts.paypal[r.ok ? "confirmed" : "failure"])
)
.catch(
(e) =>
(this.element.innerHTML = this.site.i18n.cart.layouts.paypal.failure)
);
2021-06-01 21:33:49 +00:00
}
/*
* Convert URL params to Object
*
* @return [Object]
*/
2021-11-22 17:51:50 +00:00
get params() {
if (this._params) return this._params;
2021-06-01 21:33:49 +00:00
2021-11-22 17:51:50 +00:00
this._params = Object.fromEntries(
decodeURIComponent(window.location.search.replace("?", ""))
.split("&")
.map((x) => x.split("="))
);
2021-06-01 21:33:49 +00:00
2021-11-22 17:51:50 +00:00
return this._params;
2021-06-01 21:33:49 +00:00
}
/*
* URL to contact the store and execute the payment.
*/
2021-11-22 17:51:50 +00:00
get executeURL() {
return [
window.spree.host,
"paypal",
"execute",
this.params.orderId,
this.params.paymentId,
this.params.PayerID,
].join("/");
2021-06-01 21:33:49 +00:00
}
}