node-ecpay-aio
Version:
A production-ready ECPay AIO SDK for Node.js with TypeScript support.
82 lines (81 loc) • 4 kB
JavaScript
;
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.DoAction = exports.CreditCardPeriodAction = exports.Action = void 0;
const Error_1 = require("./Error");
const utils_1 = require("../utils");
const schema_1 = require("../schema");
class Action {
constructor(merchant, params) {
this.merchant = merchant;
this.params = Object.assign({}, params);
}
_execute() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.apiUrl)
throw new Error(`API url is not provided or infeasible for ${this.merchant.mode} mode.`);
const { MerchantID, HashKey, HashIV } = this.merchant.config;
// every action requires MerchantID and CheckMacValue
const actionParams = Object.assign({ MerchantID }, this._params);
const CheckMacValue = (0, utils_1.generateCheckMacValue)(actionParams, HashKey, HashIV);
return (0, utils_1.postRequest)({
apiUrl: this.apiUrl,
params: Object.assign(Object.assign({}, actionParams), { CheckMacValue }),
});
});
}
}
exports.Action = Action;
class CreditCardPeriodAction extends Action {
constructor(merchant, params) {
super(merchant, params);
schema_1.CreditCardPeriodActionParamsSchema.validateSync(this.params);
this.apiUrl = merchant.ecpayServiceUrls.CreditCardPeriod[merchant.mode];
this._params = Object.assign(Object.assign({}, this.params), { TimeStamp: (0, utils_1.getCurrentUnixTimestampOffset)(90), PlatformID: this.merchant.config.PlatformID });
}
execute() {
return __awaiter(this, void 0, void 0, function* () {
const { HashKey, HashIV } = this.merchant.config;
const data = yield this._execute();
const result = (0, utils_1.parseIntegerFileds)(data, ['RtnCode']);
if (!(0, utils_1.isValidReceivedCheckMacValue)(data, HashKey, HashIV))
throw new Error_1.CheckMacValueError(`Invalid CheckMacValue in CreditCardPeriodAction. MerchantTradeNo: ${this.params.MerchantTradeNo}, Action: ${this.params.Action}.`, result);
if (result.RtnCode !== 1) {
// 1: success, n: fails
throw new Error_1.ActionError(result.RtnMsg, result.RtnCode, result);
}
return result;
});
}
}
exports.CreditCardPeriodAction = CreditCardPeriodAction;
class DoAction extends Action {
constructor(merchant, params) {
super(merchant, params);
schema_1.DoActionParamsSchema.validateSync(this.params);
this.apiUrl = merchant.ecpayServiceUrls.Do[merchant.mode];
this._params = Object.assign(Object.assign({}, this.params), { PlatformID: this.merchant.config.PlatformID });
}
execute() {
return __awaiter(this, void 0, void 0, function* () {
const _result = yield this._execute();
const result = (0, utils_1.parseIntegerFileds)(_result, [
'RtnCode',
]);
if (result.RtnCode !== 1) {
// 1: success, n: fails
throw new Error_1.ActionError(result.RtnMsg, result.RtnCode, result);
}
return result;
});
}
}
exports.DoAction = DoAction;