Skip to content

Commit 1697c02

Browse files
Akshay SAkshay S
authored andcommitted
feat. added kv support for payment method
1 parent ed186a5 commit 1697c02

35 files changed

+649
-158
lines changed

crates/diesel_models/src/kv.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::{
1111
refund::{Refund, RefundNew, RefundUpdate},
1212
reverse_lookup::{ReverseLookup, ReverseLookupNew},
1313
PaymentIntent, PgPooledConn,
14+
PaymentMethod, PaymentMethodNew, PaymentMethodUpdateInternal,
1415
};
1516

1617
#[derive(Debug, Serialize, Deserialize)]
@@ -37,6 +38,7 @@ impl DBOperation {
3738
Insertable::Payouts(_) => "payouts",
3839
Insertable::PayoutAttempt(_) => "payout_attempt",
3940
Insertable::ReverseLookUp(_) => "reverse_lookup",
41+
Insertable::PaymentMethod(_) => "payment_method",
4042
},
4143
Self::Update { updatable } => match updatable {
4244
Updateable::PaymentIntentUpdate(_) => "payment_intent",
@@ -45,6 +47,7 @@ impl DBOperation {
4547
Updateable::AddressUpdate(_) => "address",
4648
Updateable::PayoutsUpdate(_) => "payouts",
4749
Updateable::PayoutAttemptUpdate(_) => "payout_attempt",
50+
Updateable::PaymentMethodUpdate(_) => "payment_method",
4851
},
4952
}
5053
}
@@ -59,6 +62,7 @@ pub enum DBResult {
5962
ReverseLookUp(Box<ReverseLookup>),
6063
Payouts(Box<Payouts>),
6164
PayoutAttempt(Box<PayoutAttempt>),
65+
PaymentMethod(Box<PaymentMethod>),
6266
}
6367

6468
#[derive(Debug, Serialize, Deserialize)]
@@ -86,6 +90,9 @@ impl DBOperation {
8690
Insertable::PayoutAttempt(rev) => {
8791
DBResult::PayoutAttempt(Box::new(rev.insert(conn).await?))
8892
}
93+
Insertable::PaymentMethod(rev) => {
94+
DBResult::PaymentMethod(Box::new(rev.insert(conn).await?))
95+
}
8996
},
9097
Self::Update { updatable } => match updatable {
9198
Updateable::PaymentIntentUpdate(a) => {
@@ -106,6 +113,9 @@ impl DBOperation {
106113
Updateable::PayoutAttemptUpdate(a) => DBResult::PayoutAttempt(Box::new(
107114
a.orig.update_with_attempt_id(conn, a.update_data).await?,
108115
)),
116+
Updateable::PaymentMethodUpdate(v) => DBResult::PaymentMethod(Box::new(
117+
v.orig.update_with_payment_method_id(conn, v.update_data).await?,
118+
)),
109119
},
110120
})
111121
}
@@ -142,6 +152,7 @@ pub enum Insertable {
142152
ReverseLookUp(ReverseLookupNew),
143153
Payouts(PayoutsNew),
144154
PayoutAttempt(PayoutAttemptNew),
155+
PaymentMethod(PaymentMethodNew),
145156
}
146157

147158
#[derive(Debug, Serialize, Deserialize)]
@@ -153,6 +164,7 @@ pub enum Updateable {
153164
AddressUpdate(Box<AddressUpdateMems>),
154165
PayoutsUpdate(PayoutsUpdateMems),
155166
PayoutAttemptUpdate(PayoutAttemptUpdateMems),
167+
PaymentMethodUpdate(PaymentMethodUpdateMems),
156168
}
157169

158170
#[derive(Debug, Serialize, Deserialize)]
@@ -190,3 +202,9 @@ pub struct PayoutAttemptUpdateMems {
190202
pub orig: PayoutAttempt,
191203
pub update_data: PayoutAttemptUpdate,
192204
}
205+
206+
#[derive(Debug, Serialize, Deserialize)]
207+
pub struct PaymentMethodUpdateMems{
208+
pub orig : PaymentMethod,
209+
pub update_data: PaymentMethodUpdateInternal
210+
}

crates/diesel_models/src/payment_method.rs

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use time::PrimitiveDateTime;
66

77
use crate::{encryption::Encryption, enums as storage_enums, schema::payment_methods};
88

9-
#[derive(Clone, Debug, Eq, PartialEq, Identifiable, Queryable)]
9+
#[derive(Clone, Debug, Eq, PartialEq, Identifiable, Queryable,Serialize, Deserialize)]
1010
#[diesel(table_name = payment_methods)]
1111
pub struct PaymentMethod {
1212
pub id: i32,
@@ -41,7 +41,7 @@ pub struct PaymentMethod {
4141
pub network_transaction_id: Option<String>,
4242
}
4343

44-
#[derive(Clone, Debug, Eq, PartialEq, Insertable, router_derive::DebugAsDisplay)]
44+
#[derive(Clone, Debug, Eq, PartialEq, Insertable, router_derive::DebugAsDisplay,Serialize, Deserialize)]
4545
#[diesel(table_name = payment_methods)]
4646
pub struct PaymentMethodNew {
4747
pub customer_id: String,
@@ -138,7 +138,7 @@ pub enum PaymentMethodUpdate {
138138
},
139139
}
140140

141-
#[derive(Clone, Debug, Default, AsChangeset, router_derive::DebugAsDisplay)]
141+
#[derive(Clone, Debug, Default, AsChangeset, router_derive::DebugAsDisplay,Serialize, Deserialize)]
142142
#[diesel(table_name = payment_methods)]
143143
pub struct PaymentMethodUpdateInternal {
144144
metadata: Option<serde_json::Value>,
@@ -155,6 +155,27 @@ impl PaymentMethodUpdateInternal {
155155

156156
PaymentMethod { metadata, ..source }
157157
}
158+
159+
pub fn apply_changeset(self, source : PaymentMethod) -> PaymentMethod{
160+
let Self{
161+
metadata,
162+
payment_method_data,
163+
last_used_at,
164+
network_transaction_id,
165+
status,
166+
connector_mandate_details,
167+
} = self;
168+
169+
PaymentMethod{
170+
metadata: metadata.map_or(source.metadata, |v| Some(v.into())),
171+
payment_method_data : payment_method_data.map_or(source.payment_method_data, |v| Some(v)),
172+
last_used_at : last_used_at.unwrap_or(source.last_used_at),
173+
network_transaction_id: network_transaction_id.map_or(source.network_transaction_id, |v| Some(v)),
174+
status : status.unwrap_or(source.status),
175+
connector_mandate_details : connector_mandate_details.map_or(source.connector_mandate_details, |v| Some(v)),
176+
..source
177+
}
178+
}
158179
}
159180

160181
impl From<PaymentMethodUpdate> for PaymentMethodUpdateInternal {
@@ -218,3 +239,39 @@ impl From<PaymentMethodUpdate> for PaymentMethodUpdateInternal {
218239
}
219240
}
220241
}
242+
243+
impl From<&PaymentMethodNew> for PaymentMethod {
244+
fn from(payment_method_new : &PaymentMethodNew) -> Self{
245+
Self{
246+
id: 0i32,
247+
customer_id: payment_method_new.customer_id.clone(),
248+
merchant_id: payment_method_new.merchant_id.clone(),
249+
payment_method_id: payment_method_new.payment_method_id.clone(),
250+
locker_id: payment_method_new.locker_id.clone(),
251+
accepted_currency: payment_method_new.accepted_currency.clone(),
252+
scheme: payment_method_new.scheme.clone(),
253+
token: payment_method_new.token.clone(),
254+
cardholder_name: payment_method_new.cardholder_name.clone(),
255+
issuer_name: payment_method_new.issuer_name.clone(),
256+
issuer_country: payment_method_new.issuer_country.clone(),
257+
payer_country: payment_method_new.payer_country.clone(),
258+
is_stored: payment_method_new.is_stored.clone(),
259+
swift_code: payment_method_new.swift_code.clone(),
260+
direct_debit_token: payment_method_new.direct_debit_token.clone(),
261+
created_at: payment_method_new.created_at.clone(),
262+
last_modified: payment_method_new.last_modified.clone(),
263+
payment_method: payment_method_new.payment_method.clone(),
264+
payment_method_type: payment_method_new.payment_method_type.clone(),
265+
payment_method_issuer: payment_method_new.payment_method_issuer.clone(),
266+
payment_method_issuer_code: payment_method_new.payment_method_issuer_code.clone(),
267+
metadata: payment_method_new.metadata.clone(),
268+
payment_method_data: payment_method_new.payment_method_data.clone(),
269+
last_used_at: payment_method_new.last_used_at.clone(),
270+
connector_mandate_details: payment_method_new.connector_mandate_details.clone(),
271+
customer_acceptance: payment_method_new.customer_acceptance.clone(),
272+
status: payment_method_new.status.clone(),
273+
network_transaction_id : payment_method_new.network_transaction_id.clone(),
274+
275+
}
276+
}
277+
}

crates/diesel_models/src/query/payment_method.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl PaymentMethod {
151151
pub async fn update_with_payment_method_id(
152152
self,
153153
conn: &PgPooledConn,
154-
payment_method: payment_method::PaymentMethodUpdate,
154+
payment_method: payment_method::PaymentMethodUpdateInternal,
155155
) -> StorageResult<Self> {
156156
match generics::generic_update_with_unique_predicate_get_result::<
157157
<Self as HasTable>::Table,
@@ -172,4 +172,5 @@ impl PaymentMethod {
172172
result => result,
173173
}
174174
}
175+
175176
}

crates/router/src/core/mandate.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub async fn get_mandate(
4343
.await
4444
.to_not_found_response(errors::ApiErrorResponse::MandateNotFound)?;
4545
Ok(services::ApplicationResponse::Json(
46-
mandates::MandateResponse::from_db_mandate(&state, key_store, mandate).await?,
46+
mandates::MandateResponse::from_db_mandate(&state, key_store, mandate, merchant_account.storage_scheme).await?,
4747
))
4848
}
4949

@@ -226,7 +226,7 @@ pub async fn get_customer_mandates(
226226
let mut response_vec = Vec::with_capacity(mandates.len());
227227
for mandate in mandates {
228228
response_vec.push(
229-
mandates::MandateResponse::from_db_mandate(&state, key_store.clone(), mandate)
229+
mandates::MandateResponse::from_db_mandate(&state, key_store.clone(), mandate, merchant_account.storage_scheme)
230230
.await?,
231231
);
232232
}
@@ -471,7 +471,7 @@ pub async fn retrieve_mandates_list(
471471
.change_context(errors::ApiErrorResponse::InternalServerError)
472472
.attach_printable("Unable to retrieve mandates")?;
473473
let mandates_list = future::try_join_all(mandates.into_iter().map(|mandate| {
474-
mandates::MandateResponse::from_db_mandate(&state, key_store.clone(), mandate)
474+
mandates::MandateResponse::from_db_mandate(&state, key_store.clone(), mandate, merchant_account.storage_scheme)
475475
}))
476476
.await?;
477477
Ok(services::ApplicationResponse::Json(mandates_list))

crates/router/src/core/payment_methods.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub trait PaymentMethodRetrieve {
3838
payment_intent: &PaymentIntent,
3939
card_token_data: Option<&CardToken>,
4040
customer: &Option<domain::Customer>,
41+
storage_scheme: common_enums::enums::MerchantStorageScheme,
4142
) -> RouterResult<storage::PaymentMethodDataWithId>;
4243
}
4344

@@ -122,6 +123,7 @@ impl PaymentMethodRetrieve for Oss {
122123
payment_intent: &PaymentIntent,
123124
card_token_data: Option<&CardToken>,
124125
customer: &Option<domain::Customer>,
126+
storage_scheme: common_enums::enums::MerchantStorageScheme,
125127
) -> RouterResult<storage::PaymentMethodDataWithId> {
126128
let token = match token_data {
127129
storage::PaymentTokenData::TemporaryGeneric(generic_token) => {
@@ -172,6 +174,8 @@ impl PaymentMethodRetrieve for Oss {
172174
.unwrap_or(&card_token.token),
173175
payment_intent,
174176
card_token_data,
177+
merchant_key_store,
178+
storage_scheme,
175179
)
176180
.await
177181
.map(|card| Some((card, enums::PaymentMethod::Card)))?
@@ -201,6 +205,8 @@ impl PaymentMethodRetrieve for Oss {
201205
.unwrap_or(&card_token.token),
202206
payment_intent,
203207
card_token_data,
208+
merchant_key_store,
209+
storage_scheme,
204210
)
205211
.await
206212
.map(|card| Some((card, enums::PaymentMethod::Card)))?

0 commit comments

Comments
 (0)