Skip to content

Commit 64007aa

Browse files
Payment Response Added in webhook
1 parent 156a039 commit 64007aa

File tree

2 files changed

+46
-36
lines changed

2 files changed

+46
-36
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ todo:
22
landing Page
33
websockets
44
openapi
5-
ratelimiting:login,payment actions
6-
recoil
5+
76

87
VPay – Architecture & Flow
98
Overview
@@ -30,6 +29,13 @@ User selects a bank and amount.
3029
Creates an on-ramp transaction (status: Processing).
3130
Redirects to bank site; after payment, the bank calls the /hdfcWebhook endpoint (bank-webhook app).
3231
On webhook, the backend updates the user's balance and transaction status to Success.
32+
sample Postman Post Req:
33+
{
34+
"token": "232.23011382469227",
35+
"user_identifier": "2",
36+
"amount": "10000",
37+
"PaymentResponse":"Success"
38+
}
3339
![postman webhook call](image.png)
3440
4. P2P Transfer
3541
User enters recipient's number and amount.

apps/bank-webhook/src/index.ts

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ app.use(express.json())
1111
const paymentSchema = z.object({
1212
token: z.string(),
1313
user_identifier: z.string(),
14-
amount: z.string()
14+
amount: z.string(),
15+
PaymentResponse:z.enum(["Success", "Failure"])
1516
});
1617

1718
app.post("/hdfcWebhook", async (req, res) => {
@@ -27,20 +28,51 @@ app.post("/hdfcWebhook", async (req, res) => {
2728
errors: validation.error.errors
2829
});
2930
}
30-
31+
enum PaymentResponse {
32+
Success = "Success",
33+
failure = "Failure"
34+
}
3135
// Use validated data
3236
const paymentInformation: {
3337
token: string;
3438
userId: string;
3539
amount: string;
40+
PaymentResponse: PaymentResponse;
3641
} = {
3742
token: validation.data.token,
3843
userId: validation.data.user_identifier,
39-
amount: validation.data.amount
44+
amount: validation.data.amount,
45+
PaymentResponse: validation.data.PaymentResponse === "Success" ? PaymentResponse.Success : PaymentResponse.failure
4046
};
4147

4248
try {
43-
49+
if(paymentInformation.PaymentResponse !== PaymentResponse.Success) {
50+
await db.$transaction([
51+
db.balance.updateMany({
52+
where: {
53+
userId: Number(paymentInformation.userId)
54+
},
55+
data: {
56+
locked: {
57+
decrement: Number(paymentInformation.amount)
58+
}
59+
}
60+
}),
61+
db.onRampTransaction.updateMany({
62+
where: {
63+
token: paymentInformation.token
64+
},
65+
data: {
66+
status: "Failure",
67+
}
68+
})
69+
]);
70+
return res.status(411).json({
71+
message: "Error while processing webhook: No records updated"
72+
});
73+
}
74+
75+
else{
4476
const [balanceUpdate, transactionUpdate] = await db.$transaction([
4577
db.balance.updateMany({
4678
where: {
@@ -65,40 +97,12 @@ app.post("/hdfcWebhook", async (req, res) => {
6597
})
6698
]);
6799
console.log(balanceUpdate, transactionUpdate);
68-
// const updatedBalances = balanceUpdate?.count ?? balanceUpdate;
69-
// const updatedTransactions = transactionUpdate?.count ?? transactionUpdate;
70-
71-
// // result[0] = balance update count, result[1] = transaction update count
72-
// if ((updatedBalances === 0 || updatedTransactions === 0)) {
73-
// // If no records updated, treat as failure
74-
// await db.$transaction([
75-
// db.balance.updateMany({
76-
// where: {
77-
// userId: Number(paymentInformation.userId)
78-
// },
79-
// data: {
80-
// locked: {
81-
// decrement: Number(paymentInformation.amount)
82-
// }
83-
// }
84-
// }),
85-
// db.onRampTransaction.updateMany({
86-
// where: {
87-
// token: paymentInformation.token
88-
// },
89-
// data: {
90-
// status: "Failure",
91-
// }
92-
// })
93-
// ]);
94-
// return res.status(411).json({
95-
// message: "Error while processing webhook: No records updated"
96-
// });
97-
// }
100+
98101

99102
res.json({
100103
message: "Captured"
101104
});
105+
}
102106
} catch(e) {
103107
console.error(e);
104108
// On failure, update transaction status to 'Failed' and remove locked amount

0 commit comments

Comments
 (0)