Skip to content

Commit

Permalink
fix(router): decrease payment method token time based on payment_inte…
Browse files Browse the repository at this point in the history
…nt creation time (#1682)

Co-authored-by: Sahkal Poddar <sahkal.poddar@juspay.in>
Co-authored-by: Arun Raj M <jarnura47@gmail.com>
  • Loading branch information
3 people committed Jul 14, 2023
1 parent 08cca88 commit ce1d205
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
15 changes: 11 additions & 4 deletions crates/router/src/compatibility/stripe/customers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
core::{customers, payment_methods::cards},
routes,
services::{api, authentication as auth},
types::api::customers as customer_types,
types::api::{customers as customer_types, payment_methods},
};

#[instrument(skip_all, fields(flow = ?Flow::CustomersCreate))]
Expand Down Expand Up @@ -168,9 +168,10 @@ pub async fn list_customer_payment_method_api(
state: web::Data<routes::AppState>,
req: HttpRequest,
path: web::Path<String>,
json_payload: web::Query<payment_methods::PaymentMethodListRequest>,
) -> HttpResponse {
let customer_id = path.into_inner();

let payload = json_payload.into_inner();
let flow = Flow::CustomerPaymentMethodsList;

wrap::compatibility_api_wrap::<
Expand All @@ -186,9 +187,15 @@ pub async fn list_customer_payment_method_api(
flow,
state.get_ref(),
&req,
customer_id.as_ref(),
payload,
|state, auth, req| {
cards::list_customer_payment_method(state, auth.merchant_account, auth.key_store, req)
cards::list_customer_payment_method(
state,
auth.merchant_account,
auth.key_store,
req,
&customer_id,
)
},
&auth::ApiKeyAuth,
)
Expand Down
21 changes: 19 additions & 2 deletions crates/router/src/core/payment_methods/cards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1647,6 +1647,7 @@ pub async fn list_customer_payment_method(
state: &routes::AppState,
merchant_account: domain::MerchantAccount,
key_store: domain::MerchantKeyStore,
req: api::PaymentMethodListRequest,
customer_id: &str,
) -> errors::RouterResponse<api::CustomerPaymentMethodsListResponse> {
let db = &*state.store;
Expand Down Expand Up @@ -1702,11 +1703,23 @@ pub async fn list_customer_payment_method(
"pm_token_{}_{}_hyperswitch",
parent_payment_method_token, pma.payment_method
);

let payment_intent = helpers::verify_payment_intent_time_and_client_secret(
db,
&merchant_account,
req.client_secret.clone(),
)
.await?;
let current_datetime_utc = common_utils::date_time::now();
let time_eslapsed = current_datetime_utc
- payment_intent
.map(|intent| intent.created_at)
.unwrap_or_else(|| current_datetime_utc);
redis_conn
.set_key_with_expiry(
&key_for_hyperswitch_token,
hyperswitch_token,
consts::TOKEN_TTL,
consts::TOKEN_TTL - time_eslapsed.whole_seconds(),
)
.await
.map_err(|error| {
Expand All @@ -1731,7 +1744,11 @@ pub async fn list_customer_payment_method(
parent_payment_method_token, pma.payment_method, pm_metadata.0
);
redis_conn
.set_key_with_expiry(&key, pm_metadata.1, consts::TOKEN_TTL)
.set_key_with_expiry(
&key,
pm_metadata.1,
consts::TOKEN_TTL - time_eslapsed.whole_seconds(),
)
.await
.map_err(|error| {
logger::error!(connector_payment_method_token_kv_error=?error);
Expand Down
3 changes: 2 additions & 1 deletion crates/router/src/routes/payment_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,12 @@ pub async fn list_customer_payment_method_api(
state.get_ref(),
&req,
json_payload.into_inner(),
|state, auth, _| {
|state, auth, req| {
cards::list_customer_payment_method(
state,
auth.merchant_account,
auth.key_store,
req,
&customer_id,
)
},
Expand Down

0 comments on commit ce1d205

Please sign in to comment.