Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: api lock on PaymentsCreate #2916

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions crates/api_models/src/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
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
Expand Up @@ -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};

Expand Down Expand Up @@ -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 {
Expand All @@ -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!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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)
Expand All @@ -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!(
Expand Down
23 changes: 10 additions & 13 deletions crates/router/src/core/payments/operations/payment_confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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| core_utils::validate_id(id, "payment_id"))
.into_report()?,
mandate_type,
storage_scheme: merchant_account.storage_scheme,
requeue: matches!(
Expand Down
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
Expand Up @@ -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)
Expand All @@ -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))?;

Expand All @@ -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!(
Expand Down
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
Expand Up @@ -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};

Expand Down Expand Up @@ -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)
Expand All @@ -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!(
Expand Down
39 changes: 35 additions & 4 deletions crates/router/src/routes/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
},
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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::LockAction
where
Expand Down
Loading