Skip to content

Commit

Permalink
fix: fix queryTradeInfo buildCheckValue issue
Browse files Browse the repository at this point in the history
  • Loading branch information
depresto committed Feb 18, 2025
1 parent b8cf3e3 commit 44481a5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "newebpay-mpg-sdk",
"version": "2.0.2",
"version": "2.0.3",
"description": "藍新金流 幕前支付(MPG) SDK",
"main": "./build/index.js",
"types": "./build/index.d.ts",
Expand Down
14 changes: 12 additions & 2 deletions src/__tests__/test-refund-ewallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,22 @@ describe("MPG API", () => {

test("should refund eWallet", async () => {
const data = await client.refundEWallet({
MerchantOrderNo: "1739512946019534",
Amount: 1500,
MerchantOrderNo: "17395115177210224",
Amount: 3000,
PaymentType: "LINEPAY",
});

console.log(data);
expect(data).toBeDefined();
});

test("should query trade info", async () => {
const data = await client.queryTradeInfo({
MerchantOrderNo: "17395115177210224",
Amt: 3000,
});

console.log(data);
expect(data).toBeDefined();
});
});
22 changes: 20 additions & 2 deletions src/newebpay.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,13 @@ export class NewebpayClient {
const MerchantID = this.merchantId;
const Version = "1.3";
const TimeStamp = this.getTimeStamp();
const CheckCode = this.buildCheckCode({ Amt, MerchantID, MerchantOrderNo });
const CheckValue = this.buildCheckValue({ Amt, MerchantID, MerchantOrderNo });

const formData = new FormData();
formData.append("MerchantID", MerchantID);
formData.append("Version", Version);
formData.append("RespondType", "JSON");
formData.append("CheckValue", CheckCode);
formData.append("CheckValue", CheckValue);
formData.append("TimeStamp", TimeStamp);
formData.append("MerchantOrderNo", MerchantOrderNo);
formData.append("Amt", Amt);
Expand Down Expand Up @@ -532,6 +532,24 @@ export class NewebpayClient {
.toUpperCase();
}

public buildCheckValue(params: { [key: string]: any }) {
const data = Object.keys(params)
.sort()
.reduce((obj, key) => {
obj[key] = params[key];
return obj;
}, {} as { [key: string]: any });

const paramsStr = new URLSearchParams(data).toString();
const checkStr = `IV=${this.hashIV}&${paramsStr}&Key=${this.hashKey}`;

return crypto
.createHash("sha256")
.update(checkStr)
.digest("hex")
.toUpperCase();
}

public sendApiRequest = async (params: { apiPath: string; data: any }) => {
const headers: AxiosRequestHeaders = {};
headers["Content-Type"] = "multipart/form-data";
Expand Down

0 comments on commit 44481a5

Please sign in to comment.