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

feat(revenue_recovery): persist customer details for retry requests (#14817)

balakrishna4fafe50539ac
diff --git a/api-reference/v2/openapi_spec_v2.json b/api-reference/v2/openapi_spec_v2.json
index 46087ab1a2..98a0e518a3 100644
--- a/api-reference/v2/openapi_spec_v2.json
+++ b/api-reference/v2/openapi_spec_v2.json
@@ -25976,6 +25976,14 @@
],
"nullable": true
},
+ "customer_details": {
+ "allOf": [
+ {
+ "$ref": "#/components/schemas/CustomerDetails"
+ }
+ ],
+ "nullable": true
+ },
"shipping": {
"allOf": [
{
@@ -26815,6 +26823,14 @@
],
"nullable": true
},
+ "customer_details": {
+ "allOf": [
+ {
+ "$ref": "#/components/schemas/CustomerDetails"
+ }
+ ],
+ "nullable": true
+ },
"shipping": {
"allOf": [
{
@@ -31414,6 +31430,14 @@
],
"nullable": true
},
+ "customer_details": {
+ "allOf": [
+ {
+ "$ref": "#/components/schemas/CustomerDetails"
+ }
+ ],
+ "nullable": true
+ },
"payment_method_sub_type": {
"$ref": "#/components/schemas/PaymentMethodType"
},
diff --git a/crates/api_models/src/payments.rs b/crates/api_models/src/payments.rs
index aa40e64f59..75154968ca 100644
--- a/crates/api_models/src/payments.rs
+++ b/crates/api_models/src/payments.rs
@@ -277,6 +277,9 @@ pub struct PaymentsCreateIntentRequest {
/// The billing details of the payment. This address will be used for invoicing.
pub billing: Option<Address>,
+ /// Payer details stored with the payment intent; does not create or attach a customer.
+ pub customer_details: Option<CustomerDetails>,
+
/// The shipping address for the payment
pub shipping: Option<Address>,
@@ -8543,6 +8546,9 @@ pub struct PaymentsRequest {
/// The billing details of the payment. This address will be used for invoicing.
pub billing: Option<Address>,
+ /// Payer details stored with the payment intent; does not create or attach a customer.
+ pub customer_details: Option<CustomerDetails>,
+
/// The shipping address for the payment
pub shipping: Option<Address>,
@@ -8696,6 +8702,7 @@ impl From<&PaymentsRequest> for PaymentsCreateIntentRequest {
capture_method: request.capture_method,
authentication_type: request.authentication_type,
billing: request.billing.clone(),
+ customer_details: request.customer_details.clone(),
shipping: request.shipping.clone(),
customer_id: request.customer_id.clone(),
customer_present: request.customer_present,
@@ -14770,6 +14777,9 @@ pub struct RecoveryPaymentsCreate {
/// The billing details of the transaction.
pub billing: Option<Address>,
+ /// Payer details stored with the payment intent; does not create or attach a customer.
+ pub customer_details: Option<CustomerDetails>,
+
/// The payment method subtype used for the transaction.
#[schema(value_type = PaymentMethodType, example = "apple_pay")]
pub payment_method_sub_type: api_enums::PaymentMethodType,
diff --git a/crates/hyperswitch_connectors/src/connectors/chargebee/transformers.rs b/crates/hyperswitch_connectors/src/connectors/chargebee/transformers.rs
index 653ea9cd80..e910273d03 100644
--- a/crates/hyperswitch_connectors/src/connectors/chargebee/transformers.rs
+++ b/crates/hyperswitch_connectors/src/connectors/chargebee/transformers.rs
@@ -1089,6 +1089,7 @@ impl TryFrom<ChargebeeInvoiceBody> for revenue_recovery::RevenueRecoveryInvoiceD
currency: item.content.invoice.currency_code,
merchant_reference_id,
billing_address: Some(api_models::payments::Address::from(item.content.invoice)),
+ customer_details: None,
retry_count,
next_billing_at: invoice_next_billing_time,
billing_started_at,
diff --git a/crates/hyperswitch_connectors/src/connectors/stripebilling/transformers.rs b/crates/hyperswitch_connectors/src/connectors/stripebilling/transformers.rs
index 610f9e0a43..5018c21a69 100644
--- a/crates/hyperswitch_connectors/src/connectors/stripebilling/transformers.rs
+++ b/crates/hyperswitch_connectors/src/connectors/stripebilling/transformers.rs
@@ -422,6 +422,7 @@ impl TryFrom<StripebillingInvoiceBody> for revenue_recovery::RevenueRecoveryInvo
.object
.customer_address
.map(api_models::payments::Address::from),
+ customer_details: None,
retry_count: Some(item.data.object.attempt_count),
next_billing_at,
billing_started_at,
diff --git a/crates/hyperswitch_domain_models/src/payments.rs b/crates/hyperswitch_domain_models/src/payments.rs
index 496a20102d..7bf51cc094 100644
--- a/crates/hyperswitch_domain_models/src/payments.rs
+++ b/crates/hyperswitch_domain_models/src/payments.rs
@@ -502,6 +502,23 @@ impl PaymentIntent {
}
}
+ /// Read the payer snapshot stored on this intent without looking up a customer record.
+ #[cfg(feature = "v2")]
+ pub fn get_customer_details(
+ &self,
+ ) -> Result<Option<CustomerData>, common_utils::errors::ParsingError> {
+ self.customer_details
+ .as_ref()
+ .map(|details| {
+ ValueExt::parse_value::<CustomerData>(
+ details.clone().into_inner().expose(),
+ "CustomerData",
+ )
+ })
+ .transpose()
+ .map_err(|report| (*report.current_context()).clone())
+ }
+
pub fn get_customer_document_details(
&self,
) -> Result<Option<CustomerDocumentDetails>, common_utils::errors::ParsingError> {
@@ -1203,7 +1220,7 @@ impl PaymentIntent {
.unwrap_or_default(),
split_txns_enabled: profile.split_txns_enabled,
frm_metadata: request.frm_metadata,
- customer_details: None,
+ customer_details: decrypted_payment_intent.customer_details.clone(),
merchant_reference_id: request.merchant_reference_id,
billing_address: decrypted_payment_intent
.billing_address
diff --git a/crates/hyperswitch_domain_models/src/payments/payment_intent.rs b/crates/hyperswitch_domain_models/src/payments/payment_intent.rs
index 74350b6a00..5bf3ad10ac 100644
--- a/crates/hyperswitch_domain_models/src/payments/payment_intent.rs
+++ b/crates/hyperswitch_domain_models/src/payments/payment_intent.rs
@@ -182,6 +182,21 @@ pub struct CustomerData {
pub date_of_birth: Option<Secret<time::Date>>,
}
+#[cfg(feature = "v2")]
+impl From<api_models::payments::CustomerDetails> for CustomerData {
+ fn from(customer: api_models::payments::CustomerDetails) -> Self {
+ Self {
+ name: customer.name,
+ email: customer.email,
+ phone: customer.phone,
+ phone_country_code: customer.phone_country_code,
+ tax_registration_id: customer.tax_registration_id,
+ customer_document_details: customer.document_details,
+ date_of_birth: customer.date_of_birth,
+ }
+ }
+}
+
impl CustomerData {
pub fn fill_missing_fields(&mut self, other: &Self) {
self.name = self.name.clone().or_else(|| other.name.clone());
diff --git a/crates/hyperswitch_domain_models/src/revenue_recovery.rs b/crates/hyperswitch_domain_models/src/revenue_recovery.rs
index 0d1d49e491..568a52f9ce 100644
--- a/crates/hyperswitch_domain_models/src/revenue_recovery.rs
+++ b/crates/hyperswitch_domain_models/src/revenue_recovery.rs
@@ -76,6 +76,8 @@ pub struct RevenueRecoveryInvoiceData {
pub merchant_reference_id: id_type::PaymentReferenceId,
/// billing address id of the invoice
pub billing_address: Option<api_payments::Address>,
+ /// Payer details supplied with the recovery invoice.
+ pub customer_details: Option<api_payments::CustomerDetails>,
/// Retry count of the invoice
pub retry_count: Option<u16>,
/// Ending date of the invoice or the Next billing time of the Subscription
@@ -153,6 +155,7 @@ impl From<&RevenueRecoveryInvoiceData> for api_payments::PaymentsCreateIntentReq
capture_method: Some(common_enums::CaptureMethod::Automatic),
authentication_type: Some(common_enums::AuthenticationType::NoThreeDs),
billing: data.billing_address.clone(),
+ customer_details: data.customer_details.clone(),
shipping: None,
customer_id: None,
customer_present: Some(common_enums::PresenceOfCustomerDuringPayment::Absent),
@@ -189,6 +192,7 @@ impl From<&BillingConnectorInvoiceSyncResponse> for RevenueRecoveryInvoiceData {
currency: data.currency,
merchant_reference_id: data.merchant_reference_id.clone(),
billing_address: data.billing_address.clone(),
+ customer_details: None,
retry_count: data.retry_count,
next_billing_at: data.ends_at,
billing_started_at: data.created_at,
diff --git a/crates/router/src/core/payments/operations/payment_create_intent.rs b/crates/router/src/core/payments/operations/payment_create_intent.rs
index 229fe64943..267b75eb6c 100644
--- a/crates/router/src/core/payments/operations/payment_create_intent.rs
+++ b/crates/router/src/core/payments/operations/payment_create_intent.rs
@@ -122,7 +122,13 @@ impl<F: Send + Clone + Sync>
hyperswitch_domain_models::payments::FromRequestEncryptablePaymentIntent {
shipping_address: request.shipping.clone().map(|address| address.encode_to_value()).transpose().change_context(errors::ApiErrorResponse::InternalServerError).attach_printable("Failed to encode shipping address")?.map(hyperswitch_masking::Secret::new),
billing_address: request.billing.clone().map(|address| address.encode_to_value()).transpose().change_context(errors::ApiErrorResponse::InternalServerError).attach_printable("Failed to encode billing address")?.map(hyperswitch_masking::Secret::new),
- customer_details: None,
+ customer_details: request.customer_details.clone()
+ .map(hyperswitch_domain_models::payments::payment_intent::CustomerData::from)
+ .map(|customer| customer.encode_to_value())
+ .transpose()
+ .change_context(errors::ApiErrorResponse::InternalServerError)
+ .attach_printable("Failed to encode customer details")?
+ .map(hyperswitch_masking::Secret::new),
recipient_details: request.recipient_details.clone().map(|recipient| recipient.encode_to_value()).transpose().change_context(errors::ApiErrorResponse::InternalServerError).attach_printable("Failed to encode recipient details")?.map(hyperswitch_masking::Secret::new),
},
),
diff --git a/crates/router/src/core/payments/transformers.rs b/crates/router/src/core/payments/transformers.rs
index eff7db8d0c..8852dba107 100644
--- a/crates/router/src/core/payments/transformers.rs
+++ b/crates/router/src/core/payments/transformers.rs
@@ -410,10 +410,22 @@ pub async fn construct_payment_router_data_for_authorize<'a>(
.ok_or(errors::ApiErrorResponse::InternalServerError)
.attach_printable("connector_request_reference_id not found in payment_attempt")?;
- let email = customer
+ let customer_details = payment_data
+ .payment_intent
+ .get_customer_details()
+ .change_context(errors::ApiErrorResponse::InternalServerError)
+ .attach_printable("Failed to extract customer details from payment_intent")?;
+ let email = customer_details
.as_ref()
- .and_then(|customer| customer.email.clone())
- .map(pii::Email::from);
+ .and_then(|details| details.email.clone())
+ .or_else(|| {
+ customer
+ .as_ref()
+ .and_then(|customer| customer.email.clone())
+ .map(pii::Email::from)
+ });
+ let customer_document_details =
+ customer_details.and_then(|details| details.customer_document_details);
let browser_info = payment_data
.payment_attempt
@@ -618,7 +630,7 @@ pub async fn construct_payment_router_data_for_authorize<'a>(
l2_l3_data: None,
minor_amount_capturable: None,
authorized_amount: None,
- customer_document_details: None,
+ customer_document_details,
feature_data: None,
sender_payment_instrument_id: None,
connector_returned_payment_method_details: None,
diff --git a/crates/router/src/core/revenue_recovery/api.rs b/crates/router/src/core/revenue_recovery/api.rs
index e96d50246b..a00e37a1eb 100644
--- a/crates/router/src/core/revenue_recovery/api.rs
+++ b/crates/router/src/core/revenue_recovery/api.rs
@@ -314,6 +314,8 @@ pub async fn custom_revenue_recovery_core(
})
})?;
+ validate_customer_information(&request)?;
+
let recovery_intent =
recovery_incoming::RevenueRecoveryInvoice::get_or_create_custom_recovery_intent(
request.clone(),
@@ -444,3 +446,21 @@ pub async fn cancel_revenue_recovery_core(
response,
))
}
+
+fn validate_customer_information(
+ request: &api_models::payments::RecoveryPaymentsCreate,
+) -> RouterResult<()> {
+ if let Some(document) = request
+ .customer_details
+ .as_ref()
+ .and_then(|customer| customer.document_details.as_ref())
+ {
+ document
+ .validate()
+ .map_err(|err| errors::ApiErrorResponse::PreconditionFailed {
+ message: err.to_string(),
+ })?;
+ }
+
+ Ok(())
+}
diff --git a/crates/router/src/core/revenue_recovery/transformers.rs b/crates/router/src/core/revenue_recovery/transformers.rs
index 0debf7b0e7..711c875e84 100644
--- a/crates/router/src/core/revenue_recovery/transformers.rs
+++ b/crates/router/src/core/revenue_recovery/transformers.rs
@@ -53,6 +53,7 @@ impl ForeignFrom<api_models::payments::RecoveryPaymentsCreate>
currency: data.amount_details.currency(),
merchant_reference_id: data.merchant_reference_id,
billing_address: data.billing,
+ customer_details: data.customer_details,
retry_count: None,
next_billing_at: None,
billing_started_at: Some(data.billing_started_at),