angular-rave
Version:
Angular wrapper for integrating rave transactions
268 lines (259 loc) • 9.31 kB
JavaScript
import { InjectionToken, ɵɵdefineInjectable, ɵɵinject, Injectable, Inject, EventEmitter, Component, Input, Output, Directive, HostListener, NgModule } from '@angular/core';
import { __awaiter } from 'tslib';
import { CommonModule } from '@angular/common';
const PUBKEY_TOKEN = new InjectionToken('Rave.PubKey');
const CHECKOUT_SCRIPT_URL = 'https://checkout.flutterwave.com/v3.js';
class AngularRaveService {
constructor(publicKey) {
this.publicKey = publicKey;
}
createRaveOptionsObject(obj, callback, onclose) {
const raveOptions = {};
raveOptions.amount = obj.amount;
raveOptions.public_key = obj.public_key || this.publicKey;
raveOptions.currency = obj.currency || 'NGN';
if (obj.customer) {
raveOptions.customer = obj.customer;
}
if (obj.customizations) {
raveOptions.customizations = obj.customizations;
}
if (obj.integrity_hash) {
raveOptions.integrity_hash = obj.integrity_hash;
}
if (obj.meta) {
raveOptions.meta = obj.meta;
}
if (obj.paymentOptions && obj.paymentOptions.length) {
raveOptions.payment_options = obj.paymentOptions.join(', ');
}
if (obj.payment_plan) {
raveOptions.payment_plan = obj.payment_plan;
}
if (obj.redirect_url) {
raveOptions.redirect_url = obj.redirect_url;
}
if (obj.subaccounts) {
raveOptions.subaccounts = obj.subaccounts;
}
if (obj.tx_ref) {
raveOptions.tx_ref = obj.tx_ref;
}
raveOptions.callback = callback;
raveOptions.onclose = onclose;
return raveOptions;
}
loadScript() {
return new Promise(resolve => {
if (typeof window.FlutterwaveCheckout === 'function') {
resolve();
return;
}
const script = window.document.createElement('script');
window.document.head.appendChild(script);
const onLoadFunc = () => {
script.removeEventListener('load', onLoadFunc);
resolve();
};
script.addEventListener('load', onLoadFunc);
const url = CHECKOUT_SCRIPT_URL;
script.setAttribute('src', url);
});
}
isInvalidOptions(obj) {
console.log('Transaction Reference', obj.tx_ref);
if (!obj.public_key && !this.publicKey) {
return 'ANGULAR-RAVE: Merchant public key is required';
}
if (!obj.tx_ref) {
return 'ANGULAR-RAVE: A unique transaction reference is required';
}
if (!obj.amount) {
return 'ANGULAR-RAVE: Amount to charge is required';
}
if (!obj.customer.email || !obj.customer.phonenumber) {
return 'ANGULAR-RAVE: Customer email or phone number is required';
}
return '';
}
checkout(options) {
return window.FlutterwaveCheckout(options);
}
}
AngularRaveService.ɵprov = ɵɵdefineInjectable({ factory: function AngularRaveService_Factory() { return new AngularRaveService(ɵɵinject(PUBKEY_TOKEN)); }, token: AngularRaveService, providedIn: "root" });
AngularRaveService.decorators = [
{ type: Injectable, args: [{
providedIn: 'root',
},] }
];
AngularRaveService.ctorParameters = () => [
{ type: String, decorators: [{ type: Inject, args: [PUBKEY_TOKEN,] }] }
];
class AngularRaveComponent {
constructor(raveService) {
this.raveService = raveService;
this.onclose = new EventEmitter();
this.callback = new EventEmitter();
this.init = new EventEmitter();
}
pay() {
return __awaiter(this, void 0, void 0, function* () {
let raveOptionsError = '';
if (this.raveOptions && Object.keys(this.raveOptions).length > 1) {
raveOptionsError = this.raveService.isInvalidOptions(this.raveOptions);
this.insertRaveOptions(this.raveOptions);
}
else {
raveOptionsError = this.raveService.isInvalidOptions(this);
this.insertRaveOptions(this);
}
if (!!raveOptionsError) {
console.error(raveOptionsError);
return raveOptionsError;
}
yield this.raveService.loadScript();
this.raveService.checkout(this._raveOptions);
if (this.init.observers.length > 0) {
this.init.emit();
}
});
}
insertRaveOptions(object) {
const onclose = () => {
if (this.onclose.observers.length) {
this.onclose.emit();
}
};
const callback = (res) => {
this.callback.emit(res);
};
this._raveOptions = this.raveService.createRaveOptionsObject(object, callback, onclose);
}
ngOnInit() {
this.pay();
}
}
AngularRaveComponent.decorators = [
{ type: Component, args: [{
selector: 'angular-rave',
template: `<ng-content></ng-content>`
},] }
];
AngularRaveComponent.ctorParameters = () => [
{ type: AngularRaveService }
];
AngularRaveComponent.propDecorators = {
amount: [{ type: Input }],
public_key: [{ type: Input }],
currency: [{ type: Input }],
customer: [{ type: Input }],
customizations: [{ type: Input }],
integrity_hash: [{ type: Input }],
meta: [{ type: Input }],
paymentOptions: [{ type: Input }],
payment_plan: [{ type: Input }],
redirect_url: [{ type: Input }],
subaccounts: [{ type: Input }],
tx_ref: [{ type: Input }],
raveOptions: [{ type: Input }],
onclose: [{ type: Output }],
callback: [{ type: Output }],
init: [{ type: Output }]
};
class AngularRaveDirective {
constructor(raveService) {
this.raveService = raveService;
this.onclose = new EventEmitter();
this.callback = new EventEmitter();
this.init = new EventEmitter();
}
buttonClick() {
this.pay();
}
pay() {
return __awaiter(this, void 0, void 0, function* () {
let raveOptionsError = '';
if (this.raveOptions && Object.keys(this.raveOptions).length > 1) {
raveOptionsError = this.raveService.isInvalidOptions(this.raveOptions);
this.insertRaveOptions(this.raveOptions);
}
else {
raveOptionsError = this.raveService.isInvalidOptions(this);
this.insertRaveOptions(this);
}
if (!!raveOptionsError) {
console.error(raveOptionsError);
return;
}
yield this.raveService.loadScript();
this.raveService.checkout(this._raveOptions);
if (this.init.observers.length > 0) {
this.init.emit();
}
});
}
insertRaveOptions(object) {
const onclose = () => {
if (this.onclose.observers.length) {
this.onclose.emit();
}
};
const callback = (res) => {
this.callback.emit(res);
};
this._raveOptions = this.raveService.createRaveOptionsObject(object, callback, onclose);
}
}
AngularRaveDirective.decorators = [
{ type: Directive, args: [{
selector: '[angular-rave]',
},] }
];
AngularRaveDirective.ctorParameters = () => [
{ type: AngularRaveService }
];
AngularRaveDirective.propDecorators = {
amount: [{ type: Input }],
public_key: [{ type: Input }],
currency: [{ type: Input }],
customer: [{ type: Input }],
customizations: [{ type: Input }],
integrity_hash: [{ type: Input }],
meta: [{ type: Input }],
paymentOptions: [{ type: Input }],
payment_plan: [{ type: Input }],
redirect_url: [{ type: Input }],
subaccounts: [{ type: Input }],
tx_ref: [{ type: Input }],
raveOptions: [{ type: Input }],
onclose: [{ type: Output }],
callback: [{ type: Output }],
init: [{ type: Output }],
buttonClick: [{ type: HostListener, args: ['click',] }]
};
class AngularRaveModule {
static forRoot(publicKey) {
return {
ngModule: AngularRaveModule,
providers: [
AngularRaveService,
{ provide: PUBKEY_TOKEN, useValue: publicKey },
]
};
}
}
AngularRaveModule.decorators = [
{ type: NgModule, args: [{
imports: [CommonModule],
declarations: [AngularRaveDirective, AngularRaveComponent],
exports: [AngularRaveDirective, AngularRaveComponent],
},] }
];
/*
* Public API Surface of angular-rave
*/
/**
* Generated bundle index. Do not edit.
*/
export { AngularRaveComponent, AngularRaveDirective, AngularRaveModule, AngularRaveService, PUBKEY_TOKEN as ɵa };
//# sourceMappingURL=angular-rave.js.map