Skip to content

Commit

Permalink
refactor(core): restrict requires_customer_action in confirm (#3235)
Browse files Browse the repository at this point in the history
  • Loading branch information
Narayanbhat166 authored Jan 30, 2024
1 parent b5bc8c4 commit d2accde
Show file tree
Hide file tree
Showing 18 changed files with 51 additions and 410 deletions.
9 changes: 9 additions & 0 deletions crates/api_models/src/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,15 @@ pub struct HeaderPayload {
pub x_hs_latency: Option<bool>,
}

impl HeaderPayload {
pub fn with_source(payment_confirm_source: api_enums::PaymentSource) -> Self {
Self {
payment_confirm_source: Some(payment_confirm_source),
..Default::default()
}
}
}

#[derive(
Default, Debug, serde::Serialize, Clone, PartialEq, ToSchema, router_derive::PolymorphicSchema,
)]
Expand Down
1 change: 1 addition & 0 deletions crates/common_enums/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2069,6 +2069,7 @@ pub enum PaymentSource {
Postman,
Dashboard,
Sdk,
Webhook,
}

#[derive(
Expand Down
1 change: 1 addition & 0 deletions crates/router/src/core/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ where
&merchant_account,
&key_store,
auth_flow,
header_payload.payment_confirm_source,
)
.await?;

Expand Down
1 change: 1 addition & 0 deletions crates/router/src/core/payments/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ pub trait GetTracker<F: Clone, D, R, Ctx: PaymentMethodRetrieve>: Send {
merchant_account: &domain::MerchantAccount,
mechant_key_store: &domain::MerchantKeyStore,
auth_flow: services::AuthFlow,
payment_confirm_source: Option<enums::PaymentSource>,
) -> RouterResult<GetTrackerResponse<'a, F, R, Ctx>>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
merchant_account: &domain::MerchantAccount,
key_store: &domain::MerchantKeyStore,
_auth_flow: services::AuthFlow,
_payment_confirm_source: Option<common_enums::PaymentSource>,
) -> RouterResult<operations::GetTrackerResponse<'a, F, api::PaymentsCaptureRequest, Ctx>> {
let db = &*state.store;
let merchant_id = &merchant_account.merchant_id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
merchant_account: &domain::MerchantAccount,
key_store: &domain::MerchantKeyStore,
_auth_flow: services::AuthFlow,
_payment_confirm_source: Option<common_enums::PaymentSource>,
) -> RouterResult<operations::GetTrackerResponse<'a, F, api::PaymentsCancelRequest, Ctx>> {
let db = &*state.store;
let merchant_id = &merchant_account.merchant_id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
merchant_account: &domain::MerchantAccount,
key_store: &domain::MerchantKeyStore,
_auth_flow: services::AuthFlow,
_payment_confirm_source: Option<common_enums::PaymentSource>,
) -> RouterResult<operations::GetTrackerResponse<'a, F, api::PaymentsCaptureRequest, Ctx>> {
let db = &*state.store;
let merchant_id = &merchant_account.merchant_id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
merchant_account: &domain::MerchantAccount,
key_store: &domain::MerchantKeyStore,
_auth_flow: services::AuthFlow,
_payment_confirm_source: Option<common_enums::PaymentSource>,
) -> RouterResult<operations::GetTrackerResponse<'a, F, api::PaymentsRequest, Ctx>> {
let db = &*state.store;
let merchant_id = &merchant_account.merchant_id;
Expand Down
38 changes: 27 additions & 11 deletions crates/router/src/core/payments/operations/payment_confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
merchant_account: &domain::MerchantAccount,
key_store: &domain::MerchantKeyStore,
auth_flow: services::AuthFlow,
payment_confirm_source: Option<common_enums::PaymentSource>,
) -> RouterResult<operations::GetTrackerResponse<'a, F, api::PaymentsRequest, Ctx>> {
let merchant_id = &merchant_account.merchant_id;
let storage_scheme = merchant_account.storage_scheme;
Expand Down Expand Up @@ -117,17 +118,32 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>

helpers::validate_customer_access(&payment_intent, auth_flow, request)?;

helpers::validate_payment_status_against_not_allowed_statuses(
&payment_intent.status,
&[
storage_enums::IntentStatus::Cancelled,
storage_enums::IntentStatus::Succeeded,
storage_enums::IntentStatus::Processing,
storage_enums::IntentStatus::RequiresCapture,
storage_enums::IntentStatus::RequiresMerchantAction,
],
"confirm",
)?;
if let Some(common_enums::PaymentSource::Webhook) = payment_confirm_source {
helpers::validate_payment_status_against_not_allowed_statuses(
&payment_intent.status,
&[
storage_enums::IntentStatus::Cancelled,
storage_enums::IntentStatus::Succeeded,
storage_enums::IntentStatus::Processing,
storage_enums::IntentStatus::RequiresCapture,
storage_enums::IntentStatus::RequiresMerchantAction,
],
"confirm",
)?;
} else {
helpers::validate_payment_status_against_not_allowed_statuses(
&payment_intent.status,
&[
storage_enums::IntentStatus::Cancelled,
storage_enums::IntentStatus::Succeeded,
storage_enums::IntentStatus::Processing,
storage_enums::IntentStatus::RequiresCapture,
storage_enums::IntentStatus::RequiresMerchantAction,
storage_enums::IntentStatus::RequiresCustomerAction,
],
"confirm",
)?;
}

helpers::authenticate_client_secret(request.client_secret.as_ref(), &payment_intent)?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
merchant_account: &domain::MerchantAccount,
merchant_key_store: &domain::MerchantKeyStore,
_auth_flow: services::AuthFlow,
_payment_confirm_source: Option<common_enums::PaymentSource>,
) -> RouterResult<operations::GetTrackerResponse<'a, F, api::PaymentsRequest, Ctx>> {
let db = &*state.store;
let ephemeral_key = Self::get_ephemeral_key(request, state, merchant_account).await;
Expand Down
Loading

0 comments on commit d2accde

Please sign in to comment.