Skip to content

Commit

Permalink
refactor: add support for passing context generic to api calls (#2433)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chethan-rao authored Oct 6, 2023
1 parent 61288d5 commit 601c174
Show file tree
Hide file tree
Showing 26 changed files with 702 additions and 413 deletions.
16 changes: 8 additions & 8 deletions crates/router/src/compatibility/stripe/payment_intents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use router_env::{instrument, tracing, Flow};

use crate::{
compatibility::{stripe::errors, wrap},
core::{api_locking::GetLockingInput, payments},
core::{api_locking::GetLockingInput, payment_methods::Oss, payments},
routes,
services::{api, authentication as auth},
types::api::{self as api_types},
Expand Down Expand Up @@ -50,7 +50,7 @@ pub async fn payment_intents_create(
&req,
create_payment_req,
|state, auth, req| {
payments::payments_core::<api_types::Authorize, api_types::PaymentsResponse, _, _, _>(
payments::payments_core::<api_types::Authorize, api_types::PaymentsResponse, _, _, _,Oss>(
state,
auth.merchant_account,
auth.key_store,
Expand Down Expand Up @@ -109,7 +109,7 @@ pub async fn payment_intents_retrieve(
&req,
payload,
|state, auth, payload| {
payments::payments_core::<api_types::PSync, api_types::PaymentsResponse, _, _, _>(
payments::payments_core::<api_types::PSync, api_types::PaymentsResponse, _, _, _, Oss>(
state,
auth.merchant_account,
auth.key_store,
Expand Down Expand Up @@ -172,7 +172,7 @@ pub async fn payment_intents_retrieve_with_gateway_creds(
&req,
payload,
|state, auth, req| {
payments::payments_core::<api_types::PSync, payment_types::PaymentsResponse, _, _, _>(
payments::payments_core::<api_types::PSync, payment_types::PaymentsResponse, _, _, _,Oss>(
state,
auth.merchant_account,
auth.key_store,
Expand Down Expand Up @@ -236,7 +236,7 @@ pub async fn payment_intents_update(
&req,
payload,
|state, auth, req| {
payments::payments_core::<api_types::Authorize, api_types::PaymentsResponse, _, _, _>(
payments::payments_core::<api_types::Authorize, api_types::PaymentsResponse, _, _, _,Oss>(
state,
auth.merchant_account,
auth.key_store,
Expand Down Expand Up @@ -302,7 +302,7 @@ pub async fn payment_intents_confirm(
&req,
payload,
|state, auth, req| {
payments::payments_core::<api_types::Authorize, api_types::PaymentsResponse, _, _, _>(
payments::payments_core::<api_types::Authorize, api_types::PaymentsResponse, _, _, _,Oss>(
state,
auth.merchant_account,
auth.key_store,
Expand Down Expand Up @@ -358,7 +358,7 @@ pub async fn payment_intents_capture(
&req,
payload,
|state, auth, payload| {
payments::payments_core::<api_types::Capture, api_types::PaymentsResponse, _, _, _>(
payments::payments_core::<api_types::Capture, api_types::PaymentsResponse, _, _, _,Oss>(
state,
auth.merchant_account,
auth.key_store,
Expand Down Expand Up @@ -418,7 +418,7 @@ pub async fn payment_intents_cancel(
&req,
payload,
|state, auth, req| {
payments::payments_core::<api_types::Void, api_types::PaymentsResponse, _, _, _>(
payments::payments_core::<api_types::Void, api_types::PaymentsResponse, _, _, _, Oss>(
state,
auth.merchant_account,
auth.key_store,
Expand Down
31 changes: 26 additions & 5 deletions crates/router/src/compatibility/stripe/setup_intents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
stripe::{errors, payment_intents::types as stripe_payment_types},
wrap,
},
core::{api_locking, payments},
core::{api_locking, payment_methods::Oss, payments},
routes,
services::{api, authentication as auth},
types::api as api_types,
Expand Down Expand Up @@ -54,7 +54,14 @@ pub async fn setup_intents_create(
&req,
create_payment_req,
|state, auth, req| {
payments::payments_core::<api_types::SetupMandate, api_types::PaymentsResponse, _, _, _>(
payments::payments_core::<
api_types::SetupMandate,
api_types::PaymentsResponse,
_,
_,
_,
Oss,
>(
state,
auth.merchant_account,
auth.key_store,
Expand Down Expand Up @@ -113,7 +120,7 @@ pub async fn setup_intents_retrieve(
&req,
payload,
|state, auth, payload| {
payments::payments_core::<api_types::PSync, api_types::PaymentsResponse, _, _, _>(
payments::payments_core::<api_types::PSync, api_types::PaymentsResponse, _, _, _, Oss>(
state,
auth.merchant_account,
auth.key_store,
Expand Down Expand Up @@ -178,7 +185,14 @@ pub async fn setup_intents_update(
&req,
payload,
|state, auth, req| {
payments::payments_core::<api_types::SetupMandate, api_types::PaymentsResponse, _, _, _>(
payments::payments_core::<
api_types::SetupMandate,
api_types::PaymentsResponse,
_,
_,
_,
Oss,
>(
state,
auth.merchant_account,
auth.key_store,
Expand Down Expand Up @@ -244,7 +258,14 @@ pub async fn setup_intents_confirm(
&req,
payload,
|state, auth, req| {
payments::payments_core::<api_types::SetupMandate, api_types::PaymentsResponse, _, _, _>(
payments::payments_core::<
api_types::SetupMandate,
api_types::PaymentsResponse,
_,
_,
_,
Oss,
>(
state,
auth.merchant_account,
auth.key_store,
Expand Down
96 changes: 96 additions & 0 deletions crates/router/src/core/payment_methods.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,99 @@
pub mod cards;
pub mod transformers;
pub mod vault;

pub use api_models::{
enums::{Connector, PayoutConnectors},
payouts as payout_types,
};
pub use common_utils::request::RequestBody;
use data_models::payments::{payment_attempt::PaymentAttempt, payment_intent::PaymentIntent};
use diesel_models::enums;

use crate::{
core::{errors::RouterResult, payments::helpers},
routes::AppState,
types::api::{self, payments},
};

pub struct Oss;

#[async_trait::async_trait]
pub trait PaymentMethodRetrieve {
async fn retrieve_payment_method(
pm_data: &Option<payments::PaymentMethodData>,
state: &AppState,
payment_intent: &PaymentIntent,
payment_attempt: &PaymentAttempt,
) -> RouterResult<(Option<payments::PaymentMethodData>, Option<String>)>;
}

#[async_trait::async_trait]
impl PaymentMethodRetrieve for Oss {
async fn retrieve_payment_method(
pm_data: &Option<payments::PaymentMethodData>,
state: &AppState,
payment_intent: &PaymentIntent,
payment_attempt: &PaymentAttempt,
) -> RouterResult<(Option<payments::PaymentMethodData>, Option<String>)> {
match pm_data {
pm_opt @ Some(pm @ api::PaymentMethodData::Card(_)) => {
let payment_token = helpers::store_payment_method_data_in_vault(
state,
payment_attempt,
payment_intent,
enums::PaymentMethod::Card,
pm,
)
.await?;

Ok((pm_opt.to_owned(), payment_token))
}
pm @ Some(api::PaymentMethodData::PayLater(_)) => Ok((pm.to_owned(), None)),
pm @ Some(api::PaymentMethodData::Crypto(_)) => Ok((pm.to_owned(), None)),
pm @ Some(api::PaymentMethodData::BankDebit(_)) => Ok((pm.to_owned(), None)),
pm @ Some(api::PaymentMethodData::Upi(_)) => Ok((pm.to_owned(), None)),
pm @ Some(api::PaymentMethodData::Voucher(_)) => Ok((pm.to_owned(), None)),
pm @ Some(api::PaymentMethodData::Reward) => Ok((pm.to_owned(), None)),
pm @ Some(api::PaymentMethodData::CardRedirect(_)) => Ok((pm.to_owned(), None)),
pm @ Some(api::PaymentMethodData::GiftCard(_)) => Ok((pm.to_owned(), None)),
pm_opt @ Some(pm @ api::PaymentMethodData::BankTransfer(_)) => {
let payment_token = helpers::store_payment_method_data_in_vault(
state,
payment_attempt,
payment_intent,
enums::PaymentMethod::BankTransfer,
pm,
)
.await?;

Ok((pm_opt.to_owned(), payment_token))
}
pm_opt @ Some(pm @ api::PaymentMethodData::Wallet(_)) => {
let payment_token = helpers::store_payment_method_data_in_vault(
state,
payment_attempt,
payment_intent,
enums::PaymentMethod::Wallet,
pm,
)
.await?;

Ok((pm_opt.to_owned(), payment_token))
}
pm_opt @ Some(pm @ api::PaymentMethodData::BankRedirect(_)) => {
let payment_token = helpers::store_payment_method_data_in_vault(
state,
payment_attempt,
payment_intent,
enums::PaymentMethod::BankRedirect,
pm,
)
.await?;

Ok((pm_opt.to_owned(), payment_token))
}
_ => Ok((None, None)),
}
}
}
Loading

0 comments on commit 601c174

Please sign in to comment.