Skip to content

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

fix: customer id is not mandatory during confirm (#1317)

Narayan Bhat1261791d9f70
diff --git a/crates/router/src/core/payments/helpers.rs b/crates/router/src/core/payments/helpers.rs
index 00dcc4a851..ee2d402803 100644
--- a/crates/router/src/core/payments/helpers.rs
+++ b/crates/router/src/core/payments/helpers.rs
@@ -151,10 +151,7 @@ pub async fn get_address_for_payment_request(
}
None => {
// generate a new address here
- let customer_id = customer_id
- .as_deref()
- .get_required_value("customer_id")
- .change_context(errors::ApiErrorResponse::CustomerNotFound)?;
+ let customer_id = customer_id.as_deref().get_required_value("customer_id")?;
let address_details = address.address.clone().unwrap_or_default();
Some(
@@ -395,11 +392,12 @@ pub fn validate_request_amount_and_amount_to_capture(
pub fn validate_mandate(
req: impl Into<api::MandateValidationFields>,
+ is_confirm_operation: bool,
) -> RouterResult<Option<api::MandateTxnType>> {
let req: api::MandateValidationFields = req.into();
match req.is_mandate() {
Some(api::MandateTxnType::NewMandateTxn) => {
- validate_new_mandate_request(req)?;
+ validate_new_mandate_request(req, is_confirm_operation)?;
Ok(Some(api::MandateTxnType::NewMandateTxn))
}
Some(api::MandateTxnType::RecurringMandateTxn) => {
@@ -410,8 +408,18 @@ pub fn validate_mandate(
}
}
-fn validate_new_mandate_request(req: api::MandateValidationFields) -> RouterResult<()> {
- let _ = req.customer_id.as_ref().get_required_value("customer_id")?;
+fn validate_new_mandate_request(
+ req: api::MandateValidationFields,
+ is_confirm_operation: bool,
+) -> RouterResult<()> {
+ // We need not check for customer_id in the confirm request if it is already passed
+ //in create request
+
+ fp_utils::when(!is_confirm_operation && req.customer_id.is_none(), || {
+ Err(report!(errors::ApiErrorResponse::PreconditionFailed {
+ message: "`customer_id` is mandatory for mandates".into()
+ }))
+ })?;
let mandate_data = req
.mandate_data
@@ -776,10 +784,15 @@ pub async fn create_customer_if_not_exist<'a, F: Clone, R>(
let req = req
.get_required_value("customer")
.change_context(errors::StorageError::ValueNotFound("customer".to_owned()))?;
- let optional_customer = match req.customer_id.as_ref() {
+
+ let customer_id = req
+ .customer_id
+ .or(payment_data.payment_intent.customer_id.clone());
+
+ let optional_customer = match customer_id {
Some(customer_id) => {
let customer_data = db
- .find_customer_optional_by_customer_id_merchant_id(customer_id, merchant_id)
+ .find_customer_optional_by_customer_id_merchant_id(&customer_id, merchant_id)
.await?;
Some(match customer_data {
Some(c) => Ok(c),
diff --git a/crates/router/src/core/payments/operations/payment_complete_authorize.rs b/crates/router/src/core/payments/operations/payment_complete_authorize.rs
index 39448d6412..cf7ad77a68 100644
--- a/crates/router/src/core/payments/operations/payment_complete_authorize.rs
+++ b/crates/router/src/core/payments/operations/payment_complete_authorize.rs
@@ -9,7 +9,7 @@ use super::{BoxedOperation, Domain, GetTracker, Operation, UpdateTracker, Valida
use crate::{
core::{
errors::{self, CustomResult, RouterResult, StorageErrorExt},
- payments::{helpers, operations, CustomerDetails, PaymentAddress, PaymentData},
+ payments::{self, helpers, operations, CustomerDetails, PaymentAddress, PaymentData},
utils as core_utils,
},
db::StorageInterface,
@@ -331,7 +331,8 @@ impl<F: Send + Clone> ValidateRequest<F, api::PaymentsRequest> for CompleteAutho
helpers::validate_payment_method_fields_present(request)?;
- let mandate_type = helpers::validate_mandate(request)?;
+ 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((
diff --git a/crates/router/src/core/payments/operations/payment_confirm.rs b/crates/router/src/core/payments/operations/payment_confirm.rs
index 9d28f77dcc..7dc60813ea 100644
--- a/crates/router/src/core/payments/operations/payment_confirm.rs
+++ b/crates/router/src/core/payments/operations/payment_confirm.rs
@@ -10,7 +10,7 @@ use super::{BoxedOperation, Domain, GetTracker, Operation, UpdateTracker, Valida
use crate::{
core::{
errors::{self, CustomResult, RouterResult, StorageErrorExt},
- payments::{helpers, operations, CustomerDetails, PaymentAddress, PaymentData},
+ payments::{self, helpers, operations, CustomerDetails, PaymentAddress, PaymentData},
utils as core_utils,
},
db::StorageInterface,
@@ -467,7 +467,8 @@ impl<F: Send + Clone> ValidateRequest<F, api::PaymentsRequest> for PaymentConfir
helpers::validate_payment_method_fields_present(request)?;
- let mandate_type = helpers::validate_mandate(request)?;
+ 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((
diff --git a/crates/router/src/core/payments/operations/payment_create.rs b/crates/router/src/core/payments/operations/payment_create.rs
index 6130afd0b9..2ece098c0d 100644
--- a/crates/router/src/core/payments/operations/payment_create.rs
+++ b/crates/router/src/core/payments/operations/payment_create.rs
@@ -434,7 +434,8 @@ impl<F: Send + Clone> ValidateRequest<F, api::PaymentsRequest> for PaymentCreate
let payment_id = core_utils::get_or_generate_id("payment_id", &given_payment_id, "pay")?;
- let mandate_type = helpers::validate_mandate(request)?;
+ let mandate_type =
+ helpers::validate_mandate(request, payments::is_operation_confirm(self))?;
if request.confirm.unwrap_or(false) {
helpers::validate_pm_or_token_given(
diff --git a/crates/router/src/core/payments/operations/payment_method_validate.rs b/crates/router/src/core/payments/operations/payment_method_validate.rs
index 7cdf9fee75..1a3406dc87 100644
--- a/crates/router/src/core/payments/operations/payment_method_validate.rs
+++ b/crates/router/src/core/payments/operations/payment_method_validate.rs
@@ -45,7 +45,8 @@ impl<F: Send + Clone> ValidateRequest<F, api::VerifyRequest> for PaymentMethodVa
helpers::validate_merchant_id(&merchant_account.merchant_id, request_merchant_id)
.change_context(errors::ApiErrorResponse::MerchantAccountNotFound)?;
- let mandate_type = helpers::validate_mandate(request)?;
+ let mandate_type =
+ helpers::validate_mandate(request, payments::is_operation_confirm(self))?;
let validation_id = core_utils::get_or_generate_id("validation_id", &None, "val")?;
Ok((
diff --git a/crates/router/src/core/payments/operations/payment_start.rs b/crates/router/src/core/payments/operations/payment_start.rs
index b48cc1348c..2fbb1bd601 100644
--- a/crates/router/src/core/payments/operations/payment_start.rs
+++ b/crates/router/src/core/payments/operations/payment_start.rs
@@ -188,7 +188,7 @@ impl<F: Send + Clone> ValidateRequest<F, api::PaymentsStartRequest> for PaymentS
field_name: "merchant_id".to_string(),
expected_format: "merchant_id from merchant account".to_string(),
})?;
- // let mandate_type = validate_mandate(request)?;
+
let payment_id = request.payment_id.clone();
Ok((
diff --git a/crates/router/src/core/payments/operations/payment_update.rs b/crates/router/src/core/payments/operations/payment_update.rs
index bdf381004b..f8e206267d 100644
--- a/crates/router/src/core/payments/operations/payment_update.rs
+++ b/crates/router/src/core/payments/operations/payment_update.rs
@@ -532,7 +532,7 @@ impl<F: Send + Clone> ValidateRequest<F, api::PaymentsRequest> for PaymentUpdate
helpers::validate_payment_method_fields_present(request)?;
- let mandate_type = helpers::validate_mandate(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((