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

feat(router): Make payment_method_subtype optional in payment_attempt [V2] (#7568)

Anurag Thakur86306bd56760
diff --git a/api-reference-v2/openapi_spec.json b/api-reference-v2/openapi_spec.json
index 9467ac0820..4270edf6a4 100644
--- a/api-reference-v2/openapi_spec.json
+++ b/api-reference-v2/openapi_spec.json
@@ -14743,7 +14743,6 @@
"type": "object",
"required": [
"payment_method_type",
- "payment_method_subtype",
"customer_id",
"payment_method_data"
],
@@ -14752,7 +14751,12 @@
"$ref": "#/components/schemas/PaymentMethod"
},
"payment_method_subtype": {
- "$ref": "#/components/schemas/PaymentMethodType"
+ "allOf": [
+ {
+ "$ref": "#/components/schemas/PaymentMethodType"
+ }
+ ],
+ "nullable": true
},
"metadata": {
"type": "object",
@@ -16204,8 +16208,7 @@
"description": "Request for Payment Intent Confirm",
"required": [
"payment_method_data",
- "payment_method_type",
- "payment_method_subtype"
+ "payment_method_type"
],
"properties": {
"return_url": {
@@ -16221,7 +16224,12 @@
"$ref": "#/components/schemas/PaymentMethod"
},
"payment_method_subtype": {
- "$ref": "#/components/schemas/PaymentMethodType"
+ "allOf": [
+ {
+ "$ref": "#/components/schemas/PaymentMethodType"
+ }
+ ],
+ "nullable": true
},
"shipping": {
"allOf": [
@@ -16999,8 +17007,7 @@
"amount_details",
"customer_id",
"payment_method_data",
- "payment_method_type",
- "payment_method_subtype"
+ "payment_method_type"
],
"properties": {
"amount_details": {
@@ -17192,7 +17199,12 @@
"$ref": "#/components/schemas/PaymentMethod"
},
"payment_method_subtype": {
- "$ref": "#/components/schemas/PaymentMethodType"
+ "allOf": [
+ {
+ "$ref": "#/components/schemas/PaymentMethodType"
+ }
+ ],
+ "nullable": true
},
"customer_acceptance": {
"allOf": [
diff --git a/crates/api_models/src/payment_methods.rs b/crates/api_models/src/payment_methods.rs
index d665337fda..56bffc0264 100644
--- a/crates/api_models/src/payment_methods.rs
+++ b/crates/api_models/src/payment_methods.rs
@@ -116,8 +116,8 @@ pub struct PaymentMethodCreate {
pub payment_method_type: api_enums::PaymentMethod,
/// This is a sub-category of payment method.
- #[schema(value_type = PaymentMethodType,example = "credit")]
- pub payment_method_subtype: api_enums::PaymentMethodType,
+ #[schema(value_type = Option<PaymentMethodType>,example = "credit")]
+ pub payment_method_subtype: Option<api_enums::PaymentMethodType>,
/// You can specify up to 50 keys, with key names up to 40 characters long and values up to 500 characters long. Metadata is useful for storing additional, structured information on an object.
#[schema(value_type = Option<Object>,example = json!({ "city": "NY", "unit": "245" }))]
@@ -2790,7 +2790,7 @@ pub struct PaymentMethodSessionConfirmRequest {
/// The payment method subtype
#[schema(value_type = PaymentMethodType, example = "credit")]
- pub payment_method_subtype: common_enums::PaymentMethodType,
+ pub payment_method_subtype: Option<common_enums::PaymentMethodType>,
/// The payment instrument data to be used for the payment
#[schema(value_type = PaymentMethodDataRequest)]
diff --git a/crates/api_models/src/payments.rs b/crates/api_models/src/payments.rs
index bcd11bb65e..fbc2cb7e05 100644
--- a/crates/api_models/src/payments.rs
+++ b/crates/api_models/src/payments.rs
@@ -5232,8 +5232,8 @@ pub struct PaymentsConfirmIntentRequest {
pub payment_method_type: api_enums::PaymentMethod,
/// The payment method subtype to be used for the payment. This should match with the `payment_method_data` provided
- #[schema(value_type = PaymentMethodType, example = "apple_pay")]
- pub payment_method_subtype: api_enums::PaymentMethodType,
+ #[schema(value_type = Option<PaymentMethodType>, example = "apple_pay")]
+ pub payment_method_subtype: Option<api_enums::PaymentMethodType>,
/// The shipping address for the payment. This will override the shipping address provided in the create-intent request
pub shipping: Option<Address>,
@@ -5401,8 +5401,8 @@ pub struct PaymentsRequest {
pub payment_method_type: api_enums::PaymentMethod,
/// The payment method subtype to be used for the payment. This should match with the `payment_method_data` provided
- #[schema(value_type = PaymentMethodType, example = "apple_pay")]
- pub payment_method_subtype: api_enums::PaymentMethodType,
+ #[schema(value_type = Option<PaymentMethodType>, example = "apple_pay")]
+ pub payment_method_subtype: Option<api_enums::PaymentMethodType>,
/// This "CustomerAcceptance" object is passed during Payments-Confirm request, it enlists the type, time, and mode of acceptance properties related to an acceptance done by the customer. The customer_acceptance sub object is usually passed by the SDK or client.
#[schema(value_type = Option<CustomerAcceptance>)]
@@ -8477,7 +8477,7 @@ pub struct PaymentRevenueRecoveryMetadata {
pub payment_method_type: common_enums::PaymentMethod,
/// PaymentMethod Subtype
#[schema(example = "klarna", value_type = PaymentMethodType)]
- pub payment_method_subtype: common_enums::PaymentMethodType,
+ pub payment_method_subtype: Option<common_enums::PaymentMethodType>,
/// The name of the payment connector through which the payment attempt was made.
#[schema(value_type = Connector, example = "stripe")]
pub connector: common_enums::connector_enums::Connector,
@@ -8558,7 +8558,7 @@ pub struct PaymentsAttemptRecordRequest {
/// The payment method subtype to be used for the payment. This should match with the `payment_method_data` provided
#[schema(value_type = PaymentMethodType, example = "apple_pay")]
- pub payment_method_subtype: api_enums::PaymentMethodType,
+ pub payment_method_subtype: Option<api_enums::PaymentMethodType>,
/// The payment instrument data to be used for the payment attempt.
pub payment_method_data: Option<PaymentMethodDataRequest>,
diff --git a/crates/diesel_models/src/payment_attempt.rs b/crates/diesel_models/src/payment_attempt.rs
index 0fdb24119a..70b4c2b1e0 100644
--- a/crates/diesel_models/src/payment_attempt.rs
+++ b/crates/diesel_models/src/payment_attempt.rs
@@ -91,7 +91,7 @@ pub struct PaymentAttempt {
pub charges: Option<common_types::payments::ConnectorChargeResponseData>,
pub payment_method_type_v2: storage_enums::PaymentMethod,
pub connector_payment_id: Option<ConnectorTransactionId>,
- pub payment_method_subtype: storage_enums::PaymentMethodType,
+ pub payment_method_subtype: Option<storage_enums::PaymentMethodType>,
pub routing_result: Option<serde_json::Value>,
pub authentication_applied: Option<common_enums::AuthenticationType>,
pub external_reference_id: Option<String>,
@@ -314,7 +314,7 @@ pub struct PaymentAttemptNew {
pub feature_metadata: Option<PaymentAttemptFeatureMetadata>,
pub payment_method_type_v2: storage_enums::PaymentMethod,
pub connector_payment_id: Option<ConnectorTransactionId>,
- pub payment_method_subtype: storage_enums::PaymentMethodType,
+ pub payment_method_subtype: Option<storage_enums::PaymentMethodType>,
pub id: id_type::GlobalAttemptId,
pub connector_token_details: Option<ConnectorTokenDetails>,
pub card_discovery: Option<storage_enums::CardDiscovery>,
diff --git a/crates/diesel_models/src/schema_v2.rs b/crates/diesel_models/src/schema_v2.rs
index 5d028ad521..30be42300c 100644
--- a/crates/diesel_models/src/schema_v2.rs
+++ b/crates/diesel_models/src/schema_v2.rs
@@ -874,7 +874,7 @@ diesel::table! {
#[max_length = 128]
connector_payment_id -> Nullable<Varchar>,
#[max_length = 64]
- payment_method_subtype -> Varchar,
+ payment_method_subtype -> Nullable<Varchar>,
routing_result -> Nullable<Jsonb>,
authentication_applied -> Nullable<AuthenticationType>,
#[max_length = 128]
diff --git a/crates/diesel_models/src/types.rs b/crates/diesel_models/src/types.rs
index eb2818ba24..69916de49a 100644
--- a/crates/diesel_models/src/types.rs
+++ b/crates/diesel_models/src/types.rs
@@ -61,7 +61,7 @@ impl FeatureMetadata {
pub fn get_payment_method_sub_type(&self) -> Option<common_enums::PaymentMethodType> {
self.payment_revenue_recovery_metadata
.as_ref()
- .map(|rrm| rrm.payment_method_subtype)
+ .and_then(|val| val.payment_method_subtype)
}
pub fn get_payment_method_type(&self) -> Option<common_enums::PaymentMethod> {
@@ -158,7 +158,7 @@ pub struct PaymentRevenueRecoveryMetadata {
///Payment Method Type
pub payment_method_type: common_enums::enums::PaymentMethod,
/// PaymentMethod Subtype
- pub payment_method_subtype: common_enums::enums::PaymentMethodType,
+ pub payment_method_subtype: Option<common_enums::enums::PaymentMethodType>,
/// The name of the payment connector through which the payment attempt was made.
pub connector: common_enums::connector_enums::Connector,
}
diff --git a/crates/hyperswitch_connectors/src/connectors/chargebee/transformers.rs b/crates/hyperswitch_connectors/src/connectors/chargebee/transformers.rs
index e6e8e1b123..557d245527 100644
--- a/crates/hyperswitch_connectors/src/connectors/chargebee/transformers.rs
+++ b/crates/hyperswitch_connectors/src/connectors/chargebee/transformers.rs
@@ -447,8 +447,9 @@ impl TryFrom<ChargebeeWebhookBody> for revenue_recovery::RevenueRecoveryAttemptD
let payment_method_details: ChargebeePaymentMethodDetails =
serde_json::from_str(&item.content.transaction.payment_method_details)
.change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?;
- let payment_method_sub_type =
- enums::PaymentMethodType::from(payment_method_details.card.funding_type);
+ let payment_method_sub_type = Some(enums::PaymentMethodType::from(
+ payment_method_details.card.funding_type,
+ ));
Ok(Self {
amount,
currency,
diff --git a/crates/hyperswitch_domain_models/src/payments.rs b/crates/hyperswitch_domain_models/src/payments.rs
index 8571184038..c8e9e5444e 100644
--- a/crates/hyperswitch_domain_models/src/payments.rs
+++ b/crates/hyperswitch_domain_models/src/payments.rs
@@ -274,7 +274,7 @@ impl AmountDetails {
let order_tax_amount = match self.skip_external_tax_calculation {
common_enums::TaxCalculationOverride::Skip => {
self.tax_details.as_ref().and_then(|tax_details| {
- tax_details.get_tax_amount(Some(confirm_intent_request.payment_method_subtype))
+ tax_details.get_tax_amount(confirm_intent_request.payment_method_subtype)
})
}
common_enums::TaxCalculationOverride::Calculate => None,
diff --git a/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs b/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs
index 76c020080a..8d9913a9f9 100644
--- a/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs
+++ b/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs
@@ -428,7 +428,7 @@ pub struct PaymentAttempt {
/// The reference to the payment at the connector side
pub connector_payment_id: Option<String>,
/// The payment method subtype for the payment attempt.
- pub payment_method_subtype: storage_enums::PaymentMethodType,
+ pub payment_method_subtype: Option<storage_enums::PaymentMethodType>,
/// The authentication type that was applied for the payment attempt.
pub authentication_applied: Option<common_enums::AuthenticationType>,
/// A reference to the payment at connector side. This is returned by the connector
@@ -468,7 +468,7 @@ impl PaymentAttempt {
#[cfg(feature = "v2")]
pub fn get_payment_method_type(&self) -> Option<storage_enums::PaymentMethodType> {
// TODO: check if we can fix this
- Some(self.payment_method_subtype)
+ self.payment_method_subtype
}
#[cfg(feature = "v1")]
@@ -652,8 +652,7 @@ impl PaymentAttempt {
.unwrap_or(common_enums::PaymentMethod::Card),
payment_method_id: None,
connector_payment_id: None,
- payment_method_subtype: payment_method_subtype_data
- .unwrap_or(common_enums::PaymentMethodType::Credit),
+ payment_method_subtype: payment_method_subtype_data,
authentication_applied: None,
external_reference_id: None,
payment_method_billing_address,
diff --git a/crates/hyperswitch_domain_models/src/revenue_recovery.rs b/crates/hyperswitch_domain_models/src/revenue_recovery.rs
index 3e1bc93d91..4bf1fece02 100644
--- a/crates/hyperswitch_domain_models/src/revenue_recovery.rs
+++ b/crates/hyperswitch_domain_models/src/revenue_recovery.rs
@@ -31,7 +31,7 @@ pub struct RevenueRecoveryAttemptData {
/// payment method of payment attempt.
pub payment_method_type: common_enums::PaymentMethod,
/// payment method sub type of the payment attempt.
- pub payment_method_sub_type: common_enums::PaymentMethodType,
+ pub payment_method_sub_type: Option<common_enums::PaymentMethodType>,
}
/// This is unified struct for Revenue Recovery Invoice Data and it is constructed from billing connectors
diff --git a/crates/router/src/core/payment_methods.rs b/crates/router/src/core/payment_methods.rs
index eec47ca625..27fb3959bf 100644
--- a/crates/router/src/core/payment_methods.rs
+++ b/crates/router/src/core/payment_methods.rs
@@ -707,7 +707,7 @@ pub async fn retrieve_payment_method_with_token(
pub(crate) fn get_payment_method_create_request(
payment_method_data: &api_models::payments::PaymentMethodData,
payment_method_type: storage_enums::PaymentMethod,
- payment_method_subtype: storage_enums::PaymentMethodType,
+ payment_method_subtype: Option<storage_enums::PaymentMethodType>,
customer_id: id_type::GlobalCustomerId,
billing_address: Option<&api_models::payments::Address>,
payment_method_session: Option<&domain::payment_methods::PaymentMethodSession>,
@@ -971,7 +971,7 @@ pub async fn create_payment_method_core(
None,
network_tokenization_resp,
Some(req.payment_method_type),
- Some(req.payment_method_subtype),
+ req.payment_method_subtype,
)
.await
.attach_printable("Unable to create Payment method data")?;
diff --git a/crates/router/src/core/payments/routing.rs b/crates/router/src/core/payments/routing.rs
index ceac9eaf43..e566b69534 100644
--- a/crates/router/src/core/payments/routing.rs
+++ b/crates/router/src/core/payments/routing.rs
@@ -228,7 +228,7 @@ pub fn make_dsl_input(
};
let payment_method_input = dsl_inputs::PaymentMethodInput {
payment_method: Some(payments_dsl_input.payment_attempt.payment_method_type),
- payment_method_type: Some(payments_dsl_input.payment_attempt.payment_method_subtype),
+ payment_method_type: payments_dsl_input.payment_attempt.payment_method_subtype,
card_network: payments_dsl_input
.payment_method_data
.as_ref()
diff --git a/crates/router/src/core/payments/transformers.rs b/crates/router/src/core/payments/transformers.rs
index 0bbe5675c7..5e8b1bc105 100644
--- a/crates/router/src/core/payments/transformers.rs
+++ b/crates/router/src/core/payments/transformers.rs
@@ -285,7 +285,7 @@ pub async fn construct_payment_router_data_for_authorize<'a>(
session_token: None,
enrolled_for_3ds: true,
related_transaction_id: None,
- payment_method_type: Some(payment_data.payment_attempt.payment_method_subtype),
+ payment_method_type: payment_data.payment_attempt.payment_method_subtype,
router_return_url: Some(router_return_url),
webhook_url,
complete_authorize_url,
@@ -608,7 +608,7 @@ pub async fn construct_router_data_for_psync<'a>(
capture_method: Some(payment_intent.capture_method),
connector_meta: attempt.connector_metadata.clone().expose_option(),
sync_type: types::SyncRequestType::SinglePaymentSync,
- payment_method_type: Some(attempt.payment_method_subtype),
+ payment_method_type: attempt.payment_method_subtype,
currency: payment_intent.amount_details.currency,
// TODO: Get the charges object from feature metadata
split_payments: None,
@@ -949,7 +949,7 @@ pub async fn construct_payment_router_data_for_setup_mandate<'a>(
email,
customer_name: None,
return_url: Some(router_return_url),
- payment_method_type: Some(payment_data.payment_attempt.payment_method_subtype),
+ payment_method_type: payment_data.payment_attempt.payment_method_subtype,
request_incremental_authorization: matches!(
payment_data
.payment_intent
@@ -1667,7 +1667,7 @@ where
created: payment_intent.created_at,
payment_method_data,
payment_method_type: Some(payment_attempt.payment_method_type),
- payment_method_subtype: Some(payment_attempt.payment_method_subtype),
+ payment_method_subtype: payment_attempt.payment_method_subtype,
next_action,
connector_transaction_id: payment_attempt.connector_payment_id.clone(),
connector_reference_id: None,
@@ -1771,7 +1771,7 @@ where
payment_method_subtype: self
.payment_attempt
.as_ref()
- .map(|attempt| attempt.payment_method_subtype),
+ .and_then(|attempt| attempt.payment_method_subtype),
connector_transaction_id: self
.payment_attempt
.as_ref()
@@ -2898,7 +2898,7 @@ impl ForeignFrom<(storage::PaymentIntent, Option<storage::PaymentAttempt>)>
)),
created: pi.created_at,
payment_method_type: pa.as_ref().and_then(|p| p.payment_method_type.into()),
- payment_method_subtype: pa.as_ref().and_then(|p| p.payment_method_subtype.into()),
+ payment_method_subtype: pa.as_ref().and_then(|p| p.payment_method_subtype),
connector: pa.as_ref().and_then(|p| p.connector.clone()),
merchant_connector_id: pa.as_ref().and_then(|p| p.merchant_connector_id.clone()),
customer: None,
diff --git a/crates/router/src/types/api/payment_methods.rs b/crates/router/src/types/api/payment_methods.rs
index 9c5030a30a..4c925b9d46 100644
--- a/crates/router/src/types/api/payment_methods.rs
+++ b/crates/router/src/types/api/payment_methods.rs
@@ -69,10 +69,15 @@ impl PaymentMethodCreateExt for PaymentMethodCreate {
impl PaymentMethodCreateExt for PaymentMethodCreate {
fn validate(&self) -> RouterResult<()> {
utils::when(
- !validate_payment_method_type_against_payment_method(
- self.payment_method_type,
- self.payment_method_subtype,
- ),
+ !self
+ .payment_method_subtype
+ .map(|sub| {
+ validate_payment_method_type_against_payment_method(
+ self.payment_method_type,
+ sub,
+ )
+ })
+ .unwrap_or(true), // If payment_method_subtype is None, we assume it to be valid
|| {
Err(report!(errors::ApiErrorResponse::InvalidRequestData {
message: "Invalid 'payment_method_type' provided".to_string()
diff --git a/v2_migrations/2025-03-19-080705_payment-method-subtype-optional/down.sql b/v2_migrations/2025-03-19-080705_payment-method-subtype-optional/down.sql
new file mode 100644
index 0000000000..d898395816
--- /dev/null
+++ b/v2_migrations/2025-03-19-080705_payment-method-subtype-optional/down.sql
@@ -0,0 +1 @@
+ALTER TABLE payment_attempt ALTER COLUMN payment_method_subtype SET NOT NULL;
diff --git a/v2_migrations/2025-03-19-080705_payment-method-subtype-optional/up.sql b/v2_migrations/2025-03-19-080705_payment-method-subtype-optional/up.sql
new file mode 100644
index 0000000000..8f878c4e39
--- /dev/null
+++ b/v2_migrations/2025-03-19-080705_payment-method-subtype-optional/up.sql
@@ -0,0 +1,2 @@
+-- Your SQL goes here
+ALTER TABLE payment_attempt ALTER COLUMN payment_method_subtype DROP NOT NULL;
\ No newline at end of file