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: api lock on PaymentsCreate (#2916)
Kartikeya Hegdecfabfa60db4d
diff --git a/crates/api_models/src/payments.rs b/crates/api_models/src/payments.rsindex 9f4f151c22..c427088d68 100644--- a/crates/api_models/src/payments.rs+++ b/crates/api_models/src/payments.rs@@ -1679,6 +1679,20 @@ impl std::fmt::Display for PaymentIdType {}}+impl PaymentIdType {+ pub fn and_then<F, E>(self, f: F) -> Result<Self, E>+ where+ F: FnOnce(String) -> Result<String, E>,+ {+ match self {+ Self::PaymentIntentId(s) => f(s).map(Self::PaymentIntentId),+ Self::ConnectorTransactionId(s) => f(s).map(Self::ConnectorTransactionId),+ Self::PaymentAttemptId(s) => f(s).map(Self::PaymentAttemptId),+ Self::PreprocessingId(s) => f(s).map(Self::PreprocessingId),+ }+ }+}+impl Default for PaymentIdType {fn default() -> Self {Self::PaymentIntentId(Default::default())diff --git a/crates/router/src/core/payments/operations/payment_approve.rs b/crates/router/src/core/payments/operations/payment_approve.rsindex 78eb3fb1f1..af52105c85 100644--- a/crates/router/src/core/payments/operations/payment_approve.rs+++ b/crates/router/src/core/payments/operations/payment_approve.rs@@ -3,7 +3,7 @@ use std::marker::PhantomData;use api_models::enums::FrmSuggestion;use async_trait::async_trait;use data_models::mandates::MandateData;-use error_stack::ResultExt;+use error_stack::{report, IntoReport, ResultExt};use router_derive::PaymentOperation;use router_env::{instrument, tracing};@@ -399,15 +399,6 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> ValidateRequest<F, api::PaymenBoxedOperation<'b, F, api::PaymentsRequest, Ctx>,operations::ValidateResult<'a>,)> {- let given_payment_id = match &request.payment_id {- Some(id_type) => Some(- id_type- .get_payment_intent_id()- .change_context(errors::ApiErrorResponse::PaymentNotFound)?,- ),- None => None,- };-let request_merchant_id = request.merchant_id.as_deref();helpers::validate_merchant_id(&merchant_account.merchant_id, request_merchant_id).change_context(errors::ApiErrorResponse::InvalidDataFormat {@@ -419,13 +410,18 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> ValidateRequest<F, api::Paymenlet mandate_type =helpers::validate_mandate(request, payments::is_operation_confirm(self))?;- let payment_id = core_utils::get_or_generate_id("payment_id", &given_payment_id, "pay")?;+ let payment_id = request+ .payment_id+ .clone()+ .ok_or(report!(errors::ApiErrorResponse::PaymentNotFound))?;Ok((Box::new(self),operations::ValidateResult {merchant_id: &merchant_account.merchant_id,- payment_id: api::PaymentIdType::PaymentIntentId(payment_id),+ payment_id: payment_id+ .and_then(|id| core_utils::validate_id(id, "payment_id"))+ .into_report()?,mandate_type,storage_scheme: merchant_account.storage_scheme,requeue: matches!(diff --git a/crates/router/src/core/payments/operations/payment_complete_authorize.rs b/crates/router/src/core/payments/operations/payment_complete_authorize.rsindex 7cc1edf17f..62759bd0fd 100644--- a/crates/router/src/core/payments/operations/payment_complete_authorize.rs+++ b/crates/router/src/core/payments/operations/payment_complete_authorize.rs@@ -2,7 +2,7 @@ use std::marker::PhantomData;use api_models::enums::FrmSuggestion;use async_trait::async_trait;-use error_stack::ResultExt;+use error_stack::{report, IntoReport, ResultExt};use router_derive::PaymentOperation;use router_env::{instrument, tracing};@@ -374,14 +374,10 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> ValidateRequest<F, api::PaymenBoxedOperation<'b, F, api::PaymentsRequest, Ctx>,operations::ValidateResult<'a>,)> {- let given_payment_id = match &request.payment_id {- Some(id_type) => Some(- id_type- .get_payment_intent_id()- .change_context(errors::ApiErrorResponse::PaymentNotFound)?,- ),- None => None,- };+ let payment_id = request+ .payment_id+ .clone()+ .ok_or(report!(errors::ApiErrorResponse::PaymentNotFound))?;let request_merchant_id = request.merchant_id.as_deref();helpers::validate_merchant_id(&merchant_account.merchant_id, request_merchant_id)@@ -394,13 +390,14 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> ValidateRequest<F, api::Paymenlet mandate_type =helpers::validate_mandate(request, payments::is_operation_confirm(self))?;- let payment_id = core_utils::get_or_generate_id("payment_id", &given_payment_id, "pay")?;Ok((Box::new(self),operations::ValidateResult {merchant_id: &merchant_account.merchant_id,- payment_id: api::PaymentIdType::PaymentIntentId(payment_id),+ payment_id: payment_id+ .and_then(|id| core_utils::validate_id(id, "payment_id"))+ .into_report()?,mandate_type,storage_scheme: merchant_account.storage_scheme,requeue: matches!(diff --git a/crates/router/src/core/payments/operations/payment_confirm.rs b/crates/router/src/core/payments/operations/payment_confirm.rsindex a040782d83..8b4f91b63a 100644--- a/crates/router/src/core/payments/operations/payment_confirm.rs+++ b/crates/router/src/core/payments/operations/payment_confirm.rs@@ -6,7 +6,7 @@ use api_models::{};use async_trait::async_trait;use common_utils::ext_traits::{AsyncExt, Encode};-use error_stack::ResultExt;+use error_stack::{report, IntoReport, ResultExt};use futures::FutureExt;use redis_interface::errors::RedisError;use router_derive::PaymentOperation;@@ -19,7 +19,7 @@ use crate::{errors::{self, CustomResult, RouterResult, StorageErrorExt},payment_methods::PaymentMethodRetrieve,payments::{self, helpers, operations, CustomerDetails, PaymentAddress, PaymentData},- utils::get_individual_surcharge_detail_from_redis,+ utils::{self as core_utils, get_individual_surcharge_detail_from_redis},},db::StorageInterface,routes::AppState,@@ -820,14 +820,6 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> ValidateRequest<F, api::Paymenoperations::ValidateResult<'a>,)> {helpers::validate_customer_details_in_request(request)?;- let given_payment_id = match &request.payment_id {- Some(id_type) => Some(- id_type- .get_payment_intent_id()- .change_context(errors::ApiErrorResponse::PaymentNotFound)?,- ),- None => None,- };let request_merchant_id = request.merchant_id.as_deref();helpers::validate_merchant_id(&merchant_account.merchant_id, request_merchant_id)@@ -840,14 +832,19 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> ValidateRequest<F, api::Paymenlet mandate_type =helpers::validate_mandate(request, payments::is_operation_confirm(self))?;- let payment_id =- crate::core::utils::get_or_generate_id("payment_id", &given_payment_id, "pay")?;++ let payment_id = request+ .payment_id+ .clone()+ .ok_or(report!(errors::ApiErrorResponse::PaymentNotFound))?;Ok((Box::new(self),operations::ValidateResult {merchant_id: &merchant_account.merchant_id,- payment_id: api::PaymentIdType::PaymentIntentId(payment_id),+ payment_id: payment_id+ .and_then(|id| core_utils::validate_id(id, "payment_id"))+ .into_report()?,mandate_type,storage_scheme: merchant_account.storage_scheme,requeue: matches!(diff --git a/crates/router/src/core/payments/operations/payment_create.rs b/crates/router/src/core/payments/operations/payment_create.rsindex 526b03137b..10c237cc8a 100644--- a/crates/router/src/core/payments/operations/payment_create.rs+++ b/crates/router/src/core/payments/operations/payment_create.rs@@ -529,14 +529,9 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> ValidateRequest<F, api::Paymen)?;}- let given_payment_id = match &request.payment_id {- Some(id_type) => Some(- id_type- .get_payment_intent_id()- .change_context(errors::ApiErrorResponse::PaymentNotFound)?,- ),- None => None,- };+ let payment_id = request.payment_id.clone().ok_or(error_stack::report!(+ errors::ApiErrorResponse::PaymentNotFound+ ))?;let request_merchant_id = request.merchant_id.as_deref();helpers::validate_merchant_id(&merchant_account.merchant_id, request_merchant_id)@@ -555,8 +550,6 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> ValidateRequest<F, api::Paymenhelpers::validate_payment_method_fields_present(request)?;- let payment_id = core_utils::get_or_generate_id("payment_id", &given_payment_id, "pay")?;-let mandate_type =helpers::validate_mandate(request, payments::is_operation_confirm(self))?;@@ -583,7 +576,7 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> ValidateRequest<F, api::PaymenBox::new(self),operations::ValidateResult {merchant_id: &merchant_account.merchant_id,- payment_id: api::PaymentIdType::PaymentIntentId(payment_id),+ payment_id,mandate_type,storage_scheme: merchant_account.storage_scheme,requeue: matches!(diff --git a/crates/router/src/core/payments/operations/payment_update.rs b/crates/router/src/core/payments/operations/payment_update.rsindex 6833a6a392..817b142868 100644--- a/crates/router/src/core/payments/operations/payment_update.rs+++ b/crates/router/src/core/payments/operations/payment_update.rs@@ -3,7 +3,7 @@ use std::marker::PhantomData;use api_models::enums::FrmSuggestion;use async_trait::async_trait;use common_utils::ext_traits::{AsyncExt, Encode, ValueExt};-use error_stack::ResultExt;+use error_stack::{report, IntoReport, ResultExt};use router_derive::PaymentOperation;use router_env::{instrument, tracing};@@ -607,14 +607,10 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> ValidateRequest<F, api::Paymenoperations::ValidateResult<'a>,)> {helpers::validate_customer_details_in_request(request)?;- let given_payment_id = match &request.payment_id {- Some(id_type) => Some(- id_type- .get_payment_intent_id()- .change_context(errors::ApiErrorResponse::PaymentNotFound)?,- ),- None => None,- };+ let payment_id = request+ .payment_id+ .clone()+ .ok_or(report!(errors::ApiErrorResponse::PaymentNotFound))?;let request_merchant_id = request.merchant_id.as_deref();helpers::validate_merchant_id(&merchant_account.merchant_id, request_merchant_id)@@ -635,13 +631,14 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> ValidateRequest<F, api::Paymenhelpers::validate_payment_method_fields_present(request)?;let mandate_type = helpers::validate_mandate(request, false)?;- let payment_id = core_utils::get_or_generate_id("payment_id", &given_payment_id, "pay")?;Ok((Box::new(self),operations::ValidateResult {merchant_id: &merchant_account.merchant_id,- payment_id: api::PaymentIdType::PaymentIntentId(payment_id),+ payment_id: payment_id+ .and_then(|id| core_utils::validate_id(id, "payment_id"))+ .into_report()?,mandate_type,storage_scheme: merchant_account.storage_scheme,requeue: matches!(diff --git a/crates/router/src/routes/payments.rs b/crates/router/src/routes/payments.rsindex b05fae6533..81e53ade5e 100644--- a/crates/router/src/routes/payments.rs+++ b/crates/router/src/routes/payments.rs@@ -3,15 +3,16 @@ pub mod helpers;use actix_web::{web, Responder};use api_models::payments::HeaderPayload;-use error_stack::report;+use error_stack::{report, IntoReport};use router_env::{env, instrument, tracing, types, Flow};use crate::{self as app,core::{- errors::http_not_implemented,+ errors::{self, http_not_implemented},payment_methods::{Oss, PaymentMethodRetrieve},payments::{self, PaymentRedirectFlow},+ utils as core_utils,},// openapi::examples::{// PAYMENTS_CREATE, PAYMENTS_CREATE_MINIMUM_FIELDS, PAYMENTS_CREATE_WITH_ADDRESS,@@ -22,7 +23,10 @@ use crate::{routes::lock_utils,services::{api, authentication as auth},types::{- api::{self as api_types, enums as api_enums, payments as payment_types},+ api::{+ self as api_types, enums as api_enums,+ payments::{self as payment_types, PaymentIdTypeExt},+ },domain,transformers::ForeignTryFrom,},@@ -94,12 +98,16 @@ pub async fn payments_create(json_payload: web::Json<payment_types::PaymentsRequest>,) -> impl Responder {let flow = Flow::PaymentsCreate;- let payload = json_payload.into_inner();+ let mut payload = json_payload.into_inner();if let Some(api_enums::CaptureMethod::Scheduled) = payload.capture_method {return http_not_implemented();};+ if let Err(err) = get_or_generate_payment_id(&mut payload) {+ return api::log_and_return_error_response(err);+ }+let locking_action = payload.get_locking_input(flow.clone());Box::pin(api::server_wrap(@@ -959,6 +967,29 @@ where}}+pub fn get_or_generate_payment_id(+ payload: &mut payment_types::PaymentsRequest,+) -> errors::RouterResult<()> {+ let given_payment_id = payload+ .payment_id+ .clone()+ .map(|payment_id| {+ payment_id+ .get_payment_intent_id()+ .map_err(|err| err.change_context(errors::ApiErrorResponse::PaymentNotFound))+ })+ .transpose()?;++ let payment_id =+ core_utils::get_or_generate_id("payment_id", &given_payment_id, "pay").into_report()?;++ payload.payment_id = Some(api_models::payments::PaymentIdType::PaymentIntentId(+ payment_id,+ ));++ Ok(())+}+impl GetLockingInput for payment_types::PaymentsRequest {fn get_locking_input<F>(&self, flow: F) -> api_locking::LockActionwhere