mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-01 19:42:27 +08:00
feat(router): add connector_request_reference_id in router_data based on merchant config (#1627)
This commit is contained in:
committed by
GitHub
parent
b1ae981f82
commit
865db9411d
@ -306,3 +306,6 @@ refund_retrieve_tolerance = 100
|
||||
|
||||
[delayed_session_response]
|
||||
connectors_with_delayed_session_response = "trustpay"
|
||||
|
||||
[connector_request_reference_id_config]
|
||||
merchant_ids_send_payment_id_as_connector_request_id = []
|
||||
|
||||
@ -85,6 +85,7 @@ pub struct Settings {
|
||||
pub email: EmailSettings,
|
||||
pub required_fields: RequiredFields,
|
||||
pub delayed_session_response: DelayedSessionConfig,
|
||||
pub connector_request_reference_id_config: ConnectorRequestReferenceIdConfig,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone, Default)]
|
||||
@ -536,6 +537,11 @@ pub struct DelayedSessionConfig {
|
||||
pub connectors_with_delayed_session_response: HashSet<api_models::enums::Connector>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone, Default)]
|
||||
pub struct ConnectorRequestReferenceIdConfig {
|
||||
pub merchant_ids_send_payment_id_as_connector_request_id: HashSet<String>,
|
||||
}
|
||||
|
||||
fn delayed_session_deser<'a, D>(
|
||||
deserializer: D,
|
||||
) -> Result<HashSet<api_models::enums::Connector>, D::Error>
|
||||
|
||||
@ -2149,6 +2149,7 @@ pub fn router_data_type_conversion<F1, F2, Req1, Req2, Res1, Res2>(
|
||||
customer_id: router_data.customer_id,
|
||||
connector_customer: router_data.connector_customer,
|
||||
preprocessing_id: router_data.preprocessing_id,
|
||||
connector_request_reference_id: router_data.connector_request_reference_id,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -115,7 +115,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Pa
|
||||
payment_method_type,
|
||||
request,
|
||||
browser_info,
|
||||
db,
|
||||
state,
|
||||
)
|
||||
.await?,
|
||||
storage_scheme,
|
||||
@ -496,7 +496,7 @@ impl PaymentCreate {
|
||||
payment_method_type: Option<enums::PaymentMethodType>,
|
||||
request: &api::PaymentsRequest,
|
||||
browser_info: Option<serde_json::Value>,
|
||||
db: &dyn StorageInterface,
|
||||
state: &AppState,
|
||||
) -> RouterResult<storage::PaymentAttemptNew> {
|
||||
let created_at @ modified_at @ last_synced = Some(common_utils::date_time::now());
|
||||
let status =
|
||||
@ -507,7 +507,7 @@ impl PaymentCreate {
|
||||
.payment_method_data
|
||||
.as_ref()
|
||||
.async_map(|payment_method_data| async {
|
||||
helpers::get_additional_payment_data(payment_method_data, db).await
|
||||
helpers::get_additional_payment_data(payment_method_data, &*state.store).await
|
||||
})
|
||||
.await
|
||||
.as_ref()
|
||||
@ -515,11 +515,19 @@ impl PaymentCreate {
|
||||
.transpose()
|
||||
.change_context(errors::ApiErrorResponse::InternalServerError)
|
||||
.attach_printable("Failed to encode additional pm data")?;
|
||||
let attempt_id = if core_utils::is_merchant_enabled_for_payment_id_as_connector_request_id(
|
||||
&state.conf,
|
||||
merchant_id,
|
||||
) {
|
||||
payment_id.to_string()
|
||||
} else {
|
||||
utils::get_payment_attempt_id(payment_id, 1)
|
||||
};
|
||||
|
||||
Ok(storage::PaymentAttemptNew {
|
||||
payment_id: payment_id.to_string(),
|
||||
merchant_id: merchant_id.to_string(),
|
||||
attempt_id: utils::get_payment_attempt_id(payment_id, 1),
|
||||
attempt_id,
|
||||
status,
|
||||
currency,
|
||||
amount: amount.into(),
|
||||
|
||||
@ -96,6 +96,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::VerifyRequest> for Paym
|
||||
merchant_id,
|
||||
request.payment_method,
|
||||
request,
|
||||
state,
|
||||
),
|
||||
storage_scheme,
|
||||
)
|
||||
@ -299,14 +300,23 @@ impl PaymentMethodValidate {
|
||||
merchant_id: &str,
|
||||
payment_method: Option<api_enums::PaymentMethod>,
|
||||
_request: &api::VerifyRequest,
|
||||
state: &AppState,
|
||||
) -> storage::PaymentAttemptNew {
|
||||
let created_at @ modified_at @ last_synced = Some(date_time::now());
|
||||
let status = storage_enums::AttemptStatus::Pending;
|
||||
let attempt_id = if core_utils::is_merchant_enabled_for_payment_id_as_connector_request_id(
|
||||
&state.conf,
|
||||
merchant_id,
|
||||
) {
|
||||
payment_id.to_string()
|
||||
} else {
|
||||
utils::get_payment_attempt_id(payment_id, 1)
|
||||
};
|
||||
|
||||
storage::PaymentAttemptNew {
|
||||
payment_id: payment_id.to_string(),
|
||||
merchant_id: merchant_id.to_string(),
|
||||
attempt_id: utils::get_payment_attempt_id(payment_id, 1),
|
||||
attempt_id,
|
||||
status,
|
||||
// Amount & Currency will be zero in this case
|
||||
amount: 0,
|
||||
|
||||
@ -13,6 +13,7 @@ use crate::{
|
||||
core::{
|
||||
errors::{self, RouterResponse, RouterResult},
|
||||
payments::{self, helpers},
|
||||
utils as core_utils,
|
||||
},
|
||||
routes::{metrics, AppState},
|
||||
services::{self, RedirectForm},
|
||||
@ -122,6 +123,11 @@ where
|
||||
reference_id: None,
|
||||
payment_method_token: payment_data.pm_token,
|
||||
connector_customer: payment_data.connector_customer_id,
|
||||
connector_request_reference_id: core_utils::get_connector_request_reference_id(
|
||||
&state.conf,
|
||||
&merchant_account.merchant_id,
|
||||
&payment_data.payment_attempt,
|
||||
),
|
||||
preprocessing_id: payment_data.payment_attempt.preprocessing_step_id,
|
||||
};
|
||||
|
||||
|
||||
@ -7,6 +7,7 @@ use router_env::{instrument, tracing};
|
||||
|
||||
use super::payments::{helpers, PaymentAddress};
|
||||
use crate::{
|
||||
configs::settings,
|
||||
consts,
|
||||
core::errors::{self, RouterResult},
|
||||
routes::AppState,
|
||||
@ -18,6 +19,11 @@ use crate::{
|
||||
utils::{generate_id, OptionExt, ValueExt},
|
||||
};
|
||||
|
||||
const IRRELEVANT_CONNECTOR_REQUEST_REFERENCE_ID_IN_DISPUTE_FLOW: &str =
|
||||
"irrelevant_connector_request_reference_id_in_dispute_flow";
|
||||
const IRRELEVANT_PAYMENT_ID_IN_DISPUTE_FLOW: &str = "irrelevant_payment_id_in_dispute_flow";
|
||||
const IRRELEVANT_ATTEMPT_ID_IN_DISPUTE_FLOW: &str = "irrelevant_attempt_id_in_dispute_flow";
|
||||
|
||||
#[instrument(skip_all)]
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub async fn construct_refund_router_data<'a, F>(
|
||||
@ -107,6 +113,11 @@ pub async fn construct_refund_router_data<'a, F>(
|
||||
payment_method_token: None,
|
||||
connector_customer: None,
|
||||
preprocessing_id: None,
|
||||
connector_request_reference_id: get_connector_request_reference_id(
|
||||
&state.conf,
|
||||
&merchant_account.merchant_id,
|
||||
payment_attempt,
|
||||
),
|
||||
};
|
||||
|
||||
Ok(router_data)
|
||||
@ -294,6 +305,11 @@ pub async fn construct_accept_dispute_router_data<'a>(
|
||||
connector_customer: None,
|
||||
customer_id: None,
|
||||
preprocessing_id: None,
|
||||
connector_request_reference_id: get_connector_request_reference_id(
|
||||
&state.conf,
|
||||
&merchant_account.merchant_id,
|
||||
payment_attempt,
|
||||
),
|
||||
};
|
||||
Ok(router_data)
|
||||
}
|
||||
@ -355,6 +371,11 @@ pub async fn construct_submit_evidence_router_data<'a>(
|
||||
connector_customer: None,
|
||||
customer_id: None,
|
||||
preprocessing_id: None,
|
||||
connector_request_reference_id: get_connector_request_reference_id(
|
||||
&state.conf,
|
||||
&merchant_account.merchant_id,
|
||||
payment_attempt,
|
||||
),
|
||||
};
|
||||
Ok(router_data)
|
||||
}
|
||||
@ -417,6 +438,11 @@ pub async fn construct_upload_file_router_data<'a>(
|
||||
connector_customer: None,
|
||||
customer_id: None,
|
||||
preprocessing_id: None,
|
||||
connector_request_reference_id: get_connector_request_reference_id(
|
||||
&state.conf,
|
||||
&merchant_account.merchant_id,
|
||||
payment_attempt,
|
||||
),
|
||||
};
|
||||
Ok(router_data)
|
||||
}
|
||||
@ -481,6 +507,11 @@ pub async fn construct_defend_dispute_router_data<'a>(
|
||||
customer_id: None,
|
||||
connector_customer: None,
|
||||
preprocessing_id: None,
|
||||
connector_request_reference_id: get_connector_request_reference_id(
|
||||
&state.conf,
|
||||
&merchant_account.merchant_id,
|
||||
payment_attempt,
|
||||
),
|
||||
};
|
||||
Ok(router_data)
|
||||
}
|
||||
@ -517,8 +548,8 @@ pub async fn construct_retrieve_file_router_data<'a>(
|
||||
connector: connector_id.to_string(),
|
||||
customer_id: None,
|
||||
connector_customer: None,
|
||||
payment_id: "irrelevant_payment_id_in_dispute_flow".to_string(),
|
||||
attempt_id: "irrelevant_attempt_id_in_dispute_flow".to_string(),
|
||||
payment_id: IRRELEVANT_PAYMENT_ID_IN_DISPUTE_FLOW.to_string(),
|
||||
attempt_id: IRRELEVANT_ATTEMPT_ID_IN_DISPUTE_FLOW.to_string(),
|
||||
status: storage_models::enums::AttemptStatus::default(),
|
||||
payment_method: storage_models::enums::PaymentMethod::default(),
|
||||
connector_auth_type: auth_type,
|
||||
@ -543,6 +574,33 @@ pub async fn construct_retrieve_file_router_data<'a>(
|
||||
reference_id: None,
|
||||
payment_method_token: None,
|
||||
preprocessing_id: None,
|
||||
connector_request_reference_id: IRRELEVANT_CONNECTOR_REQUEST_REFERENCE_ID_IN_DISPUTE_FLOW
|
||||
.to_string(),
|
||||
};
|
||||
Ok(router_data)
|
||||
}
|
||||
|
||||
pub fn is_merchant_enabled_for_payment_id_as_connector_request_id(
|
||||
conf: &settings::Settings,
|
||||
merchant_id: &str,
|
||||
) -> bool {
|
||||
let config_map = &conf
|
||||
.connector_request_reference_id_config
|
||||
.merchant_ids_send_payment_id_as_connector_request_id;
|
||||
config_map.contains(merchant_id)
|
||||
}
|
||||
|
||||
pub fn get_connector_request_reference_id(
|
||||
conf: &settings::Settings,
|
||||
merchant_id: &str,
|
||||
payment_attempt: &storage_models::payment_attempt::PaymentAttempt,
|
||||
) -> String {
|
||||
let is_config_enabled_for_merchant =
|
||||
is_merchant_enabled_for_payment_id_as_connector_request_id(conf, merchant_id);
|
||||
// Send payment_id if config is enabled for a merchant, else send attempt_id
|
||||
if is_config_enabled_for_merchant {
|
||||
payment_attempt.payment_id.clone()
|
||||
} else {
|
||||
payment_attempt.attempt_id.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@ -204,6 +204,9 @@ pub struct RouterData<Flow, Request, Response> {
|
||||
|
||||
/// Contains any error response that the connector returns.
|
||||
pub payment_method_id: Option<String>,
|
||||
|
||||
/// Contains a reference ID that should be sent in the connector request
|
||||
pub connector_request_reference_id: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@ -812,6 +815,7 @@ impl<F1, F2, T1, T2> From<(&RouterData<F1, T1, PaymentsResponseData>, T2)>
|
||||
payment_method_token: None,
|
||||
preprocessing_id: None,
|
||||
connector_customer: data.connector_customer.clone(),
|
||||
connector_request_reference_id: data.connector_request_reference_id.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -80,6 +80,7 @@ fn construct_payment_router_data() -> types::PaymentsAuthorizeRouterData {
|
||||
payment_method_token: None,
|
||||
connector_customer: None,
|
||||
preprocessing_id: None,
|
||||
connector_request_reference_id: uuid::Uuid::new_v4().to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -124,6 +125,7 @@ fn construct_refund_router_data<F>() -> types::RefundsRouterData<F> {
|
||||
payment_method_token: None,
|
||||
connector_customer: None,
|
||||
preprocessing_id: None,
|
||||
connector_request_reference_id: uuid::Uuid::new_v4().to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -403,6 +403,7 @@ pub trait ConnectorActions: Connector {
|
||||
payment_method_token: None,
|
||||
connector_customer: None,
|
||||
preprocessing_id: None,
|
||||
connector_request_reference_id: uuid::Uuid::new_v4().to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user