Skip to content

Commit

Permalink
feat(router): add kv implementation for address for payment flows (#2177
Browse files Browse the repository at this point in the history
)
  • Loading branch information
sai-harsha-vardhan authored Sep 21, 2023
1 parent eb10aca commit afff3e1
Show file tree
Hide file tree
Showing 20 changed files with 418 additions and 113 deletions.
5 changes: 3 additions & 2 deletions crates/diesel_models/src/address.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use diesel::{AsChangeset, Identifiable, Insertable, Queryable};
use serde::{Deserialize, Serialize};
use time::PrimitiveDateTime;

use crate::{encryption::Encryption, enums, schema::address};

#[derive(Clone, Debug, Insertable, router_derive::DebugAsDisplay)]
#[derive(Clone, Debug, Insertable, Serialize, Deserialize, router_derive::DebugAsDisplay)]
#[diesel(table_name = address)]
pub struct AddressNew {
pub address_id: String,
Expand All @@ -25,7 +26,7 @@ pub struct AddressNew {
pub modified_at: PrimitiveDateTime,
}

#[derive(Clone, Debug, Queryable, Identifiable)]
#[derive(Clone, Debug, Queryable, Identifiable, Serialize, Deserialize)]
#[diesel(table_name = address, primary_key(address_id))]
pub struct Address {
pub id: Option<i32>,
Expand Down
2 changes: 2 additions & 0 deletions crates/diesel_models/src/kv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use error_stack::{IntoReport, ResultExt};
use serde::{Deserialize, Serialize};

use crate::{
address::AddressNew,
errors,
payment_attempt::{PaymentAttempt, PaymentAttemptNew, PaymentAttemptUpdate},
payment_intent::{PaymentIntent, PaymentIntentNew, PaymentIntentUpdate},
Expand Down Expand Up @@ -39,6 +40,7 @@ pub enum Insertable {
PaymentIntent(PaymentIntentNew),
PaymentAttempt(PaymentAttemptNew),
Refund(RefundNew),
Address(Box<AddressNew>),
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down
4 changes: 4 additions & 0 deletions crates/drainer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ async fn drainer(
let payment_intent = "payment_intent";
let payment_attempt = "payment_attempt";
let refund = "refund";
let address = "address";
match db_op {
// TODO: Handle errors
kv::DBOperation::Insert { insertable } => {
Expand All @@ -170,6 +171,9 @@ async fn drainer(
kv::Insertable::Refund(a) => {
macro_util::handle_resp!(a.insert(&conn).await, insert_op, refund)
}
kv::Insertable::Address(addr) => {
macro_util::handle_resp!(addr.insert(&conn).await, insert_op, address)
}
}
})
.await;
Expand Down
2 changes: 2 additions & 0 deletions crates/router/src/core/payment_methods/cards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ pub async fn list_payment_methods(
&key_store,
pi.payment_id.clone(),
merchant_account.merchant_id.clone(),
merchant_account.storage_scheme,
)
.await
})
Expand All @@ -818,6 +819,7 @@ pub async fn list_payment_methods(
&key_store,
pi.payment_id.clone(),
merchant_account.merchant_id.clone(),
merchant_account.storage_scheme,
)
.await
})
Expand Down
6 changes: 6 additions & 0 deletions crates/router/src/core/payments/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ pub fn filter_mca_based_on_business_profile(
}

#[instrument(skip_all)]
#[allow(clippy::too_many_arguments)]
pub async fn create_or_find_address_for_payment_by_request(
db: &dyn StorageInterface,
req_address: Option<&api::Address>,
Expand All @@ -112,6 +113,7 @@ pub async fn create_or_find_address_for_payment_by_request(
customer_id: Option<&String>,
merchant_key_store: &domain::MerchantKeyStore,
payment_id: &str,
storage_scheme: storage_enums::MerchantStorageScheme,
) -> CustomResult<Option<domain::Address>, errors::ApiErrorResponse> {
let key = merchant_key_store.key.get_inner().peek();

Expand All @@ -122,6 +124,7 @@ pub async fn create_or_find_address_for_payment_by_request(
payment_id,
id,
merchant_key_store,
storage_scheme,
)
.await,
)
Expand All @@ -148,6 +151,7 @@ pub async fn create_or_find_address_for_payment_by_request(
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Failed while encrypting address while insert")?,
merchant_key_store,
storage_scheme,
)
.await
.change_context(errors::ApiErrorResponse::InternalServerError)
Expand Down Expand Up @@ -224,6 +228,7 @@ pub async fn get_address_by_id(
merchant_key_store: &domain::MerchantKeyStore,
payment_id: String,
merchant_id: String,
storage_scheme: storage_enums::MerchantStorageScheme,
) -> CustomResult<Option<domain::Address>, errors::ApiErrorResponse> {
match address_id {
None => Ok(None),
Expand All @@ -233,6 +238,7 @@ pub async fn get_address_by_id(
&payment_id,
&address_id,
merchant_key_store,
storage_scheme,
)
.await
.ok()),
Expand Down
2 changes: 2 additions & 0 deletions crates/router/src/core/payments/operations/payment_approve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Pa
payment_intent.customer_id.as_ref(),
key_store,
&payment_intent.payment_id,
merchant_account.storage_scheme,
)
.await?;
let billing_address = helpers::create_or_find_address_for_payment_by_request(
Expand All @@ -154,6 +155,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Pa
payment_intent.customer_id.as_ref(),
key_store,
&payment_intent.payment_id,
merchant_account.storage_scheme,
)
.await?;

Expand Down
2 changes: 2 additions & 0 deletions crates/router/src/core/payments/operations/payment_cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsCancelRequest>
payment_intent.customer_id.as_ref(),
key_store,
&payment_intent.payment_id,
merchant_account.storage_scheme,
)
.await?;

Expand All @@ -98,6 +99,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsCancelRequest>
payment_intent.customer_id.as_ref(),
key_store,
&payment_intent.payment_id,
merchant_account.storage_scheme,
)
.await?;

Expand Down
2 changes: 2 additions & 0 deletions crates/router/src/core/payments/operations/payment_capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ impl<F: Send + Clone> GetTracker<F, payments::PaymentData<F>, api::PaymentsCaptu
payment_intent.customer_id.as_ref(),
key_store,
&payment_intent.payment_id,
merchant_account.storage_scheme,
)
.await?;

Expand All @@ -172,6 +173,7 @@ impl<F: Send + Clone> GetTracker<F, payments::PaymentData<F>, api::PaymentsCaptu
payment_intent.customer_id.as_ref(),
key_store,
&payment_intent.payment_id,
merchant_account.storage_scheme,
)
.await?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Co
payment_intent.customer_id.as_ref(),
key_store,
&payment_intent.payment_id,
merchant_account.storage_scheme,
)
.await?;
let billing_address = helpers::create_or_find_address_for_payment_by_request(
Expand All @@ -163,6 +164,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Co
payment_intent.customer_id.as_ref(),
key_store,
&payment_intent.payment_id,
merchant_account.storage_scheme,
)
.await?;

Expand Down
2 changes: 2 additions & 0 deletions crates/router/src/core/payments/operations/payment_confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Pa
.or(customer_details.customer_id.as_ref()),
key_store,
&payment_intent.payment_id,
merchant_account.storage_scheme,
);

let billing_address_fut = helpers::create_or_find_address_for_payment_by_request(
Expand All @@ -128,6 +129,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Pa
.or(customer_details.customer_id.as_ref()),
key_store,
&payment_intent.payment_id,
merchant_account.storage_scheme,
);

let config_update_fut = request
Expand Down
2 changes: 2 additions & 0 deletions crates/router/src/core/payments/operations/payment_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Pa
customer_details.customer_id.as_ref(),
merchant_key_store,
&payment_id,
merchant_account.storage_scheme,
)
.await?;

Expand All @@ -111,6 +112,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Pa
customer_details.customer_id.as_ref(),
merchant_key_store,
&payment_id,
merchant_account.storage_scheme,
)
.await?;

Expand Down
2 changes: 2 additions & 0 deletions crates/router/src/core/payments/operations/payment_reject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, PaymentsRejectRequest> for P
payment_intent.customer_id.as_ref(),
key_store,
&payment_intent.payment_id,
merchant_account.storage_scheme,
)
.await?;

Expand All @@ -96,6 +97,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, PaymentsRejectRequest> for P
payment_intent.customer_id.as_ref(),
key_store,
&payment_intent.payment_id,
merchant_account.storage_scheme,
)
.await?;

Expand Down
2 changes: 2 additions & 0 deletions crates/router/src/core/payments/operations/payment_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsSessionRequest>
payment_intent.customer_id.as_ref(),
key_store,
&payment_intent.payment_id,
merchant_account.storage_scheme,
)
.await?;

Expand All @@ -109,6 +110,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsSessionRequest>
payment_intent.customer_id.as_ref(),
key_store,
&payment_intent.payment_id,
merchant_account.storage_scheme,
)
.await?;

Expand Down
2 changes: 2 additions & 0 deletions crates/router/src/core/payments/operations/payment_start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsStartRequest> f
payment_intent.customer_id.as_ref(),
mechant_key_store,
&payment_intent.payment_id,
merchant_account.storage_scheme,
)
.await?;
let billing_address = helpers::create_or_find_address_for_payment_by_request(
Expand All @@ -103,6 +104,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsStartRequest> f
payment_intent.customer_id.as_ref(),
mechant_key_store,
&payment_intent.payment_id,
merchant_account.storage_scheme,
)
.await?;

Expand Down
2 changes: 2 additions & 0 deletions crates/router/src/core/payments/operations/payment_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ async fn get_tracker_for_sync<
mechant_key_store,
payment_intent.payment_id.clone(),
merchant_account.merchant_id.clone(),
merchant_account.storage_scheme,
)
.await?;
let billing_address = helpers::get_address_by_id(
Expand All @@ -251,6 +252,7 @@ async fn get_tracker_for_sync<
mechant_key_store,
payment_intent.payment_id.clone(),
merchant_account.merchant_id.clone(),
merchant_account.storage_scheme,
)
.await?;

Expand Down
2 changes: 2 additions & 0 deletions crates/router/src/core/payments/operations/payment_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Pa
.or(customer_details.customer_id.as_ref()),
key_store,
&payment_intent.payment_id,
merchant_account.storage_scheme,
)
.await?;
let billing_address = helpers::create_or_find_address_for_payment_by_request(
Expand All @@ -170,6 +171,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Pa
.or(customer_details.customer_id.as_ref()),
key_store,
&payment_intent.payment_id,
merchant_account.storage_scheme,
)
.await?;

Expand Down
2 changes: 2 additions & 0 deletions crates/router/src/core/payouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1166,6 +1166,7 @@ pub async fn payout_create_db_entries(
Some(&customer_id.to_owned()),
key_store,
payout_id,
merchant_account.storage_scheme,
)
.await?;
let address_id = billing_address
Expand Down Expand Up @@ -1302,6 +1303,7 @@ pub async fn make_payout_data(
Some(&payouts.customer_id.to_owned()),
key_store,
&payouts.payout_id,
merchant_account.storage_scheme,
)
.await?;

Expand Down
Loading

0 comments on commit afff3e1

Please sign in to comment.