Skip to content

Commit

Permalink
refactor: payment methods list and payout_attempt KV lookup consumption
Browse files Browse the repository at this point in the history
  • Loading branch information
kashif-m committed Mar 18, 2024
1 parent ac5da4b commit 73d0d29
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 154 deletions.
12 changes: 4 additions & 8 deletions crates/data_models/src/payouts/payout_attempt.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use common_enums as storage_enums;
use serde::{Deserialize, Serialize};
use storage_enums::MerchantStorageScheme;
use time::PrimitiveDateTime;

Expand All @@ -12,13 +13,6 @@ pub trait PayoutAttemptInterface {
_storage_scheme: MerchantStorageScheme,
) -> error_stack::Result<PayoutAttempt, errors::StorageError>;

async fn find_payout_attempt_by_merchant_id_payout_id(
&self,
_merchant_id: &str,
_payout_id: &str,
_storage_scheme: MerchantStorageScheme,
) -> error_stack::Result<PayoutAttempt, errors::StorageError>;

async fn update_payout_attempt(
&self,
_this: &PayoutAttempt,
Expand All @@ -34,7 +28,7 @@ pub trait PayoutAttemptInterface {
) -> error_stack::Result<PayoutAttempt, errors::StorageError>;
}

#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct PayoutAttempt {
pub payout_attempt_id: String,
pub payout_id: String,
Expand All @@ -50,7 +44,9 @@ pub struct PayoutAttempt {
pub error_code: Option<String>,
pub business_country: Option<storage_enums::CountryAlpha2>,
pub business_label: Option<String>,
#[serde(with = "common_utils::custom_serde::iso8601")]
pub created_at: PrimitiveDateTime,
#[serde(with = "common_utils::custom_serde::iso8601")]
pub last_modified_at: PrimitiveDateTime,
pub profile_id: String,
pub merchant_connector_id: Option<String>,
Expand Down
75 changes: 45 additions & 30 deletions crates/router/src/core/payment_methods/cards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ use crate::{
self,
types::{decrypt, encrypt_optional, AsyncLift},
},
storage::{self, enums, PaymentTokenData},
storage::{self, enums, PaymentMethodListContext, PaymentTokenData},
transformers::ForeignFrom,
},
utils::{self, ConnectorResponseExt, OptionExt},
Expand Down Expand Up @@ -2799,25 +2799,21 @@ pub async fn list_customer_payment_method(
for pm in resp.into_iter() {
let parent_payment_method_token = generate_id(consts::ID_LENGTH, "token");

#[allow(unused_variables)]
let (card, pmd, hyperswitch_token_data): (
Option<api::CardDetailFromLocker>,
Option<api::BankPayout>,
PaymentTokenData,
) = match pm.payment_method {
let payment_method_retrieval_context = match pm.payment_method {
enums::PaymentMethod::Card => {
let card_details = get_card_details_with_locker_fallback(&pm, key, state).await?;

if card_details.is_some() {
(
PaymentMethodListContext {
card_details,
None,
PaymentTokenData::permanent_card(
#[cfg(feature = "payouts")]
bank_transfer_details: None,
hyperswitch_token_data: PaymentTokenData::permanent_card(
Some(pm.payment_method_id.clone()),
pm.locker_id.clone().or(Some(pm.payment_method_id.clone())),
pm.locker_id.clone().unwrap_or(pm.payment_method_id.clone()),
),
)
}
} else {
continue;
}
Expand All @@ -2833,22 +2829,31 @@ pub async fn list_customer_payment_method(
});
if let Some(data) = bank_account_token_data {
let token_data = PaymentTokenData::AuthBankDebit(data);
(None, None, token_data)

PaymentMethodListContext {
card_details: None,
#[cfg(feature = "payouts")]
bank_transfer_details: None,
hyperswitch_token_data: token_data,
}
} else {
continue;
}
}

enums::PaymentMethod::Wallet => (
None,
None,
PaymentTokenData::wallet_token(pm.payment_method_id.clone()),
),
enums::PaymentMethod::Wallet => PaymentMethodListContext {
card_details: None,
#[cfg(feature = "payouts")]
bank_transfer_details: None,
hyperswitch_token_data: PaymentTokenData::wallet_token(
pm.payment_method_id.clone(),
),
},

#[cfg(feature = "payouts")]
enums::PaymentMethod::BankTransfer => (
None,
Some(
enums::PaymentMethod::BankTransfer => PaymentMethodListContext {
card_details: None,
bank_transfer_details: Some(
get_bank_from_hs_locker(
state,
&key_store,
Expand All @@ -2859,14 +2864,20 @@ pub async fn list_customer_payment_method(
)
.await?,
),
PaymentTokenData::temporary_generic(parent_payment_method_token.clone()),
),
hyperswitch_token_data: PaymentTokenData::temporary_generic(
parent_payment_method_token.clone(),
),
},

_ => (
None,
None,
PaymentTokenData::temporary_generic(generate_id(consts::ID_LENGTH, "token")),
),
_ => PaymentMethodListContext {
card_details: None,
#[cfg(feature = "payouts")]
bank_transfer_details: None,
hyperswitch_token_data: PaymentTokenData::temporary_generic(generate_id(
consts::ID_LENGTH,
"token",
)),
},
};

// Retrieve the masked bank details to be sent as a response
Expand All @@ -2890,15 +2901,15 @@ pub async fn list_customer_payment_method(
payment_method: pm.payment_method,
payment_method_type: pm.payment_method_type,
payment_method_issuer: pm.payment_method_issuer,
card,
card: payment_method_retrieval_context.card_details,
metadata: pm.metadata,
payment_method_issuer_code: pm.payment_method_issuer_code,
recurring_enabled: false,
installment_payment_enabled: false,
payment_experience: Some(vec![api_models::enums::PaymentExperience::RedirectToUrl]),
created: Some(pm.created_at),
#[cfg(feature = "payouts")]
bank_transfer: pmd,
bank_transfer: payment_method_retrieval_context.bank_transfer_details,
bank: bank_details,
surcharge_details: None,
requires_cvv: requires_cvv
Expand All @@ -2920,7 +2931,11 @@ pub async fn list_customer_payment_method(
&parent_payment_method_token,
pma.payment_method,
))
.insert(intent_created, hyperswitch_token_data, state)
.insert(
intent_created,
payment_method_retrieval_context.hyperswitch_token_data,
state,
)
.await?;

if let Some(metadata) = pma.metadata {
Expand Down
7 changes: 4 additions & 3 deletions crates/router/src/core/payouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,10 @@ pub async fn fulfill_payout(
let payout_attempt = &payout_data.payout_attempt;
match router_data_resp.response {
Ok(payout_response_data) => {
let status = payout_response_data
.status
.unwrap_or(payout_attempt.status.to_owned());
payout_data.payouts.status = status;
if payout_data.payouts.recurring && payout_data.payouts.payout_method_id.is_none() {
helpers::save_payout_data_to_locker(
state,
Expand All @@ -1215,9 +1219,6 @@ pub async fn fulfill_payout(
)
.await?;
}
let status = payout_response_data
.status
.unwrap_or(payout_attempt.status.to_owned());
let updated_payout_attempt = storage::PayoutAttemptUpdate::StatusUpdate {
connector_payout_id: payout_attempt.connector_payout_id.to_owned(),
status,
Expand Down
11 changes: 0 additions & 11 deletions crates/router/src/db/kafka_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1368,17 +1368,6 @@ impl PayoutAttemptInterface for KafkaStore {}
#[cfg(feature = "payouts")]
#[async_trait::async_trait]
impl PayoutAttemptInterface for KafkaStore {
async fn find_payout_attempt_by_merchant_id_payout_id(
&self,
merchant_id: &str,
payout_id: &str,
storage_scheme: MerchantStorageScheme,
) -> CustomResult<storage::PayoutAttempt, errors::DataStorageError> {
self.diesel_store
.find_payout_attempt_by_merchant_id_payout_id(merchant_id, payout_id, storage_scheme)
.await
}

async fn find_payout_attempt_by_merchant_id_payout_attempt_id(
&self,
merchant_id: &str,
Expand Down
10 changes: 9 additions & 1 deletion crates/router/src/types/storage/payment_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub use diesel_models::payment_method::{
TokenizeCoreWorkflow,
};

use crate::types::api::payments;
use crate::types::api::{self, payments};

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
Expand Down Expand Up @@ -78,6 +78,14 @@ impl PaymentTokenData {
}
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct PaymentMethodListContext {
pub card_details: Option<api::CardDetailFromLocker>,
pub hyperswitch_token_data: PaymentTokenData,
#[cfg(feature = "payouts")]
pub bank_transfer_details: Option<api::BankPayout>,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct PaymentsMandateReferenceRecord {
pub connector_mandate_id: String,
Expand Down
10 changes: 0 additions & 10 deletions crates/storage_impl/src/mock_db/payout_attempt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@ use super::MockDb;

#[async_trait::async_trait]
impl PayoutAttemptInterface for MockDb {
async fn find_payout_attempt_by_merchant_id_payout_id(
&self,
_merchant_id: &str,
_payout_id: &str,
_storage_scheme: storage_enums::MerchantStorageScheme,
) -> CustomResult<PayoutAttempt, StorageError> {
// TODO: Implement function for `MockDb`
Err(StorageError::MockDbError)?
}

async fn update_payout_attempt(
&self,
_this: &PayoutAttempt,
Expand Down
Loading

0 comments on commit 73d0d29

Please sign in to comment.