ecpay-einvoice-ts
Version:
ECPay e-invoice SDK for TypeScript
241 lines (240 loc) • 9.6 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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ecUtils_1 = __importDefault(require("./utils/ecUtils"));
const doCommand = (url, input, init, opt) => __awaiter(void 0, void 0, void 0, function* () {
const { encode, decode } = (0, ecUtils_1.default)(init);
const encryptedData = encode(input);
const resp = yield fetch(url, {
method: 'POST',
headers: {
'Content-Type': "application/json"
},
body: JSON.stringify({
MerchantID: init.MerchantID,
RqHeader: {
Timestamp: Date.now()
},
Data: encryptedData
})
});
if (opt === null || opt === void 0 ? void 0 : opt.plain) {
const ret = yield resp.json();
return ret;
}
const json = yield resp.json();
const { Data } = json;
const decryptedData = decode(Data);
const data = JSON.parse(decryptedData);
if (data.RtnCode != 1) {
throw new Error(data.RtnMsg);
}
return Object.assign({}, data);
});
function ECPay(init) {
const allowance = (input) => __awaiter(this, void 0, void 0, function* () {
const url = `${init.BaseURL}/B2CInvoice/Allowance`;
const { encode } = (0, ecUtils_1.default)(init);
const encryptedData = encode(input);
const resp = yield fetch(url, {
method: 'POST',
headers: {
'Content-Type': "application/json"
},
body: JSON.stringify({
MerchantID: init.MerchantID,
RqHeader: {
Timestamp: Date.now()
},
Data: encryptedData
})
});
const json = yield resp.json();
const { Data } = json;
const decryptedData = (0, ecUtils_1.default)(init).decode(Data);
const data = JSON.parse(decryptedData);
if (data.RtnCode != 1) {
throw new Error(data.RtnMsg);
}
return Object.assign(Object.assign({}, json), { DecryptData: data });
});
const issue = (issueData) => __awaiter(this, void 0, void 0, function* () {
const url = `${init.BaseURL}/B2CInvoice/Issue`;
const { encode } = (0, ecUtils_1.default)(init);
const encryptedData = encode(issueData);
const resp = yield fetch(url, {
method: 'POST',
headers: {
'Content-Type': "application/json"
},
body: JSON.stringify({
MerchantID: init.MerchantID,
RqHeader: {
Timestamp: Date.now()
},
Data: encryptedData
})
});
const json = yield resp.json();
const { Data } = json;
const decryptedData = (0, ecUtils_1.default)(init).decode(Data);
const data = JSON.parse(decryptedData);
if (data.RtnCode != 1) {
throw new Error(data.RtnMsg);
}
return Object.assign(Object.assign({}, json), { DecryptedData: data });
});
const allowanceInvalid = (input) => __awaiter(this, void 0, void 0, function* () {
const url = `${init.BaseURL}/B2CInvoice/AllowanceInvalid`;
const result = yield doCommand(url, input, init);
return Object.assign({}, result);
});
const invoiceInvalid = (input) => __awaiter(this, void 0, void 0, function* () {
const url = `${init.BaseURL}/B2CInvoice/Invalid`;
const result = yield doCommand(url, input, init);
return Object.assign({}, result);
});
const getIssueList = (input) => __awaiter(this, void 0, void 0, function* () {
const url = `${init.BaseURL}/B2CInvoice/GetIssueList`;
const result = yield doCommand(url, input, init, {
plain: true
});
return Object.assign({}, result.Data);
});
const invoicePrint = (input) => __awaiter(this, void 0, void 0, function* () {
const url = `${init.BaseURL}/B2CInvoice/InvoicePrint`;
const result = yield doCommand(url, input, init);
return Object.assign({}, result);
});
const getIssue = (input) => __awaiter(this, void 0, void 0, function* () {
const url = `${init.BaseURL}/B2CInvoice/GetIssue`;
const { encode } = (0, ecUtils_1.default)(init);
const encryptedData = encode(input);
const resp = yield fetch(url, {
method: 'POST',
headers: {
'Content-Type': "application/json"
},
body: JSON.stringify({
MerchantID: init.MerchantID,
RqHeader: {
Timestamp: Date.now()
},
Data: encryptedData
})
});
const json = yield resp.json();
const { Data } = json;
const decryptedData = (0, ecUtils_1.default)(init).decode(Data);
const data = JSON.parse(decryptedData);
if (data.RtnCode != 1) {
throw new Error(data.RtnMsg);
}
return Object.assign(Object.assign({}, json), { DecryptData: data });
});
const getAllowanceList = (input) => __awaiter(this, void 0, void 0, function* () {
const url = `${init.BaseURL}/B2CInvoice/GetAllowanceList`;
const result = yield doCommand(url, input, init);
return Object.assign({}, result);
});
const invoiceNotify = (input) => __awaiter(this, void 0, void 0, function* () {
const url = `${init.BaseURL}/B2CInvoice/InvoiceNotify`;
const result = yield doCommand(url, input, init);
return Object.assign({}, result);
});
const getAllowanceInvalids = (input) => __awaiter(this, void 0, void 0, function* () {
const url = `${init.BaseURL}/B2CInvoice/AllowanceInvalid`;
const result = yield doCommand(url, input, init);
return Object.assign({}, result);
});
const getInvalids = (input) => __awaiter(this, void 0, void 0, function* () {
const url = `${init.BaseURL}/B2CInvoice/GetInvalid`;
const result = yield doCommand(url, input, init);
return Object.assign({}, result);
});
const getCompanyNameByTaxID = (input) => __awaiter(this, void 0, void 0, function* () {
const url = `${init.BaseURL}/B2CInvoice/GetCompanyNameByTaxID`;
const result = yield doCommand(url, input, init);
return Object.assign({}, result);
});
const checkBarcode = (input) => __awaiter(this, void 0, void 0, function* () {
const url = `${init.BaseURL}/B2CInvoice/CheckBarcode`;
const result = yield doCommand(url, input, init);
return Object.assign({}, result);
});
const checkLoveCode = (input) => __awaiter(this, void 0, void 0, function* () {
const url = `${init.BaseURL}/B2CInvoice/CheckLoveCode`;
const result = yield doCommand(url, input, init);
return Object.assign({}, result);
});
return {
// 檢查愛心碼
CheckLoveCode(input) {
return checkLoveCode(input);
},
// 檢查手機條碼
CheckBarcode(input) {
return checkBarcode(input);
},
// 檢查統一編號
GetCompanyNameByTaxID(input) {
return getCompanyNameByTaxID(input);
},
// 取得作廢發票
GetInvalid(input) {
return getInvalids(input);
},
// 取得作廢折讓單
GetAllowanceInvalid(input) {
return getAllowanceInvalids(input);
},
// 發票通知
InvoiceNotify(input) {
return invoiceNotify(input);
},
// 取得所有折讓單
GetAllowanceList(input) {
return getAllowanceList(input);
},
// 取得單筆折讓單
Allowance(input) {
return allowance(input);
},
// 作廢折讓單
AllowanceInvalid(input) {
return allowanceInvalid(input);
},
// 取得發票
GetIssue(input) {
return getIssue(input);
},
// 查詢多筆發票
GetIssueList(input) {
return getIssueList(input);
},
// 作廢發票
Invalid(input) {
return invoiceInvalid(input);
},
// 發票列印
InvoicePrint(input) {
return invoicePrint(input);
},
// 開立發票
Issue(input) {
return issue(input);
}
};
}
exports.default = ECPay;