mn-payment
Version:
369 lines (364 loc) • 11.4 kB
JavaScript
"use strict";
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
// index.ts
var mn_payment_exports = {};
__export(mn_payment_exports, {
Qpay: () => Qpay,
StorePay: () => StorePay,
UbPay: () => UbPay
});
module.exports = __toCommonJS(mn_payment_exports);
// payments/qpay.ts
var Qpay = class {
constructor(username, password, invoiceCode, saveToken) {
this.baseUrl = "https://merchant.qpay.mn";
this.username = username;
this.password = password;
this.invoiceCode = invoiceCode;
this.saveToken = saveToken;
}
login() {
return __async(this, null, function* () {
const req = yield fetch(`${this.baseUrl}/v2/auth/token`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Basic ${btoa(`${this.username}:${this.password}`)}`
}
});
const res = yield req.json();
if (res.error) {
throw new Error(res.message);
}
const response = res;
return {
access_token: response.access_token,
refresh_token: response.refresh_token,
expires_in: response.expires_in,
refresh_expires_in: response.refresh_expires_in
};
});
}
getRefreshToken(refreshToken) {
return __async(this, null, function* () {
const req = yield fetch(`${this.baseUrl}/v2/auth/refresh`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${refreshToken}`
}
});
const res = yield req.json();
if (res.error) {
throw new Error(res.message);
}
const response = res;
return {
access_token: response.access_token,
refresh_token: response.refresh_token,
expires_in: response.expires_in,
refresh_expires_in: response.refresh_expires_in
};
});
}
checkToken(inputToken) {
return __async(this, null, function* () {
let token = inputToken;
if (!token || !token.refresh_expires_in) {
token = yield this.login();
}
if (token.refresh_expires_in && new Date(token.refresh_expires_in * 1e3) <= /* @__PURE__ */ new Date()) {
token = yield this.login();
} else if (new Date(token.expires_in * 1e3) <= /* @__PURE__ */ new Date()) {
token = yield this.getRefreshToken(token.refresh_token);
}
if (this.saveToken && JSON.stringify(inputToken) !== JSON.stringify(token)) {
yield this.saveToken(token);
}
return token;
});
}
createInvoice(input, token) {
return __async(this, null, function* () {
token = yield this.checkToken(token);
const req = yield fetch(`${this.baseUrl}/v2/invoice`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token.access_token}`
},
body: JSON.stringify(__spreadProps(__spreadValues({}, input), {
invoice_code: this.invoiceCode
}))
});
const res = yield req.json();
return {
data: res,
token
};
});
}
checkInvoice(invoiceId, token) {
return __async(this, null, function* () {
token = yield this.checkToken(token);
const req = yield fetch(`${this.baseUrl}/v2/payment/check`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token.access_token}`
},
body: JSON.stringify({
object_type: "INVOICE",
object_id: invoiceId
})
});
const res = yield req.json();
return {
data: res,
token
};
});
}
};
// payments/storepay.ts
var StorePay = class {
constructor(username, password, appUsername, appPassword, saveToken) {
this.baseUrl = "https://service.storepay.mn:8778";
this.username = username;
this.password = password;
this.appUsername = appUsername;
this.appPassword = appPassword;
this.saveToken = saveToken;
}
login() {
return __async(this, null, function* () {
const req = yield fetch(`${this.baseUrl}/merchant-uaa/oauth/token`, {
method: "POST",
body: new URLSearchParams({
username: this.username,
password: this.password,
grant_type: "password"
}).toString(),
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Authorization: `Basic ${Buffer.from(
`${this.appUsername}:${this.appPassword}`
).toString("base64")}`
}
});
const res = yield req.json();
if (res.error) {
throw new Error(res.error_description);
}
return res;
});
}
checkToken(inputToken) {
return __async(this, null, function* () {
let token = inputToken;
if (!token) {
token = yield this.login();
}
if (new Date(token.expires_in * 1e3) <= /* @__PURE__ */ new Date()) {
token = yield this.login();
}
if (this.saveToken && JSON.stringify(inputToken) !== JSON.stringify(token)) {
yield this.saveToken(token);
}
return token;
});
}
createInvoice(input, token) {
return __async(this, null, function* () {
token = yield this.checkToken(token);
const req = yield fetch(`${this.baseUrl}/lend-merchant/merchant/loan`, {
method: "POST",
body: JSON.stringify(input),
headers: {
Authorization: `Bearer ${token.access_token}`,
"Content-Type": "application/json"
}
});
const res = yield req.json();
if (res.status !== "Success") {
throw Error(res.msgList[0].text);
}
return {
data: res,
token
};
});
}
checkInvoice(invoiceId, token) {
return __async(this, null, function* () {
token = yield this.checkToken(token);
const req = yield fetch(`${this.baseUrl}/lend-merchant/merchant/loan/check/${invoiceId}`, {
method: "GET",
headers: {
Authorization: `Bearer ${token.access_token}`,
"Content-Type": "application/json"
}
});
const res = yield req.json();
return {
data: res,
token
};
});
}
};
// payments/ubpay.ts
var UbPay = class {
constructor(clientId, clientSecret, saveToken, baseUrl) {
this.baseUrl = "https://merchant-payment-api.dev.p.ubcabtech.com";
this.clientId = clientId;
this.clientSecret = clientSecret;
this.baseUrl = baseUrl || this.baseUrl;
this.saveToken = saveToken;
}
login() {
return __async(this, null, function* () {
const req = yield fetch(`${this.baseUrl}/auth/token`, {
method: "POST",
body: JSON.stringify({
clientId: this.clientId,
clientSecret: this.clientSecret
})
});
const res = yield req.json();
if (!res.success) {
throw new Error(res.error.message);
}
return {
access_token: res.data.accessToken,
refresh_token: res.data.refreshToken,
refresh_expires_in: res.data.refreshTokenExpiresIn,
expires_in: res.data.accessTokenExpiresIn
};
});
}
getRefreshToken(token) {
return __async(this, null, function* () {
const req = yield fetch(`${this.baseUrl}/auth/refresh-token`, {
method: "POST",
body: JSON.stringify({
refreshToken: token.refresh_token
})
});
const res = yield req.json();
return __spreadProps(__spreadValues({}, token), {
access_token: res.data.accessToken,
expires_in: parseInt(res.data.accessTokenExpriresIn, 10)
});
});
}
checkToken(inputToken) {
return __async(this, null, function* () {
let token = inputToken;
if (!token || !token.refresh_expires_in) {
token = yield this.login();
}
if (token.refresh_expires_in && new Date(token.refresh_expires_in * 1e3) <= /* @__PURE__ */ new Date()) {
token = yield this.login();
} else if (new Date(token.expires_in * 1e3) <= /* @__PURE__ */ new Date()) {
token = yield this.getRefreshToken(token);
}
if (this.saveToken && JSON.stringify(inputToken) !== JSON.stringify(token)) {
yield this.saveToken(token);
}
return token;
});
}
createInvoice(input, token) {
return __async(this, null, function* () {
token = yield this.checkToken(token);
const req = yield fetch(`${this.baseUrl}/api/v1/invoices`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token.access_token}`
},
body: JSON.stringify(input)
});
const res = yield req.json();
return {
data: res,
token
};
});
}
checkInvoice(invoiceId, token) {
return __async(this, null, function* () {
token = yield this.checkToken(token);
const req = yield fetch(`${this.baseUrl}/api/v1/invoices/${invoiceId}`, {
headers: {
Authorization: `Bearer ${token.access_token}`
}
});
const res = yield req.json();
return {
data: res,
token
};
});
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Qpay,
StorePay,
UbPay
});