1
1
use diesel:: { associations:: HasTable , BoolExpressionMethods , ExpressionMethods } ;
2
- use router_env:: { instrument, tracing} ;
2
+ use router_env:: { instrument, logger , tracing} ;
3
3
4
4
use super :: generics;
5
5
use crate :: {
@@ -8,13 +8,44 @@ use crate::{
8
8
ConnectorResponseUpdateInternal ,
9
9
} ,
10
10
errors,
11
- schema:: connector_response:: dsl,
11
+ payment_attempt:: { PaymentAttempt , PaymentAttemptUpdate , PaymentAttemptUpdateInternal } ,
12
+ schema:: { connector_response:: dsl, payment_attempt:: dsl as pa_dsl} ,
12
13
PgPooledConn , StorageResult ,
13
14
} ;
14
15
15
16
impl ConnectorResponseNew {
16
17
#[ instrument( skip( conn) ) ]
17
18
pub async fn insert ( self , conn : & PgPooledConn ) -> StorageResult < ConnectorResponse > {
19
+ let payment_attempt_update = PaymentAttemptUpdate :: ConnectorResponse {
20
+ authentication_data : self . authentication_data . clone ( ) ,
21
+ encoded_data : self . encoded_data . clone ( ) ,
22
+ connector_transaction_id : self . connector_transaction_id . clone ( ) ,
23
+ connector : self . connector_name . clone ( ) ,
24
+ updated_by : self . updated_by . clone ( ) ,
25
+ } ;
26
+
27
+ let _payment_attempt: Result < PaymentAttempt , _ > =
28
+ generics:: generic_update_with_unique_predicate_get_result :: <
29
+ <PaymentAttempt as HasTable >:: Table ,
30
+ _ ,
31
+ _ ,
32
+ _ ,
33
+ > (
34
+ conn,
35
+ pa_dsl:: attempt_id
36
+ . eq ( self . attempt_id . to_owned ( ) )
37
+ . and ( pa_dsl:: merchant_id. eq ( self . merchant_id . to_owned ( ) ) ) ,
38
+ PaymentAttemptUpdateInternal :: from ( payment_attempt_update) ,
39
+ )
40
+ . await
41
+ . map_err ( |err| {
42
+ logger:: error!(
43
+ "Error while updating payment attempt in connector_response flow {:?}" ,
44
+ err
45
+ ) ;
46
+ err
47
+ } ) ;
48
+
18
49
generics:: generic_insert ( conn, self ) . await
19
50
}
20
51
}
@@ -26,27 +57,78 @@ impl ConnectorResponse {
26
57
conn : & PgPooledConn ,
27
58
connector_response : ConnectorResponseUpdate ,
28
59
) -> StorageResult < Self > {
29
- match generics:: generic_update_with_unique_predicate_get_result :: <
30
- <Self as HasTable >:: Table ,
31
- _ ,
32
- _ ,
33
- _ ,
34
- > (
35
- conn,
36
- dsl:: merchant_id
37
- . eq ( self . merchant_id . clone ( ) )
38
- . and ( dsl:: payment_id. eq ( self . payment_id . clone ( ) ) )
39
- . and ( dsl:: attempt_id. eq ( self . attempt_id . clone ( ) ) ) ,
40
- ConnectorResponseUpdateInternal :: from ( connector_response) ,
41
- )
42
- . await
43
- {
44
- Err ( error) => match error. current_context ( ) {
45
- errors:: DatabaseError :: NoFieldsToUpdate => Ok ( self ) ,
46
- _ => Err ( error) ,
60
+ let payment_attempt_update = match connector_response. clone ( ) {
61
+ ConnectorResponseUpdate :: ResponseUpdate {
62
+ connector_transaction_id,
63
+ authentication_data,
64
+ encoded_data,
65
+ connector_name,
66
+ updated_by,
67
+ } => PaymentAttemptUpdate :: ConnectorResponse {
68
+ authentication_data,
69
+ encoded_data,
70
+ connector_transaction_id,
71
+ connector : connector_name,
72
+ updated_by,
47
73
} ,
48
- result => result,
49
- }
74
+ ConnectorResponseUpdate :: ErrorUpdate {
75
+ connector_name,
76
+ updated_by,
77
+ } => PaymentAttemptUpdate :: ConnectorResponse {
78
+ authentication_data : None ,
79
+ encoded_data : None ,
80
+ connector_transaction_id : None ,
81
+ connector : connector_name,
82
+ updated_by,
83
+ } ,
84
+ } ;
85
+
86
+ let _payment_attempt: Result < PaymentAttempt , _ > =
87
+ generics:: generic_update_with_unique_predicate_get_result :: <
88
+ <PaymentAttempt as HasTable >:: Table ,
89
+ _ ,
90
+ _ ,
91
+ _ ,
92
+ > (
93
+ conn,
94
+ pa_dsl:: attempt_id
95
+ . eq ( self . attempt_id . to_owned ( ) )
96
+ . and ( pa_dsl:: merchant_id. eq ( self . merchant_id . to_owned ( ) ) ) ,
97
+ PaymentAttemptUpdateInternal :: from ( payment_attempt_update) ,
98
+ )
99
+ . await
100
+ . map_err ( |err| {
101
+ logger:: error!(
102
+ "Error while updating payment attempt in connector_response flow {:?}" ,
103
+ err
104
+ ) ;
105
+ err
106
+ } ) ;
107
+
108
+ let connector_response_result =
109
+ match generics:: generic_update_with_unique_predicate_get_result :: <
110
+ <Self as HasTable >:: Table ,
111
+ _ ,
112
+ _ ,
113
+ _ ,
114
+ > (
115
+ conn,
116
+ dsl:: merchant_id
117
+ . eq ( self . merchant_id . clone ( ) )
118
+ . and ( dsl:: payment_id. eq ( self . payment_id . clone ( ) ) )
119
+ . and ( dsl:: attempt_id. eq ( self . attempt_id . clone ( ) ) ) ,
120
+ ConnectorResponseUpdateInternal :: from ( connector_response) ,
121
+ )
122
+ . await
123
+ {
124
+ Err ( error) => match error. current_context ( ) {
125
+ errors:: DatabaseError :: NoFieldsToUpdate => Ok ( self ) ,
126
+ _ => Err ( error) ,
127
+ } ,
128
+ result => result,
129
+ } ;
130
+
131
+ connector_response_result
50
132
}
51
133
52
134
#[ instrument( skip( conn) ) ]
@@ -56,14 +138,69 @@ impl ConnectorResponse {
56
138
merchant_id : & str ,
57
139
attempt_id : & str ,
58
140
) -> StorageResult < Self > {
59
- generics:: generic_find_one :: < <Self as HasTable >:: Table , _ , _ > (
141
+ let connector_response: Self =
142
+ generics:: generic_find_one :: < <Self as HasTable >:: Table , _ , _ > (
143
+ conn,
144
+ dsl:: merchant_id. eq ( merchant_id. to_owned ( ) ) . and (
145
+ dsl:: payment_id
146
+ . eq ( payment_id. to_owned ( ) )
147
+ . and ( dsl:: attempt_id. eq ( attempt_id. to_owned ( ) ) ) ,
148
+ ) ,
149
+ )
150
+ . await ?;
151
+
152
+ match generics:: generic_find_one :: < <PaymentAttempt as HasTable >:: Table , _ , _ > (
60
153
conn,
61
- dsl :: merchant_id . eq ( merchant_id . to_owned ( ) ) . and (
62
- dsl :: payment_id
63
- . eq ( payment_id . to_owned ( ) )
64
- . and ( dsl :: attempt_id. eq ( attempt_id. to_owned ( ) ) ) ,
154
+ pa_dsl :: payment_id . eq ( payment_id . to_owned ( ) ) . and (
155
+ pa_dsl :: merchant_id
156
+ . eq ( merchant_id . to_owned ( ) )
157
+ . and ( pa_dsl :: attempt_id. eq ( attempt_id. to_owned ( ) ) ) ,
65
158
) ,
66
159
)
67
160
. await
161
+ {
162
+ Ok :: < PaymentAttempt , _ > ( payment_attempt) => {
163
+ if payment_attempt. authentication_data != connector_response. authentication_data {
164
+ logger:: error!(
165
+ "Not Equal pa_authentication_data : {:?}, cr_authentication_data: {:?} " ,
166
+ payment_attempt. authentication_data,
167
+ connector_response. authentication_data
168
+ ) ;
169
+ }
170
+
171
+ if payment_attempt. encoded_data != connector_response. encoded_data {
172
+ logger:: error!(
173
+ "Not Equal pa_encoded_data : {:?}, cr_encoded_data: {:?} " ,
174
+ payment_attempt. encoded_data,
175
+ connector_response. encoded_data
176
+ ) ;
177
+ }
178
+
179
+ if payment_attempt. connector_transaction_id
180
+ != connector_response. connector_transaction_id
181
+ {
182
+ logger:: error!(
183
+ "Not Equal pa_connector_transaction_id : {:?}, cr_connector_transaction_id: {:?} " ,
184
+ payment_attempt. connector_transaction_id,
185
+ connector_response. connector_transaction_id
186
+ ) ;
187
+ }
188
+ if payment_attempt. connector != connector_response. connector_name {
189
+ logger:: error!(
190
+ "Not Equal pa_connector : {:?}, cr_connector_name: {:?} " ,
191
+ payment_attempt. connector,
192
+ connector_response. connector_name
193
+ ) ;
194
+ }
195
+ }
196
+ Err ( err) => {
197
+ logger:: error!(
198
+ "Error while finding payment attempt in connector_response flow {:?}" ,
199
+ err
200
+ ) ;
201
+ }
202
+ }
203
+
204
+ Ok ( connector_response)
68
205
}
69
206
}
0 commit comments