@givtnl/angular-wepay-service
Version:
WePay service used to integrate WePay Elements with Angular. Forked from (https://github.com/fireflysemantics/angular-stripe-service).
85 lines (80 loc) • 2.56 kB
JavaScript
import { ɵɵdefineInjectable, Injectable } from '@angular/core';
const WEPAY_API_URL = "https://cdn.wepay.com/wepay.min.js";
/**
* This service has a `wepay` property to that gets
* initialized to `window["WePay"]`.
*
* The constructor calls `inject()` which will
* inject a script tag with containing the URL that loads
* wepay and return a `Promise<WePayFactory>`.
*
* The script tag will only load wepay if
* it is not available.
*
* If `window["WePay"]` is available then `inject()` resolves
* the promise with that instance immediately, and does not create and
* wait for the script tag to load.
*
*
*/
class AngularWePayService {
constructor() {
// @ts-ignore
this._wepay = window['WePay'];
this.wePayPromise = this.inject();
}
get wepay() {
return this._wepay;
}
set wepay(s) {
this._wepay = s;
}
create() {
return this.wePayPromise.then(() => {
return this.wepay;
});
}
inject() {
if (this.wepay) {
return Promise.resolve(this.wepay);
}
return new Promise((res, rej) => {
const head = this.getHeadElement();
const script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", WEPAY_API_URL);
head.appendChild(script);
script.addEventListener("load", () => {
// @ts-ignore
this.wepay = window["WePay"];
res(this.wepay);
});
});
}
/**
* Returns the `head` element.
* @throws Error('Application does not have a head element');
*/
getHeadElement() {
let elm = document.getElementsByTagName("head")[0];
if (!elm) {
throw new Error('Application does not have a head element');
}
return elm;
}
}
AngularWePayService.ɵprov = ɵɵdefineInjectable({ factory: function AngularWePayService_Factory() { return new AngularWePayService(); }, token: AngularWePayService, providedIn: "root" });
AngularWePayService.decorators = [
{ type: Injectable, args: [{
providedIn: 'root'
},] }
];
AngularWePayService.ctorParameters = () => [];
/*
* Public API Surface of angular-wepay-service
*/
/**
* Generated bundle index. Do not edit.
*/
export { AngularWePayService };
//# sourceMappingURL=givtnl-angular-wepay-service.js.map