@@ -11,7 +11,8 @@ app.use(express.json())
11
11
const paymentSchema = z . object ( {
12
12
token : z . string ( ) ,
13
13
user_identifier : z . string ( ) ,
14
- amount : z . string ( )
14
+ amount : z . string ( ) ,
15
+ PaymentResponse :z . enum ( [ "Success" , "Failure" ] )
15
16
} ) ;
16
17
17
18
app . post ( "/hdfcWebhook" , async ( req , res ) => {
@@ -27,20 +28,51 @@ app.post("/hdfcWebhook", async (req, res) => {
27
28
errors : validation . error . errors
28
29
} ) ;
29
30
}
30
-
31
+ enum PaymentResponse {
32
+ Success = "Success" ,
33
+ failure = "Failure"
34
+ }
31
35
// Use validated data
32
36
const paymentInformation : {
33
37
token : string ;
34
38
userId : string ;
35
39
amount : string ;
40
+ PaymentResponse : PaymentResponse ;
36
41
} = {
37
42
token : validation . data . token ,
38
43
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
40
46
} ;
41
47
42
48
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 {
44
76
const [ balanceUpdate , transactionUpdate ] = await db . $transaction ( [
45
77
db . balance . updateMany ( {
46
78
where : {
@@ -65,40 +97,12 @@ app.post("/hdfcWebhook", async (req, res) => {
65
97
} )
66
98
] ) ;
67
99
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
+
98
101
99
102
res . json ( {
100
103
message : "Captured"
101
104
} ) ;
105
+ }
102
106
} catch ( e ) {
103
107
console . error ( e ) ;
104
108
// On failure, update transaction status to 'Failed' and remove locked amount
0 commit comments