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
fix(ucs): record request rejections as payment failures (#14187)
Pa10654e8161e33
diff --git a/crates/router/src/core/payments.rs b/crates/router/src/core/payments.rsindex edc6a17ada..a7beda12c5 100644--- a/crates/router/src/core/payments.rs+++ b/crates/router/src/core/payments.rs@@ -645,6 +645,136 @@ where))}+/// Whether a rejection reached us before the connector was called, so the payment can be put back+/// to the state it had before the pre-update tracker committed. A response-phase failure must not+/// be rolled back, since the outcome is then unknown.+#[cfg(feature = "v1")]+trait PreCallRejection {+ fn rejected_before_connector_call(&self) -> bool;+}++#[cfg(feature = "v1")]+impl PreCallRejection for error_stack::Report<errors::ApiErrorResponse> {+ fn rejected_before_connector_call(&self) -> bool {+ self.downcast_ref::<errors::ConnectorError>()+ .is_some_and(|connector_error| {+ matches!(+ connector_error,+ errors::ConnectorError::NotSupported { .. }+ | errors::ConnectorError::NotImplemented(_)+ | errors::ConnectorError::MissingRequiredField { .. }+ | errors::ConnectorError::MissingRequiredFields { .. }+ | errors::ConnectorError::RequestEncodingFailed+ | errors::ConnectorError::FailedToObtainAuthType+ | errors::ConnectorError::InvalidConnectorName+ )+ })+ }+}++/// The attempt and intent as they were fetched from the DB, before this flow mutated them.+#[cfg(feature = "v1")]+pub trait PreviousDbRecords {+ fn get_previous_db_records(&self) -> &(storage::PaymentAttempt, storage::PaymentIntent);+}++#[cfg(feature = "v1")]+impl<F: Clone> PreviousDbRecords for PaymentData<F> {+ fn get_previous_db_records(&self) -> &(storage::PaymentAttempt, storage::PaymentIntent) {+ &self.previous_db_records+ }+}++/// Put the payment back to the state it had when it was fetched from the DB, so a UCS rejection+/// leaves it retryable as the direct path does. A write failure is logged and swallowed.+#[cfg(feature = "v1")]+async fn restore_pre_call_state<F, D>(+ state: &SessionState,+ processor: &domain::Processor,+ payment_data: &D,+) where+ F: Send + Clone + Sync + Debug + 'static,+ D: OperationSessionGetters<F> + PreviousDbRecords + Send + Sync,+{+ let (attempt, intent) = payment_data.get_previous_db_records();++ let storage_scheme = processor.get_account().storage_scheme;+ let key_store = processor.get_key_store();++ let attempt_update = storage::PaymentAttemptUpdate::ConfirmUpdate {+ net_amount: attempt.net_amount.clone(),+ currency: payment_data.get_currency(),+ status: attempt.status,+ authentication_type: attempt.authentication_type,+ capture_method: attempt.capture_method,+ payment_method: attempt.payment_method,+ browser_info: attempt.browser_info.clone(),+ connector: attempt.connector.clone(),+ payment_token: attempt.payment_token.clone(),+ payment_method_data: attempt.payment_method_data.clone(),+ payment_method_type: attempt.payment_method_type,+ payment_experience: attempt.payment_experience,+ business_sub_label: attempt.business_sub_label.clone(),+ straight_through_algorithm: attempt.straight_through_algorithm.clone(),+ error_code: Some(attempt.error_code.clone()),+ error_message: Some(attempt.error_message.clone()),+ updated_by: storage_scheme.to_string(),+ merchant_connector_id: attempt.merchant_connector_id.clone(),+ external_three_ds_authentication_attempted: attempt+ .external_three_ds_authentication_attempted,+ external_threeds_authentication_type: attempt.external_threeds_authentication_type,+ authentication_connector: attempt.authentication_connector.clone(),+ authentication_id: attempt.authentication_id.clone(),+ payment_method_billing_address_id: attempt.payment_method_billing_address_id.clone(),+ fingerprint_id: attempt.fingerprint_id.clone(),+ fingerprint_type: attempt.fingerprint_type,+ payment_method_id: attempt.payment_method_id.clone(),+ client_source: attempt.client_source.clone(),+ client_version: attempt.client_version.clone(),+ customer_acceptance: attempt.customer_acceptance.clone(),+ installment_data: attempt.installment_data.clone(),+ connector_mandate_detail: attempt.connector_mandate_detail.clone(),+ tokenization: attempt.tokenization,+ card_discovery: attempt.card_discovery,+ routing_approach: attempt.routing_approach.clone(),+ connector_request_reference_id: attempt.connector_request_reference_id.clone(),+ network_transaction_id: attempt.network_transaction_id.clone(),+ network_transaction_link_id: attempt.network_transaction_link_id.clone(),+ is_stored_credential: attempt.is_stored_credential,+ request_extended_authorization: attempt.request_extended_authorization,+ external_surcharge_details: attempt.external_surcharge_details.clone(),+ applied_offer_details: attempt.applied_offer_details.clone(),+ active_frm_id: attempt.active_frm_id.clone(),+ };++ let intent_update = storage::PaymentIntentUpdate::PGStatusUpdate {+ status: enums::IntentStatus::from(attempt.status),+ updated_by: storage_scheme.to_string(),+ incremental_authorization_allowed: intent.incremental_authorization_allowed,+ feature_metadata: intent.feature_metadata.clone(),+ };++ let (attempt_result, intent_result) = tokio::join!(+ state.store.update_payment_attempt_with_attempt_id(+ attempt.clone(),+ attempt_update,+ storage_scheme,+ key_store,+ ),+ state+ .store+ .update_payment_intent(intent.clone(), intent_update, key_store, storage_scheme),+ );++ if let Err(restore_error) = attempt_result {+ logger::error!(?restore_error, "failed to restore the payment attempt");+ }++ if let Err(restore_error) = intent_result {+ logger::error!(?restore_error, "failed to restore the payment intent");+ }+}+#[cfg(feature = "v1")]#[allow(clippy::too_many_arguments, clippy::type_complexity)]#[instrument(skip_all, fields(payment_id, merchant_id))]@@ -667,7 +797,12 @@ whereF: Send + Clone + Sync + Debug + 'static,Req: Authenticate + Clone,Op: Operation<F, Req, Data = D> + Send + Sync,- D: OperationSessionGetters<F> + OperationSessionSetters<F> + Send + Sync + Clone,+ D: OperationSessionGetters<F>+ + OperationSessionSetters<F>+ + PreviousDbRecords+ + Send+ + Sync+ + Clone,// To create connector flow specific interface dataD: ConstructFlowSpecificData<F, FData, router_types::PaymentsResponseData>,@@ -724,6 +859,12 @@ wherepayment_pre_fetched_info,).await?;++ // The attempt and intent as fetched from the DB, before this flow mutates them.+ let previous_attempt = payment_data.get_payment_attempt().clone();+ let previous_intent = payment_data.get_payment_intent().clone();+ payment_data.set_previous_db_records(previous_attempt, previous_intent);+let dimensions = dimensions.with_profile_id(business_profile.get_id().clone());operation@@ -1076,7 +1217,7 @@ where).await?;- let (router_data, mca) = Box::pin(complete_connector_service(+ let (router_data, mca) = match Box::pin(complete_connector_service(&updated_state,platform.get_processor(),&operation,@@ -1095,7 +1236,21 @@ wherecall_connector_service_response,&dimensions.without_profile_id(),))- .await?;+ .await+ {+ Ok(result) => result,+ Err(api_error) => {+ if api_error.rejected_before_connector_call() {+ restore_pre_call_state(+ state,+ platform.get_processor(),+ &payment_data,+ )+ .await;+ }+ return Err(api_error);+ }+ };let op_ref = &operation;let should_trigger_post_processing_flows = is_operation_confirm(&operation);@@ -1267,7 +1422,7 @@ where).await?;- let (router_data, mca) = Box::pin(complete_connector_service(+ let (router_data, mca) = match Box::pin(complete_connector_service(&updated_state,platform.get_processor(),&operation,@@ -1286,7 +1441,21 @@ wherecall_connector_service_response,&dimensions.without_profile_id(),))- .await?;+ .await+ {+ Ok(result) => result,+ Err(api_error) => {+ if api_error.rejected_before_connector_call() {+ restore_pre_call_state(+ state,+ platform.get_processor(),+ &payment_data,+ )+ .await;+ }+ return Err(api_error);+ }+ };#[cfg(all(feature = "retry", feature = "v1"))]let mut router_data = router_data;@@ -2887,7 +3056,12 @@ whereFData: Send + Sync + Clone + router_types::Capturable + 'static + serde::Serialize,Op: Operation<F, Req, Data = D> + Send + Sync + Clone,Req: Debug + Authenticate + Clone,- D: OperationSessionGetters<F> + OperationSessionSetters<F> + Send + Sync + Clone,+ D: OperationSessionGetters<F>+ + OperationSessionSetters<F>+ + PreviousDbRecords+ + Send+ + Sync+ + Clone,Res: transformers::ToResponse<F, D, Op>,// To create connector flow specific interface dataD: ConstructFlowSpecificData<F, FData, router_types::PaymentsResponseData>,@@ -10098,6 +10272,9 @@ where/// Fields from the update request payload used to compare against/// the stored payment intent. Populated only for the payment-update flow.pub update_request_fields: Option<PaymentDataUpdateRequestFields>,+ /// The attempt and intent as they were fetched from the DB, before this flow mutated them.+ /// A UCS pre-call rejection restores the rows from these records.+ pub previous_db_records: (storage::PaymentAttempt, storage::PaymentIntent),}/// Decrypts an Apple Pay wallet token for the pre-confirm eligibility check, using the specific@@ -15758,6 +15935,13 @@ pub trait OperationSessionSetters<F> {#[cfg(feature = "v2")]fn set_client_secret(&mut self, client_secret: Option<Secret<String>>);fn set_payment_attempt(&mut self, payment_attempt: storage::PaymentAttempt);+ /// Store the attempt and intent as they were fetched from the DB.+ fn set_previous_db_records(+ &mut self,+ _attempt: storage::PaymentAttempt,+ _intent: storage::PaymentIntent,+ ) {+ }fn set_payment_method_data(&mut self, payment_method_data: Option<domain::PaymentMethodData>);fn set_payment_method_token(&mut self, payment_method_token: Option<PaymentMethodToken>);fn set_payment_method_info(&mut self, payment_method_info: Option<domain::PaymentMethod>);@@ -16036,6 +16220,14 @@ impl<F: Clone> OperationSessionSetters<F> for PaymentData<F> {self.payment_attempt = payment_attempt;}+ fn set_previous_db_records(+ &mut self,+ attempt: storage::PaymentAttempt,+ intent: storage::PaymentIntent,+ ) {+ self.previous_db_records = (attempt, intent);+ }+fn set_payment_method_data(&mut self, payment_method_data: Option<domain::PaymentMethodData>) {self.payment_method_data = payment_method_data;}diff --git a/crates/router/src/core/payments/operations/payment_approve.rs b/crates/router/src/core/payments/operations/payment_approve.rsindex 278dd1d383..813c00f273 100644--- a/crates/router/src/core/payments/operations/payment_approve.rs+++ b/crates/router/src/core/payments/operations/payment_approve.rs@@ -165,6 +165,7 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsCaptureRlet payment_data = PaymentData {flow: PhantomData,+ previous_db_records: (payment_attempt.clone(), payment_intent.clone()),payment_intent,payment_attempt,currency,diff --git a/crates/router/src/core/payments/operations/payment_cancel.rs b/crates/router/src/core/payments/operations/payment_cancel.rsindex fefabe590d..3e370daf49 100644--- a/crates/router/src/core/payments/operations/payment_cancel.rs+++ b/crates/router/src/core/payments/operations/payment_cancel.rs@@ -168,6 +168,7 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsCancelRelet payment_data = PaymentData {flow: PhantomData,+ previous_db_records: (payment_attempt.clone(), payment_intent.clone()),payment_intent,payment_attempt,currency,diff --git a/crates/router/src/core/payments/operations/payment_cancel_post_capture.rs b/crates/router/src/core/payments/operations/payment_cancel_post_capture.rsindex bfbcad236e..d642bfaa54 100644--- a/crates/router/src/core/payments/operations/payment_cancel_post_capture.rs+++ b/crates/router/src/core/payments/operations/payment_cancel_post_capture.rs@@ -150,6 +150,7 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsCancelPolet payment_data = PaymentData {flow: PhantomData,+ previous_db_records: (payment_attempt.clone(), payment_intent.clone()),payment_intent,payment_attempt,currency,diff --git a/crates/router/src/core/payments/operations/payment_cancel_post_capture_sync.rs b/crates/router/src/core/payments/operations/payment_cancel_post_capture_sync.rsindex c4b72d4ccf..10b6d4b1a5 100644--- a/crates/router/src/core/payments/operations/payment_cancel_post_capture_sync.rs+++ b/crates/router/src/core/payments/operations/payment_cancel_post_capture_sync.rs@@ -141,6 +141,7 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, id_type::PaymentId>let payment_data = PaymentData {flow: PhantomData,+ previous_db_records: (payment_attempt.clone(), payment_intent.clone()),payment_intent,payment_attempt,currency,diff --git a/crates/router/src/core/payments/operations/payment_capture.rs b/crates/router/src/core/payments/operations/payment_capture.rsindex 90908e2ba8..d4969907cd 100644--- a/crates/router/src/core/payments/operations/payment_capture.rs+++ b/crates/router/src/core/payments/operations/payment_capture.rs@@ -223,6 +223,7 @@ impl<F: Send + Clone + Sync> GetTracker<F, payments::PaymentData<F>, api::Paymenlet payment_data = payments::PaymentData {flow: PhantomData,+ previous_db_records: (payment_attempt.clone(), payment_intent.clone()),payment_intent,payment_attempt,currency,diff --git a/crates/router/src/core/payments/operations/payment_complete_authorize.rs b/crates/router/src/core/payments/operations/payment_complete_authorize.rsindex 725881e91d..51e715d21c 100644--- a/crates/router/src/core/payments/operations/payment_complete_authorize.rs+++ b/crates/router/src/core/payments/operations/payment_complete_authorize.rs@@ -343,6 +343,7 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsRequest>let payment_data = PaymentData {flow: PhantomData,+ previous_db_records: (payment_attempt.clone(), payment_intent.clone()),payment_intent,payment_attempt,currency,diff --git a/crates/router/src/core/payments/operations/payment_confirm.rs b/crates/router/src/core/payments/operations/payment_confirm.rsindex a975f9d79f..b2c996ff51 100644--- a/crates/router/src/core/payments/operations/payment_confirm.rs+++ b/crates/router/src/core/payments/operations/payment_confirm.rs@@ -929,6 +929,7 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsRequest>let payment_data = PaymentData {flow: PhantomData,+ previous_db_records: (payment_attempt.clone(), payment_intent.clone()),payment_intent,payment_attempt,currency,diff --git a/crates/router/src/core/payments/operations/payment_confirm_external_vault_proxy.rs b/crates/router/src/core/payments/operations/payment_confirm_external_vault_proxy.rsindex 237af2ccb7..3e1cca0c9f 100644--- a/crates/router/src/core/payments/operations/payment_confirm_external_vault_proxy.rs+++ b/crates/router/src/core/payments/operations/payment_confirm_external_vault_proxy.rs@@ -379,6 +379,7 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, PaymentsRequest>let payment_data = PaymentData {flow: PhantomData,+ previous_db_records: (payment_attempt.clone(), payment_intent.clone()),payment_intent,currency,amount,diff --git a/crates/router/src/core/payments/operations/payment_create.rs b/crates/router/src/core/payments/operations/payment_create.rsindex c38b6bbc96..f624544a49 100644--- a/crates/router/src/core/payments/operations/payment_create.rs+++ b/crates/router/src/core/payments/operations/payment_create.rs@@ -672,6 +672,7 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsRequest>let payment_data = PaymentData {flow: PhantomData,+ previous_db_records: (payment_attempt.clone(), payment_intent.clone()),payment_intent,payment_attempt,currency,diff --git a/crates/router/src/core/payments/operations/payment_post_session_tokens.rs b/crates/router/src/core/payments/operations/payment_post_session_tokens.rsindex 6b049283ce..3b4afdeaf4 100644--- a/crates/router/src/core/payments/operations/payment_post_session_tokens.rs+++ b/crates/router/src/core/payments/operations/payment_post_session_tokens.rs@@ -135,6 +135,7 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsPostSesslet payment_data = PaymentData {flow: PhantomData,+ previous_db_records: (payment_attempt.clone(), payment_intent.clone()),payment_intent,payment_attempt,currency,diff --git a/crates/router/src/core/payments/operations/payment_recurrence.rs b/crates/router/src/core/payments/operations/payment_recurrence.rsindex a9408620a8..994c8127d6 100644--- a/crates/router/src/core/payments/operations/payment_recurrence.rs+++ b/crates/router/src/core/payments/operations/payment_recurrence.rs@@ -347,6 +347,7 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsRequest>let payment_data = PaymentData {flow: PhantomData,+ previous_db_records: (payment_attempt.clone(), payment_intent.clone()),payment_intent,payment_attempt,currency,diff --git a/crates/router/src/core/payments/operations/payment_reject.rs b/crates/router/src/core/payments/operations/payment_reject.rsindex dd743fe26f..07ad12e629 100644--- a/crates/router/src/core/payments/operations/payment_reject.rs+++ b/crates/router/src/core/payments/operations/payment_reject.rs@@ -161,6 +161,7 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, PaymentsCancelRequestlet payment_data = PaymentData {flow: PhantomData,+ previous_db_records: (payment_attempt.clone(), payment_intent.clone()),payment_intent,payment_attempt,currency,diff --git a/crates/router/src/core/payments/operations/payment_session.rs b/crates/router/src/core/payments/operations/payment_session.rsindex 0531299505..9293c66ddf 100644--- a/crates/router/src/core/payments/operations/payment_session.rs+++ b/crates/router/src/core/payments/operations/payment_session.rs@@ -182,6 +182,7 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsSessionRlet payment_data = PaymentData {flow: PhantomData,+ previous_db_records: (payment_attempt.clone(), payment_intent.clone()),payment_intent,payment_attempt,currency,diff --git a/crates/router/src/core/payments/operations/payment_start.rs b/crates/router/src/core/payments/operations/payment_start.rsindex 443b759418..d34bb7070e 100644--- a/crates/router/src/core/payments/operations/payment_start.rs+++ b/crates/router/src/core/payments/operations/payment_start.rs@@ -184,6 +184,7 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsStartReqlet payment_data = PaymentData {flow: PhantomData,+ previous_db_records: (payment_attempt.clone(), payment_intent.clone()),payment_intent,currency,amount,diff --git a/crates/router/src/core/payments/operations/payment_status.rs b/crates/router/src/core/payments/operations/payment_status.rsindex 79558aee7a..7e4a076655 100644--- a/crates/router/src/core/payments/operations/payment_status.rs+++ b/crates/router/src/core/payments/operations/payment_status.rs@@ -524,6 +524,7 @@ async fn get_tracker_for_sync<let payment_data = PaymentData {flow: PhantomData,+ previous_db_records: (payment_attempt.clone(), payment_intent.clone()),payment_intent,currency,amount,diff --git a/crates/router/src/core/payments/operations/payment_update.rs b/crates/router/src/core/payments/operations/payment_update.rsindex c5285d9f70..34eb8b5ced 100644--- a/crates/router/src/core/payments/operations/payment_update.rs+++ b/crates/router/src/core/payments/operations/payment_update.rs@@ -505,6 +505,7 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsRequest>let payment_data = PaymentData {flow: PhantomData,+ previous_db_records: (payment_attempt.clone(), payment_intent.clone()),payment_intent,payment_attempt,currency,diff --git a/crates/router/src/core/payments/operations/payment_update_metadata.rs b/crates/router/src/core/payments/operations/payment_update_metadata.rsindex 8e4f8fac7d..d5b1b784de 100644--- a/crates/router/src/core/payments/operations/payment_update_metadata.rs+++ b/crates/router/src/core/payments/operations/payment_update_metadata.rs@@ -146,6 +146,7 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsUpdateMelet payment_data = PaymentData {flow: PhantomData,+ previous_db_records: (payment_attempt.clone(), payment_intent.clone()),payment_intent,payment_attempt,currency,diff --git a/crates/router/src/core/payments/operations/payments_extend_authorization.rs b/crates/router/src/core/payments/operations/payments_extend_authorization.rsindex 575a18a73e..65720e8850 100644--- a/crates/router/src/core/payments/operations/payments_extend_authorization.rs+++ b/crates/router/src/core/payments/operations/payments_extend_authorization.rs@@ -156,6 +156,7 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsExtendAulet payment_data = PaymentData {flow: PhantomData,+ previous_db_records: (payment_attempt.clone(), payment_intent.clone()),payment_intent,payment_attempt,currency,diff --git a/crates/router/src/core/payments/operations/payments_incremental_authorization.rs b/crates/router/src/core/payments/operations/payments_incremental_authorization.rsindex e6f620bcb6..26822392c9 100644--- a/crates/router/src/core/payments/operations/payments_incremental_authorization.rs+++ b/crates/router/src/core/payments/operations/payments_incremental_authorization.rs@@ -135,6 +135,7 @@ impl<F: Send + Clone + Sync>let payment_data = payments::PaymentData {flow: PhantomData,+ previous_db_records: (payment_attempt.clone(), payment_intent.clone()),payment_intent,payment_attempt,currency,diff --git a/crates/router/src/core/payments/operations/tax_calculation.rs b/crates/router/src/core/payments/operations/tax_calculation.rsindex 4016360a60..1b6ef5c3b1 100644--- a/crates/router/src/core/payments/operations/tax_calculation.rs+++ b/crates/router/src/core/payments/operations/tax_calculation.rs@@ -151,6 +151,7 @@ impl<F: Send + Clone + Sync>let payment_data = PaymentData {flow: PhantomData,+ previous_db_records: (payment_attempt.clone(), payment_intent.clone()),payment_intent,payment_attempt,currency,