node-ecpay-aio
Version:
A production-ready ECPay AIO SDK for Node.js with TypeScript support.
227 lines (226 loc) • 10.7 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ALLPayment = exports.AndroidPayPayment = exports.BARCODEPayment = exports.CVSPayment = exports.ATMPayment = exports.WebATMPayment = exports.CreditPeriodPayment = exports.CreditDividePayment = exports.CreditOneTimePayment = exports.Payment = void 0;
const Query_1 = require("./Query");
const utils_1 = require("../utils");
const schema_1 = require("../schema");
class Payment {
constructor(merchant, baseParams, params) {
schema_1.BasePaymentParamsSchema.validateSync(baseParams);
this.merchant = merchant;
this.apiUrl = merchant.ecpayServiceUrls.AioCheckOut[merchant.mode];
this.baseParams = Object.assign(Object.assign({ NeedExtraPaidInfo: 'N', ReturnURL: this.merchant.config.ReturnURL }, baseParams), { ChoosePayment: 'ALL', InvoiceMark: 'N', PaymentType: 'aio', EncryptType: 1 });
this.params = Object.assign({}, params);
}
_prepareOrder(invoice) {
const { MerchantID, HashKey, HashIV } = this.merchant.config;
if (invoice) {
invoice.InvType = invoice.InvType || '07'; // fixed
invoice.DelayDay = invoice.DelayDay || 0;
invoice.CarruerType = invoice.CarruerType || ''; // default to No Carruer
this.baseParams.InvoiceMark = 'Y';
schema_1.InvoiceParamsSchema.validateSync(invoice);
validateInvoiceItems(invoice);
this.invoice = invoice;
}
const encodedInvoice = (0, utils_1.getEncodedInvoice)(this.invoice);
const order = Object.assign(Object.assign(Object.assign({ MerchantID }, this.baseParams), this.params), encodedInvoice);
const CheckMacValue = (0, utils_1.generateCheckMacValue)(order, HashKey, HashIV);
const postingOrder = Object.assign(Object.assign({}, order), { CheckMacValue });
return postingOrder;
}
checkout(invoice) {
return __awaiter(this, void 0, void 0, function* () {
const postOrder = this._prepareOrder(invoice);
return (0, utils_1.generateRedirectPostForm)(this.apiUrl, postOrder);
});
}
_placeOrder(invoice) {
return __awaiter(this, void 0, void 0, function* () {
const postOrder = this._prepareOrder(invoice);
try {
const _result = yield (0, utils_1.placeOrderRequest)({
aioBaseUrl: this.merchant.ecpayServiceUrls.Aio[this.merchant.mode],
apiUrl: this.apiUrl,
params: postOrder,
});
return this.merchant
.createQuery(Query_1.PaymentInfoQuery, {
MerchantTradeNo: this.baseParams.MerchantTradeNo,
})
.read();
}
catch (err) {
throw err;
}
});
}
}
exports.Payment = Payment;
/*
* Specific Payment Classes
*/
class CreditOneTimePayment extends Payment {
constructor(merchant, baseParams, params = {}) {
super(merchant, baseParams, params);
this.baseParams.ChoosePayment = 'Credit';
if (!this.params.BindingCard)
delete this.params.MerchantMemberID;
schema_1.CreditBasePaymentParamsSchema.validateSync(this.params);
schema_1.CreditOneTimePaymentParamsSchema.validateSync(this.params);
}
}
exports.CreditOneTimePayment = CreditOneTimePayment;
class CreditDividePayment extends Payment {
constructor(merchant, baseParams, params) {
super(merchant, baseParams, params);
this.baseParams.ChoosePayment = 'Credit';
if (!this.params.BindingCard)
delete this.params.MerchantMemberID;
schema_1.CreditBasePaymentParamsSchema.validateSync(this.params);
schema_1.CreditDividePaymentParamsSchema.validateSync(this.params);
}
}
exports.CreditDividePayment = CreditDividePayment;
class CreditPeriodPayment extends Payment {
constructor(merchant, baseParams, params) {
var _a;
super(merchant, baseParams, params);
this.baseParams.ChoosePayment = 'Credit';
if (!this.params.BindingCard)
delete this.params.MerchantMemberID;
this.params.PeriodReturnURL =
(_a = params.PeriodReturnURL) !== null && _a !== void 0 ? _a : this.merchant.config.PeriodReturnURL;
schema_1.CreditBasePaymentParamsSchema.validateSync(this.params);
schema_1.CreditPeriodPaymentParamsSchema.validateSync(this.params);
if (this.baseParams.TotalAmount !== this.params.PeriodAmount) {
throw new Error('PeriodAmount must equal to TotalAmount');
}
}
}
exports.CreditPeriodPayment = CreditPeriodPayment;
class WebATMPayment extends Payment {
constructor(merchant, baseParams, params = {}) {
super(merchant, baseParams, params);
this.baseParams.ChoosePayment = 'WebATM';
schema_1.WebATMPaymentParamsSchema.validateSync(this.params);
}
}
exports.WebATMPayment = WebATMPayment;
class ATMPayment extends Payment {
constructor(merchant, baseParams, params = {}) {
var _a, _b;
super(merchant, baseParams, params);
this.baseParams.ChoosePayment = 'ATM';
this.params.ClientRedirectURL =
(_a = params.ClientRedirectURL) !== null && _a !== void 0 ? _a : this.merchant.config.ClientRedirectURL;
this.params.PaymentInfoURL =
(_b = params.PaymentInfoURL) !== null && _b !== void 0 ? _b : this.merchant.config.PaymentInfoURL;
schema_1.ATMPaymentParamsSchema.validateSync(this.params);
}
placeOrder(invoice) {
return __awaiter(this, void 0, void 0, function* () {
this.params.ChooseSubPayment = this.params.ChooseSubPayment || 'BOT';
return this._placeOrder(invoice);
});
}
}
exports.ATMPayment = ATMPayment;
class CVSPayment extends Payment {
constructor(merchant, baseParams, params = {}) {
var _a, _b;
super(merchant, baseParams, params);
this.baseParams.ChoosePayment = 'CVS';
this.params.ClientRedirectURL =
(_a = params.ClientRedirectURL) !== null && _a !== void 0 ? _a : this.merchant.config.ClientRedirectURL;
this.params.PaymentInfoURL =
(_b = params.PaymentInfoURL) !== null && _b !== void 0 ? _b : this.merchant.config.PaymentInfoURL;
schema_1.CVSPaymentParamsSchema.validateSync(this.params);
}
placeOrder(invoice) {
return __awaiter(this, void 0, void 0, function* () {
this.params.ChooseSubPayment = this.params.ChooseSubPayment || 'CVS';
return this._placeOrder(invoice);
});
}
}
exports.CVSPayment = CVSPayment;
class BARCODEPayment extends Payment {
constructor(merchant, baseParams, params = {}) {
var _a, _b;
super(merchant, baseParams, params);
this.baseParams.ChoosePayment = 'BARCODE';
this.params.ChooseSubPayment = this.params.ChooseSubPayment || 'BARCODE';
this.params.ClientRedirectURL =
(_a = params.ClientRedirectURL) !== null && _a !== void 0 ? _a : this.merchant.config.ClientRedirectURL;
this.params.PaymentInfoURL =
(_b = params.PaymentInfoURL) !== null && _b !== void 0 ? _b : this.merchant.config.PaymentInfoURL;
schema_1.BARCODEPaymentParamsSchema.validateSync(this.params);
}
placeOrder(invoice) {
return __awaiter(this, void 0, void 0, function* () {
this.params.ChooseSubPayment = this.params.ChooseSubPayment || 'BARCODE';
this.params.ClientRedirectURL = undefined;
this.params.PaymentInfoURL = undefined;
return this._placeOrder(invoice);
});
}
}
exports.BARCODEPayment = BARCODEPayment;
class AndroidPayPayment extends Payment {
constructor(merchant, baseParams, params) {
super(merchant, baseParams, params);
this.baseParams.ChoosePayment = 'AndroidPay';
schema_1.AndroidPayPaymentParamsSchema.validateSync(params);
throw new Error('AndroidPay is not supported in this version.');
}
}
exports.AndroidPayPayment = AndroidPayPayment;
class ALLPayment extends Payment {
constructor(merchant, baseParams, params) {
var _a, _b, _c;
super(merchant, baseParams, params);
this.baseParams.ChoosePayment = 'ALL';
if (!this.params.BindingCard)
delete this.params.MerchantMemberID;
this.params.PeriodReturnURL =
(_a = params.PeriodReturnURL) !== null && _a !== void 0 ? _a : this.merchant.config.PeriodReturnURL;
this.params.ClientRedirectURL =
(_b = params.ClientRedirectURL) !== null && _b !== void 0 ? _b : this.merchant.config.ClientRedirectURL;
this.params.PaymentInfoURL =
(_c = params.PaymentInfoURL) !== null && _c !== void 0 ? _c : this.merchant.config.PaymentInfoURL;
schema_1.ALLPaymentParamsSchema.validateSync(params);
}
}
exports.ALLPayment = ALLPayment;
function validateInvoiceItems(invoice) {
const { InvoiceItemName, InvoiceItemCount, InvoiceItemWord, InvoiceItemPrice, InvoiceItemTaxType = '', } = invoice;
const [InvoiceItemNameArr, InvoiceItemCountArr, InvoiceItemWordArr, InvoiceItemPriceArr, InvoiceItemTaxTypeArr,] = [
InvoiceItemName.split('|'),
InvoiceItemCount.split('|'),
InvoiceItemWord.split('|'),
InvoiceItemPrice.split('|'),
InvoiceItemTaxType.split('|'),
];
if (InvoiceItemCountArr.length !== InvoiceItemNameArr.length ||
InvoiceItemWordArr.length !== InvoiceItemNameArr.length ||
InvoiceItemPriceArr.length !== InvoiceItemNameArr.length) {
throw new TypeError('Number of item names, counts, words, prices must be identical.');
}
if (InvoiceItemWordArr.length) {
InvoiceItemWordArr.forEach((unitStr) => {
if (unitStr.length > 6) {
throw new TypeError('InvoiceItemWord string length must be less or equal to 6.');
}
});
}
}