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(stripe): payouts mapping (#12420)

Siddhesh Kultheeef99e22bef6
diff --git a/crates/api_models/src/payouts.rs b/crates/api_models/src/payouts.rs
index 1cd1c34773..1c08d3a8f7 100644
--- a/crates/api_models/src/payouts.rs
+++ b/crates/api_models/src/payouts.rs
@@ -1183,9 +1183,18 @@ pub struct PayoutVendorDetails {
pub struct PayoutIndividualDetails {
pub tos_acceptance_date: Option<i64>,
pub tos_acceptance_ip: Option<Secret<String>>,
+ /// Deprecated. Use `date_of_birth`.
+ #[schema(deprecated, value_type = Option<String>)]
pub individual_dob_day: Option<Secret<String>>,
+ /// Deprecated. Use `date_of_birth`.
+ #[schema(deprecated, value_type = Option<String>)]
pub individual_dob_month: Option<Secret<String>>,
+ /// Deprecated. Use `date_of_birth`.
+ #[schema(deprecated, value_type = Option<String>)]
pub individual_dob_year: Option<Secret<String>>,
+ /// The individual's date of birth.
+ #[schema(value_type = Option<time::Date>, example = "1990-01-31")]
+ pub date_of_birth: Option<Secret<time::Date>>,
pub individual_id_number: Option<Secret<String>>,
pub individual_ssn_last_4: Option<Secret<String>>,
pub external_account_account_holder_type: Option<String>,
diff --git a/crates/hyperswitch_connectors/src/connectors/stripe/transformers/connect.rs b/crates/hyperswitch_connectors/src/connectors/stripe/transformers/connect.rs
index 96fa2d8fe0..0eebb862f3 100644
--- a/crates/hyperswitch_connectors/src/connectors/stripe/transformers/connect.rs
+++ b/crates/hyperswitch_connectors/src/connectors/stripe/transformers/connect.rs
@@ -3,7 +3,7 @@ use common_utils::{ext_traits::OptionExt as _, pii::Email};
use error_stack::ResultExt;
use hyperswitch_domain_models::types::{PayoutsResponseData, PayoutsRouterData};
use hyperswitch_interfaces::errors;
-use hyperswitch_masking::Secret;
+use hyperswitch_masking::{PeekInterface, Secret};
use serde::{Deserialize, Serialize};
use super::ErrorDetails;
@@ -83,10 +83,58 @@ pub struct StripeConnectReversalResponse {
source_refund: Option<String>,
}
+#[derive(Clone, Copy, Debug, Serialize)]
+#[serde(rename_all = "snake_case")]
+enum PayoutAccountType {
+ Custom,
+ Express,
+ Standard,
+}
+
+#[derive(Clone, Copy, Debug, Serialize)]
+#[serde(rename_all = "snake_case")]
+enum PayoutBusinessType {
+ Company,
+ Individual,
+}
+
+impl TryFrom<&str> for PayoutAccountType {
+ type Error = Error;
+
+ fn try_from(value: &str) -> Result<Self, Self::Error> {
+ match value {
+ "custom" => Ok(Self::Custom),
+ "express" => Ok(Self::Express),
+ "standard" => Ok(Self::Standard),
+ _ => Err(error_stack::report!(
+ errors::ConnectorError::InvalidDataFormat {
+ field_name: "vendor_details.account_type".into(),
+ }
+ )),
+ }
+ }
+}
+
+impl TryFrom<&str> for PayoutBusinessType {
+ type Error = Error;
+
+ fn try_from(value: &str) -> Result<Self, Self::Error> {
+ match value {
+ "company" => Ok(Self::Company),
+ "individual" => Ok(Self::Individual),
+ _ => Err(error_stack::report!(
+ errors::ConnectorError::InvalidDataFormat {
+ field_name: "business_type".into(),
+ }
+ )),
+ }
+ }
+}
+
#[derive(Clone, Debug, Serialize)]
pub struct StripeConnectRecipientCreateRequest {
#[serde(rename = "type")]
- account_type: String,
+ account_type: PayoutAccountType,
country: Option<enums::CountryAlpha2>,
email: Option<Email>,
#[serde(rename = "capabilities[card_payments][requested]")]
@@ -97,7 +145,7 @@ pub struct StripeConnectRecipientCreateRequest {
tos_acceptance_date: Option<i64>,
#[serde(rename = "tos_acceptance[ip]")]
tos_acceptance_ip: Option<Secret<String>>,
- business_type: String,
+ business_type: PayoutBusinessType,
#[serde(rename = "business_profile[mcc]")]
business_profile_mcc: Option<i32>,
#[serde(rename = "business_profile[url]")]
@@ -195,7 +243,7 @@ pub struct RecipientBankAccountRequest {
#[serde(rename = "external_account[account_number]")]
external_account_account_number: Secret<String>,
#[serde(rename = "external_account[account_holder_type]")]
- external_account_account_holder_type: String,
+ external_account_account_holder_type: PayoutBusinessType,
#[serde(rename = "external_account[routing_number]")]
external_account_routing_number: Secret<String>,
}
@@ -331,15 +379,19 @@ impl<F> TryFrom<&PayoutsRouterData<F>> for StripeConnectRecipientCreateRequest {
payout_vendor_details.vendor_details,
payout_vendor_details.individual_details,
);
+ let date_of_birth = individual_details
+ .date_of_birth
+ .as_ref()
+ .map(|date| date.peek());
Ok(Self {
- account_type: vendor_details.account_type,
+ account_type: PayoutAccountType::try_from(vendor_details.account_type.as_str())?,
country: address.country,
email: Some(customer_email.clone()),
capabilities_card_payments: vendor_details.capabilities_card_payments,
capabilities_transfers: vendor_details.capabilities_transfers,
tos_acceptance_date: individual_details.tos_acceptance_date,
tos_acceptance_ip: individual_details.tos_acceptance_ip,
- business_type: vendor_details.business_type,
+ business_type: PayoutBusinessType::try_from(vendor_details.business_type.as_str())?,
business_profile_mcc: vendor_details.business_profile_mcc,
business_profile_url: vendor_details.business_profile_url,
business_profile_name: vendor_details.business_profile_name.clone(),
@@ -354,9 +406,15 @@ impl<F> TryFrom<&PayoutsRouterData<F>> for StripeConnectRecipientCreateRequest {
company_owners_provided: vendor_details.company_owners_provided,
individual_first_name: address.first_name,
individual_last_name: address.last_name,
- individual_dob_day: individual_details.individual_dob_day,
- individual_dob_month: individual_details.individual_dob_month,
- individual_dob_year: individual_details.individual_dob_year,
+ individual_dob_day: date_of_birth
+ .map(|date| Secret::new(date.day().to_string()))
+ .or(individual_details.individual_dob_day),
+ individual_dob_month: date_of_birth
+ .map(|date| Secret::new(u8::from(date.month()).to_string()))
+ .or(individual_details.individual_dob_month),
+ individual_dob_year: date_of_birth
+ .map(|date| Secret::new(date.year().to_string()))
+ .or(individual_details.individual_dob_year),
individual_address_line1: address.line1,
individual_address_line2: address.line2,
individual_address_postal_code: address.zip,
@@ -422,9 +480,12 @@ impl<F> TryFrom<&PayoutsRouterData<F>> for StripeConnectRecipientAccountCreateRe
})?,
external_account_currency: request.destination_currency.to_owned(),
external_account_account_holder_name: customer_name,
- external_account_account_holder_type: payout_vendor_details
- .individual_details
- .get_external_account_account_holder_type()?,
+ external_account_account_holder_type: PayoutBusinessType::try_from(
+ payout_vendor_details
+ .individual_details
+ .get_external_account_account_holder_type()?
+ .as_str(),
+ )?,
external_account_account_number: bank_details.bank_account_number,
external_account_routing_number: bank_details.bank_routing_number,
}))
diff --git a/crates/router/src/core/unified_connector_service/transformers.rs b/crates/router/src/core/unified_connector_service/transformers.rs
index 9d6ccfb878..fab97ae0b6 100644
--- a/crates/router/src/core/unified_connector_service/transformers.rs
+++ b/crates/router/src/core/unified_connector_service/transformers.rs
@@ -8164,6 +8164,22 @@ impl ForeignFrom<&common_types::payments::BillingDescriptor> for payments_grpc::
}
}
+#[cfg(feature = "payouts")]
+impl ForeignFrom<&common_types::payouts::PayoutsBillingDescriptor>
+ for payments_grpc::BillingDescriptor
+{
+ fn foreign_from(billing_descriptor: &common_types::payouts::PayoutsBillingDescriptor) -> Self {
+ Self {
+ name: None,
+ city: None,
+ phone: None,
+ statement_descriptor: billing_descriptor.statement_descriptor.clone(),
+ statement_descriptor_suffix: None,
+ reference: billing_descriptor.reference.clone(),
+ }
+ }
+}
+
impl transformers::ForeignTryFrom<&common_enums::PaymentChannel> for payments_grpc::PaymentChannel {
type Error = error_stack::Report<UnifiedConnectorServiceError>;
@@ -8938,20 +8954,20 @@ impl ForeignFrom<common_enums::PayoutSendPriority> for payments_grpc::payout_enu
}
#[cfg(feature = "payouts")]
-impl transformers::ForeignTryFrom<&router_request_types::CustomerDetails>
+impl transformers::ForeignTryFrom<(&router_request_types::CustomerDetails, Option<String>)>
for payments_grpc::Customer
{
type Error = error_stack::Report<UnifiedConnectorServiceError>;
fn foreign_try_from(
- customer: &router_request_types::CustomerDetails,
+ (customer, connector_customer_id): (&router_request_types::CustomerDetails, Option<String>),
) -> Result<Self, Self::Error> {
Ok(Self {
id: customer
.customer_id
.clone()
.map(|id| id.get_string_repr().to_string()),
- connector_customer_id: None,
+ connector_customer_id,
name: customer.name.clone().map(|s| s.expose()),
email: customer.email.clone().map(|e| e.expose().expose().into()),
phone_number: customer.phone.clone(),
@@ -9090,7 +9106,12 @@ impl
.request
.customer_details
.as_ref()
- .map(payments_grpc::Customer::foreign_try_from)
+ .map(|customer| {
+ payments_grpc::Customer::foreign_try_from((
+ customer,
+ router_data.connector_customer.clone(),
+ ))
+ })
.transpose()?;
let priority = router_data
@@ -9219,7 +9240,7 @@ impl
.request
.customer_details
.as_ref()
- .map(payments_grpc::Customer::foreign_try_from)
+ .map(|customer| payments_grpc::Customer::foreign_try_from((customer, None)))
.transpose()?
.ok_or(
error_stack::Report::new(UnifiedConnectorServiceError::MissingRequiredField {
@@ -9289,7 +9310,12 @@ impl
.request
.customer_details
.as_ref()
- .map(payments_grpc::Customer::foreign_try_from)
+ .map(|customer| {
+ payments_grpc::Customer::foreign_try_from((
+ customer,
+ router_data.connector_customer.clone(),
+ ))
+ })
.transpose()?
.ok_or(
error_stack::Report::new(UnifiedConnectorServiceError::MissingRequiredField {
@@ -9312,8 +9338,8 @@ impl
payout_connector_metadata: router_data
.request
.payout_connector_metadata
- .clone()
- .map(|metadata| Secret::new(metadata.expose().to_string())),
+ .as_ref()
+ .map(|secret| Secret::new(secret.peek().to_string())),
destination_currency: destination_currency.into(),
customer: Some(customer),
access_token: router_data.access_token.clone().map(|at| at.token),
@@ -9344,11 +9370,7 @@ impl
.request
.billing_descriptor
.as_ref()
- .map(|descriptor| payments_grpc::BillingDescriptor {
- reference: descriptor.reference.clone(),
- statement_descriptor: descriptor.statement_descriptor.clone(),
- ..Default::default()
- }),
+ .map(payments_grpc::BillingDescriptor::foreign_from),
})
}
}
@@ -9386,7 +9408,12 @@ impl
.request
.customer_details
.as_ref()
- .map(payments_grpc::Customer::foreign_try_from)
+ .map(|customer| {
+ payments_grpc::Customer::foreign_try_from((
+ customer,
+ router_data.connector_customer.clone(),
+ ))
+ })
.transpose()?
.ok_or(
error_stack::Report::new(UnifiedConnectorServiceError::MissingRequiredField {
@@ -9418,6 +9445,119 @@ impl
}
}
+#[cfg(feature = "payouts")]
+impl<F>
+ transformers::ForeignTryFrom<(
+ &api_models::payouts::PayoutVendorAccountDetails,
+ &RouterData<F, router_request_types::PayoutsData, PayoutsResponseData>,
+ )> for payments_grpc::PayoutVendorAccountDetails
+{
+ type Error = error_stack::Report<UnifiedConnectorServiceError>;
+
+ fn foreign_try_from(
+ (vendor_account_details, router_data): (
+ &api_models::payouts::PayoutVendorAccountDetails,
+ &RouterData<F, router_request_types::PayoutsData, PayoutsResponseData>,
+ ),
+ ) -> Result<Self, Self::Error> {
+ let vendor_details = &vendor_account_details.vendor_details;
+ let individual_details = &vendor_account_details.individual_details;
+
+ let billing_details = router_data
+ .address
+ .get_payment_method_billing()
+ .and_then(|billing_address| billing_address.address.as_ref());
+
+ let business_profile_name = vendor_details
+ .business_profile_name
+ .as_ref()
+ .map(|name| name.peek().to_string().into());
+
+ Ok(Self {
+ vendor_details: Some(payments_grpc::VendorDetails {
+ account_type: match vendor_details.account_type.as_str() {
+ "custom" => Some(i32::from(
+ payments_grpc::payout_enums::PayoutAccountType::Custom,
+ )),
+ "express" => Some(i32::from(
+ payments_grpc::payout_enums::PayoutAccountType::Express,
+ )),
+ "standard" => Some(i32::from(
+ payments_grpc::payout_enums::PayoutAccountType::Standard,
+ )),
+ _ => None,
+ },
+ vendor_category_code: vendor_details
+ .business_profile_mcc
+ .map(|mcc| mcc.to_string()),
+ vendor_url: vendor_details
+ .business_profile_url
+ .as_ref()
+ .map(|url| url.clone().into()),
+ vendor_name: business_profile_name.clone(),
+ statement_descriptor: business_profile_name,
+ owners_provided: vendor_details.company_owners_provided,
+ vendor_type: match vendor_details.business_type.as_str() {
+ "company" => Some(i32::from(payments_grpc::BankHolderType::Business)),
+ "individual" => Some(i32::from(payments_grpc::BankHolderType::Personal)),
+ _ => None,
+ },
+ card_payments_enabled: vendor_details.capabilities_card_payments,
+ transfers_enabled: vendor_details.capabilities_transfers,
+ }),
+ individual_details: Some(payments_grpc::IndividualDetails {
+ first_name: billing_details
+ .and_then(|billing_details| billing_details.first_name.as_ref())
+ .map(|first_name| first_name.peek().to_string().into()),
+ last_name: billing_details
+ .and_then(|billing_details| billing_details.last_name.as_ref())
+ .map(|last_name| last_name.peek().to_string().into()),
+ phone: router_data
+ .request
+ .customer_details
+ .as_ref()
+ .and_then(|customer_details| customer_details.phone.as_ref())
+ .map(|phone| phone.peek().to_string().into()),
+ ssn_last_4: individual_details
+ .individual_ssn_last_4
+ .as_ref()
+ .map(|ssn| ssn.peek().to_string().into()),
+ id_number: individual_details
+ .individual_id_number
+ .as_ref()
+ .map(|id_number| id_number.peek().to_string().into()),
+ date_of_birth: match individual_details.date_of_birth.as_ref() {
+ Some(date) => Some(format_date_of_birth(date)?),
+ None => match (
+ individual_details.individual_dob_year.as_ref(),
+ individual_details.individual_dob_month.as_ref(),
+ individual_details.individual_dob_day.as_ref(),
+ ) {
+ (Some(year), Some(month), Some(day)) => Some(
+ format!("{}-{:0>2}-{:0>2}", year.peek(), month.peek(), day.peek())
+ .into(),
+ ),
+ _ => None,
+ },
+ },
+ tos_acceptance_ip: individual_details
+ .tos_acceptance_ip
+ .as_ref()
+ .map(|ip| ip.peek().to_string().into()),
+ external_account_account_holder_type: match individual_details
+ .external_account_account_holder_type
+ .as_deref()
+ {
+ Some("individual") => Some(i32::from(payments_grpc::BankHolderType::Personal)),
+ Some("company") => Some(i32::from(payments_grpc::BankHolderType::Business)),
+ _ => None,
+ },
+ tos_acceptance_date: individual_details.tos_acceptance_date,
+ }),
+ })
+ }
+}
+
#[cfg(feature = "payouts")]
impl
transformers::ForeignTryFrom<
@@ -9443,7 +9583,7 @@ impl
.request
.customer_details
.as_ref()
- .map(payments_grpc::Customer::foreign_try_from)
+ .map(|customer| payments_grpc::Customer::foreign_try_from((customer, None)))
.transpose()?
.ok_or(
error_stack::Report::new(UnifiedConnectorServiceError::MissingRequiredField {
@@ -9459,6 +9599,18 @@ impl
currency: source_currency.into(),
};
+ let vendor_account_details = router_data
+ .request
+ .vendor_details
+ .as_ref()
+ .map(|vendor_details| {
+ payments_grpc::PayoutVendorAccountDetails::foreign_try_from((
+ vendor_details,
+ router_data,
+ ))
+ })
+ .transpose()?;
+
Ok(Self {
merchant_payout_id: router_data.payout_id.clone(),
address,
@@ -9471,8 +9623,8 @@ impl
router_data.request.entity_type,
),
),
+ vendor_account_details,
merchant_request_id: Some(router_data.connector_request_reference_id.clone()),
- vendor_account_details: None,
})
}
}
@@ -9510,7 +9662,12 @@ impl
.request
.customer_details
.as_ref()
- .map(payments_grpc::Customer::foreign_try_from)
+ .map(|customer| {
+ payments_grpc::Customer::foreign_try_from((
+ customer,
+ router_data.connector_customer.clone(),
+ ))
+ })
.transpose()?
.ok_or(
error_stack::Report::new(UnifiedConnectorServiceError::MissingRequiredField {
@@ -9521,6 +9678,21 @@ impl
),
)?;
+ let vendor_account_details = router_data
+ .request
+ .vendor_details
+ .as_ref()
+ .map(|vendor_details| {
+ payments_grpc::PayoutVendorAccountDetails::foreign_try_from((
+ vendor_details,
+ router_data,
+ ))
+ })
+ .transpose()?;
+
+ let destination_currency =
+ payments_grpc::Currency::foreign_try_from(router_data.request.destination_currency)?;
+
Ok(Self {
merchant_payout_id: router_data.payout_id.clone(),
address,
@@ -9528,9 +9700,9 @@ impl
amount: Some(money),
customer: Some(customer),
access_token: router_data.access_token.clone().map(|at| at.token),
+ vendor_account_details,
+ destination_currency: Some(destination_currency.into()),
merchant_request_id: Some(router_data.connector_request_reference_id.clone()),
- destination_currency: None,
- vendor_account_details: None,
})
}
}
@@ -9558,6 +9730,14 @@ impl
merchant_payout_id: router_data.payout_id.clone(),
connector_payout_id: router_data.request.connector_payout_id.clone(),
access_token: router_data.access_token.clone().map(|at| at.token),
+ customer: Some(payments_grpc::Customer::foreign_try_from((
+ router_data
+ .request
+ .customer_details
+ .as_ref()
+ .unwrap_or(&router_request_types::CustomerDetails::default()),
+ router_data.connector_customer.clone(),
+ ))?),
// Debtor account for connectors that need it to perform a status enquiry
source_bank_data: router_data
.request
@@ -9566,7 +9746,6 @@ impl
.map(payments_grpc::SourceBankData::foreign_try_from)
.transpose()?,
merchant_request_id: Some(router_data.connector_request_reference_id.clone()),
- customer: None,
payout_method_type: router_data
.payment_method_type
.map(|pmt| payments_grpc::PaymentMethodType::foreign_from(pmt).into()),
diff --git a/crates/router/src/core/utils.rs b/crates/router/src/core/utils.rs
index c9a34952a2..07f294877f 100644
--- a/crates/router/src/core/utils.rs
+++ b/crates/router/src/core/utils.rs
@@ -264,13 +264,12 @@ pub(super) async fn construct_payout_router_data_common<F>(
|err| report!(errors::ApiErrorResponse::InternalServerError).attach_printable(err),
)? {
api_models::enums::PayoutConnectors::Stripe => {
- payout_data.payouts.metadata.to_owned().and_then(|meta| {
- let val = meta
+ payout_data.payouts.metadata.as_ref().and_then(|metadata| {
+ metadata
.peek()
.to_owned()
.parse_value("PayoutVendorAccountDetails")
- .ok();
- val
+ .ok()
})
}
_ => None,