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(revenue_recovery): keep customer_present and setup_future_usage on each recovery attempt (#15173)
CHALLA NISHANTH BABUbfaa6408aedd
diff --git a/api-reference/v2/openapi_spec_v2.json b/api-reference/v2/openapi_spec_v2.jsonindex eedef30d60..4b35217aa7 100644--- a/api-reference/v2/openapi_spec_v2.json+++ b/api-reference/v2/openapi_spec_v2.json@@ -23512,6 +23512,22 @@"type": "string","example": "ch_123abc456def789ghi012klmn","nullable": true+ },+ "customer_present": {+ "allOf": [+ {+ "$ref": "#/components/schemas/PresenceOfCustomerDuringPayment"+ }+ ],+ "nullable": true+ },+ "setup_future_usage": {+ "allOf": [+ {+ "$ref": "#/components/schemas/FutureUsage"+ }+ ],+ "nullable": true}}},diff --git a/crates/api_models/src/payments.rs b/crates/api_models/src/payments.rsindex 5f20e489f7..0986290b15 100644--- a/crates/api_models/src/payments.rs+++ b/crates/api_models/src/payments.rs@@ -2438,6 +2438,14 @@ pub struct PaymentAttemptRevenueRecoveryData {// stripe specific field used to identify duplicate attempts.#[schema(value_type = Option<String>, example = "ch_123abc456def789ghi012klmn")]pub charge_id: Option<String>,+ /// Whether the customer was present for the transaction of this attempt, as reported. Absent+ /// when it was not reported, and on attempts recorded before this was kept.+ #[schema(value_type = Option<PresenceOfCustomerDuringPayment>, example = "absent")]+ pub customer_present: Option<common_enums::PresenceOfCustomerDuringPayment>,+ /// How the payment method of this attempt's transaction is set up for future use, as reported.+ /// Absent when it was not reported, and on attempts recorded before this was kept.+ #[schema(value_type = Option<FutureUsage>, example = "off_session")]+ pub setup_future_usage: Option<common_enums::FutureUsage>,}#[derive(@@ -14717,6 +14725,18 @@ pub struct RecoveryPaymentsCreate {pub metadata: Option<pii::SecretSerdeValue>,}+#[cfg(feature = "v2")]+impl RecoveryPaymentsCreate {+ /// Who started the reported transaction, when the request says both whether the customer was+ /// present and how the payment method is set up for future use.+ pub fn get_transaction_initiator(&self) -> Option<common_enums::TransactionInitiator> {+ Some(common_enums::TransactionInitiator::new(+ self.customer_present?,+ self.setup_future_usage?,+ ))+ }+}+/// Error details for the payment#[cfg(feature = "v2")]#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, ToSchema)]diff --git a/crates/common_enums/src/enums.rs b/crates/common_enums/src/enums.rsindex d49f23eeb5..77fb8995a3 100644--- a/crates/common_enums/src/enums.rs+++ b/crates/common_enums/src/enums.rs@@ -417,6 +417,37 @@ pub enum RevenueRecoveryTriggerModel {ExternalTriggers,}+/// Who started a transaction of a revenue recovery invoice, worked out from whether the customer+/// was present and how the payment method is set up for future use. It is never stored: an attempt+/// keeps those two as reported.+#[derive(Clone, Copy, Debug, Eq, PartialEq)]+pub enum TransactionInitiator {+ /// The customer was present, paying on session, or also saving the payment method for+ /// off-session use.+ CustomerInitiated(FutureUsage),+ /// The merchant charged a payment method saved for off-session use, with the customer absent.+ MerchantInitiated,+}++impl TransactionInitiator {+ pub fn new(+ customer_present: PresenceOfCustomerDuringPayment,+ setup_future_usage: FutureUsage,+ ) -> Self {+ match (customer_present, setup_future_usage) {+ (PresenceOfCustomerDuringPayment::Absent, FutureUsage::OffSession) => {+ Self::MerchantInitiated+ }+ (PresenceOfCustomerDuringPayment::Present, setup_future_usage) => {+ Self::CustomerInitiated(setup_future_usage)+ }+ (PresenceOfCustomerDuringPayment::Absent, FutureUsage::OnSession) => {+ Self::CustomerInitiated(FutureUsage::OnSession)+ }+ }+ }+}+/// The retry implementations available within the `Smart` arm of revenue recovery.#[derive(Clone,@@ -11282,7 +11313,7 @@ pub enum MitExemptionRequest {/// Set to `present` to indicate that the customer is in your checkout flow during this payment, and therefore is able to authenticate. This parameter should be `absent` when merchant's doing merchant initiated payments and customer is not present while doing the payment.#[derive(- Clone, Copy, Debug, PartialEq, serde::Serialize, serde::Deserialize, Default, ToSchema,+ Clone, Copy, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize, Default, ToSchema,)]#[serde(rename_all = "snake_case")]pub enum PresenceOfCustomerDuringPayment {diff --git a/crates/diesel_models/src/payment_attempt.rs b/crates/diesel_models/src/payment_attempt.rsindex 5575aaabae..7d59a8014d 100644--- a/crates/diesel_models/src/payment_attempt.rs+++ b/crates/diesel_models/src/payment_attempt.rs@@ -4822,6 +4822,12 @@ pub struct PaymentAttemptRecoveryData {/// Transaction id returned by the billing connector when the payment was recorded/// back to it. Used to issue an offline refund if a dispute is later lost.pub billing_connector_transaction_id: Option<String>,+ /// Whether the customer was present for the transaction of this attempt, as reported. Absent on+ /// attempts recorded before this was kept.+ pub customer_present: Option<common_enums::PresenceOfCustomerDuringPayment>,+ /// How the payment method of this attempt's transaction is set up for future use, as reported.+ /// Absent on attempts recorded before this was kept.+ pub setup_future_usage: Option<common_enums::FutureUsage>,}#[cfg(feature = "v2")]common_utils::impl_to_sql_from_sql_json!(PaymentAttemptFeatureMetadata);diff --git a/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs b/crates/hyperswitch_domain_models/src/payments/payment_attempt.rsindex 5680732e38..e024935665 100644--- a/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs+++ b/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs@@ -1271,6 +1271,18 @@ impl PaymentAttempt {// Populated later, when the payment is recorded back to the// billing connector.billing_connector_transaction_id: None,+ customer_present: request.feature_metadata.as_ref().and_then(|metadata| {+ metadata+ .revenue_recovery+ .as_ref()+ .and_then(|data| data.customer_present)+ }),+ setup_future_usage: request.feature_metadata.as_ref().and_then(|metadata| {+ metadata+ .revenue_recovery+ .as_ref()+ .and_then(|data| data.setup_future_usage)+ }),}}),};@@ -3111,6 +3123,10 @@ pub struct PaymentAttemptRevenueRecoveryData {pub charge_id: Option<String>,/// Transaction id returned by the billing connector at record-back time.pub billing_connector_transaction_id: Option<String>,+ /// Whether the customer was present for the transaction of this attempt, as reported.+ pub customer_present: Option<common_enums::PresenceOfCustomerDuringPayment>,+ /// How the payment method of this attempt's transaction is set up for future use, as reported.+ pub setup_future_usage: Option<common_enums::FutureUsage>,}#[cfg(feature = "v2")]@@ -3125,6 +3141,8 @@ impl From<&PaymentAttemptFeatureMetadata> for DieselPaymentAttemptFeatureMetadatbilling_connector_transaction_id: recovery_data.billing_connector_transaction_id.clone(),+ customer_present: recovery_data.customer_present,+ setup_future_usage: recovery_data.setup_future_usage,});Self { revenue_recovery }}@@ -3140,6 +3158,8 @@ impl From<DieselPaymentAttemptFeatureMetadata> for PaymentAttemptFeatureMetadatacharge_id: recovery_data.charge_id,billing_connector_transaction_id: recovery_data.billing_connector_transaction_id,+ customer_present: recovery_data.customer_present,+ setup_future_usage: recovery_data.setup_future_usage,});Self { revenue_recovery }}diff --git a/crates/hyperswitch_domain_models/src/revenue_recovery.rs b/crates/hyperswitch_domain_models/src/revenue_recovery.rsindex 3a2bb6e386..6f4063eb22 100644--- a/crates/hyperswitch_domain_models/src/revenue_recovery.rs+++ b/crates/hyperswitch_domain_models/src/revenue_recovery.rs@@ -345,6 +345,8 @@ impl From<&payments::payment_attempt::PaymentAttempt> for RecoveryPaymentAttemptapi_payments::PaymentAttemptRevenueRecoveryData {attempt_triggered_by: recovery.attempt_triggered_by,charge_id: recovery.charge_id,+ customer_present: recovery.customer_present,+ setup_future_usage: recovery.setup_future_usage,}}),},diff --git a/crates/router/src/core/payments/transformers.rs b/crates/router/src/core/payments/transformers.rsindex aeb59fbc5f..e70cdfb5b5 100644--- a/crates/router/src/core/payments/transformers.rs+++ b/crates/router/src/core/payments/transformers.rs@@ -8327,6 +8327,8 @@ implapi_models::payments::PaymentAttemptRevenueRecoveryData {attempt_triggered_by: recovery.attempt_triggered_by,charge_id: recovery.charge_id.clone(),+ customer_present: recovery.customer_present,+ setup_future_usage: recovery.setup_future_usage,}});Self { revenue_recovery }diff --git a/crates/router/src/core/revenue_recovery/api.rs b/crates/router/src/core/revenue_recovery/api.rsindex bd57ed9d1d..95dd2400f7 100644--- a/crates/router/src/core/revenue_recovery/api.rs+++ b/crates/router/src/core/revenue_recovery/api.rs@@ -227,6 +227,8 @@ pub async fn record_internal_attempt_api(state,&RecoveryConnectorDetails::ConnectorAccounts(connector_accounts),common_enums::TriggeredBy::Internal,+ None,+ None,).await.change_context(errors::ApiErrorResponse::GenericNotFoundError {diff --git a/crates/router/src/core/webhooks/recovery_incoming.rs b/crates/router/src/core/webhooks/recovery_incoming.rsindex 806dfbecc5..6a17cd04a6 100644--- a/crates/router/src/core/webhooks/recovery_incoming.rs+++ b/crates/router/src/core/webhooks/recovery_incoming.rs@@ -511,6 +511,8 @@ impl RevenueRecoveryAttempt {profile,payment_intent,connector_details,+ data.customer_present,+ data.setup_future_usage,).await}@@ -717,6 +719,8 @@ impl RevenueRecoveryAttempt {profile: &domain::Profile,payment_intent: &revenue_recovery::RecoveryPaymentIntent,connector_details: &common_types::payments::RecoveryConnectorDetails,+ customer_present: Option<common_enums::PresenceOfCustomerDuringPayment>,+ setup_future_usage: Option<common_enums::FutureUsage>,) -> CustomResult<(revenue_recovery::RecoveryPaymentAttempt,@@ -729,6 +733,8 @@ impl RevenueRecoveryAttempt {state,connector_details,common_enums::TriggeredBy::External,+ customer_present,+ setup_future_usage,).await?;let attempt_response = Box::pin(payments::record_attempt_core(@@ -843,6 +849,8 @@ impl RevenueRecoveryAttempt {state: &SessionState,connector_details: &common_types::payments::RecoveryConnectorDetails,triggered_by: common_enums::TriggeredBy,+ customer_present: Option<common_enums::PresenceOfCustomerDuringPayment>,+ setup_future_usage: Option<common_enums::FutureUsage>,) -> CustomResult<api_payments::PaymentsAttemptRecordRequest, errors::RevenueRecoveryError>{let revenue_recovery_attempt_data = &self.0;@@ -853,6 +861,8 @@ impl RevenueRecoveryAttempt {// Since we are recording the external paymenmt attempt, this is hardcoded to Externalattempt_triggered_by: triggered_by,charge_id: self.0.charge_id.clone(),+ customer_present,+ setup_future_usage,}),};@@ -1006,6 +1016,8 @@ impl RevenueRecoveryAttempt {.get_connector_name(),},),+ None,+ None,).await})