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

refactor(payouts): remove implicit customer creation (#15305)

Sakil Mostak3ce1e249ef38
diff --git a/api-reference/v1/openapi_spec_v1.json b/api-reference/v1/openapi_spec_v1.json
index 7be5b074b2..d6e2c00be9 100644
--- a/api-reference/v1/openapi_spec_v1.json
+++ b/api-reference/v1/openapi_spec_v1.json
@@ -7963,6 +7963,9 @@
"400": {
"description": "Missing mandatory fields or invalid request"
},
+ "404": {
+ "description": "Customer does not exist in the merchant's records"
+ },
"501": {
"description": "External-vault proxy payout execution is not implemented yet"
}
@@ -8062,7 +8065,10 @@
}
},
"400": {
- "description": "Missing Mandatory fields"
+ "description": "Missing mandatory fields or invalid request"
+ },
+ "404": {
+ "description": "Payout does not exist in the merchant's records"
}
},
"security": [
@@ -8334,7 +8340,10 @@
}
},
"400": {
- "description": "Missing Mandatory fields"
+ "description": "Missing mandatory fields or invalid request"
+ },
+ "404": {
+ "description": "Payout does not exist in the merchant's records"
}
},
"security": [
@@ -40422,7 +40431,7 @@
},
"customer_id": {
"type": "string",
- "description": "Customer identifier for normal and proxy payouts; normal creates may generate it when omitted, while proxy creates require this or customer.id owning payout_method_id. _Deprecated: Use customer.id instead._",
+ "description": "Existing customer ID used at creation. Ignored on update and confirm.\n_Deprecated: Use customer.id instead._",
"deprecated": true,
"example": "cus_y3oqhf46pyzuxjbcn2giaqnb44",
"nullable": true,
@@ -40558,7 +40567,7 @@
},
"email": {
"type": "string",
- "description": "Customer's email. _Deprecated: Use customer object instead._",
+ "description": "Ignored. _Deprecated: Manage customer details through the Customers API._",
"deprecated": true,
"example": "[email protected]",
"nullable": true,
@@ -40566,7 +40575,7 @@
},
"name": {
"type": "string",
- "description": "Customer's name. _Deprecated: Use customer object instead._",
+ "description": "Ignored. _Deprecated: Manage customer details through the Customers API._",
"deprecated": true,
"example": "John Test",
"nullable": true,
@@ -40574,7 +40583,7 @@
},
"phone": {
"type": "string",
- "description": "Customer's phone. _Deprecated: Use customer object instead._",
+ "description": "Ignored. _Deprecated: Manage customer details through the Customers API._",
"deprecated": true,
"example": "9123456789",
"nullable": true,
@@ -40582,7 +40591,7 @@
},
"phone_country_code": {
"type": "string",
- "description": "Customer's phone country code. _Deprecated: Use customer object instead._",
+ "description": "Ignored. _Deprecated: Manage customer details through the Customers API._",
"deprecated": true,
"example": "+1",
"nullable": true,
@@ -40771,7 +40780,7 @@
},
"customer_id": {
"type": "string",
- "description": "The identifier for the customer object. If not provided the customer ID will be autogenerated. _Deprecated: Use the customer object instead._",
+ "description": "Attached customer ID, if any. _Deprecated: Use customer.id instead._",
"deprecated": true,
"example": "cus_y3oqhf46pyzuxjbcn2giaqnb44",
"maxLength": 255
@@ -41611,7 +41620,7 @@
},
"customer_id": {
"type": "string",
- "description": "Customer identifier for normal and proxy payouts; normal creates may generate it when omitted, while proxy creates require this or customer.id owning payout_method_id. _Deprecated: Use customer.id instead._",
+ "description": "Existing customer ID used at creation. Ignored on update and confirm.\n_Deprecated: Use customer.id instead._",
"deprecated": true,
"example": "cus_y3oqhf46pyzuxjbcn2giaqnb44",
"nullable": true,
@@ -41747,7 +41756,7 @@
},
"email": {
"type": "string",
- "description": "Customer's email. _Deprecated: Use customer object instead._",
+ "description": "Ignored. _Deprecated: Manage customer details through the Customers API._",
"deprecated": true,
"example": "[email protected]",
"nullable": true,
@@ -41755,7 +41764,7 @@
},
"name": {
"type": "string",
- "description": "Customer's name. _Deprecated: Use customer object instead._",
+ "description": "Ignored. _Deprecated: Manage customer details through the Customers API._",
"deprecated": true,
"example": "John Test",
"nullable": true,
@@ -41763,7 +41772,7 @@
},
"phone": {
"type": "string",
- "description": "Customer's phone. _Deprecated: Use customer object instead._",
+ "description": "Ignored. _Deprecated: Manage customer details through the Customers API._",
"deprecated": true,
"example": "9123456789",
"nullable": true,
@@ -41771,7 +41780,7 @@
},
"phone_country_code": {
"type": "string",
- "description": "Customer's phone country code. _Deprecated: Use customer object instead._",
+ "description": "Ignored. _Deprecated: Manage customer details through the Customers API._",
"deprecated": true,
"example": "+1",
"nullable": true,
@@ -41899,7 +41908,7 @@
},
"customer_id": {
"type": "string",
- "description": "Customer identifier for normal and proxy payouts; normal creates may generate it when omitted, while proxy creates require this or customer.id owning payout_method_id. _Deprecated: Use customer.id instead._",
+ "description": "Existing customer ID used at creation. Ignored on update and confirm.\n_Deprecated: Use customer.id instead._",
"deprecated": true,
"example": "cus_y3oqhf46pyzuxjbcn2giaqnb44",
"nullable": true,
@@ -42035,7 +42044,7 @@
},
"email": {
"type": "string",
- "description": "Customer's email. _Deprecated: Use customer object instead._",
+ "description": "Ignored. _Deprecated: Manage customer details through the Customers API._",
"deprecated": true,
"example": "[email protected]",
"nullable": true,
@@ -42043,7 +42052,7 @@
},
"name": {
"type": "string",
- "description": "Customer's name. _Deprecated: Use customer object instead._",
+ "description": "Ignored. _Deprecated: Manage customer details through the Customers API._",
"deprecated": true,
"example": "John Test",
"nullable": true,
@@ -42051,7 +42060,7 @@
},
"phone": {
"type": "string",
- "description": "Customer's phone. _Deprecated: Use customer object instead._",
+ "description": "Ignored. _Deprecated: Manage customer details through the Customers API._",
"deprecated": true,
"example": "9123456789",
"nullable": true,
@@ -42059,7 +42068,7 @@
},
"phone_country_code": {
"type": "string",
- "description": "Customer's phone country code. _Deprecated: Use customer object instead._",
+ "description": "Ignored. _Deprecated: Manage customer details through the Customers API._",
"deprecated": true,
"example": "+1",
"nullable": true,
diff --git a/api-reference/v1/payouts/payouts--confirm.mdx b/api-reference/v1/payouts/payouts--confirm.mdx
index 14373744eb..149e179cad 100644
--- a/api-reference/v1/payouts/payouts--confirm.mdx
+++ b/api-reference/v1/payouts/payouts--confirm.mdx
@@ -1,3 +1,5 @@
---
openapi: post /payouts/{payout_id}/confirm
----
\ No newline at end of file
+---
+
+Confirmation keeps the customer assigned at payout creation. Request customer IDs and inline details are ignored.
diff --git a/api-reference/v1/payouts/payouts--create.mdx b/api-reference/v1/payouts/payouts--create.mdx
index 14b91af356..6131f15717 100644
--- a/api-reference/v1/payouts/payouts--create.mdx
+++ b/api-reference/v1/payouts/payouts--create.mdx
@@ -4,6 +4,14 @@ openapi: post /payouts/create
Create a payout with `payout_method_data` (including raw card details) or a saved `payout_method_id`.
+## Customer creation
+
+Create the customer with `POST /customers`, then pass its ID as `customer.id` or `customer_id`. A customer ID is required for every payout create request. Unknown IDs return `CustomerNotFound` (HTTP 404). Inline customer attributes are ignored.
+
+The customer association is fixed at creation. Update and confirm use the stored ID and ignore supplied IDs.
+
+Payout links require the top-level `customer_id`.
+
## External-vault card payouts
Use a card saved during a payment. Provide a unique `payout_id`, the saved `payout_method_id`, and the card owner's `customer_id` or `customer.id`.
diff --git a/api-reference/v1/payouts/payouts--update.mdx b/api-reference/v1/payouts/payouts--update.mdx
index c6a61c7715..e880c55d9d 100644
--- a/api-reference/v1/payouts/payouts--update.mdx
+++ b/api-reference/v1/payouts/payouts--update.mdx
@@ -1,3 +1,5 @@
---
openapi: post /payouts/{payout_id}
----
\ No newline at end of file
+---
+
+Updates keep the customer assigned at payout creation. Request customer IDs and inline details are ignored.
diff --git a/api-reference/v2/openapi_spec_v2.json b/api-reference/v2/openapi_spec_v2.json
index e5879af259..4cc52a8f8d 100644
--- a/api-reference/v2/openapi_spec_v2.json
+++ b/api-reference/v2/openapi_spec_v2.json
@@ -28546,7 +28546,7 @@
},
"customer_id": {
"type": "string",
- "description": "Customer identifier for normal and proxy payouts; normal creates may generate it when omitted, while proxy creates require this or customer.id owning payout_method_id. _Deprecated: Use customer.id instead._",
+ "description": "Existing customer ID used at creation. Ignored on update and confirm.\n_Deprecated: Use customer.id instead._",
"deprecated": true,
"example": "cus_y3oqhf46pyzuxjbcn2giaqnb44",
"nullable": true,
@@ -28689,7 +28689,7 @@
},
"email": {
"type": "string",
- "description": "Customer's email. _Deprecated: Use customer object instead._",
+ "description": "Ignored. _Deprecated: Manage customer details through the Customers API._",
"deprecated": true,
"example": "[email protected]",
"nullable": true,
@@ -28697,7 +28697,7 @@
},
"name": {
"type": "string",
- "description": "Customer's name. _Deprecated: Use customer object instead._",
+ "description": "Ignored. _Deprecated: Manage customer details through the Customers API._",
"deprecated": true,
"example": "John Test",
"nullable": true,
@@ -28705,7 +28705,7 @@
},
"phone": {
"type": "string",
- "description": "Customer's phone. _Deprecated: Use customer object instead._",
+ "description": "Ignored. _Deprecated: Manage customer details through the Customers API._",
"deprecated": true,
"example": "9123456789",
"nullable": true,
@@ -28713,7 +28713,7 @@
},
"phone_country_code": {
"type": "string",
- "description": "Customer's phone country code. _Deprecated: Use customer object instead._",
+ "description": "Ignored. _Deprecated: Manage customer details through the Customers API._",
"deprecated": true,
"example": "+1",
"nullable": true,
@@ -28828,7 +28828,7 @@
},
"customer_id": {
"type": "string",
- "description": "The identifier for the customer object. If not provided the customer ID will be autogenerated. _Deprecated: Use the customer object instead._",
+ "description": "Attached customer ID, if any. _Deprecated: Use customer.id instead._",
"deprecated": true,
"example": "cus_y3oqhf46pyzuxjbcn2giaqnb44",
"maxLength": 255
diff --git a/crates/api_models/src/payouts.rs b/crates/api_models/src/payouts.rs
index e43abb856d..1c823d4a0a 100644
--- a/crates/api_models/src/payouts.rs
+++ b/crates/api_models/src/payouts.rs
@@ -120,11 +120,13 @@ pub struct PayoutCreateRequest {
#[schema(value_type = Option<bool>, example = true, default = false)]
pub auto_fulfill: Option<bool>,
- /// Customer identifier for normal and proxy payouts; normal creates may generate it when omitted, while proxy creates require this or customer.id owning payout_method_id. _Deprecated: Use customer.id instead._
+ /// Existing customer ID used at creation. Ignored on update and confirm.
+ /// _Deprecated: Use customer.id instead._
#[schema(deprecated, value_type = Option<String>, max_length = 255, example = "cus_y3oqhf46pyzuxjbcn2giaqnb44")]
pub customer_id: Option<id_type::CustomerId>,
- /// Passing this object creates a new customer or attaches an existing customer to the payout
+ /// On create, attach an existing customer using customer.id.
+ /// Other details are ignored. Update and confirm keep the stored customer.
#[schema(value_type = Option<CustomerDetails>)]
pub customer: Option<payments::CustomerDetails>,
@@ -203,19 +205,19 @@ pub struct PayoutCreateRequest {
#[schema(value_type = Option<u32>, example = 900)]
pub session_expiry: Option<u32>,
- /// Customer's email. _Deprecated: Use customer object instead._
+ /// Ignored. _Deprecated: Manage customer details through the Customers API._
#[schema(deprecated, max_length = 255, value_type = Option<String>, example = "[email protected]")]
pub email: Option<Email>,
- /// Customer's name. _Deprecated: Use customer object instead._
+ /// Ignored. _Deprecated: Manage customer details through the Customers API._
#[schema(deprecated, value_type = Option<String>, max_length = 255, example = "John Test")]
pub name: Option<Secret<String>>,
- /// Customer's phone. _Deprecated: Use customer object instead._
+ /// Ignored. _Deprecated: Manage customer details through the Customers API._
#[schema(deprecated, value_type = Option<String>, max_length = 255, example = "9123456789")]
pub phone: Option<Secret<String>>,
- /// Customer's phone country code. _Deprecated: Use customer object instead._
+ /// Ignored. _Deprecated: Manage customer details through the Customers API._
#[schema(deprecated, max_length = 255, example = "+1")]
pub phone_country_code: Option<String>,
@@ -944,11 +946,11 @@ pub struct PayoutCreateResponse {
#[schema(value_type = bool, example = true, default = false)]
pub auto_fulfill: bool,
- /// The identifier for the customer object. If not provided the customer ID will be autogenerated. _Deprecated: Use the customer object instead._
+ /// Attached customer ID, if any. _Deprecated: Use customer.id instead._
#[schema(deprecated, value_type = String, max_length = 255, example = "cus_y3oqhf46pyzuxjbcn2giaqnb44")]
pub customer_id: Option<id_type::CustomerId>,
- /// Passing this object creates a new customer or attaches an existing customer to the payout
+ /// Attached customer details, if any.
#[schema(value_type = Option<CustomerDetailsResponse>)]
pub customer: Option<payments::CustomerDetailsResponse>,
diff --git a/crates/openapi/src/routes/payouts.rs b/crates/openapi/src/routes/payouts.rs
index e5a5b6ea25..b2b718d902 100644
--- a/crates/openapi/src/routes/payouts.rs
+++ b/crates/openapi/src/routes/payouts.rs
@@ -34,6 +34,7 @@
responses(
(status = 200, description = "Payout created", body = PayoutCreateResponse),
(status = 400, description = "Missing mandatory fields or invalid request"),
+ (status = 404, description = "Customer does not exist in the merchant's records"),
(status = 501, description = "External-vault proxy payout execution is not implemented yet")
),
tag = "Payouts",
@@ -70,7 +71,8 @@ pub async fn payouts_retrieve() {}
request_body=PayoutUpdateRequest,
responses(
(status = 200, description = "Payout updated", body = PayoutCreateResponse),
- (status = 400, description = "Missing Mandatory fields")
+ (status = 400, description = "Missing mandatory fields or invalid request"),
+ (status = 404, description = "Payout does not exist in the merchant's records")
),
tag = "Payouts",
operation_id = "Update a Payout",
@@ -212,7 +214,8 @@ pub async fn payouts_list_by_filter_profile() {}
request_body=PayoutConfirmRequest,
responses(
(status = 200, description = "Payout updated", body = PayoutCreateResponse),
- (status = 400, description = "Missing Mandatory fields")
+ (status = 400, description = "Missing mandatory fields or invalid request"),
+ (status = 404, description = "Payout does not exist in the merchant's records")
),
tag = "Payouts",
operation_id = "Confirm a Payout",
diff --git a/crates/router/src/core/payouts/helpers.rs b/crates/router/src/core/payouts/helpers.rs
index 5f4d437b7f..d1bfa484c6 100644
--- a/crates/router/src/core/payouts/helpers.rs
+++ b/crates/router/src/core/payouts/helpers.rs
@@ -1,7 +1,6 @@
use ::payment_methods::controller::PaymentMethodsController;
-use api_models::{customers::CustomerDocumentDetails, enums, payment_methods::Card, payouts};
+use api_models::{enums, payment_methods::Card, payouts};
use common_utils::{
- crypto::Encryptable,
encryption::Encryption,
errors::CustomResult,
ext_traits::{AsyncExt, StringExt, ValueExt},
@@ -11,11 +10,9 @@ use common_utils::{
MinorUnit, UnifiedCode, UnifiedMessage,
},
};
-#[cfg(feature = "v1")]
-use common_utils::{generate_customer_id_of_default_length, types::keymanager::ToEncryptable};
use error_stack::{report, ResultExt};
use hyperswitch_domain_models::type_encryption::{crypto_operation, CryptoOperation};
-use hyperswitch_masking::{ExposeInterface, PeekInterface, Secret, SwitchStrategy};
+use hyperswitch_masking::{ExposeInterface, PeekInterface, Secret};
use router_env::logger;
use super::{
@@ -36,7 +33,7 @@ use crate::{
transformers::{DataDuplicationCheck, StoreCardReq, StoreGenericReq, StoreLockerReq},
vault,
},
- payments::{helpers as payment_helpers, routing, CustomerDetails},
+ payments::{helpers as payment_helpers, routing},
routing::TransactionData,
utils as core_utils,
},
@@ -913,173 +910,41 @@ pub async fn save_payout_data_to_locker(
}
#[cfg(feature = "v2")]
-pub(super) async fn get_or_create_customer_details(
+pub(super) async fn get_existing_customer_details(
_state: &SessionState,
- _customer_details: &CustomerDetails,
- _merchant_context: &domain::Platform,
-) -> RouterResult<Option<domain::Customer>> {
+ _customer_id: &id_type::CustomerId,
+ _platform: &domain::Platform,
+) -> RouterResult<domain::Customer> {
todo!()
}
#[cfg(feature = "v1")]
-pub(super) async fn get_or_create_customer_details(
+pub(super) async fn get_existing_customer_details(
state: &SessionState,
- customer_details: &CustomerDetails,
+ customer_id: &id_type::CustomerId,
platform: &domain::Platform,
-) -> RouterResult<Option<domain::Customer>> {
- let db: &dyn StorageInterface = &*state.store;
- // Create customer_id if not passed in request
- let customer_id = customer_details
- .customer_id
- .clone()
- .unwrap_or_else(generate_customer_id_of_default_length);
-
- let merchant_id = platform.get_processor().get_account().get_id();
- let key = platform
- .get_processor()
- .get_key_store()
- .key
- .get_inner()
- .peek();
-
+) -> RouterResult<domain::Customer> {
+ let processor = platform.get_processor();
let use_merchant_reference_id =
payments_helpers::should_use_merchant_reference_id_as_customer_id_for_provider(
state,
platform.get_provider(),
- Some(&customer_id),
+ Some(customer_id),
)
.await;
- match db
+
+ state
+ .store
.find_customer_optional_by_customer_id_merchant_id(
- &customer_id,
- merchant_id,
- platform.get_processor().get_key_store(),
- platform.get_processor().get_account().storage_scheme,
+ customer_id,
+ processor.get_account().get_id(),
+ processor.get_key_store(),
+ processor.get_account().storage_scheme,
use_merchant_reference_id,
)
.await
- .change_context(errors::ApiErrorResponse::InternalServerError)?
- {
- // Customer found
- Some(customer) => Ok(Some(customer)),
-
- // Customer not found
- // create only if atleast one of the fields were provided for customer creation or else throw error
- None => {
- if customer_details.name.is_some()
- || customer_details.email.is_some()
- || customer_details.phone.is_some()
- || customer_details.phone_country_code.is_some()
- {
- let encrypted_data = crypto_operation(
- &state.into(),
- type_name!(domain::Customer),
- CryptoOperation::BatchEncrypt(
- domain::FromRequestEncryptableCustomer::to_encryptable(
- domain::FromRequestEncryptableCustomer {
- name: customer_details.name.clone(),
- email: customer_details
- .email
- .clone()
- .map(|a| a.expose().switch_strategy()),
- phone: customer_details.phone.clone(),
- tax_registration_id: customer_details.tax_registration_id.clone(),
- },
- ),
- ),
- Identifier::Merchant(
- platform.get_processor().get_key_store().merchant_id.clone(),
- ),
- key,
- )
- .await
- .and_then(|val| val.try_into_batchoperation())
- .change_context(errors::ApiErrorResponse::InternalServerError)
- .attach_printable("Failed to encrypt customer")?;
- let encryptable_customer =
- domain::FromRequestEncryptableCustomer::from_encryptable(encrypted_data)
- .change_context(errors::ApiErrorResponse::InternalServerError)
- .attach_printable("Failed to form EncryptableCustomer")?;
-
- let document_details = customer_details
- .document_details
- .clone()
- .async_lift(|inner| async move {
- let encrypted_inner = inner
- .as_ref()
- .map(CustomerDocumentDetails::to)
- .transpose()
- .change_context(common_utils::errors::CryptoError::EncodingFailed)
- .attach_printable(
- "Failed to convert CustomerDocumentDetails to SecretSerdeValue",
- )?;
-
- crypto_operation(
- &state.into(),
- common_utils::type_name!(domain::Customer),
- CryptoOperation::EncryptOptional(encrypted_inner),
- Identifier::Merchant(
- platform.get_processor().get_key_store().merchant_id.clone(),
- ),
- platform.get_processor().get_key_store().key.peek(),
- )
- .await
- .and_then(|val| val.try_into_optionaloperation())
- })
- .await
- .change_context(errors::ApiErrorResponse::InternalServerError)
- .attach_printable("Unable to encrypt document_details")?;
-
- let customer = domain::Customer::new(
- customer_id.clone(),
- merchant_id.to_owned().clone(),
- encryptable_customer.name,
- encryptable_customer.email.map(|email| {
- let encryptable: Encryptable<Secret<String, pii::EmailStrategy>> =
- Encryptable::new(
- email.clone().into_inner().switch_strategy(),
- email.into_encrypted(),
- );
- encryptable
- }),
- encryptable_customer.phone,
- customer_details.phone_country_code.to_owned(),
- None,
- None,
- None,
- None,
- encryptable_customer.tax_registration_id,
- document_details,
- platform
- .get_initiator()
- .and_then(|initiator| initiator.to_created_by()),
- platform
- .get_initiator()
- .and_then(|initiator| initiator.to_created_by()), // Same as created_by on creation
- id_type::GlobalCustomerId::generate(&state.conf.cell_information.id),
- );
-
- Ok(Some(
- db.insert_customer(
- customer,
- platform.get_processor().get_key_store(),
- platform.get_processor().get_account().storage_scheme,
- )
- .await
- .change_context(errors::ApiErrorResponse::InternalServerError)
- .attach_printable_lazy(|| {
- format!(
- "Failed to insert customer [id - {customer_id:?}] for merchant [id - {merchant_id:?}]",
- )
- })?,
- ))
- } else {
- Err(report!(errors::ApiErrorResponse::InvalidRequestData {
- message: format!("customer for id - {customer_id:?} not found"),
- }))
- }
- }
- }
+ .to_not_found_response(errors::ApiErrorResponse::CustomerNotFound)?
+ .ok_or_else(|| report!(errors::ApiErrorResponse::CustomerNotFound))
}
#[cfg(all(feature = "payouts", feature = "v1"))]
@@ -1485,23 +1350,8 @@ pub async fn update_payouts_and_payout_attempt(
)
.await?;
- // Fetch customer details from request and create new or else use existing customer that was attached
- let customer = get_customer_details_from_request(req);
- let customer_id = if customer.customer_id.is_some()
- || customer.name.is_some()
- || customer.email.is_some()
- || customer.phone.is_some()
- || customer.phone_country_code.is_some()
- {
- payout_data.customer_details =
- get_or_create_customer_details(state, &customer, platform).await?;
- payout_data
- .customer_details
- .as_ref()
- .map(|customer| customer.get_id().clone())
- } else {
- payout_data.payouts.customer_id.clone()
- };
+ // Update and confirm retain the stored customer.
+ let customer_id = payouts.customer_id.clone();
let (billing_address, address_id) = resolve_billing_address_for_payout(
state,
@@ -1626,57 +1476,6 @@ pub async fn update_payouts_and_payout_attempt(
Ok(())
}
-pub(super) fn get_customer_details_from_request(
- request: &payouts::PayoutCreateRequest,
-) -> CustomerDetails {
- let customer_id = request.get_customer_id().map(ToOwned::to_owned);
-
- let customer_name = request
- .customer
- .as_ref()
- .and_then(|customer_details| customer_details.name.clone())
- .or(request.name.clone());
-
- let customer_email = request
- .customer
- .as_ref()
- .and_then(|customer_details| customer_details.email.clone())
- .or(request.email.clone());
-
- let customer_phone = request
- .customer
- .as_ref()
- .and_then(|customer_details| customer_details.phone.clone())
- .or(request.phone.clone());
-
- let customer_phone_code = request
- .customer
- .as_ref()
- .and_then(|customer_details| customer_details.phone_country_code.clone())
- .or(request.phone_country_code.clone());
-
- let tax_registration_id = request
- .customer
- .as_ref()
- .and_then(|customer_details| customer_details.tax_registration_id.clone());
-
- let document_details = request
- .customer
- .as_ref()
- .and_then(|customer_details| customer_details.document_details.clone());
-
- CustomerDetails {
- customer_id,
- name: customer_name,
- email: customer_email,
- phone: customer_phone,
- phone_country_code: customer_phone_code,
- tax_registration_id,
- document_details,
- date_of_birth: None,
- }
-}
-
pub async fn get_translated_unified_code_and_message(
state: &SessionState,
unified_code: Option<&UnifiedCode>,
diff --git a/crates/router/src/core/payouts/validator.rs b/crates/router/src/core/payouts/validator.rs
index a7e52a7993..207c0db968 100644
--- a/crates/router/src/core/payouts/validator.rs
+++ b/crates/router/src/core/payouts/validator.rs
@@ -94,6 +94,22 @@ pub(super) fn validate_payout_condition(invalid: bool, message: &str) -> RouterR
}
}
+#[cfg(feature = "v1")]
+async fn get_and_validate_payout_customer(
+ state: &SessionState,
+ platform: &domain::Platform,
+ req: &payouts::PayoutCreateRequest,
+) -> RouterResult<Option<domain::Customer>> {
+ match req.get_customer_id() {
+ Some(customer_id) => helpers::get_existing_customer_details(state, customer_id, platform)
+ .await
+ .map(Some),
+ None => Err(report!(errors::ApiErrorResponse::MissingRequiredField {
+ field_name: "customer_id".into(),
+ })),
+ }
+}
+
/// Validate payout ID uniqueness within the merchant's storage scheme.
#[instrument(skip(db))]
pub async fn validate_uniqueness_of_payout_id_against_merchant_id(
@@ -141,7 +157,6 @@ pub async fn validate_create_request(
todo!()
}
-/// Validate identity before customer creation or raw-method lookup.
#[cfg(feature = "v1")]
pub async fn validate_create_request(
state: &SessionState,
@@ -178,8 +193,7 @@ pub async fn validate_create_request(
.attach_printable("invalid merchant_id in request"))
})?;
- // Resolve the profile before any customer side effects so the cap applies to the
- // exact merchant/profile context that will own the payout.
+ // Resolve the profile before checking its payout limit.
let business_profile = core_utils::get_profile_from_business_details(
req.business_country,
req.business_label.as_ref(),
@@ -234,7 +248,6 @@ pub async fn validate_create_request(
None => Ok(()),
}?;
- // Resolve the profile and kill switch before creating customers or resolving payout methods.
let business_profile = core_utils::get_profile_from_business_details(
req.business_country,
req.business_label.as_ref(),
@@ -247,19 +260,7 @@ pub async fn validate_create_request(
.await?;
let profile_id = business_profile.get_id().to_owned();
- // Fetch customer details (merge of loose fields + customer object) and create DB entry
- let customer_in_request = helpers::get_customer_details_from_request(req);
- let customer = if customer_in_request.customer_id.is_some()
- || customer_in_request.name.is_some()
- || customer_in_request.email.is_some()
- || customer_in_request.phone.is_some()
- || customer_in_request.phone_country_code.is_some()
- || customer_in_request.document_details.is_some()
- {
- helpers::get_or_create_customer_details(state, &customer_in_request, platform).await?
- } else {
- None
- };
+ let customer = get_and_validate_payout_customer(state, platform, req).await?;
let payment_method: Option<PaymentMethod> =
match (req.payout_token.as_ref(), req.payout_method_id.clone()) {