Skip to content

feat(payment-methods): add filtering logic for payment method list v2 #8606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Aug 5, 2025
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 3 additions & 7 deletions api-reference/v2/openapi_spec_v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -22358,7 +22358,8 @@
"type": "object",
"required": [
"payment_method_type",
"payment_method_subtype"
"payment_method_subtype",
"required_fields"
],
"properties": {
"payment_method_type": {
Expand All @@ -22376,12 +22377,7 @@
"nullable": true
},
"required_fields": {
"allOf": [
{
"$ref": "#/components/schemas/RequiredFieldInfo"
}
],
"nullable": true
"$ref": "#/components/schemas/RequiredFieldInfo"
},
"surcharge_details": {
"allOf": [
Expand Down
22 changes: 11 additions & 11 deletions config/payment_required_fields_v2.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2139,19 +2139,19 @@ non_mandate = []
# Payment method type: Sepa
[required_fields.bank_debit.sepa.fields.Stripe]
common = [
{ required_field = "payment_method_data.billing.address.first_name", display_name = "billing_address_first_name", field_type = "text" },
{ required_field = "payment_method_data.billing.address.last_name", display_name = "billing_address_last_name", field_type = "text" },
{ required_field = "payment_method_data.bank_debit.sepa.iban", display_name = "iban", field_type = "text" },
{ required_field = "email", display_name = "email", field_type = "text" }
{ required_field = "payment_method_data.billing.address.first_name", display_name = "owner_name", field_type = "user_billing_name" },
{ required_field = "payment_method_data.billing.address.last_name", display_name = "owner_name", field_type = "user_billing_name" },
{ required_field = "payment_method_data.bank_debit.sepa_bank_debit.iban", display_name = "iban", field_type = "user_iban" },
{ required_field = "email", display_name = "email", field_type = "user_iban" }
]
mandate = []
non_mandate = []

[required_fields.bank_debit.sepa.fields.Adyen]
common = [
{ required_field = "payment_method_data.billing.address.first_name", display_name = "billing_address_first_name", field_type = "text" },
{ required_field = "payment_method_data.billing.address.last_name", display_name = "billing_address_last_name", field_type = "text" },
{ required_field = "payment_method_data.bank_debit.sepa.iban", display_name = "iban", field_type = "text" }
{ required_field = "payment_method_data.billing.address.first_name", display_name = "owner_name", field_type = "user_billing_name" },
{ required_field = "payment_method_data.billing.address.last_name", display_name = "owner_name", field_type = "user_billing_name" },
{ required_field = "payment_method_data.bank_debit.sepa_bank_debit.iban", display_name = "iban", field_type = "user_iban" }
]
mandate = []
non_mandate = []
Expand Down Expand Up @@ -2210,10 +2210,10 @@ non_mandate = []

[required_fields.bank_debit.becs.fields.Adyen]
common = [
{ required_field = "payment_method_data.billing.address.first_name", display_name = "billing_address_first_name", field_type = "text" },
{ required_field = "payment_method_data.billing.address.last_name", display_name = "billing_address_last_name", field_type = "text" },
{ required_field = "payment_method_data.bank_debit.becs.account_number", display_name = "account_number", field_type = "text" },
{ required_field = "payment_method_data.bank_debit.bacs.sort_code", display_name = "sort_code", field_type = "text" } # Corrected from becs.sort_code as per V1
{ required_field = "payment_method_data.billing.address.first_name", display_name = "owner_name", field_type = "user_billing_name" },
{ required_field = "payment_method_data.billing.address.last_name", display_name = "owner_name", field_type = "user_billing_name" },
{ required_field = "ppayment_method_data.bank_debit.becs_bank_debit.account_number", display_name = "bank_account_number", field_type = "user_bank_account_number" },
{ required_field = "payment_method_data.bank_debit.becs_bank_debit.sort_code", display_name = "bank_sort_code", field_type = "user_bank_sort_code" } # Corrected from becs.sort_code as per V1
]
mandate = []
non_mandate = []
Expand Down
1 change: 1 addition & 0 deletions crates/api_models/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ time = { version = "0.3.41", features = ["serde", "serde-well-known", "std"] }
url = { version = "2.5.4", features = ["serde"] }
utoipa = { version = "4.2.3", features = ["preserve_order", "preserve_path_order"] }
nutype = { version = "0.4.3", features = ["serde"] }
deserialize_form_style_query_parameter = "0.2.2"

# First party crates
cards = { version = "0.1.0", path = "../cards" }
Expand Down
39 changes: 36 additions & 3 deletions crates/api_models/src/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ use common_utils::{
pii::{self, Email},
types::{MinorUnit, StringMajorUnit},
};
#[cfg(feature = "v2")]
use deserialize_form_style_query_parameter::option_form_vec_deserialize;
use error_stack::ResultExt;
use masking::{PeekInterface, Secret, WithType};
use router_derive::Setter;
Expand Down Expand Up @@ -7701,7 +7703,38 @@ pub enum SdkType {

#[cfg(feature = "v2")]
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, ToSchema)]
pub struct PaymentMethodsListRequest {}
pub struct PaymentMethodsListRequest {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have a PaymentMethodListRequest struct in api_models/src/payment_methods.rs. Please consider using that. We can also remove the extra deserializer crate dependency.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This struct was introduced by @Narayanbhat166 for v2, while the struct in api_models/src/payment_methods.rs is for v1. Are we sure about merging them given if we try to differentiate v1 and v2 parameters?

Copy link
Contributor Author

@Sakilmostak Sakilmostak Jul 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deserialize_form_style_query_parameter crate is import for v2 specifically to take vector as a input in query params. for example {{baseUrl}}/v2/payments/:id/payment-methods?accepted_countries=US,UK,NZ

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have 2 versions of PaymentMethodListRequest in payment_methods.rs, one for v1 and another for v2.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two separate structure present to differentiate payment's and payment method's PML. In case of future deviation in implementation, this will be useful. Currently updating the name of both as per suggestion

/// This is a 15 minute expiry token which shall be used from the client to authenticate and perform sessions from the SDK
#[schema(max_length = 30, min_length = 30, example = "secret_k2uj3he2893eiu2d")]
pub client_secret: Option<String>,

/// The two-letter ISO currency code
#[serde(deserialize_with = "option_form_vec_deserialize", default)]
#[schema(value_type = Option<Vec<CountryAlpha2>>, example = json!(["US", "UK", "IN"]))]
pub accepted_countries: Option<Vec<api_enums::CountryAlpha2>>,

/// Filter by amount
#[schema(example = 60)]
pub amount: Option<MinorUnit>,

/// The three-letter ISO currency code
#[serde(deserialize_with = "option_form_vec_deserialize", default)]
#[schema(value_type = Option<Vec<Currency>>,example = json!(["USD", "EUR"]))]
pub accepted_currencies: Option<Vec<api_enums::Currency>>,

/// Indicates whether the payment method supports recurring payments. Optional.
#[schema(example = true)]
pub recurring_enabled: Option<bool>,

/// Indicates whether the payment method is eligible for card networks
#[serde(deserialize_with = "option_form_vec_deserialize", default)]
#[schema(value_type = Option<Vec<CardNetwork>>, example = json!(["visa", "mastercard"]))]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please have a test case added for the deserializer which is added

pub card_networks: Option<Vec<api_enums::CardNetwork>>,

/// Indicates the limit of last used payment methods
#[schema(example = 1)]
pub limit: Option<i64>,
}

#[cfg(feature = "v2")]
#[derive(Debug, serde::Serialize, ToSchema)]
Expand Down Expand Up @@ -7737,8 +7770,8 @@ pub struct ResponsePaymentMethodTypesForPayments {

/// Required fields for the payment_method_type.
/// This is the union of all the required fields for the payment method type enabled in all the connectors.
#[schema(value_type = Option<RequiredFieldInfo>)]
pub required_fields: Option<Vec<payment_methods::RequiredFieldInfo>>,
#[schema(value_type = RequiredFieldInfo)]
pub required_fields: Vec<payment_methods::RequiredFieldInfo>,

/// surcharge details for this payment method type if exists
#[schema(value_type = Option<SurchargeDetailsResponse>)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ pub struct PaymentMethodsEnabledForConnector {
pub payment_methods_enabled: common_types::payment_methods::RequestPaymentMethodTypes,
pub payment_method: common_enums::PaymentMethod,
pub connector: common_enums::connector_enums::Connector,
pub merchant_connector_id: id_type::MerchantConnectorAccountId,
}

#[cfg(feature = "v2")]
Expand Down Expand Up @@ -365,29 +366,40 @@ impl FlattenedPaymentMethodsEnabled {
.into_iter()
.map(|connector| {
(
connector.payment_methods_enabled.unwrap_or_default(),
connector
.payment_methods_enabled
.clone()
.unwrap_or_default(),
connector.connector_name,
connector.get_id(),
)
})
.flat_map(|(payment_method_enabled, connector_name)| {
payment_method_enabled
.into_iter()
.flat_map(move |payment_method| {
let request_payment_methods_enabled =
payment_method.payment_method_subtypes.unwrap_or_default();
let length = request_payment_methods_enabled.len();
request_payment_methods_enabled.into_iter().zip(
std::iter::repeat((connector_name, payment_method.payment_method_type))
.flat_map(
|(payment_method_enabled, connector, merchant_connector_id)| {
payment_method_enabled
.into_iter()
.flat_map(move |payment_method| {
let request_payment_methods_enabled =
payment_method.payment_method_subtypes.unwrap_or_default();
let length = request_payment_methods_enabled.len();
request_payment_methods_enabled.into_iter().zip(
std::iter::repeat((
connector,
merchant_connector_id.clone(),
payment_method.payment_method_type,
))
.take(length),
)
})
})
)
})
},
)
.map(
|(request_payment_methods, (connector_name, payment_method))| {
|(request_payment_methods, (connector, merchant_connector_id, payment_method))| {
PaymentMethodsEnabledForConnector {
payment_methods_enabled: request_payment_methods,
connector: connector_name,
connector,
payment_method,
merchant_connector_id,
}
},
)
Expand Down
26 changes: 4 additions & 22 deletions crates/router/src/core/payment_methods/cards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ use common_utils::{
};
use diesel_models::payment_method;
use error_stack::{report, ResultExt};
#[cfg(feature = "v1")]
use euclid::dssa::graph::{AnalysisContext, CgraphExt};
use euclid::frontend::dir;
use euclid::{
dssa::graph::{AnalysisContext, CgraphExt},
frontend::dir,
};
use hyperswitch_constraint_graph as cgraph;
#[cfg(feature = "v1")]
use hyperswitch_domain_models::customer::CustomerUpdate;
Expand Down Expand Up @@ -3962,25 +3963,6 @@ pub async fn filter_payment_methods(
Ok(())
}

// v2 type for PaymentMethodListRequest will not have the installment_payment_enabled field,
// need to re-evaluate filter logic
#[cfg(feature = "v2")]
#[allow(clippy::too_many_arguments)]
pub async fn filter_payment_methods(
_graph: &cgraph::ConstraintGraph<dir::DirValue>,
_mca_id: String,
_payment_methods: &[Secret<serde_json::Value>],
_req: &mut api::PaymentMethodListRequest,
_resp: &mut [ResponsePaymentMethodIntermediate],
_payment_intent: Option<&storage::PaymentIntent>,
_payment_attempt: Option<&storage::PaymentAttempt>,
_address: Option<&domain::Address>,
_connector: String,
_configs: &settings::Settings<RawSecret>,
) -> errors::CustomResult<(), errors::ApiErrorResponse> {
todo!()
}

fn filter_amount_based(
payment_method: &RequestPaymentMethodTypes,
amount: Option<MinorUnit>,
Expand Down
Loading
Loading