Skip to content

Commit

Permalink
fix: customer id is not mandatory during confirm (#1317)
Browse files Browse the repository at this point in the history
  • Loading branch information
Narayanbhat166 authored May 31, 2023
1 parent 798881a commit 1261791
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 17 deletions.
31 changes: 22 additions & 9 deletions crates/router/src/core/payments/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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) => {
Expand All @@ -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
Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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((
Expand Down
5 changes: 3 additions & 2 deletions crates/router/src/core/payments/operations/payment_confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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((
Expand Down
3 changes: 2 additions & 1 deletion crates/router/src/core/payments/operations/payment_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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((
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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((
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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((
Expand Down

0 comments on commit 1261791

Please sign in to comment.