ngx-niubiz
Version:
Pasarela de pago Niubiz.
259 lines (251 loc) • 11.1 kB
JavaScript
import * as i0 from '@angular/core';
import { Injectable, Inject, EventEmitter, Component, Input, Output } from '@angular/core';
import { DOCUMENT } from '@angular/common';
import { lastValueFrom, of, catchError } from 'rxjs';
import axios from 'axios';
import * as i1 from '@angular/common/http';
import { HttpClientModule } from '@angular/common/http';
class Payment {
constructor(amount, callback) {
this.amount = amount;
this.callback = callback;
}
}
class NgxNiubizService {
constructor(_http) {
this._http = _http;
this.authorization = '';
this.developerMode = true;
}
getIp() {
const apiUrl = 'https://api.ipify.org?format=json';
return lastValueFrom(this._http.get(apiUrl));
}
getToken(url, webpay, authorization = '', developerMode = true) {
this.authorization = authorization;
var headers = new axios.AxiosHeaders({
'Content-Type': 'application/json'
});
if (authorization != '') {
headers.setAuthorization(authorization, true);
}
return axios.post(url, webpay, {
headers: headers,
}).then(response => {
if (developerMode) {
console.log('Token Session: ', response);
}
return response;
}).catch(error => {
return error;
});
}
getAuthorization(config, params) {
console.log(config);
console.log(params);
var headers = new axios.AxiosHeaders({
'Content-Type': 'application/json'
});
if (this.authorization != '') {
headers.setAuthorization(this.authorization, true);
}
var webpay = {
channel: params.channel,
order: {
tokenId: params.token,
purchaseNumber: config.purchasenumber,
amount: config.amount,
currency: 'PEN'
}
};
return axios.post(config.action, webpay, {
headers: headers,
}).then(response => {
if (this.developerMode) {
console.log('Authorization : ', response);
}
return response;
}).catch(error => {
return error;
});
}
handleError(operation = 'operation', result) {
return (error) => {
console.error(`${operation} failed: ${error.message}`);
return of(result);
};
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: NgxNiubizService, deps: [{ token: i1.HttpClient }], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: NgxNiubizService, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: NgxNiubizService, decorators: [{
type: Injectable,
args: [{
providedIn: 'root'
}]
}], ctorParameters: () => [{ type: i1.HttpClient }] });
class ScriptLoaderService {
constructor(niubizService, document) {
this.niubizService = niubizService;
this.document = document;
}
loadScript(developerMode = true) {
const apiNiubizPRD = 'https://static-content.vnforapps.com/v2/js/checkout.js';
const apiNiubizTest = 'https://static-content-qas.vnforapps.com/v2/js/checkout.js?qa=true';
return new Promise((resolve, reject) => {
const script = this.document.createElement('script');
script.src = developerMode ? apiNiubizTest : apiNiubizPRD;
script.onload = () => resolve();
script.onerror = () => reject(new Error(`Failed to load script ${developerMode ? apiNiubizTest : apiNiubizPRD}`));
this.document.head.appendChild(script);
});
}
async initializeCheckout(config, onSuccess, onFailed, developerMode = true) {
try {
await this.loadScript(developerMode).catch(error => catchError(error));
if (typeof window.VisanetCheckout !== 'undefined') {
window.VisanetCheckout.configure(config);
window.VisanetCheckout.open();
window.VisanetCheckout.configuration.complete = (params) => this.procesar(config, params, onSuccess, onFailed);
}
else {
console.error('El script de Visanet no se ha cargado correctamente');
}
}
catch (error) {
console.error('Error al cargar el script de Visanet:', error);
}
return Promise.resolve();
}
procesar(config, params, onSuccess, onFailed) {
this.niubizService.getAuthorization(config, params).then(async (response) => {
console.log('Authorization: ', response);
if (response.data.isSuccess) {
onSuccess.emit();
}
else {
onFailed.emit();
}
});
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ScriptLoaderService, deps: [{ token: NgxNiubizService }, { token: DOCUMENT }], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ScriptLoaderService, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ScriptLoaderService, decorators: [{
type: Injectable,
args: [{
providedIn: 'root'
}]
}], ctorParameters: () => [{ type: NgxNiubizService }, { type: Document, decorators: [{
type: Inject,
args: [DOCUMENT]
}] }] });
class NgxNiubizComponent {
constructor(scriptLoader, niubizService) {
this.scriptLoader = scriptLoader;
this.niubizService = niubizService;
this.authentication = '';
this.channel = 'web';
this.developerMode = false;
this.expirationminutes = '20';
this.urltimeouturl = 'about:blank';
this.merchantlogo = 'img/comercio.png';
this.formbuttoncolor = '#000000';
this.onSuccess = new EventEmitter();
this.onFailed = new EventEmitter();
this.sessionToken = '';
}
ngOnInit() {
this.niubizService.getIp().then((data) => {
if (data.ip != null) {
this.webpay = {
'merchantId': this.merchantId,
'channel': this.channel,
'amount': this.amount,
'ip': data.ip,
'dataMap': this.dataMap,
};
this.niubizService.getToken(this.urlToken, this.webpay, this.authentication, this.developerMode).then(async (response) => {
if (this.developerMode) {
console.log('Token Session: ', response);
}
if (response.status == 200) {
var scriptInit = {
sessiontoken: response.data?.data?.sessionKey,
channel: 'web',
merchantid: this.merchantId,
purchasenumber: this.purchasenumber,
amount: this.amount,
expirationminutes: this.expirationminutes,
timeouturl: this.redirect_url,
merchantlogo: this.merchantlogo,
formbuttoncolor: this.formbuttoncolor,
action: this.urlAuthorization,
complete: function (params) {
alert(JSON.stringify(params));
}
};
await this.scriptLoader.initializeCheckout(scriptInit, this.onSuccess, this.onFailed, this.developerMode);
}
});
}
});
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: NgxNiubizComponent, deps: [{ token: ScriptLoaderService }, { token: NgxNiubizService }], target: i0.ɵɵFactoryTarget.Component }); }
static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "17.3.12", type: NgxNiubizComponent, isStandalone: true, selector: "ngx-niubiz", inputs: { urlToken: "urlToken", urlAuthorization: "urlAuthorization", authentication: "authentication", channel: "channel", merchantId: "merchantId", amount: "amount", dataMap: "dataMap", developerMode: "developerMode", purchasenumber: "purchasenumber", expirationminutes: "expirationminutes", urltimeouturl: "urltimeouturl", merchantlogo: "merchantlogo", formbuttoncolor: "formbuttoncolor", redirect_url: "redirect_url" }, outputs: { onSuccess: "onSuccess", onFailed: "onFailed" }, providers: [
NgxNiubizService,
ScriptLoaderService,
], ngImport: i0, template: `
`, isInline: true, styles: [""], dependencies: [{ kind: "ngmodule", type: HttpClientModule }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: NgxNiubizComponent, decorators: [{
type: Component,
args: [{ selector: 'ngx-niubiz', standalone: true, imports: [
HttpClientModule
], providers: [
NgxNiubizService,
ScriptLoaderService,
], template: `
` }]
}], ctorParameters: () => [{ type: ScriptLoaderService }, { type: NgxNiubizService }], propDecorators: { urlToken: [{
type: Input
}], urlAuthorization: [{
type: Input
}], authentication: [{
type: Input
}], channel: [{
type: Input
}], merchantId: [{
type: Input
}], amount: [{
type: Input
}], dataMap: [{
type: Input
}], developerMode: [{
type: Input
}], purchasenumber: [{
type: Input
}], expirationminutes: [{
type: Input
}], urltimeouturl: [{
type: Input
}], merchantlogo: [{
type: Input
}], formbuttoncolor: [{
type: Input
}], redirect_url: [{
type: Input
}], onSuccess: [{
type: Output
}], onFailed: [{
type: Output
}] } });
/*
* Public API Surface of ngx-niubiz
*/
// export * from './lib/ngx-niubiz.service';
/**
* Generated bundle index. Do not edit.
*/
export { NgxNiubizComponent, Payment };
//# sourceMappingURL=ngx-niubiz.mjs.map