fix(router): decrease payment method token time based on payment_intent creation time (#1682)

Co-authored-by: Sahkal Poddar <sahkal.poddar@juspay.in>
Co-authored-by: Arun Raj M <jarnura47@gmail.com>
This commit is contained in:
Sahkal Poddar
2023-07-14 12:12:54 +05:30
committed by GitHub
parent 08cca881c2
commit ce1d205219
3 changed files with 32 additions and 7 deletions

View File

@ -9,7 +9,7 @@ use crate::{
core::{customers, payment_methods::cards}, core::{customers, payment_methods::cards},
routes, routes,
services::{api, authentication as auth}, 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))] #[instrument(skip_all, fields(flow = ?Flow::CustomersCreate))]
@ -168,9 +168,10 @@ pub async fn list_customer_payment_method_api(
state: web::Data<routes::AppState>, state: web::Data<routes::AppState>,
req: HttpRequest, req: HttpRequest,
path: web::Path<String>, path: web::Path<String>,
json_payload: web::Query<payment_methods::PaymentMethodListRequest>,
) -> HttpResponse { ) -> HttpResponse {
let customer_id = path.into_inner(); let customer_id = path.into_inner();
let payload = json_payload.into_inner();
let flow = Flow::CustomerPaymentMethodsList; let flow = Flow::CustomerPaymentMethodsList;
wrap::compatibility_api_wrap::< wrap::compatibility_api_wrap::<
@ -186,9 +187,15 @@ pub async fn list_customer_payment_method_api(
flow, flow,
state.get_ref(), state.get_ref(),
&req, &req,
customer_id.as_ref(), payload,
|state, auth, req| { |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, &auth::ApiKeyAuth,
) )

View File

@ -1647,6 +1647,7 @@ pub async fn list_customer_payment_method(
state: &routes::AppState, state: &routes::AppState,
merchant_account: domain::MerchantAccount, merchant_account: domain::MerchantAccount,
key_store: domain::MerchantKeyStore, key_store: domain::MerchantKeyStore,
req: api::PaymentMethodListRequest,
customer_id: &str, customer_id: &str,
) -> errors::RouterResponse<api::CustomerPaymentMethodsListResponse> { ) -> errors::RouterResponse<api::CustomerPaymentMethodsListResponse> {
let db = &*state.store; let db = &*state.store;
@ -1702,11 +1703,23 @@ pub async fn list_customer_payment_method(
"pm_token_{}_{}_hyperswitch", "pm_token_{}_{}_hyperswitch",
parent_payment_method_token, pma.payment_method 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 redis_conn
.set_key_with_expiry( .set_key_with_expiry(
&key_for_hyperswitch_token, &key_for_hyperswitch_token,
hyperswitch_token, hyperswitch_token,
consts::TOKEN_TTL, consts::TOKEN_TTL - time_eslapsed.whole_seconds(),
) )
.await .await
.map_err(|error| { .map_err(|error| {
@ -1731,7 +1744,11 @@ pub async fn list_customer_payment_method(
parent_payment_method_token, pma.payment_method, pm_metadata.0 parent_payment_method_token, pma.payment_method, pm_metadata.0
); );
redis_conn 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 .await
.map_err(|error| { .map_err(|error| {
logger::error!(connector_payment_method_token_kv_error=?error); logger::error!(connector_payment_method_token_kv_error=?error);

View File

@ -138,11 +138,12 @@ pub async fn list_customer_payment_method_api(
state.get_ref(), state.get_ref(),
&req, &req,
json_payload.into_inner(), json_payload.into_inner(),
|state, auth, _| { |state, auth, req| {
cards::list_customer_payment_method( cards::list_customer_payment_method(
state, state,
auth.merchant_account, auth.merchant_account,
auth.key_store, auth.key_store,
req,
&customer_id, &customer_id,
) )
}, },