Skip to content
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

feat(router): add v2 endpoint retrieve payment filters based on merchant profile #7171

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
62 changes: 62 additions & 0 deletions crates/router/src/core/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4890,6 +4890,68 @@ pub async fn get_payment_filters(
))
}

#[cfg(all(feature = "olap", feature = "v2"))]
pub async fn get_payment_filters(
state: SessionState,
merchant: domain::MerchantAccount,
profile_id_list: Option<Vec<id_type::ProfileId>>,
) -> RouterResponse<api::PaymentListFiltersV2> {
let merchant_connector_accounts = if let services::ApplicationResponse::Json(data) =
super::admin::list_payment_connectors(state, merchant.get_id().to_owned(), profile_id_list)
.await?
{
data
} else {
return Err(errors::ApiErrorResponse::InternalServerError.into());
};

let flattened_payment_methods =
PaymentMethodsEnabledForConnector::from_payment_connectors_list(merchant_connector_accounts)
.payment_methods_enabled;

let mut connector_map: HashMap<String, Vec<MerchantConnectorInfo>> = HashMap::new();
Copy link
Member

Choose a reason for hiding this comment

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

can you use this function

pub fn from_payment_connectors_list(payment_connectors: Vec<MerchantConnectorAccount>) -> Self {

Copy link
Member

Choose a reason for hiding this comment

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

And once you receive the flattened list, you can construct a hashmap from that

let mut payment_method_types_map: HashMap<
enums::PaymentMethod,
HashSet<enums::PaymentMethodType>,
> = HashMap::new();

// Populate connector map
merchant_connector_accounts
.iter()
.filter_map(|merchant_connector_account| {
merchant_connector_account
.connector_label
.as_ref()
.map(|label| {
let info = merchant_connector_account.to_merchant_connector_info(label);
(merchant_connector_account.connector_name.clone(), info)
})
})
.for_each(|(connector_name, info)| {
connector_map.entry(connector_name).or_default().push(info);
});

// Populate payment method type map using the flattened data
flattened_payment_methods.into_iter().for_each(|item| {
payment_method_types_map
.entry(item.payment_method)
.or_default()
.insert(item.payment_methods_enabled);
});

Ok(services::ApplicationResponse::Json(
api::PaymentListFiltersV2 {
connector: connector_map,
currency: enums::Currency::iter().collect(),
status: enums::IntentStatus::iter().collect(),
payment_method: payment_method_types_map,
authentication_type: enums::AuthenticationType::iter().collect(),
card_network: enums::CardNetwork::iter().collect(),
},
))
}


#[cfg(all(feature = "olap", feature = "v1"))]
pub async fn get_aggregates_for_payments(
state: SessionState,
Expand Down
14 changes: 11 additions & 3 deletions crates/router/src/routes/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,9 +551,17 @@ pub struct Payments;
impl Payments {
pub fn server(state: AppState) -> Scope {
let mut route = web::scope("/v2/payments").app_data(web::Data::new(state));
route = route.service(
web::resource("/create-intent").route(web::post().to(payments::payments_create_intent)),
);
route = route
.service(
web::resource("/create-intent").route(web::post().to(payments::payments_create_intent)),
)
.service(
web::resource("/v2/filter").route(web::get().to(payments::get_payment_filters)),
)
.service(
web::resource("/v2/profile/filter")
.route(web::get().to(payments::get_payment_filters_profile)),
);

route = route.service(
web::scope("/{payment_id}")
Expand Down
41 changes: 38 additions & 3 deletions crates/router/src/routes/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1306,7 +1306,7 @@ pub async fn get_filters_for_payments(
}

#[instrument(skip_all, fields(flow = ?Flow::PaymentsFilters))]
#[cfg(all(feature = "olap", feature = "v1"))]
#[cfg(feature = "olap", feature = "v1")]
pub async fn get_payment_filters(
state: web::Data<app::AppState>,
req: actix_web::HttpRequest,
Expand All @@ -1320,9 +1320,44 @@ pub async fn get_payment_filters(
|state, auth: auth::AuthenticationData, _, _| {
payments::get_payment_filters(state, auth.merchant_account, None)
},
&auth::JWTAuth {
permission: Permission::MerchantPaymentRead,
auth::auth_type(
&auth::HeaderAuth(auth::ApiKeyAuth),
&auth::JWTAuth {
permission: Permission::ProfilePaymentRead,
},
req.headers(),
),
api_locking::LockAction::NotApplicable,
))
.await
}

#[instrument(skip_all, fields(flow = ?Flow::PaymentsFilters))]
#[cfg(all(feature = "olap", feature = "v2"))]
pub async fn get_payment_filters(
state: web::Data<app::AppState>,
req: actix_web::HttpRequest,
) -> impl Responder {
let flow = Flow::PaymentsFilters;
Box::pin(api::server_wrap(
flow,
state,
&req,
(),
|state, auth: auth::AuthenticationData, _, _| {
payments::get_payment_filters(
state,
auth.merchant_account,
Some(vec![auth.profile.get_id().clone()]),
)
},
auth::auth_type(
&auth::HeaderAuth(auth::ApiKeyAuth),
&auth::JWTAuth {
permission: Permission::ProfilePaymentRead,
},
req.headers(),
),
api_locking::LockAction::NotApplicable,
))
.await
Expand Down
Loading