@fireflysemantics/angular-stripe-service
Version:
Stripe service used to integrate Stripe Elements with Angular.
87 lines (82 loc) • 2.85 kB
JavaScript
import * as i0 from '@angular/core';
import { Injectable } from '@angular/core';
const STRIPE_API_URL = "https://js.stripe.com/v3/";
/**
* This service has a `stripe` property to that gets
* initialized to `window["Stripe"]`.
*
* The constructor calls `inject()` which will
* inject a script tag with containing the URL that loads
* stripe and return a `Promise<StripeFactory>`.
*
* The script tag will only load stripe if
* c is not available.
*
* If `window["Stripe"]` is available then `inject()` resolves
* the promise with that instance immediately, and does not create and
* wait for the script tag to load.
*
*
*/
class AngularStripeService {
constructor() {
// @ts-ignore
this._stripe = window['Stripe'];
this.stripePromise = this.inject();
}
get stripe() {
return this._stripe;
}
set stripe(s) {
this._stripe = s;
}
setPublishableKey(key, options) {
return this.stripePromise.then(() => {
return this.stripe(key, options);
});
}
inject() {
if (this.stripe) {
return Promise.resolve(this.stripe);
}
return new Promise((res, rej) => {
const head = this.getHeadElement();
const script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", STRIPE_API_URL);
head.appendChild(script);
script.addEventListener("load", () => {
// @ts-ignore
this.stripe = window["Stripe"];
res(this.stripe);
});
});
}
/**
* 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;
}
}
AngularStripeService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.0.2", ngImport: i0, type: AngularStripeService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
AngularStripeService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.0.2", ngImport: i0, type: AngularStripeService, providedIn: 'root' });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.0.2", ngImport: i0, type: AngularStripeService, decorators: [{
type: Injectable,
args: [{
providedIn: 'root'
}]
}], ctorParameters: function () { return []; } });
/*
* Public API Surface of angular-stripe-service
*/
/**
* Generated bundle index. Do not edit.
*/
export { AngularStripeService };
//# sourceMappingURL=fireflysemantics-angular-stripe-service.mjs.map