@vigilio/payments
Version:
Payments niubiz, paypal, etc.
358 lines • 15.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
function useNiubiz(auth) {
var message = "No se realizó el pago correctamente: ";
/**
*
* https://desarrolladores.niubiz.com.pe/docs/api-de-seguridad
*/
function onSecurity() {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var response, result;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, fetch("".concat(auth.sandbox, "/api.security/v1/security"), {
headers: {
Authorization: "Basic ".concat(btoa("".concat(auth.user, ":").concat(auth.password))),
},
})];
case 1:
response = _a.sent();
return [4 /*yield*/, response.text()];
case 2:
result = _a.sent();
return [2 /*return*/, result];
}
});
});
}
// https://desarrolladores.niubiz.com.pe/docs/api-de-autorizaci%C3%B3n
function onAuthorization(order) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var security, merchantId, response, result;
var _a;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, onSecurity()];
case 1:
security = _b.sent();
merchantId = auth.merchantId;
return [4 /*yield*/, fetch("".concat(auth.sandbox, "/api.authorization/v3/authorization/ecommerce/").concat(merchantId), {
method: "POST",
headers: {
Authorization: security,
"Content-Type": "application/json",
},
body: JSON.stringify(order),
})];
case 2:
response = _b.sent();
return [4 /*yield*/, response.json()];
case 3:
result = _b.sent();
if (!response.ok)
throw new Error((_a = result.errorMessage) !== null && _a !== void 0 ? _a : message);
return [2 /*return*/, result];
}
});
});
}
/**
*
* @param signature
* @returns
* https://desarrolladores.niubiz.com.pe/docs/api-de-anulaci%C3%B3n
*/
function onAnulacion(signature) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var security, response, result;
var _a;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, onSecurity()];
case 1:
security = _b.sent();
return [4 /*yield*/, fetch("".concat(auth.sandbox, "/api.authorization/v3/void/ecommerce/").concat(auth.merchantId, "/").concat(signature), {
method: "PUT",
headers: {
Authorization: security,
"Content-Type": "application/json",
},
})];
case 2:
response = _b.sent();
return [4 /*yield*/, response.json()];
case 3:
result = _b.sent();
if (!response.ok)
throw new Error((_a = result.errorMessage) !== null && _a !== void 0 ? _a : message);
return [2 /*return*/, result];
}
});
});
}
function onAntifraude(body) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var security, response, result;
var _a;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, onSecurity()];
case 1:
security = _b.sent();
return [4 /*yield*/, fetch("".concat(auth.sandbox, "/api.antifraud/v1/antifraud/ecommerce/").concat(auth.merchantId), {
method: "POST",
headers: {
Authorization: security,
"Content-Type": "application/json",
},
body: JSON.stringify(body),
})];
case 2:
response = _b.sent();
return [4 /*yield*/, response.json()];
case 3:
result = _b.sent();
if (!response.ok)
throw new Error((_a = result.errorMessage) !== null && _a !== void 0 ? _a : message);
return [2 /*return*/, result];
}
});
});
}
function onConsultaBin(bin) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var security, response, result;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, onSecurity()];
case 1:
security = _a.sent();
return [4 /*yield*/, fetch("".concat(auth.sandbox, "/api.ecommerce/v2/queryBin/").concat(bin), {
method: "GET",
headers: {
Authorization: security,
"Content-Type": "application/json",
},
})];
case 2:
response = _a.sent();
return [4 /*yield*/, response.json()];
case 3:
result = _a.sent();
return [2 /*return*/, result];
}
});
});
}
function onElegibilidadDCC(body) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var merchantId, security, response, result;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
merchantId = auth.merchantId;
return [4 /*yield*/, onSecurity()];
case 1:
security = _a.sent();
return [4 /*yield*/, fetch("".concat(auth.sandbox, "/api.ecommerce/v2/currency/conversion/").concat(merchantId), {
method: "POST",
body: JSON.stringify(body),
headers: {
Authorization: security,
"Content-Type": "application/json",
},
})];
case 2:
response = _a.sent();
return [4 /*yield*/, response.json()];
case 3:
result = _a.sent();
return [2 /*return*/, result];
}
});
});
}
function onConfirmacion(order) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var security, response, result;
var _a;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, onSecurity()];
case 1:
security = _b.sent();
return [4 /*yield*/, fetch("".concat(auth.sandbox, "/api.confirmation/v1/confirmation/ecommerce/").concat(auth.merchantId), {
method: "POST",
headers: {
Authorization: security,
"Content-Type": "application/json",
},
body: JSON.stringify(order),
})];
case 2:
response = _b.sent();
return [4 /*yield*/, response.json()];
case 3:
result = _b.sent();
if (!response.ok)
throw new Error((_a = result.errorMessage) !== null && _a !== void 0 ? _a : message);
return [2 /*return*/, result];
}
});
});
}
function getIp() {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var ipResponse, ipResult;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, fetch("https://api.ipify.org")];
case 1:
ipResponse = _a.sent();
return [4 /*yield*/, ipResponse.text()];
case 2:
ipResult = _a.sent();
return [2 /*return*/, ipResult];
}
});
});
}
/**
*
* @param order
* https://desarrolladores.niubiz.com.pe/docs/pago-con-yape
*/
function onYape(order) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var security, response, result;
var _a;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, onSecurity()];
case 1:
security = _b.sent();
return [4 /*yield*/, fetch("".concat(auth.sandbox, "/api.yape/v2/yape/transaction/").concat(auth.merchantId), {
method: "POST",
headers: {
Authorization: security,
"Content-Type": "application/json",
},
body: JSON.stringify(order),
})];
case 2:
response = _b.sent();
return [4 /*yield*/, response.json()];
case 3:
result = _b.sent();
if (!response.ok)
throw new Error((_a = result.responseMessage) !== null && _a !== void 0 ? _a : message);
return [2 /*return*/, result];
}
});
});
}
/**
*
* @param order
* https://desarrolladores.niubiz.com.pe/docs/pago-con-pagoefectivo
*/
function onPagoEfectivo(order) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var security, response, result;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, onSecurity()];
case 1:
security = _a.sent();
return [4 /*yield*/, fetch("".concat(auth.sandbox, "/api.pagoefectivo/v1/create/").concat(auth.merchantId), {
method: "POST",
headers: {
Authorization: security,
"Content-Type": "application/json",
},
body: JSON.stringify(order),
})];
case 2:
response = _a.sent();
return [4 /*yield*/, response.json()];
case 3:
result = _a.sent();
return [2 /*return*/, result];
}
});
});
}
/**
*
* @param order
* https://desarrolladores.niubiz.com.pe/docs/callback-pago-efectivo
*/
function onCallBackPagoEfectivo(order) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var security, response, result;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, onSecurity()];
case 1:
security = _a.sent();
return [4 /*yield*/, fetch("${auth.sandbox}/api.pagoefectivocallback/v1/callback", {
method: "POST",
headers: {
Authorization: security,
"Content-Type": "application/json",
},
body: JSON.stringify(order),
})];
case 2:
response = _a.sent();
return [4 /*yield*/, response.json()];
case 3:
result = _a.sent();
return [2 /*return*/, result];
}
});
});
}
function onCertificadoApp() {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var security, response, result;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, onSecurity()];
case 1:
security = _a.sent();
return [4 /*yield*/, fetch("".concat(auth.sandbox, "/api.certificate/v1/query/").concat(auth.merchantId), {
method: "POST",
headers: {
Authorization: security,
"Content-Type": "application/json",
},
})];
case 2:
response = _a.sent();
return [4 /*yield*/, response.json()];
case 3:
result = _a.sent();
return [2 /*return*/, result];
}
});
});
}
return {
onAuthorization: onAuthorization,
onYape: onYape,
onConsultaBin: onConsultaBin,
onElegibilidadDCC: onElegibilidadDCC,
onPagoEfectivo: onPagoEfectivo,
onCallBackPagoEfectivo: onCallBackPagoEfectivo,
onAnulacion: onAnulacion,
onConfirmacion: onConfirmacion,
onAntifraude: onAntifraude,
onCertificadoApp: onCertificadoApp,
getIp: getIp,
};
}
exports.default = useNiubiz;
//# sourceMappingURL=niubiz.js.map