stripe-angular
Version:
Angular to Stripe module containing useful providers, components, and directives
355 lines (345 loc) • 11.8 kB
JavaScript
import * as i0 from '@angular/core';
import { InjectionToken, Injectable, Inject, EventEmitter, Component, Output, Input, ElementRef, NgModule } from '@angular/core';
import * as i1 from '@angular/common';
import { DOCUMENT, CommonModule } from '@angular/common';
const STRIPE_PUBLISHABLE_KEY = new InjectionToken('Stripe Publishable Key');
const STRIPE_OPTIONS = new InjectionToken('Stripe Options');
class StripeScriptTag {
constructor(document, key, options) {
this.document = document;
this.src = "https://js.stripe.com/v3/";
this.window = this.document.defaultView;
this.load = this.injectIntoHead();
if (key)
this.setPublishableKey(key, options);
}
promiseStripe() {
return this.load;
}
promiseInstance() {
return this.promiseStripe()
.then(stripe => {
if (!this.StripeInstance) {
const err = new Error("Stripe PublishableKey NOT SET. Use method StripeScriptTag.setPublishableKey()");
err["code"] = "STRIPEKEYNOTSET";
throw err;
//return Promise.reject( err )
}
return this.StripeInstance;
});
}
setPublishableKey(key, options) {
return this.load.then(() => this.StripeInstance = this.Stripe(key, options));
}
injectIntoHead() {
if (this.window && this.window["Stripe"]) {
return Promise.resolve(this.Stripe = this.window["Stripe"]);
}
return new Promise((res, rej) => {
const head = this.getTargetTagDropElement();
const script = this.document.createElement("script");
script.setAttribute("src", this.src);
script.setAttribute("type", "text/javascript");
script.addEventListener("load", () => {
this.Stripe = this.grabStripe();
res(this.Stripe);
});
head.appendChild(script);
});
}
grabStripe() {
return window["Stripe"];
}
getTargetTagDropElement() {
let elm = this.document.getElementsByTagName("head");
if (elm.length)
return elm[0];
return this.document.getElementsByTagName("body")[0];
}
}
StripeScriptTag.ɵprov = i0.ɵɵdefineInjectable({ factory: function StripeScriptTag_Factory() { return new StripeScriptTag(i0.ɵɵinject(i1.DOCUMENT), i0.ɵɵinject(STRIPE_PUBLISHABLE_KEY), i0.ɵɵinject(STRIPE_OPTIONS)); }, token: StripeScriptTag, providedIn: "root" });
StripeScriptTag.decorators = [
{ type: Injectable, args: [{ providedIn: 'root' },] }
];
StripeScriptTag.ctorParameters = () => [
{ type: undefined, decorators: [{ type: Inject, args: [DOCUMENT,] }] },
{ type: String, decorators: [{ type: Inject, args: [STRIPE_PUBLISHABLE_KEY,] }] },
{ type: undefined, decorators: [{ type: Inject, args: [STRIPE_OPTIONS,] }] }
];
class StripeComponent {
constructor(StripeScriptTag) {
this.StripeScriptTag = StripeScriptTag;
this.catcher = new EventEmitter();
this.invalidChange = new EventEmitter();
}
ngOnInit() {
this.init();
}
init() {
return this.StripeScriptTag.promiseInstance()
.then(i => this.stripe = i);
}
}
StripeComponent.decorators = [
{ type: Component, args: [{
selector: "stripe-component", template: ``
},] }
];
StripeComponent.ctorParameters = () => [
{ type: StripeScriptTag }
];
StripeComponent.propDecorators = {
catcher: [{ type: Output, args: ["catch",] }],
invalid: [{ type: Input }],
invalidChange: [{ type: Output }]
};
class StripeSource extends StripeComponent {
constructor(StripeScriptTag) {
super(StripeScriptTag);
this.StripeScriptTag = StripeScriptTag;
this.sourceChange = new EventEmitter();
this.paymentMethodChange = new EventEmitter();
}
createSource(extraData) {
delete this.invalid;
this.invalidChange.emit(this.invalid);
return this.stripe.createSource(this.elements, extraData)
.then((result) => this.processSourceResult(result));
}
processSourceResult(result) {
if (result.error) {
const rError = result.error;
if (rError.type === "validation_error") {
this.invalidChange.emit(this.invalid = rError);
}
else {
this.catcher.emit(rError);
throw rError;
}
}
const source = result.source;
if (source) {
this.sourceChange.emit(this.source = source);
return source;
}
}
createPaymentMethod(extraData) {
delete this.invalid;
this.invalidChange.emit(this.invalid);
return this.stripe.createPaymentMethod('card', this.elements, extraData)
.then((result) => this.processPaymentMethodResult(result));
}
processPaymentMethodResult(result) {
if (result.error) {
const rError = result.error;
if (rError.type === "validation_error") {
this.invalidChange.emit(this.invalid = rError);
}
else {
this.catcher.emit(rError);
throw rError;
}
}
const paymentMethod = result.paymentMethod;
if (paymentMethod) {
this.paymentMethodChange.emit(this.paymentMethod = paymentMethod);
return paymentMethod;
}
}
}
StripeSource.decorators = [
{ type: Component, args: [{
selector: "stripe-source",
template: `
<ng-container *ngIf="!StripeScriptTag.StripeInstance">
<div style="color:red;">Stripe PublishableKey NOT SET. Use method StripeScriptTag.setPublishableKey()</div>
</ng-container>
`,
exportAs: "StripeSource"
},] }
];
StripeSource.ctorParameters = () => [
{ type: StripeScriptTag }
];
StripeSource.propDecorators = {
source: [{ type: Input }],
sourceChange: [{ type: Output }],
paymentMethod: [{ type: Input }],
paymentMethodChange: [{ type: Output }]
};
class StripeCard extends StripeSource {
constructor(ElementRef, StripeScriptTag) {
super(StripeScriptTag);
this.ElementRef = ElementRef;
this.StripeScriptTag = StripeScriptTag;
this.tokenChange = new EventEmitter();
this.cardMounted = new EventEmitter();
this.complete = false;
this.completeChange = new EventEmitter();
this.changed = new EventEmitter();
this.drawn = false;
}
ngOnInit() {
super.init().then(() => this.redraw());
}
ngOnChanges(changes) {
if (this.drawn && (changes.options || changes.createOptions)) {
this.redraw();
}
}
redraw() {
if (this.drawn) {
this.elements.unmount();
this.elements.destroy();
}
this.elements = this.stripe.elements(this.createOptions).create('card', this.options);
this.elements.mount(this.ElementRef.nativeElement);
this.cardMounted.emit(this.elements);
this.elements.on('change', (result) => {
this.changed.emit(result);
if (result.complete || (this.complete && !result.complete)) {
this.completeChange.emit(this.complete = result.complete);
}
});
this.elements.addEventListener('change', (result) => {
if (result.error) {
this.invalidChange.emit(this.invalid = result.error);
}
});
this.drawn = true;
}
createToken(extraData) {
delete this.invalid;
this.invalidChange.emit(this.invalid);
return this.stripe.createToken(this.elements, extraData)
.then((result) => {
if (result.error) {
if (result.error.type == "validation_error") {
this.invalidChange.emit(this.invalid = result.error);
}
else {
this.catcher.emit(result.error);
throw result.error;
}
}
else {
this.tokenChange.emit(this.token = result.token);
return result.token;
}
});
}
}
StripeCard.decorators = [
{ type: Component, args: [{
selector: "stripe-card",
template: `
<ng-container *ngIf="!StripeScriptTag.StripeInstance">
<div style="color:red;">Stripe PublishableKey NOT SET. Use method StripeScriptTag.setPublishableKey()</div>
</ng-container>
`,
exportAs: "StripeCard"
},] }
];
StripeCard.ctorParameters = () => [
{ type: ElementRef },
{ type: StripeScriptTag }
];
StripeCard.propDecorators = {
createOptions: [{ type: Input }],
options: [{ type: Input }],
token: [{ type: Input }],
tokenChange: [{ type: Output }],
cardMounted: [{ type: Output }],
complete: [{ type: Input }],
completeChange: [{ type: Output }],
changed: [{ type: Output }]
};
class StripeBank extends StripeComponent {
constructor(StripeScriptTag) {
super(StripeScriptTag);
this.StripeScriptTag = StripeScriptTag;
this.tokenChange = new EventEmitter();
}
createToken(data) {
delete this.invalid;
this.invalidChange.emit(this.invalid);
return this.stripe.createToken('bank_account', data)
.then((result) => {
if (result.error) {
if (result.error.type == "validation_error") {
this.invalidChange.emit(this.invalid = result.error);
}
else {
this.catcher.emit(result.error);
throw result.error;
}
}
else {
this.tokenChange.emit(this.token = result.token);
return result.token;
}
});
}
}
StripeBank.decorators = [
{ type: Component, args: [{
selector: "stripe-bank",
template: `
<ng-container *ngIf="!StripeScriptTag.StripeInstance">
<div style="color:red;">Stripe PublishableKey NOT SET. Use method StripeScriptTag.setPublishableKey()</div>
</ng-container>
`,
exportAs: "StripeBank"
},] }
];
StripeBank.ctorParameters = () => [
{ type: StripeScriptTag }
];
StripeBank.propDecorators = {
options: [{ type: Input }],
token: [{ type: Input }],
tokenChange: [{ type: Output }]
};
const declarations = [
StripeComponent,
StripeSource,
StripeCard,
StripeBank
];
class StripeModule {
static forRoot(publishableKey, options) {
return {
ngModule: StripeModule,
providers: [
StripeScriptTag,
{
provide: STRIPE_PUBLISHABLE_KEY,
useValue: publishableKey
},
{
provide: STRIPE_OPTIONS,
useValue: options
}
],
};
}
}
StripeModule.decorators = [
{ type: NgModule, args: [{
imports: [
CommonModule
],
declarations,
// providers: [ StripeScriptTag ],
exports: [...declarations]
},] }
];
/**
* @deprecated Please import `StripeModule` directly
*/
const Module = StripeModule;
/**
* Generated bundle index. Do not edit.
*/
export { Module, STRIPE_OPTIONS, STRIPE_PUBLISHABLE_KEY, StripeBank, StripeCard, StripeModule, StripeScriptTag, StripeSource, StripeComponent as ɵa };
//# sourceMappingURL=stripe-angular.js.map