Skip to content

Commit 1d97155

Browse files
committed
Add compareCrtBanxico utility and integrate it into sendQrPayment controller
1 parent c9351ed commit 1d97155

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

controllers/sendQrPayment.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const { getCodiQrUrl } = require("./utils/getCodiQrUrl");
99
const { getSellerApiKey } = require("./utils/getSellerApiKey");
1010
const { verifySignature } = require("./utils/verifySignature");
1111
const { getKeyCredentials } = require("./utils/getKeyCredentials");
12+
const { compareCrtBanxico } = require("./utils/compareCrtBanxico");
1213
const { generateSignature } = require("./utils/generateDigitalSignature");
1314
const { getBanxicoCredentials } = require("./utils/getBanxicoCredentials");
1415
const { getDeveloperCredentials } = require("./utils/getDeveloperCredentials");
@@ -81,15 +82,19 @@ module.exports = {
8182
});
8283
}
8384

84-
// Send the data
85+
// Send the data to Banxico
8586
const response = await axios.post(codiApiQrEndpoint, requestBody, {
8687
headers: {
8788
"Content-Type": "application/json; charset=utf-8",
8889
},
8990
});
9091
// console.log("\n🔵 Respuesta de Banxico: ", response.data);
9192

92-
// Verify the signed data
93+
// Verify th crtBdeM value sent in response
94+
const crtBanxicoVerified = compareCrtBanxico(crtBanxico, response.data);
95+
// console.log("\n🔵 crtBdeM verificado: ", crtBanxicoVerified);
96+
97+
// Verify the signature: Banxico
9398
const responseIsVerified = verifySignature(
9499
response.data,
95100
publicKeyBanxico
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function compareCrtBanxico(crtBanxico, data) {
2+
if (crtBanxico === data.crtBdeM) {
3+
return true;
4+
} else {
5+
console.error(
6+
"Mismatch: data.crtBdeM does not match with crtBanxico public key"
7+
);
8+
return false;
9+
}
10+
}
11+
12+
module.exports = {
13+
compareCrtBanxico,
14+
};

tests/compareCrtBanxico.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const { compareCrtBanxico } = require("../controllers/utils/compareCrtBanxico");
2+
3+
describe("compareCrtBanxico", () => {
4+
test("should return true when crtBanxico matches data.crtBdeM", () => {
5+
const crtBanxico = "00000100000100015974";
6+
const data = {
7+
crtBdeM: "00000100000100015974",
8+
};
9+
expect(compareCrtBanxico(crtBanxico, data)).toBe(true);
10+
});
11+
12+
test("should return false and log error when crtBanxico does not match data.crtBdeM", () => {
13+
const crtBanxico = "00000100000100015974";
14+
const data = {
15+
crtBdeM: "00000100000100015975",
16+
};
17+
console.error = jest.fn(); // Mock console.error
18+
expect(compareCrtBanxico(crtBanxico, data)).toBe(false);
19+
expect(console.error).toHaveBeenCalledWith(
20+
"Mismatch: data.crtBdeM does not match with crtBanxico public key"
21+
);
22+
});
23+
});

0 commit comments

Comments
 (0)