@givtnl/angular-wepay-service
Version:
WePay service used to integrate WePay Elements with Angular. Forked from (https://github.com/fireflysemantics/angular-stripe-service).
100 lines (93 loc) • 3.91 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core')) :
typeof define === 'function' && define.amd ? define('@givtnl/angular-wepay-service', ['exports', '@angular/core'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.givtnl = global.givtnl || {}, global.givtnl['angular-wepay-service'] = {}), global.ng.core));
}(this, (function (exports, i0) { 'use strict';
var 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.
*
*
*/
var AngularWePayService = /** @class */ (function () {
function AngularWePayService() {
// @ts-ignore
this._wepay = window['WePay'];
this.wePayPromise = this.inject();
}
Object.defineProperty(AngularWePayService.prototype, "wepay", {
get: function () {
return this._wepay;
},
set: function (s) {
this._wepay = s;
},
enumerable: false,
configurable: true
});
AngularWePayService.prototype.create = function () {
var _this = this;
return this.wePayPromise.then(function () {
return _this.wepay;
});
};
AngularWePayService.prototype.inject = function () {
var _this = this;
if (this.wepay) {
return Promise.resolve(this.wepay);
}
return new Promise(function (res, rej) {
var head = _this.getHeadElement();
var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", WEPAY_API_URL);
head.appendChild(script);
script.addEventListener("load", function () {
// @ts-ignore
_this.wepay = window["WePay"];
res(_this.wepay);
});
});
};
/**
* Returns the `head` element.
* @throws Error('Application does not have a head element');
*/
AngularWePayService.prototype.getHeadElement = function () {
var elm = document.getElementsByTagName("head")[0];
if (!elm) {
throw new Error('Application does not have a head element');
}
return elm;
};
return AngularWePayService;
}());
AngularWePayService.ɵprov = i0.ɵɵdefineInjectable({ factory: function AngularWePayService_Factory() { return new AngularWePayService(); }, token: AngularWePayService, providedIn: "root" });
AngularWePayService.decorators = [
{ type: i0.Injectable, args: [{
providedIn: 'root'
},] }
];
AngularWePayService.ctorParameters = function () { return []; };
/*
* Public API Surface of angular-wepay-service
*/
/**
* Generated bundle index. Do not edit.
*/
exports.AngularWePayService = AngularWePayService;
Object.defineProperty(exports, '__esModule', { value: true });
})));
//# sourceMappingURL=givtnl-angular-wepay-service.umd.js.map