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(authorization): add backward compatibility for incremental authorization (#11240)
Apoorv Dixitb8eae3d5c76c
diff --git a/crates/diesel_models/src/query/authorization.rs b/crates/diesel_models/src/query/authorization.rsindex 8423ed0f13..86e2b10d37 100644--- a/crates/diesel_models/src/query/authorization.rs+++ b/crates/diesel_models/src/query/authorization.rs@@ -56,6 +56,46 @@ impl Authorization {}}+ // Fallback function for stagger release - updates by merchant_id when processor_merchant_id is NULL+ pub async fn update_by_merchant_id_authorization_id(+ conn: &PgPooledConn,+ merchant_id: common_utils::id_type::MerchantId,+ authorization_id: String,+ authorization_update: AuthorizationUpdate,+ ) -> StorageResult<Self> {+ match generics::generic_update_with_unique_predicate_get_result::<+ <Self as HasTable>::Table,+ _,+ _,+ _,+ >(+ conn,+ dsl::merchant_id+ .eq(merchant_id.to_owned())+ .and(dsl::authorization_id.eq(authorization_id.to_owned())),+ AuthorizationUpdateInternal::from(authorization_update),+ )+ .await+ {+ Err(error) => match error.current_context() {+ errors::DatabaseError::NotFound => Err(error.attach_printable(+ "Authorization with the given Authorization ID does not exist",+ )),+ errors::DatabaseError::NoFieldsToUpdate => {+ generics::generic_find_one::<<Self as HasTable>::Table, _, _>(+ conn,+ dsl::merchant_id+ .eq(merchant_id.to_owned())+ .and(dsl::authorization_id.eq(authorization_id.to_owned())),+ )+ .await+ }+ _ => Err(error),+ },+ result => result,+ }+ }+pub async fn find_by_processor_merchant_id_payment_id(conn: &PgPooledConn,processor_merchant_id: &common_utils::id_type::MerchantId,@@ -72,4 +112,22 @@ impl Authorization {).await}++ // Fallback function for stagger release - queries by merchant_id when processor_merchant_id is NULL+ pub async fn find_by_merchant_id_payment_id(+ conn: &PgPooledConn,+ merchant_id: &common_utils::id_type::MerchantId,+ payment_id: &common_utils::id_type::PaymentId,+ ) -> StorageResult<Vec<Self>> {+ generics::generic_filter::<<Self as HasTable>::Table, _, _, _>(+ conn,+ dsl::merchant_id+ .eq(merchant_id.to_owned())+ .and(dsl::payment_id.eq(payment_id.to_owned())),+ None,+ None,+ Some(dsl::created_at.asc()),+ )+ .await+ }}diff --git a/crates/router/src/db/authorization.rs b/crates/router/src/db/authorization.rsindex 8465974afc..09a66bb947 100644--- a/crates/router/src/db/authorization.rs+++ b/crates/router/src/db/authorization.rs@@ -51,13 +51,27 @@ impl AuthorizationInterface for Store {payment_id: &common_utils::id_type::PaymentId,) -> CustomResult<Vec<storage::Authorization>, errors::StorageError> {let conn = connection::pg_connection_read(self).await?;- storage::Authorization::find_by_processor_merchant_id_payment_id(+ // Stagger release fallback: first try processor_merchant_id, if empty fallback to merchant_id+ // For old records processor_merchant_id is NULL, so we use merchant_id (which has the same value)+ let authorizations = storage::Authorization::find_by_processor_merchant_id_payment_id(&conn,processor_merchant_id,payment_id,).await- .map_err(|error| report!(errors::StorageError::from(error)))+ .map_err(|error| report!(errors::StorageError::from(error)))?;++ if authorizations.is_empty() {+ storage::Authorization::find_by_merchant_id_payment_id(+ &conn,+ processor_merchant_id,+ payment_id,+ )+ .await+ .map_err(|error| report!(errors::StorageError::from(error)))+ } else {+ Ok(authorizations)+ }}#[instrument(skip_all)]@@ -68,14 +82,36 @@ impl AuthorizationInterface for Store {authorization: storage::AuthorizationUpdate,) -> CustomResult<storage::Authorization, errors::StorageError> {let conn = connection::pg_connection_write(self).await?;- storage::Authorization::update_by_processor_merchant_id_authorization_id(+ // Stagger release fallback: first try processor_merchant_id, if not found fallback to merchant_id+ // For old records processor_merchant_id is NULL, so we use merchant_id (which has the same value)+ let result = storage::Authorization::update_by_processor_merchant_id_authorization_id(&conn,- processor_merchant_id,- authorization_id,- authorization,+ processor_merchant_id.clone(),+ authorization_id.clone(),+ authorization.clone(),)- .await- .map_err(|error| report!(errors::StorageError::from(error)))+ .await;++ match result {+ Ok(auth) => Ok(auth),+ Err(error) => {+ if matches!(+ error.current_context(),+ diesel_models::errors::DatabaseError::NotFound+ ) {+ storage::Authorization::update_by_merchant_id_authorization_id(+ &conn,+ processor_merchant_id,+ authorization_id,+ authorization,+ )+ .await+ .map_err(|error| report!(errors::StorageError::from(error)))+ } else {+ Err(report!(errors::StorageError::from(error)))+ }+ }+ }}}@@ -118,6 +154,8 @@ impl AuthorizationInterface for MockDb {payment_id: &common_utils::id_type::PaymentId,) -> CustomResult<Vec<storage::Authorization>, errors::StorageError> {let authorizations = self.authorizations.lock().await;+ // Stagger release fallback: first try processor_merchant_id, if empty fallback to merchant_id+ // For old records processor_merchant_id is NULL, so we use merchant_id (which has the same value)let authorizations_found: Vec<storage::Authorization> = authorizations.iter().filter(|a| {@@ -127,7 +165,15 @@ impl AuthorizationInterface for MockDb {.cloned().collect();- Ok(authorizations_found)+ if authorizations_found.is_empty() {+ Ok(authorizations+ .iter()+ .filter(|a| a.merchant_id == *processor_merchant_id && a.payment_id == *payment_id)+ .cloned()+ .collect())+ } else {+ Ok(authorizations_found)+ }}async fn update_authorization_by_processor_merchant_id_authorization_id(@@ -137,15 +183,30 @@ impl AuthorizationInterface for MockDb {authorization_update: storage::AuthorizationUpdate,) -> CustomResult<storage::Authorization, errors::StorageError> {let mut authorizations = self.authorizations.lock().await;- authorizations- .iter_mut()- .find(|authorization| authorization.authorization_id == authorization_id && authorization.processor_merchant_id.as_ref() == Some(&processor_merchant_id))- .map(|authorization| {- let authorization_updated =- AuthorizationUpdateInternal::from(authorization_update)- .create_authorization(authorization.clone());- *authorization = authorization_updated.clone();- authorization_updated+ // Stagger release fallback: first try processor_merchant_id, if not found fallback to merchant_id+ // For old records processor_merchant_id is NULL, so we use merchant_id (which has the same value)+ let index = authorizations+ .iter()+ .position(|authorization| {+ authorization.authorization_id == authorization_id+ && authorization.processor_merchant_id.as_ref() == Some(&processor_merchant_id)+ })+ .or_else(|| {+ authorizations.iter().position(|authorization| {+ authorization.authorization_id == authorization_id+ && authorization.merchant_id == processor_merchant_id+ })+ });++ index+ .and_then(|idx| {+ authorizations.get_mut(idx).map(|auth| {+ let authorization_updated =+ AuthorizationUpdateInternal::from(authorization_update)+ .create_authorization(auth.clone());+ *auth = authorization_updated.clone();+ authorization_updated+ })}).ok_or(errors::StorageError::ValueNotFound(format!(