Skip to content

Commit

Permalink
fix(mandates): store network transaction id only when pg_agnostic c…
Browse files Browse the repository at this point in the history
…onfig is enabled in the `authorize_flow` (#4318)
  • Loading branch information
ShankarSinghC authored Apr 5, 2024
1 parent 6694852 commit 7b4c4fe
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 5 deletions.
13 changes: 12 additions & 1 deletion crates/router/src/connector/stripe/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2663,12 +2663,23 @@ impl<F, T>
item.response.id.clone(),
))
} else {
let network_transaction_id = match item.response.latest_charge.clone() {
Some(StripeChargeEnum::ChargeObject(charge_object)) => charge_object
.payment_method_details
.and_then(|payment_method_details| match payment_method_details {
StripePaymentMethodDetailsResponse::Card { card } => {
card.network_transaction_id
}
_ => None,
}),
_ => None,
};
Ok(types::PaymentsResponseData::TransactionResponse {
resource_id: types::ResponseId::ConnectorTransactionId(item.response.id.clone()),
redirection_data,
mandate_reference,
connector_metadata,
network_txn_id: None,
network_txn_id: network_transaction_id,
connector_response_reference_id: Some(item.response.id.clone()),
incremental_authorization_allowed: None,
})
Expand Down
3 changes: 3 additions & 0 deletions crates/router/src/core/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1347,6 +1347,7 @@ where
merchant_account,
connector_request,
key_store,
payment_data.payment_intent.profile_id.clone(),
)
.await
} else {
Expand Down Expand Up @@ -1475,6 +1476,7 @@ where
merchant_account,
None,
key_store,
payment_data.payment_intent.profile_id.clone(),
);

join_handlers.push(res);
Expand Down Expand Up @@ -3069,6 +3071,7 @@ pub async fn decide_multiplex_connector_for_normal_or_recurring_payment<F: Clone
connector_data.connector_name,
payment_method_info,
) {
logger::info!("using network_transaction_id for MIT flow");
let network_transaction_id = payment_method_info
.network_transaction_id
.as_ref()
Expand Down
1 change: 1 addition & 0 deletions crates/router/src/core/payments/flows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub trait Feature<F, T> {
merchant_account: &domain::MerchantAccount,
connector_request: Option<services::Request>,
key_store: &domain::MerchantKeyStore,
profile_id: Option<String>,
) -> RouterResult<Self>
where
Self: Sized,
Expand Down
1 change: 1 addition & 0 deletions crates/router/src/core/payments/flows/approve_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ impl Feature<api::Approve, types::PaymentsApproveData>
_merchant_account: &domain::MerchantAccount,
_connector_request: Option<services::Request>,
_key_store: &domain::MerchantKeyStore,
_profile_id: Option<String>,
) -> RouterResult<Self> {
Err(ApiErrorResponse::NotImplemented {
message: NotImplementedMessage::Reason("Flow not supported".to_string()),
Expand Down
3 changes: 3 additions & 0 deletions crates/router/src/core/payments/flows/authorize_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ impl Feature<api::Authorize, types::PaymentsAuthorizeData> for types::PaymentsAu
merchant_account: &domain::MerchantAccount,
connector_request: Option<services::Request>,
key_store: &domain::MerchantKeyStore,
profile_id: Option<String>,
) -> RouterResult<Self> {
let connector_integration: services::BoxedConnectorIntegration<
'_,
Expand Down Expand Up @@ -103,6 +104,7 @@ impl Feature<api::Authorize, types::PaymentsAuthorizeData> for types::PaymentsAu
key_store,
Some(resp.request.amount),
Some(resp.request.currency),
profile_id,
))
.await?;

Expand Down Expand Up @@ -132,6 +134,7 @@ impl Feature<api::Authorize, types::PaymentsAuthorizeData> for types::PaymentsAu
key_store,
Some(resp.request.amount),
Some(resp.request.currency),
profile_id,
))
.await;

Expand Down
1 change: 1 addition & 0 deletions crates/router/src/core/payments/flows/cancel_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl Feature<api::Void, types::PaymentsCancelData>
_merchant_account: &domain::MerchantAccount,
connector_request: Option<services::Request>,
_key_store: &domain::MerchantKeyStore,
_profile_id: Option<String>,
) -> RouterResult<Self> {
metrics::PAYMENT_CANCEL_COUNT.add(
&metrics::CONTEXT,
Expand Down
1 change: 1 addition & 0 deletions crates/router/src/core/payments/flows/capture_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ impl Feature<api::Capture, types::PaymentsCaptureData>
_merchant_account: &domain::MerchantAccount,
connector_request: Option<services::Request>,
_key_store: &domain::MerchantKeyStore,
_profile_id: Option<String>,
) -> RouterResult<Self> {
let connector_integration: services::BoxedConnectorIntegration<
'_,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ impl Feature<api::CompleteAuthorize, types::CompleteAuthorizeData>
_merchant_account: &domain::MerchantAccount,
connector_request: Option<services::Request>,
_key_store: &domain::MerchantKeyStore,
_profile_id: Option<String>,
) -> RouterResult<Self> {
let connector_integration: services::BoxedConnectorIntegration<
'_,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ impl Feature<api::IncrementalAuthorization, types::PaymentsIncrementalAuthorizat
_merchant_account: &domain::MerchantAccount,
connector_request: Option<services::Request>,
_key_store: &domain::MerchantKeyStore,
_profile_id: Option<String>,
) -> RouterResult<Self> {
let connector_integration: services::BoxedConnectorIntegration<
'_,
Expand Down
1 change: 1 addition & 0 deletions crates/router/src/core/payments/flows/psync_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ impl Feature<api::PSync, types::PaymentsSyncData>
_merchant_account: &domain::MerchantAccount,
connector_request: Option<services::Request>,
_key_store: &domain::MerchantKeyStore,
_profile_id: Option<String>,
) -> RouterResult<Self> {
let connector_integration: services::BoxedConnectorIntegration<
'_,
Expand Down
1 change: 1 addition & 0 deletions crates/router/src/core/payments/flows/reject_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl Feature<api::Reject, types::PaymentsRejectData>
_merchant_account: &domain::MerchantAccount,
_connector_request: Option<services::Request>,
_key_store: &domain::MerchantKeyStore,
_profile_id: Option<String>,
) -> RouterResult<Self> {
Err(ApiErrorResponse::NotImplemented {
message: NotImplementedMessage::Reason("Flow not supported".to_string()),
Expand Down
1 change: 1 addition & 0 deletions crates/router/src/core/payments/flows/session_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ impl Feature<api::Session, types::PaymentsSessionData> for types::PaymentsSessio
_merchant_account: &domain::MerchantAccount,
_connector_request: Option<services::Request>,
_key_store: &domain::MerchantKeyStore,
_profile_id: Option<String>,
) -> RouterResult<Self> {
metrics::SESSION_TOKEN_CREATED.add(
&metrics::CONTEXT,
Expand Down
7 changes: 7 additions & 0 deletions crates/router/src/core/payments/flows/setup_mandate_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ impl Feature<api::SetupMandate, types::SetupMandateRequestData> for types::Setup
merchant_account: &domain::MerchantAccount,
connector_request: Option<services::Request>,
key_store: &domain::MerchantKeyStore,
profile_id: Option<String>,
) -> RouterResult<Self> {
if let Some(mandate_id) = self
.request
Expand All @@ -79,6 +80,7 @@ impl Feature<api::SetupMandate, types::SetupMandateRequestData> for types::Setup
&state.conf.mandates.update_mandate_supported,
connector_request,
maybe_customer,
profile_id,
))
.await
} else {
Expand Down Expand Up @@ -109,6 +111,7 @@ impl Feature<api::SetupMandate, types::SetupMandateRequestData> for types::Setup
key_store,
resp.request.amount,
Some(resp.request.currency),
profile_id,
))
.await?;

Expand Down Expand Up @@ -217,6 +220,7 @@ impl types::SetupMandateRouterData {
call_connector_action: payments::CallConnectorAction,
merchant_account: &domain::MerchantAccount,
key_store: &domain::MerchantKeyStore,
profile_id: Option<String>,
) -> RouterResult<Self> {
match confirm {
Some(true) => {
Expand Down Expand Up @@ -248,6 +252,7 @@ impl types::SetupMandateRouterData {
key_store,
resp.request.amount,
Some(resp.request.currency),
profile_id,
))
.await?;

Expand Down Expand Up @@ -278,6 +283,7 @@ impl types::SetupMandateRouterData {
supported_connectors_for_update_mandate: &settings::SupportedPaymentMethodsForMandate,
connector_request: Option<services::Request>,
maybe_customer: &Option<domain::Customer>,
profile_id: Option<String>,
) -> RouterResult<Self> {
let payment_method_type = self.request.payment_method_type;

Expand Down Expand Up @@ -336,6 +342,7 @@ impl types::SetupMandateRouterData {
key_store,
resp.request.amount,
Some(resp.request.currency),
profile_id,
))
.await?
.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1001,12 +1001,13 @@ async fn update_payment_method_status_and_ntid<F: Clone>(
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("The pg_agnostic config was not found in the DB")?;

if &pg_agnostic.config == "true" {
if &pg_agnostic.config == "true"
&& payment_data.payment_intent.setup_future_usage
== Some(diesel_models::enums::FutureUsage::OffSession)
{
Some(network_transaction_id)
} else {
logger::info!(
"Skip storing network transaction id as pg_agnostic config is not enabled"
);
logger::info!("Skip storing network transaction id");
None
}
} else {
Expand Down
30 changes: 30 additions & 0 deletions crates/router/src/core/payments/tokenization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub async fn save_payment_method<F: Clone, FData>(
key_store: &domain::MerchantKeyStore,
amount: Option<i64>,
currency: Option<storage_enums::Currency>,
profile_id: Option<String>,
) -> RouterResult<(Option<String>, Option<common_enums::PaymentMethodStatus>)>
where
FData: mandate::MandateBehaviour,
Expand All @@ -64,6 +65,35 @@ where
_ => None,
};

let network_transaction_id =
if let Some(network_transaction_id) = network_transaction_id {
let profile_id = profile_id
.as_ref()
.ok_or(errors::ApiErrorResponse::ResourceIdNotFound)?;

let pg_agnostic = state
.store
.find_config_by_key_unwrap_or(
&format!("pg_agnostic_mandate_{}", profile_id),
Some("false".to_string()),
)
.await
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("The pg_agnostic config was not found in the DB")?;

if &pg_agnostic.config == "true"
&& resp.request.get_setup_future_usage()
== Some(storage_enums::FutureUsage::OffSession)
{
Some(network_transaction_id)
} else {
logger::info!("Skip storing network transaction id");
None
}
} else {
None
};

let connector_token = if token_store {
let tokens = resp
.payment_method_token
Expand Down

0 comments on commit 7b4c4fe

Please sign in to comment.