Skip to content

Commit 9d9fc2a

Browse files
refactor(db): migrate to payment_attempt from connector_response (#2656)
1 parent 8984627 commit 9d9fc2a

File tree

9 files changed

+259
-30
lines changed

9 files changed

+259
-30
lines changed

crates/data_models/src/payments/payment_attempt.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ pub struct PaymentAttempt {
142142
pub connector_response_reference_id: Option<String>,
143143
pub amount_capturable: i64,
144144
pub updated_by: String,
145+
pub authentication_data: Option<serde_json::Value>,
146+
pub encoded_data: Option<String>,
145147
pub merchant_connector_id: Option<String>,
146148
}
147149

@@ -202,6 +204,8 @@ pub struct PaymentAttemptNew {
202204
pub multiple_capture_count: Option<i16>,
203205
pub amount_capturable: i64,
204206
pub updated_by: String,
207+
pub authentication_data: Option<serde_json::Value>,
208+
pub encoded_data: Option<String>,
205209
pub merchant_connector_id: Option<String>,
206210
}
207211

@@ -325,6 +329,13 @@ pub enum PaymentAttemptUpdate {
325329
connector_response_reference_id: Option<String>,
326330
updated_by: String,
327331
},
332+
ConnectorResponse {
333+
authentication_data: Option<serde_json::Value>,
334+
encoded_data: Option<String>,
335+
connector_transaction_id: Option<String>,
336+
connector: Option<String>,
337+
updated_by: String,
338+
},
328339
}
329340

330341
impl ForeignIDRef for PaymentAttempt {

crates/diesel_models/src/payment_attempt.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ pub struct PaymentAttempt {
5959
pub amount_capturable: i64,
6060
pub updated_by: String,
6161
pub merchant_connector_id: Option<String>,
62+
pub authentication_data: Option<serde_json::Value>,
63+
pub encoded_data: Option<String>,
6264
}
6365

6466
#[derive(Clone, Debug, Eq, PartialEq, Queryable, Serialize, Deserialize)]
@@ -120,6 +122,8 @@ pub struct PaymentAttemptNew {
120122
pub amount_capturable: i64,
121123
pub updated_by: String,
122124
pub merchant_connector_id: Option<String>,
125+
pub authentication_data: Option<serde_json::Value>,
126+
pub encoded_data: Option<String>,
123127
}
124128

125129
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -242,6 +246,13 @@ pub enum PaymentAttemptUpdate {
242246
connector_response_reference_id: Option<String>,
243247
updated_by: String,
244248
},
249+
ConnectorResponse {
250+
authentication_data: Option<serde_json::Value>,
251+
encoded_data: Option<String>,
252+
connector_transaction_id: Option<String>,
253+
connector: Option<String>,
254+
updated_by: String,
255+
},
245256
}
246257

247258
#[derive(Clone, Debug, Default, AsChangeset, router_derive::DebugAsDisplay)]
@@ -279,6 +290,8 @@ pub struct PaymentAttemptUpdateInternal {
279290
amount_capturable: Option<i64>,
280291
updated_by: String,
281292
merchant_connector_id: Option<String>,
293+
authentication_data: Option<serde_json::Value>,
294+
encoded_data: Option<String>,
282295
}
283296

284297
impl PaymentAttemptUpdate {
@@ -331,6 +344,8 @@ impl PaymentAttemptUpdate {
331344
.unwrap_or(source.amount_capturable),
332345
updated_by: pa_update.updated_by,
333346
merchant_connector_id: pa_update.merchant_connector_id,
347+
authentication_data: pa_update.authentication_data.or(source.authentication_data),
348+
encoded_data: pa_update.encoded_data.or(source.encoded_data),
334349
..source
335350
}
336351
}
@@ -581,6 +596,20 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
581596
updated_by,
582597
..Default::default()
583598
},
599+
PaymentAttemptUpdate::ConnectorResponse {
600+
authentication_data,
601+
encoded_data,
602+
connector_transaction_id,
603+
connector,
604+
updated_by,
605+
} => Self {
606+
authentication_data,
607+
encoded_data,
608+
connector_transaction_id,
609+
connector,
610+
updated_by,
611+
..Default::default()
612+
},
584613
}
585614
}
586615
}
Lines changed: 164 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use diesel::{associations::HasTable, BoolExpressionMethods, ExpressionMethods};
2-
use router_env::{instrument, tracing};
2+
use router_env::{instrument, logger, tracing};
33

44
use super::generics;
55
use crate::{
@@ -8,13 +8,44 @@ use crate::{
88
ConnectorResponseUpdateInternal,
99
},
1010
errors,
11-
schema::connector_response::dsl,
11+
payment_attempt::{PaymentAttempt, PaymentAttemptUpdate, PaymentAttemptUpdateInternal},
12+
schema::{connector_response::dsl, payment_attempt::dsl as pa_dsl},
1213
PgPooledConn, StorageResult,
1314
};
1415

1516
impl ConnectorResponseNew {
1617
#[instrument(skip(conn))]
1718
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+
1849
generics::generic_insert(conn, self).await
1950
}
2051
}
@@ -26,27 +57,78 @@ impl ConnectorResponse {
2657
conn: &PgPooledConn,
2758
connector_response: ConnectorResponseUpdate,
2859
) -> 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,
4773
},
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
50132
}
51133

52134
#[instrument(skip(conn))]
@@ -56,14 +138,69 @@ impl ConnectorResponse {
56138
merchant_id: &str,
57139
attempt_id: &str,
58140
) -> 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, _, _>(
60153
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())),
65158
),
66159
)
67160
.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)
68205
}
69206
}

crates/diesel_models/src/schema.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,8 @@ diesel::table! {
580580
updated_by -> Varchar,
581581
#[max_length = 32]
582582
merchant_connector_id -> Nullable<Varchar>,
583+
authentication_data -> Nullable<Json>,
584+
encoded_data -> Nullable<Text>,
583585
}
584586
}
585587

crates/router/src/core/payments/helpers.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2933,6 +2933,8 @@ impl AttemptType {
29332933
connector_response_reference_id: None,
29342934
amount_capturable: old_payment_attempt.amount,
29352935
updated_by: storage_scheme.to_string(),
2936+
authentication_data: None,
2937+
encoded_data: None,
29362938
merchant_connector_id: None,
29372939
}
29382940
}

crates/storage_impl/src/mock_db/payment_attempt.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ impl PaymentAttemptInterface for MockDb {
141141
connector_response_reference_id: None,
142142
amount_capturable: payment_attempt.amount_capturable,
143143
updated_by: storage_scheme.to_string(),
144+
authentication_data: payment_attempt.authentication_data,
145+
encoded_data: payment_attempt.encoded_data,
144146
merchant_connector_id: payment_attempt.merchant_connector_id,
145147
};
146148
payment_attempts.push(payment_attempt.clone());

0 commit comments

Comments
 (0)