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
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
feat(core): add surcharge_percentage in surcharge details in payment response (#14354)
Swangi Kumarif46bd9901f37
diff --git a/api-reference/v1/openapi_spec_v1.json b/api-reference/v1/openapi_spec_v1.jsonindex adcfa1d84b..eccc7bff21 100644--- a/api-reference/v1/openapi_spec_v1.json+++ b/api-reference/v1/openapi_spec_v1.json@@ -24558,6 +24558,12 @@"external_surcharge_amount": {"$ref": "#/components/schemas/MinorUnit"},+ "surcharge_percentage": {+ "type": "number",+ "format": "double",+ "description": "Surcharge percentage returned by the connector (e.g. InterPayments), if provided.\nStored as a `Decimal` so the enclosing attempt model can keep deriving `Eq`.",+ "nullable": true+ },"sale_notified": {"type": "boolean","description": "Whether /v1/ch/sale has been successfully called"@@ -45188,6 +45194,13 @@}],"nullable": true+ },+ "surcharge_percentage": {+ "type": "number",+ "format": "double",+ "description": "The surcharge percentage returned by the surcharge connector (e.g. InterPayments), if\navailable. Present only on responses when an external surcharge connector supplied it;\nignored on requests.",+ "example": 3.25,+ "nullable": true}}},diff --git a/api-reference/v2/openapi_spec_v2.json b/api-reference/v2/openapi_spec_v2.jsonindex 07eee781f9..a65f3f92bd 100644--- a/api-reference/v2/openapi_spec_v2.json+++ b/api-reference/v2/openapi_spec_v2.json@@ -15725,6 +15725,12 @@"external_surcharge_amount": {"$ref": "#/components/schemas/MinorUnit"},+ "surcharge_percentage": {+ "type": "number",+ "format": "double",+ "description": "Surcharge percentage returned by the connector (e.g. InterPayments), if provided.\nStored as a `Decimal` so the enclosing attempt model can keep deriving `Eq`.",+ "nullable": true+ },"sale_notified": {"type": "boolean","description": "Whether /v1/ch/sale has been successfully called"@@ -31573,6 +31579,13 @@}],"nullable": true+ },+ "surcharge_percentage": {+ "type": "number",+ "format": "double",+ "description": "The surcharge percentage returned by the surcharge connector (e.g. InterPayments), if\navailable. Present only on responses when an external surcharge connector supplied it;\nignored on requests.",+ "example": 3.25,+ "nullable": true}}},diff --git a/crates/api_models/src/payments.rs b/crates/api_models/src/payments.rsindex df8f782923..2cf221953a 100644--- a/crates/api_models/src/payments.rs+++ b/crates/api_models/src/payments.rs@@ -1997,6 +1997,12 @@ pub struct RequestSurchargeDetails {pub surcharge_amount: MinorUnit,#[smithy(value_type = "Option<i64>")]pub tax_amount: Option<MinorUnit>,+ /// The surcharge percentage returned by the surcharge connector (e.g. InterPayments), if+ /// available. Present only on responses when an external surcharge connector supplied it;+ /// ignored on requests.+ #[schema(value_type = Option<f64>, example = 3.25)]+ #[smithy(value_type = "Option<f64>")]+ pub surcharge_percentage: Option<f64>,}// for v2 use the type from common_utils::typesdiff --git a/crates/common_types/src/payments.rs b/crates/common_types/src/payments.rsindex 4120ab7790..f65016f481 100644--- a/crates/common_types/src/payments.rs+++ b/crates/common_types/src/payments.rs@@ -1723,10 +1723,27 @@ pub struct ExternalSurchargeDetails {pub external_surcharge_id: String,/// Surcharge amount in minor unitspub external_surcharge_amount: MinorUnit,+ /// Surcharge percentage returned by the connector (e.g. InterPayments), if provided.+ /// Stored as a `Decimal` so the enclosing attempt model can keep deriving `Eq`.+ #[schema(value_type = Option<f64>)]+ pub surcharge_percentage: Option<Decimal>,/// Whether /v1/ch/sale has been successfully calledpub sale_notified: bool,}+impl ExternalSurchargeDetails {+ /// Convert an `f64` surcharge percentage (as produced by the surcharge connector) into the+ /// `Decimal` representation stored on the attempt. Returns `None` for `None`/non-finite input.+ pub fn decimal_percentage_from_f64(value: Option<f64>) -> Option<Decimal> {+ value.and_then(Decimal::from_f64)+ }++ /// The surcharge percentage as an `f64` for API responses, if present.+ pub fn surcharge_percentage_as_f64(&self) -> Option<f64> {+ self.surcharge_percentage.and_then(|value| value.to_f64())+ }+}+impl_to_sql_from_sql_json!(ExternalSurchargeDetails);/// Applied-offer details from Offer Engine `/apply`, persisted on `payment_attempt`diff --git a/crates/hyperswitch_domain_models/src/router_request_types.rs b/crates/hyperswitch_domain_models/src/router_request_types.rsindex 1ae06ab0ce..49f874993b 100644--- a/crates/hyperswitch_domain_models/src/router_request_types.rs+++ b/crates/hyperswitch_domain_models/src/router_request_types.rs@@ -1449,6 +1449,7 @@ pub struct ExternalSurchargeDetails {pub payment_method: common_enums::PaymentMethod,pub payment_method_type: Option<common_enums::PaymentMethodType>,pub external_surcharge_id: String,+ pub surcharge_percentage: Option<f64>,}impl ExternalSurchargeDetails {diff --git a/crates/router/src/core/payments.rs b/crates/router/src/core/payments.rsindex 469787a727..fe4e3c3362 100644--- a/crates/router/src/core/payments.rs+++ b/crates/router/src/core/payments.rs@@ -2356,6 +2356,10 @@ where.map(|cached| common_types::payments::ExternalSurchargeDetails {external_surcharge_id: cached.external_surcharge_id,external_surcharge_amount: cached.surcharge_amount,+ surcharge_percentage:+ common_types::payments::ExternalSurchargeDetails::decimal_percentage_from_f64(+ cached.surcharge_percentage,+ ),sale_notified: false,})};@@ -14695,6 +14699,7 @@ async fn previous_connector_surcharge_id(}#[cfg(all(feature = "oltp", feature = "v1"))]+#[allow(clippy::too_many_arguments)]async fn store_external_surcharge_in_redis(state: &SessionState,payment_id: &id_type::PaymentId,@@ -14703,6 +14708,7 @@ async fn store_external_surcharge_in_redis(payment_method: common_enums::PaymentMethod,payment_method_type: Option<common_enums::PaymentMethodType>,external_surcharge_id: String,+ surcharge_percentage: Option<f64>,) -> RouterResult<()> {let redis_conn = state.store@@ -14717,6 +14723,7 @@ async fn store_external_surcharge_in_redis(payment_method,payment_method_type,external_surcharge_id,+ surcharge_percentage,};redis_conn.serialize_and_set_key_with_expiry(@@ -14792,6 +14799,10 @@ async fn calculate_external_surcharge({Some(resp) => {let surcharge_amount = resp.surcharge_amount;+ let surcharge_percentage = resp+ .surcharge_fee_percent+ .as_ref()+ .map(|percent| percent.get_percentage());let external_surcharge_id = resp.connector_surcharge_id.clone();let merchant_id = processor.get_account().get_id().clone();let storage_scheme = processor.get_account().storage_scheme;@@ -14806,6 +14817,7 @@ async fn calculate_external_surcharge(inputs.payment_method,inputs.payment_method_type,external_surcharge_id,+ surcharge_percentage,).await.attach_printable("eligibility: failed to write surcharge to Redis")?;@@ -15031,6 +15043,12 @@ async fn calculate_mit_external_surcharge(Ok(Some(resp)) => Some(common_types::payments::ExternalSurchargeDetails {external_surcharge_id: resp.connector_surcharge_id,external_surcharge_amount: resp.surcharge_amount,+ surcharge_percentage:+ common_types::payments::ExternalSurchargeDetails::decimal_percentage_from_f64(+ resp.surcharge_fee_percent+ .as_ref()+ .map(|percent| percent.get_percentage()),+ ),sale_notified: false,}),Ok(None) => None,diff --git a/crates/router/src/core/payments/transformers.rs b/crates/router/src/core/payments/transformers.rsindex 2f740125e0..35bb57a18b 100644--- a/crates/router/src/core/payments/transformers.rs+++ b/crates/router/src/core/payments/transformers.rs@@ -3933,6 +3933,10 @@ where.map(|surcharge_amount| RequestSurchargeDetails {surcharge_amount,tax_amount: payment_attempt.net_amount.get_tax_on_surcharge(),+ surcharge_percentage: payment_attempt+ .external_surcharge_details+ .as_ref()+ .and_then(|details| details.surcharge_percentage_as_f64()),});let merchant_decision = payment_intent.merchant_decision.to_owned();let frm_message = payment_data.get_frm_message().map(FrmMessage::foreign_from);@@ -4866,6 +4870,10 @@ impl ForeignFrom<(storage::PaymentIntent, storage::PaymentAttempt)> for api::PayRequestSurchargeDetails {surcharge_amount,tax_amount: pa.net_amount.get_tax_on_surcharge(),+ surcharge_percentage: pa+ .external_surcharge_details+ .as_ref()+ .and_then(|details| details.surcharge_percentage_as_f64()),}}),merchant_decision: None,diff --git a/crates/router/src/core/webhooks/outgoing.rs b/crates/router/src/core/webhooks/outgoing.rsindex 26c862a20e..084ab5928a 100644--- a/crates/router/src/core/webhooks/outgoing.rs+++ b/crates/router/src/core/webhooks/outgoing.rs@@ -833,6 +833,7 @@ async fn update_payment_attempt_from_webhook_response(let updated_details = common_types::payments::ExternalSurchargeDetails {external_surcharge_id: existing_details.external_surcharge_id,external_surcharge_amount: existing_details.external_surcharge_amount,+ surcharge_percentage: existing_details.surcharge_percentage,sale_notified: true,};let update = hyperswitch_domain_models::payments::payment_attempt::PaymentAttemptUpdate::ExternalSurchargeUpdate {diff --git a/crates/router/src/types/storage/payment_attempt.rs b/crates/router/src/types/storage/payment_attempt.rsindex b4ccc03104..82e065099c 100644--- a/crates/router/src/types/storage/payment_attempt.rs+++ b/crates/router/src/types/storage/payment_attempt.rs@@ -88,6 +88,10 @@ impl PaymentAttemptExt for PaymentAttempt {|surcharge_amount| api_models::payments::RequestSurchargeDetails {surcharge_amount,tax_amount: self.net_amount.get_tax_on_surcharge(),+ surcharge_percentage: self+ .external_surcharge_details+ .as_ref()+ .and_then(|details| details.surcharge_percentage_as_f64()),},)}