@nkhind/vivawallet-sdk
Version:
Non-Official VivaWallet API SDK
155 lines (154 loc) • 6.2 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSmartCheckout = void 0;
const VivaAuth_class_1 = __importDefault(require("../vivabases/VivaAuth.class"));
const functions_1 = require("../utils/functions");
const VivaEndpoints_class_1 = __importDefault(require("../vivabases/VivaEndpoints.class"));
class Vivawallet extends VivaAuth_class_1.default {
constructor(datas) {
super(datas);
}
/** ------------------ SOURCE CODE ------------------ */
/** Set the Viva Wallet payment source (needed for Transaction integrations), return `true` if setup is OK, `false` if the payment already exist or on error */
async setVivawalletSource(data) {
if (!this.merchantId || !this.apikey)
throw new Error('Init not called');
if (!this.sourceCode && !data.sourceCode)
throw new Error('Source code is required');
if (!data.sourceCode && this.sourceCode)
data.sourceCode = this.sourceCode;
try {
await (0, functions_1.requests)(this.endpoints.source.url, this.endpoints.source.method, {
Authorization: 'Basic ' + this.getVivaBasicToken(),
}, data);
return true;
}
catch (e) {
// if the source already exist
if (e.status === 409)
return true;
}
return false;
}
/** ------------------------------------------------- */
/** ------------------ TRANSACTION ------------------ */
/** Return the transaction if exist, or `null` if error/not exist */
async getTransactionById(transactionId) {
if (!this.vivaTotken)
throw new Error('Init not called');
try {
const r = await (0, functions_1.requests)(this.endpoints.transaction.get.url.replace('{transactionId}', transactionId), this.endpoints.transaction.get.method, {
Authorization: 'Bearer ' + this.vivaTotken,
});
if (r.data)
return r.data;
}
catch (e) {
console.log(e);
}
return null;
}
/** Make transaction by transactionId */
async makeTransaction(options) {
if (!this.merchantId || !this.apikey)
throw new Error('Init not called');
if (!options.amount || !options.id)
return null;
try {
const transactionUrl = this.endpoints.transaction.create.url.replace('{transaction_id}', options.id);
const r = await (0, functions_1.requests)(transactionUrl, this.endpoints.transaction.create.method, {
Authorization: 'Bearer ' + this.getVivaBasicToken(),
}, options);
// console.log("R", r);
if (r.data && r.data.Success && r.data.StatusId === 'F')
return r.data;
}
catch (e) {
console.log(e);
}
return null;
}
/** Allow refund transaction, return `true` if the transaction be refund succefuly */
async refundTransaction(transactionId, refundOptions) {
if (!this.vivaTotken)
throw new Error('Init not called');
const queries = Object.keys(refundOptions)
.map((key) => {
const k = key;
return (key +
'=' +
encodeURIComponent(refundOptions[k]));
})
.join('&');
try {
const r = await (0, functions_1.requests)(this.endpoints.transaction.cancel.url.replace('{transactionId}', transactionId) +
'?' +
queries, this.endpoints.transaction.cancel.method, {
Authorization: 'Bearer ' + this.vivaTotken,
});
if (r.data && r.data.Success)
return true;
}
catch (e) {
console.log(e);
}
return false;
}
/** ------------------------------------------------- */
/** -------------------- PAYMENT -------------------- */
/** Make new VivaWallet order, return `orderCode` */
async createOrder(orderData) {
if (!this.vivaTotken)
throw new Error('Init not called');
try {
const r = await (0, functions_1.requests)(this.endpoints.payment.create.url, this.endpoints.payment.create.method, {
Authorization: 'Bearer ' + this.vivaTotken,
}, orderData);
if (r.data && r.data.orderCode)
return r.data.orderCode;
}
catch (e) {
console.log(e);
}
return null;
}
/** Allow cancel operation on non-validate orders, return `true` if the order canceled succefuly */
async cancelOrder(orderCode) {
if (!this.merchantId || !this.apikey)
throw new Error('Init not called');
if (!orderCode)
return false;
try {
const cancelUrl = this.endpoints.payment.cancel.url.replace('{orderCode}', orderCode);
const r = await (0, functions_1.requests)(cancelUrl, this.endpoints.payment.cancel.method, {
Authorization: 'Bearer ' + this.getVivaBasicToken(),
});
// console.log("R", r);
if (r.data && (r.data.Success || r.data.ErrorCode === 404))
return true;
}
catch (e) {
console.log(e);
if (e.status === 404)
return true;
}
return false;
}
}
/** Return the smart checkout url with the `orderCode` */
function getSmartCheckout(options) {
const endpoints = options.dev
? VivaEndpoints_class_1.default.demoEndpoints
: VivaEndpoints_class_1.default.prodEndpoints;
let res = endpoints.checkout.url.replace('{orderCode}', options.orderCode);
if (options.color)
res += '&color=' + options.color;
if (options.paymentMethod)
res += '&paymentMethod=' + options.paymentMethod;
return res;
}
exports.getSmartCheckout = getSmartCheckout;
exports.default = Vivawallet;