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: add surcharge_applicable to payment_intent and remove surcharge_metadata from payment_attempt (#2642)

Hrithikeshe5fbaae0d427
diff --git a/crates/api_models/src/payment_methods.rs b/crates/api_models/src/payment_methods.rs
index 4656f6362c..cdb8ad6ad5 100644
--- a/crates/api_models/src/payment_methods.rs
+++ b/crates/api_models/src/payment_methods.rs
@@ -345,6 +345,23 @@ pub struct SurchargeMetadata {
pub surcharge_results: HashMap<String, SurchargeDetailsResponse>,
}
+impl SurchargeMetadata {
+ pub fn get_key_for_surcharge_details_hash_map(
+ payment_method: &common_enums::PaymentMethod,
+ payment_method_type: &common_enums::PaymentMethodType,
+ card_network: Option<&common_enums::CardNetwork>,
+ ) -> String {
+ if let Some(card_network) = card_network {
+ format!(
+ "{}_{}_{}",
+ payment_method, payment_method_type, card_network
+ )
+ } else {
+ format!("{}_{}", payment_method, payment_method_type)
+ }
+ }
+}
+
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize, ToSchema)]
#[serde(rename_all = "snake_case", tag = "type", content = "value")]
pub enum Surcharge {
diff --git a/crates/data_models/src/payments.rs b/crates/data_models/src/payments.rs
index 029f710850..4e7a0923f6 100644
--- a/crates/data_models/src/payments.rs
+++ b/crates/data_models/src/payments.rs
@@ -47,5 +47,7 @@ pub struct PaymentIntent {
// Manual review can occur when the transaction is marked as risky by the frm_processor, payment processor or when there is underpayment/over payment incase of crypto payment
pub merchant_decision: Option<String>,
pub payment_confirm_source: Option<storage_enums::PaymentSource>,
+
pub updated_by: String,
+ pub surcharge_applicable: Option<bool>,
}
diff --git a/crates/data_models/src/payments/payment_attempt.rs b/crates/data_models/src/payments/payment_attempt.rs
index c31230229d..b9021ae1ce 100644
--- a/crates/data_models/src/payments/payment_attempt.rs
+++ b/crates/data_models/src/payments/payment_attempt.rs
@@ -141,7 +141,6 @@ pub struct PaymentAttempt {
// reference to the payment at connector side
pub connector_response_reference_id: Option<String>,
pub amount_capturable: i64,
- pub surcharge_metadata: Option<serde_json::Value>,
pub updated_by: String,
}
@@ -201,7 +200,6 @@ pub struct PaymentAttemptNew {
pub connector_response_reference_id: Option<String>,
pub multiple_capture_count: Option<i16>,
pub amount_capturable: i64,
- pub surcharge_metadata: Option<serde_json::Value>,
pub updated_by: String,
}
@@ -323,10 +321,6 @@ pub enum PaymentAttemptUpdate {
connector_response_reference_id: Option<String>,
updated_by: String,
},
- SurchargeMetadataUpdate {
- surcharge_metadata: Option<serde_json::Value>,
- updated_by: String,
- },
}
impl ForeignIDRef for PaymentAttempt {
diff --git a/crates/data_models/src/payments/payment_intent.rs b/crates/data_models/src/payments/payment_intent.rs
index 155e6b5ca6..342818f43e 100644
--- a/crates/data_models/src/payments/payment_intent.rs
+++ b/crates/data_models/src/payments/payment_intent.rs
@@ -104,7 +104,9 @@ pub struct PaymentIntentNew {
pub merchant_decision: Option<String>,
pub payment_link_id: Option<String>,
pub payment_confirm_source: Option<storage_enums::PaymentSource>,
+
pub updated_by: String,
+ pub surcharge_applicable: Option<bool>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -204,7 +206,9 @@ pub struct PaymentIntentUpdateInternal {
// Manual review can occur when the transaction is marked as risky by the frm_processor, payment processor or when there is underpayment/over payment incase of crypto payment
pub merchant_decision: Option<String>,
pub payment_confirm_source: Option<storage_enums::PaymentSource>,
+
pub updated_by: String,
+ pub surcharge_applicable: Option<bool>,
}
impl PaymentIntentUpdate {
diff --git a/crates/diesel_models/src/payment_attempt.rs b/crates/diesel_models/src/payment_attempt.rs
index cf68154392..7d2033bcbb 100644
--- a/crates/diesel_models/src/payment_attempt.rs
+++ b/crates/diesel_models/src/payment_attempt.rs
@@ -57,7 +57,6 @@ pub struct PaymentAttempt {
// reference to the payment at connector side
pub connector_response_reference_id: Option<String>,
pub amount_capturable: i64,
- pub surcharge_metadata: Option<serde_json::Value>,
pub updated_by: String,
}
@@ -117,7 +116,6 @@ pub struct PaymentAttemptNew {
pub connector_response_reference_id: Option<String>,
pub multiple_capture_count: Option<i16>,
pub amount_capturable: i64,
- pub surcharge_metadata: Option<serde_json::Value>,
pub updated_by: String,
}
@@ -239,10 +237,6 @@ pub enum PaymentAttemptUpdate {
connector_response_reference_id: Option<String>,
updated_by: String,
},
- SurchargeMetadataUpdate {
- surcharge_metadata: Option<serde_json::Value>,
- updated_by: String,
- },
}
#[derive(Clone, Debug, Default, AsChangeset, router_derive::DebugAsDisplay)]
@@ -278,7 +272,6 @@ pub struct PaymentAttemptUpdateInternal {
surcharge_amount: Option<i64>,
tax_amount: Option<i64>,
amount_capturable: Option<i64>,
- surcharge_metadata: Option<serde_json::Value>,
updated_by: String,
}
@@ -330,7 +323,6 @@ impl PaymentAttemptUpdate {
amount_capturable: pa_update
.amount_capturable
.unwrap_or(source.amount_capturable),
- surcharge_metadata: pa_update.surcharge_metadata.or(source.surcharge_metadata),
updated_by: pa_update.updated_by,
..source
}
@@ -578,14 +570,6 @@ impl From<PaymentAttemptUpdate> for PaymentAttemptUpdateInternal {
updated_by,
..Default::default()
},
- PaymentAttemptUpdate::SurchargeMetadataUpdate {
- surcharge_metadata,
- updated_by,
- } => Self {
- surcharge_metadata,
- updated_by,
- ..Default::default()
- },
}
}
}
diff --git a/crates/diesel_models/src/payment_intent.rs b/crates/diesel_models/src/payment_intent.rs
index f449cadbba..31d157edb5 100644
--- a/crates/diesel_models/src/payment_intent.rs
+++ b/crates/diesel_models/src/payment_intent.rs
@@ -48,7 +48,9 @@ pub struct PaymentIntent {
pub merchant_decision: Option<String>,
pub payment_link_id: Option<String>,
pub payment_confirm_source: Option<storage_enums::PaymentSource>,
+
pub updated_by: String,
+ pub surcharge_applicable: Option<bool>,
}
#[derive(
@@ -101,7 +103,9 @@ pub struct PaymentIntentNew {
pub merchant_decision: Option<String>,
pub payment_link_id: Option<String>,
pub payment_confirm_source: Option<storage_enums::PaymentSource>,
+
pub updated_by: String,
+ pub surcharge_applicable: Option<bool>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -202,7 +206,9 @@ pub struct PaymentIntentUpdateInternal {
pub profile_id: Option<String>,
merchant_decision: Option<String>,
payment_confirm_source: Option<storage_enums::PaymentSource>,
+
pub updated_by: String,
+ pub surcharge_applicable: Option<bool>,
}
impl PaymentIntentUpdate {
diff --git a/crates/diesel_models/src/schema.rs b/crates/diesel_models/src/schema.rs
index 2cbd6027a7..2f3e7d345a 100644
--- a/crates/diesel_models/src/schema.rs
+++ b/crates/diesel_models/src/schema.rs
@@ -559,7 +559,6 @@ diesel::table! {
#[max_length = 128]
connector_response_reference_id -> Nullable<Varchar>,
amount_capturable -> Int8,
- surcharge_metadata -> Nullable<Jsonb>,
#[max_length = 32]
updated_by -> Varchar,
}
@@ -622,6 +621,7 @@ diesel::table! {
payment_confirm_source -> Nullable<PaymentSource>,
#[max_length = 32]
updated_by -> Varchar,
+ surcharge_applicable -> Nullable<Bool>,
}
}
diff --git a/crates/router/src/core/payments.rs b/crates/router/src/core/payments.rs
index 20712a6439..20a571cd94 100644
--- a/crates/router/src/core/payments.rs
+++ b/crates/router/src/core/payments.rs
@@ -214,6 +214,7 @@ where
&operation,
payment_data,
&customer,
+ None,
)
.await?
}
@@ -791,6 +792,7 @@ where
router_data_res
}
+#[allow(clippy::too_many_arguments)]
pub async fn call_multiple_connectors_service<F, Op, Req, Ctx>(
state: &AppState,
merchant_account: &domain::MerchantAccount,
@@ -799,6 +801,7 @@ pub async fn call_multiple_connectors_service<F, Op, Req, Ctx>(
_operation: &Op,
mut payment_data: PaymentData<F>,
customer: &Option<domain::Customer>,
+ session_surcharge_metadata: Option<SurchargeMetadata>,
) -> RouterResult<PaymentData<F>>
where
Op: Debug,
@@ -818,19 +821,6 @@ where
{
let call_connectors_start_time = Instant::now();
let mut join_handlers = Vec::with_capacity(connectors.len());
- let surcharge_metadata = payment_data
- .payment_attempt
- .surcharge_metadata
- .as_ref()
- .map(|surcharge_metadata_value| {
- surcharge_metadata_value
- .clone()
- .parse_value::<SurchargeMetadata>("SurchargeMetadata")
- })
- .transpose()
- .change_context(errors::ApiErrorResponse::InternalServerError)
- .attach_printable("Failed to Deserialize SurchargeMetadata")?;
-
for session_connector_data in connectors.iter() {
let connector_id = session_connector_data.connector.connector.id();
@@ -843,14 +833,18 @@ where
false,
)
.await?;
- payment_data.surcharge_details =
- surcharge_metadata.as_ref().and_then(|surcharge_metadata| {
- let payment_method_type = session_connector_data.payment_method_type;
- surcharge_metadata
- .surcharge_results
- .get(&payment_method_type.to_string())
- .cloned()
- });
+ payment_data.surcharge_details = session_surcharge_metadata
+ .as_ref()
+ .and_then(|surcharge_metadata| {
+ surcharge_metadata.surcharge_results.get(
+ &SurchargeMetadata::get_key_for_surcharge_details_hash_map(
+ &session_connector_data.payment_method_type.into(),
+ &session_connector_data.payment_method_type,
+ None,
+ ),
+ )
+ })
+ .cloned();
let router_data = payment_data
.construct_router_data(
diff --git a/crates/router/src/core/payments/helpers.rs b/crates/router/src/core/payments/helpers.rs
index efc752355d..d7d24b1540 100644
--- a/crates/router/src/core/payments/helpers.rs
+++ b/crates/router/src/core/payments/helpers.rs
@@ -2409,6 +2409,7 @@ mod tests {
profile_id: None,
merchant_decision: None,
payment_confirm_source: None,
+ surcharge_applicable: None,
updated_by: storage_enums::MerchantStorageScheme::PostgresOnly.to_string(),
};
let req_cs = Some("1".to_string());
@@ -2458,6 +2459,7 @@ mod tests {
profile_id: None,
merchant_decision: None,
payment_confirm_source: None,
+ surcharge_applicable: None,
updated_by: storage_enums::MerchantStorageScheme::PostgresOnly.to_string(),
};
let req_cs = Some("1".to_string());
@@ -2507,6 +2509,7 @@ mod tests {
profile_id: None,
merchant_decision: None,
payment_confirm_source: None,
+ surcharge_applicable: None,
updated_by: storage_enums::MerchantStorageScheme::PostgresOnly.to_string(),
};
let req_cs = Some("1".to_string());
@@ -2900,7 +2903,6 @@ impl AttemptType {
multiple_capture_count: None,
connector_response_reference_id: None,
amount_capturable: old_payment_attempt.amount,
- surcharge_metadata: old_payment_attempt.surcharge_metadata,
updated_by: storage_scheme.to_string(),
}
}
diff --git a/crates/router/src/core/payments/operations/payment_create.rs b/crates/router/src/core/payments/operations/payment_create.rs
index 2cc15fbfc3..87195510fc 100644
--- a/crates/router/src/core/payments/operations/payment_create.rs
+++ b/crates/router/src/core/payments/operations/payment_create.rs
@@ -720,6 +720,7 @@ impl PaymentCreate {
merchant_decision: None,
payment_link_id,
payment_confirm_source: None,
+ surcharge_applicable: None,
updated_by: merchant_account.storage_scheme.to_string(),
})
}
diff --git a/crates/router/src/core/payments/operations/payment_method_validate.rs b/crates/router/src/core/payments/operations/payment_method_validate.rs
index 6af25221b2..6d97f7b66c 100644
--- a/crates/router/src/core/payments/operations/payment_method_validate.rs
+++ b/crates/router/src/core/payments/operations/payment_method_validate.rs
@@ -401,6 +401,7 @@ impl PaymentMethodValidate {
profile_id: Default::default(),
merchant_decision: Default::default(),
payment_confirm_source: Default::default(),
+ surcharge_applicable: Default::default(),
payment_link_id: Default::default(),
updated_by: storage_scheme.to_string(),
}
diff --git a/crates/storage_impl/src/mock_db/payment_attempt.rs b/crates/storage_impl/src/mock_db/payment_attempt.rs
index 8674fd711a..34bf881717 100644
--- a/crates/storage_impl/src/mock_db/payment_attempt.rs
+++ b/crates/storage_impl/src/mock_db/payment_attempt.rs
@@ -140,7 +140,6 @@ impl PaymentAttemptInterface for MockDb {
multiple_capture_count: payment_attempt.multiple_capture_count,
connector_response_reference_id: None,
amount_capturable: payment_attempt.amount_capturable,
- surcharge_metadata: payment_attempt.surcharge_metadata,
updated_by: storage_scheme.to_string(),
};
payment_attempts.push(payment_attempt.clone());
diff --git a/crates/storage_impl/src/mock_db/payment_intent.rs b/crates/storage_impl/src/mock_db/payment_intent.rs
index edc17a0cf5..d1979cba01 100644
--- a/crates/storage_impl/src/mock_db/payment_intent.rs
+++ b/crates/storage_impl/src/mock_db/payment_intent.rs
@@ -104,6 +104,7 @@ impl PaymentIntentInterface for MockDb {
payment_link_id: new.payment_link_id,
payment_confirm_source: new.payment_confirm_source,
updated_by: storage_scheme.to_string(),
+ surcharge_applicable: new.surcharge_applicable,
};
payment_intents.push(payment_intent.clone());
Ok(payment_intent)
diff --git a/crates/storage_impl/src/payments/payment_attempt.rs b/crates/storage_impl/src/payments/payment_attempt.rs
index 518aaa2e3d..386c673b36 100644
--- a/crates/storage_impl/src/payments/payment_attempt.rs
+++ b/crates/storage_impl/src/payments/payment_attempt.rs
@@ -360,7 +360,7 @@ impl<T: DatabaseStore> PaymentAttemptInterface for KVRouterStore<T> {
multiple_capture_count: payment_attempt.multiple_capture_count,
connector_response_reference_id: None,
amount_capturable: payment_attempt.amount_capturable,
- surcharge_metadata: payment_attempt.surcharge_metadata.clone(),
+
updated_by: storage_scheme.to_string(),
};
@@ -956,7 +956,7 @@ 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,
- surcharge_metadata: self.surcharge_metadata,
+
updated_by: self.updated_by,
}
}
@@ -1006,7 +1006,7 @@ 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,
- surcharge_metadata: storage_model.surcharge_metadata,
+
updated_by: storage_model.updated_by,
}
}
@@ -1056,7 +1056,7 @@ impl DataModelExt for PaymentAttemptNew {
connector_response_reference_id: self.connector_response_reference_id,
multiple_capture_count: self.multiple_capture_count,
amount_capturable: self.amount_capturable,
- surcharge_metadata: self.surcharge_metadata,
+
updated_by: self.updated_by,
}
}
@@ -1104,7 +1104,7 @@ 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,
- surcharge_metadata: storage_model.surcharge_metadata,
+
updated_by: storage_model.updated_by,
}
}
@@ -1330,13 +1330,6 @@ impl DataModelExt for PaymentAttemptUpdate {
amount_capturable,
updated_by,
},
- Self::SurchargeMetadataUpdate {
- surcharge_metadata,
- updated_by,
- } => DieselPaymentAttemptUpdate::SurchargeMetadataUpdate {
- surcharge_metadata,
- updated_by,
- },
}
}
@@ -1557,13 +1550,6 @@ impl DataModelExt for PaymentAttemptUpdate {
amount_capturable,
updated_by,
},
- DieselPaymentAttemptUpdate::SurchargeMetadataUpdate {
- surcharge_metadata,
- updated_by,
- } => Self::SurchargeMetadataUpdate {
- surcharge_metadata,
- updated_by,
- },
}
}
}
diff --git a/crates/storage_impl/src/payments/payment_intent.rs b/crates/storage_impl/src/payments/payment_intent.rs
index e1149098ec..4631df5d15 100644
--- a/crates/storage_impl/src/payments/payment_intent.rs
+++ b/crates/storage_impl/src/payments/payment_intent.rs
@@ -96,6 +96,7 @@ impl<T: DatabaseStore> PaymentIntentInterface for KVRouterStore<T> {
payment_link_id: new.payment_link_id.clone(),
payment_confirm_source: new.payment_confirm_source,
updated_by: storage_scheme.to_string(),
+ surcharge_applicable: new.surcharge_applicable,
};
let redis_entry = kv::TypedSql {
op: kv::DBOperation::Insert {
@@ -752,6 +753,7 @@ impl DataModelExt for PaymentIntentNew {
payment_link_id: self.payment_link_id,
payment_confirm_source: self.payment_confirm_source,
updated_by: self.updated_by,
+ surcharge_applicable: self.surcharge_applicable,
}
}
@@ -791,6 +793,7 @@ impl DataModelExt for PaymentIntentNew {
payment_link_id: storage_model.payment_link_id,
payment_confirm_source: storage_model.payment_confirm_source,
updated_by: storage_model.updated_by,
+ surcharge_applicable: storage_model.surcharge_applicable,
}
}
}
@@ -835,6 +838,7 @@ impl DataModelExt for PaymentIntent {
payment_link_id: self.payment_link_id,
payment_confirm_source: self.payment_confirm_source,
updated_by: self.updated_by,
+ surcharge_applicable: self.surcharge_applicable,
}
}
@@ -875,6 +879,7 @@ impl DataModelExt for PaymentIntent {
payment_link_id: storage_model.payment_link_id,
payment_confirm_source: storage_model.payment_confirm_source,
updated_by: storage_model.updated_by,
+ surcharge_applicable: storage_model.surcharge_applicable,
}
}
}
diff --git a/migrations/2023-10-19-075810_add_surcharge_applicable_payment_intent/down.sql b/migrations/2023-10-19-075810_add_surcharge_applicable_payment_intent/down.sql
new file mode 100644
index 0000000000..8d4394202e
--- /dev/null
+++ b/migrations/2023-10-19-075810_add_surcharge_applicable_payment_intent/down.sql
@@ -0,0 +1,5 @@
+ALTER TABLE payment_attempt
+ADD COLUMN IF NOT EXISTS surcharge_metadata JSONB DEFAULT NULL;
+
+ALTER TABLE payment_intent
+DROP COLUMN surcharge_applicable;
diff --git a/migrations/2023-10-19-075810_add_surcharge_applicable_payment_intent/up.sql b/migrations/2023-10-19-075810_add_surcharge_applicable_payment_intent/up.sql
new file mode 100644
index 0000000000..8d5730ba09
--- /dev/null
+++ b/migrations/2023-10-19-075810_add_surcharge_applicable_payment_intent/up.sql
@@ -0,0 +1,6 @@
+ALTER TABLE payment_attempt
+DROP COLUMN surcharge_metadata;
+
+
+ALTER TABLE payment_intent
+ADD surcharge_applicable boolean;
\ No newline at end of file