Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

refactor(db): migrate to payment_attempt from connector_response (#2656)

Abhishek Marrivagu9d9fc2a8c5e9
diff --git a/crates/data_models/src/payments/payment_attempt.rs b/crates/data_models/src/payments/payment_attempt.rs
index a3ddbe8906..734de8fe4a 100644
--- a/crates/data_models/src/payments/payment_attempt.rs
+++ b/crates/data_models/src/payments/payment_attempt.rs
@@ -142,6 +142,8 @@ pub struct PaymentAttempt {
pub connector_response_reference_id: Option<String>,
pub amount_capturable: i64,
pub updated_by: String,
+ pub authentication_data: Option<serde_json::Value>,
+ pub encoded_data: Option<String>,
pub merchant_connector_id: Option<String>,
}
@@ -202,6 +204,8 @@ pub struct PaymentAttemptNew {
pub multiple_capture_count: Option<i16>,
pub amount_capturable: i64,
pub updated_by: String,
+ pub authentication_data: Option<serde_json::Value>,
+ pub encoded_data: Option<String>,
pub merchant_connector_id: Option<String>,
}
@@ -325,6 +329,13 @@ pub enum PaymentAttemptUpdate {
connector_response_reference_id: Option<String>,
updated_by: String,
},
+ ConnectorResponse {
+ authentication_data: Option<serde_json::Value>,
+ encoded_data: Option<String>,
+ connector_transaction_id: Option<String>,
+ connector: Option<String>,
+ updated_by: String,
+ },
}
impl ForeignIDRef for PaymentAttempt {
diff --git a/crates/diesel_models/src/payment_attempt.rs b/crates/diesel_models/src/payment_attempt.rs
index 8603aee5cb..0580861061 100644
--- a/crates/diesel_models/src/payment_attempt.rs
+++ b/crates/diesel_models/src/payment_attempt.rs
@@ -59,6 +59,8 @@ pub struct PaymentAttempt {
pub amount_capturable: i64,
pub updated_by: String,
pub merchant_connector_id: Option<String>,
+ pub authentication_data: Option<serde_json::Value>,
+ pub encoded_data: Option<String>,
}
#[derive(Clone, Debug, Eq, PartialEq, Queryable, Serialize, Deserialize)]
@@ -120,6 +122,8 @@ pub struct PaymentAttemptNew {
pub amount_capturable: i64,
pub updated_by: String,
pub merchant_connector_id: Option<String>,
+ pub authentication_data: Option<serde_json::Value>,
+ pub encoded_data: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -242,6 +246,13 @@ pub enum PaymentAttemptUpdate {
connector_response_reference_id: Option<String>,
updated_by: String,
},
+ ConnectorResponse {
+ authentication_data: Option<serde_json::Value>,
+ encoded_data: Option<String>,
+ connector_transaction_id: Option<String>,
+ connector: Option<String>,
+ updated_by: String,
+ },
}
#[derive(Clone, Debug, Default, AsChangeset, router_derive::DebugAsDisplay)]
@@ -279,6 +290,8 @@ pub struct PaymentAttemptUpdateInternal {
amount_capturable: Option<i64>,
updated_by: String,
merchant_connector_id: Option<String>,
+ authentication_data: Option<serde_json::Value>,
+ encoded_data: Option<String>,
}
impl PaymentAttemptUpdate {
@@ -331,6 +344,8 @@ impl PaymentAttemptUpdate {
.unwrap_or(source.amount_capturable),
updated_by: pa_update.updated_by,
merchant_connector_id: pa_update.merchant_connector_id,
+ authentication_data: pa_update.authentication_data.or(source.authentication_data),
+ encoded_data: pa_update.encoded_data.or(source.encoded_data),
..source
}
}
@@ -581,6 +596,20 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
updated_by,
..Default::default()
},
+ PaymentAttemptUpdate::ConnectorResponse {
+ authentication_data,
+ encoded_data,
+ connector_transaction_id,
+ connector,
+ updated_by,
+ } => Self {
+ authentication_data,
+ encoded_data,
+ connector_transaction_id,
+ connector,
+ updated_by,
+ ..Default::default()
+ },
}
}
}
diff --git a/crates/diesel_models/src/query/connector_response.rs b/crates/diesel_models/src/query/connector_response.rs
index eea7e779d8..952db945ae 100644
--- a/crates/diesel_models/src/query/connector_response.rs
+++ b/crates/diesel_models/src/query/connector_response.rs
@@ -1,5 +1,5 @@
use diesel::{associations::HasTable, BoolExpressionMethods, ExpressionMethods};
-use router_env::{instrument, tracing};
+use router_env::{instrument, logger, tracing};
use super::generics;
use crate::{
@@ -8,13 +8,44 @@ use crate::{
ConnectorResponseUpdateInternal,
},
errors,
- schema::connector_response::dsl,
+ payment_attempt::{PaymentAttempt, PaymentAttemptUpdate, PaymentAttemptUpdateInternal},
+ schema::{connector_response::dsl, payment_attempt::dsl as pa_dsl},
PgPooledConn, StorageResult,
};
impl ConnectorResponseNew {
#[instrument(skip(conn))]
pub async fn insert(self, conn: &PgPooledConn) -> StorageResult<ConnectorResponse> {
+ let payment_attempt_update = PaymentAttemptUpdate::ConnectorResponse {
+ authentication_data: self.authentication_data.clone(),
+ encoded_data: self.encoded_data.clone(),
+ connector_transaction_id: self.connector_transaction_id.clone(),
+ connector: self.connector_name.clone(),
+ updated_by: self.updated_by.clone(),
+ };
+
+ let _payment_attempt: Result<PaymentAttempt, _> =
+ generics::generic_update_with_unique_predicate_get_result::<
+ <PaymentAttempt as HasTable>::Table,
+ _,
+ _,
+ _,
+ >(
+ conn,
+ pa_dsl::attempt_id
+ .eq(self.attempt_id.to_owned())
+ .and(pa_dsl::merchant_id.eq(self.merchant_id.to_owned())),
+ PaymentAttemptUpdateInternal::from(payment_attempt_update),
+ )
+ .await
+ .map_err(|err| {
+ logger::error!(
+ "Error while updating payment attempt in connector_response flow {:?}",
+ err
+ );
+ err
+ });
+
generics::generic_insert(conn, self).await
}
}
@@ -26,27 +57,78 @@ impl ConnectorResponse {
conn: &PgPooledConn,
connector_response: ConnectorResponseUpdate,
) -> StorageResult<Self> {
- match generics::generic_update_with_unique_predicate_get_result::<
- <Self as HasTable>::Table,
- _,
- _,
- _,
- >(
- conn,
- dsl::merchant_id
- .eq(self.merchant_id.clone())
- .and(dsl::payment_id.eq(self.payment_id.clone()))
- .and(dsl::attempt_id.eq(self.attempt_id.clone())),
- ConnectorResponseUpdateInternal::from(connector_response),
- )
- .await
- {
- Err(error) => match error.current_context() {
- errors::DatabaseError::NoFieldsToUpdate => Ok(self),
- _ => Err(error),
+ let payment_attempt_update = match connector_response.clone() {
+ ConnectorResponseUpdate::ResponseUpdate {
+ connector_transaction_id,
+ authentication_data,
+ encoded_data,
+ connector_name,
+ updated_by,
+ } => PaymentAttemptUpdate::ConnectorResponse {
+ authentication_data,
+ encoded_data,
+ connector_transaction_id,
+ connector: connector_name,
+ updated_by,
},
- result => result,
- }
+ ConnectorResponseUpdate::ErrorUpdate {
+ connector_name,
+ updated_by,
+ } => PaymentAttemptUpdate::ConnectorResponse {
+ authentication_data: None,
+ encoded_data: None,
+ connector_transaction_id: None,
+ connector: connector_name,
+ updated_by,
+ },
+ };
+
+ let _payment_attempt: Result<PaymentAttempt, _> =
+ generics::generic_update_with_unique_predicate_get_result::<
+ <PaymentAttempt as HasTable>::Table,
+ _,
+ _,
+ _,
+ >(
+ conn,
+ pa_dsl::attempt_id
+ .eq(self.attempt_id.to_owned())
+ .and(pa_dsl::merchant_id.eq(self.merchant_id.to_owned())),
+ PaymentAttemptUpdateInternal::from(payment_attempt_update),
+ )
+ .await
+ .map_err(|err| {
+ logger::error!(
+ "Error while updating payment attempt in connector_response flow {:?}",
+ err
+ );
+ err
+ });
+
+ let connector_response_result =
+ match generics::generic_update_with_unique_predicate_get_result::<
+ <Self as HasTable>::Table,
+ _,
+ _,
+ _,
+ >(
+ conn,
+ dsl::merchant_id
+ .eq(self.merchant_id.clone())
+ .and(dsl::payment_id.eq(self.payment_id.clone()))
+ .and(dsl::attempt_id.eq(self.attempt_id.clone())),
+ ConnectorResponseUpdateInternal::from(connector_response),
+ )
+ .await
+ {
+ Err(error) => match error.current_context() {
+ errors::DatabaseError::NoFieldsToUpdate => Ok(self),
+ _ => Err(error),
+ },
+ result => result,
+ };
+
+ connector_response_result
}
#[instrument(skip(conn))]
@@ -56,14 +138,69 @@ impl ConnectorResponse {
merchant_id: &str,
attempt_id: &str,
) -> StorageResult<Self> {
- generics::generic_find_one::<<Self as HasTable>::Table, _, _>(
+ let connector_response: Self =
+ generics::generic_find_one::<<Self as HasTable>::Table, _, _>(
+ conn,
+ dsl::merchant_id.eq(merchant_id.to_owned()).and(
+ dsl::payment_id
+ .eq(payment_id.to_owned())
+ .and(dsl::attempt_id.eq(attempt_id.to_owned())),
+ ),
+ )
+ .await?;
+
+ match generics::generic_find_one::<<PaymentAttempt as HasTable>::Table, _, _>(
conn,
- dsl::merchant_id.eq(merchant_id.to_owned()).and(
- dsl::payment_id
- .eq(payment_id.to_owned())
- .and(dsl::attempt_id.eq(attempt_id.to_owned())),
+ pa_dsl::payment_id.eq(payment_id.to_owned()).and(
+ pa_dsl::merchant_id
+ .eq(merchant_id.to_owned())
+ .and(pa_dsl::attempt_id.eq(attempt_id.to_owned())),
),
)
.await
+ {
+ Ok::<PaymentAttempt, _>(payment_attempt) => {
+ if payment_attempt.authentication_data != connector_response.authentication_data {
+ logger::error!(
+ "Not Equal pa_authentication_data : {:?}, cr_authentication_data: {:?} ",
+ payment_attempt.authentication_data,
+ connector_response.authentication_data
+ );
+ }
+
+ if payment_attempt.encoded_data != connector_response.encoded_data {
+ logger::error!(
+ "Not Equal pa_encoded_data : {:?}, cr_encoded_data: {:?} ",
+ payment_attempt.encoded_data,
+ connector_response.encoded_data
+ );
+ }
+
+ if payment_attempt.connector_transaction_id
+ != connector_response.connector_transaction_id
+ {
+ logger::error!(
+ "Not Equal pa_connector_transaction_id : {:?}, cr_connector_transaction_id: {:?} ",
+ payment_attempt.connector_transaction_id,
+ connector_response.connector_transaction_id
+ );
+ }
+ if payment_attempt.connector != connector_response.connector_name {
+ logger::error!(
+ "Not Equal pa_connector : {:?}, cr_connector_name: {:?} ",
+ payment_attempt.connector,
+ connector_response.connector_name
+ );
+ }
+ }
+ Err(err) => {
+ logger::error!(
+ "Error while finding payment attempt in connector_response flow {:?}",
+ err
+ );
+ }
+ }
+
+ Ok(connector_response)
}
}
diff --git a/crates/diesel_models/src/schema.rs b/crates/diesel_models/src/schema.rs
index 926b31c0ce..02abfb842b 100644
--- a/crates/diesel_models/src/schema.rs
+++ b/crates/diesel_models/src/schema.rs
@@ -580,6 +580,8 @@ diesel::table! {
updated_by -> Varchar,
#[max_length = 32]
merchant_connector_id -> Nullable<Varchar>,
+ authentication_data -> Nullable<Json>,
+ encoded_data -> Nullable<Text>,
}
}
diff --git a/crates/router/src/core/payments/helpers.rs b/crates/router/src/core/payments/helpers.rs
index 4deac54c70..f78e37ac7d 100644
--- a/crates/router/src/core/payments/helpers.rs
+++ b/crates/router/src/core/payments/helpers.rs
@@ -2933,6 +2933,8 @@ impl AttemptType {
connector_response_reference_id: None,
amount_capturable: old_payment_attempt.amount,
updated_by: storage_scheme.to_string(),
+ authentication_data: None,
+ encoded_data: None,
merchant_connector_id: None,
}
}
diff --git a/crates/storage_impl/src/mock_db/payment_attempt.rs b/crates/storage_impl/src/mock_db/payment_attempt.rs
index a9f9b9d3c0..cb2f81daa7 100644
--- a/crates/storage_impl/src/mock_db/payment_attempt.rs
+++ b/crates/storage_impl/src/mock_db/payment_attempt.rs
@@ -141,6 +141,8 @@ impl PaymentAttemptInterface for MockDb {
connector_response_reference_id: None,
amount_capturable: payment_attempt.amount_capturable,
updated_by: storage_scheme.to_string(),
+ authentication_data: payment_attempt.authentication_data,
+ encoded_data: payment_attempt.encoded_data,
merchant_connector_id: payment_attempt.merchant_connector_id,
};
payment_attempts.push(payment_attempt.clone());
diff --git a/crates/storage_impl/src/payments/payment_attempt.rs b/crates/storage_impl/src/payments/payment_attempt.rs
index ae756c38c0..e3047221b6 100644
--- a/crates/storage_impl/src/payments/payment_attempt.rs
+++ b/crates/storage_impl/src/payments/payment_attempt.rs
@@ -361,6 +361,8 @@ impl<T: DatabaseStore> PaymentAttemptInterface for KVRouterStore<T> {
connector_response_reference_id: None,
amount_capturable: payment_attempt.amount_capturable,
updated_by: storage_scheme.to_string(),
+ authentication_data: payment_attempt.authentication_data.clone(),
+ encoded_data: payment_attempt.encoded_data.clone(),
merchant_connector_id: payment_attempt.merchant_connector_id.clone(),
};
@@ -956,8 +958,9 @@ impl DataModelExt for PaymentAttempt {
multiple_capture_count: self.multiple_capture_count,
connector_response_reference_id: self.connector_response_reference_id,
amount_capturable: self.amount_capturable,
-
updated_by: self.updated_by,
+ authentication_data: self.authentication_data,
+ encoded_data: self.encoded_data,
merchant_connector_id: self.merchant_connector_id,
}
}
@@ -1007,8 +1010,9 @@ impl DataModelExt for PaymentAttempt {
multiple_capture_count: storage_model.multiple_capture_count,
connector_response_reference_id: storage_model.connector_response_reference_id,
amount_capturable: storage_model.amount_capturable,
-
updated_by: storage_model.updated_by,
+ authentication_data: storage_model.authentication_data,
+ encoded_data: storage_model.encoded_data,
merchant_connector_id: storage_model.merchant_connector_id,
}
}
@@ -1060,6 +1064,8 @@ impl DataModelExt for PaymentAttemptNew {
amount_capturable: self.amount_capturable,
updated_by: self.updated_by,
+ authentication_data: self.authentication_data,
+ encoded_data: self.encoded_data,
merchant_connector_id: self.merchant_connector_id,
}
}
@@ -1107,8 +1113,9 @@ impl DataModelExt for PaymentAttemptNew {
connector_response_reference_id: storage_model.connector_response_reference_id,
multiple_capture_count: storage_model.multiple_capture_count,
amount_capturable: storage_model.amount_capturable,
-
updated_by: storage_model.updated_by,
+ authentication_data: storage_model.authentication_data,
+ encoded_data: storage_model.encoded_data,
merchant_connector_id: storage_model.merchant_connector_id,
}
}
@@ -1338,6 +1345,19 @@ impl DataModelExt for PaymentAttemptUpdate {
amount_capturable,
updated_by,
},
+ Self::ConnectorResponse {
+ authentication_data,
+ encoded_data,
+ connector_transaction_id,
+ connector,
+ updated_by,
+ } => DieselPaymentAttemptUpdate::ConnectorResponse {
+ authentication_data,
+ encoded_data,
+ connector_transaction_id,
+ connector,
+ updated_by,
+ },
}
}
@@ -1562,6 +1582,19 @@ impl DataModelExt for PaymentAttemptUpdate {
amount_capturable,
updated_by,
},
+ DieselPaymentAttemptUpdate::ConnectorResponse {
+ authentication_data,
+ encoded_data,
+ connector_transaction_id,
+ connector,
+ updated_by,
+ } => Self::ConnectorResponse {
+ authentication_data,
+ encoded_data,
+ connector_transaction_id,
+ connector,
+ updated_by,
+ },
}
}
}
diff --git a/migrations/2023-10-19-102636_back_fill_n_remove_connector_response/down.sql b/migrations/2023-10-19-102636_back_fill_n_remove_connector_response/down.sql
new file mode 100644
index 0000000000..7d14e420e9
--- /dev/null
+++ b/migrations/2023-10-19-102636_back_fill_n_remove_connector_response/down.sql
@@ -0,0 +1,2 @@
+
+ALTER TABLE payment_attempt DROP COLUMN authentication_data, DROP COLUMN encoded_data;
diff --git a/migrations/2023-10-19-102636_back_fill_n_remove_connector_response/up.sql b/migrations/2023-10-19-102636_back_fill_n_remove_connector_response/up.sql
new file mode 100644
index 0000000000..e80d4db1b4
--- /dev/null
+++ b/migrations/2023-10-19-102636_back_fill_n_remove_connector_response/up.sql
@@ -0,0 +1,11 @@
+-- Your SQL goes here
+ALTER TABLE payment_attempt
+ADD COLUMN authentication_data JSON,
+ADD COLUMN encoded_data TEXT;
+
+UPDATE payment_attempt
+SET (authentication_data, encoded_data) = (connector_response.authentication_data, connector_response.encoded_data)
+from connector_response
+where payment_attempt.payment_id = connector_response.payment_id
+ and payment_attempt.attempt_id = connector_response.attempt_id
+ and payment_attempt.merchant_id = connector_response.merchant_id;