Skip to content

Commit

Permalink
fix: api lock on PaymentsCreate
Browse files Browse the repository at this point in the history
  • Loading branch information
dracarys18 committed Nov 17, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent c39beb2 commit 52ed74c
Showing 7 changed files with 86 additions and 61 deletions.
14 changes: 14 additions & 0 deletions crates/api_models/src/payments.rs
Original file line number Diff line number Diff line change
@@ -1675,6 +1675,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())
20 changes: 8 additions & 12 deletions crates/router/src/core/payments/operations/payment_approve.rs
Original file line number Diff line number Diff line change
@@ -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};

@@ -381,15 +381,6 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> ValidateRequest<F, api::Paymen
BoxedOperation<'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 {
@@ -401,13 +392,18 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> ValidateRequest<F, api::Paymen

let 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!(
Original file line number Diff line number Diff line change
@@ -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};

@@ -357,14 +357,10 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> ValidateRequest<F, api::Paymen
BoxedOperation<'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)
@@ -377,13 +373,14 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> ValidateRequest<F, api::Paymen

let 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!(
21 changes: 9 additions & 12 deletions crates/router/src/core/payments/operations/payment_confirm.rs
Original file line number Diff line number Diff line change
@@ -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;
@@ -774,14 +774,6 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> ValidateRequest<F, api::Paymen
operations::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)
@@ -794,14 +786,19 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> ValidateRequest<F, api::Paymen

let 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| crate::core::utils::validate_id(id, "payment_id"))
.into_report()?,
mandate_type,
storage_scheme: merchant_account.storage_scheme,
requeue: matches!(
15 changes: 4 additions & 11 deletions crates/router/src/core/payments/operations/payment_create.rs
Original file line number Diff line number Diff line change
@@ -514,14 +514,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)
@@ -540,8 +535,6 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> ValidateRequest<F, api::Paymen

helpers::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))?;

@@ -568,7 +561,7 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> ValidateRequest<F, api::Paymen
Box::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!(
19 changes: 8 additions & 11 deletions crates/router/src/core/payments/operations/payment_update.rs
Original file line number Diff line number Diff line change
@@ -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};

@@ -592,14 +592,10 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> ValidateRequest<F, api::Paymen
operations::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)
@@ -620,13 +616,14 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> ValidateRequest<F, api::Paymen
helpers::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!(
39 changes: 35 additions & 4 deletions crates/router/src/routes/payments.rs
Original file line number Diff line number Diff line change
@@ -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
}
}

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::LockAction
where

0 comments on commit 52ed74c

Please sign in to comment.