Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

fix(connectors): [worldpayvantiv] map cnp_txn_id to connector_txn_id and enforce max length for customer_id and connector_request_reference_id (#9031)

AkshayaFoigerb1486f8bd31d
diff --git a/Cargo.lock b/Cargo.lock
index fdb8ceb970..5fae101a38 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4048,6 +4048,7 @@ dependencies = [
"lazy_static",
"masking",
"mime",
+ "nanoid",
"num-traits",
"openssl",
"pem",
diff --git a/crates/hyperswitch_connectors/Cargo.toml b/crates/hyperswitch_connectors/Cargo.toml
index d06d6f299d..3f4653b02a 100644
--- a/crates/hyperswitch_connectors/Cargo.toml
+++ b/crates/hyperswitch_connectors/Cargo.toml
@@ -36,6 +36,7 @@ josekit = "0.8.7"
lazy_static = "1.5.0"
mime = "0.3.17"
num-traits = "0.2.19"
+nanoid = "0.4.0"
openssl = {version = "0.10.70"}
pem = "3.0.5"
qrcode = "0.14.1"
diff --git a/crates/hyperswitch_connectors/src/connectors/worldpayvantiv.rs b/crates/hyperswitch_connectors/src/connectors/worldpayvantiv.rs
index 8333a90e85..2447e6a203 100644
--- a/crates/hyperswitch_connectors/src/connectors/worldpayvantiv.rs
+++ b/crates/hyperswitch_connectors/src/connectors/worldpayvantiv.rs
@@ -1694,4 +1694,33 @@ impl ConnectorSpecifications for Worldpayvantiv {
fn get_supported_webhook_flows(&self) -> Option<&'static [common_enums::EventClass]> {
Some(&WORLDPAYVANTIV_SUPPORTED_WEBHOOK_FLOWS)
}
+ #[cfg(feature = "v1")]
+ fn generate_connector_request_reference_id(
+ &self,
+ payment_intent: &hyperswitch_domain_models::payments::PaymentIntent,
+ payment_attempt: &hyperswitch_domain_models::payments::payment_attempt::PaymentAttempt,
+ is_config_enabled_to_send_payment_id_as_connector_request_id: bool,
+ ) -> String {
+ if is_config_enabled_to_send_payment_id_as_connector_request_id
+ && payment_intent.is_payment_id_from_merchant.unwrap_or(false)
+ {
+ payment_attempt.payment_id.get_string_repr().to_owned()
+ } else {
+ let max_payment_reference_id_length =
+ worldpayvantiv::worldpayvantiv_constants::MAX_PAYMENT_REFERENCE_ID_LENGTH;
+ nanoid::nanoid!(max_payment_reference_id_length)
+ }
+ }
+ #[cfg(feature = "v2")]
+ fn generate_connector_request_reference_id(
+ &self,
+ payment_intent: &hyperswitch_domain_models::payments::PaymentIntent,
+ payment_attempt: &hyperswitch_domain_models::payments::payment_attempt::PaymentAttempt,
+ ) -> String {
+ if payment_intent.is_payment_id_from_merchant.unwrap_or(false) {
+ payment_attempt.payment_id.get_string_repr().to_owned()
+ } else {
+ connector_utils::generate_12_digit_number().to_string()
+ }
+ }
}
diff --git a/crates/hyperswitch_connectors/src/connectors/worldpayvantiv/transformers.rs b/crates/hyperswitch_connectors/src/connectors/worldpayvantiv/transformers.rs
index 2894ad68fc..d40692e4d1 100644
--- a/crates/hyperswitch_connectors/src/connectors/worldpayvantiv/transformers.rs
+++ b/crates/hyperswitch_connectors/src/connectors/worldpayvantiv/transformers.rs
@@ -1,6 +1,5 @@
use common_utils::{
ext_traits::Encode,
- id_type::CustomerId,
types::{MinorUnit, StringMinorUnitForConnector},
};
use error_stack::ResultExt;
@@ -49,10 +48,11 @@ pub mod worldpayvantiv_constants {
pub const XML_VERSION: &str = "1.0";
pub const XML_ENCODING: &str = "UTF-8";
pub const XMLNS: &str = "http://www.vantivcnp.com/schema";
- pub const MAX_ID_LENGTH: usize = 26;
+ pub const MAX_PAYMENT_REFERENCE_ID_LENGTH: usize = 28;
pub const XML_STANDALONE: &str = "yes";
pub const XML_CHARGEBACK: &str = "http://www.vantivcnp.com/chargebacks";
pub const MAC_FIELD_NUMBER: &str = "39";
+ pub const CUSTOMER_ID_MAX_LENGTH: usize = 50;
pub const CUSTOMER_REFERENCE_MAX_LENGTH: usize = 17;
}
@@ -600,6 +600,37 @@ where
})
}
+fn extract_customer_id<T>(item: &T) -> Option<String>
+where
+ T: UtilsRouterData,
+{
+ item.get_optional_customer_id().and_then(|customer_id| {
+ let customer_id_str = customer_id.get_string_repr().to_string();
+ if customer_id_str.len() <= worldpayvantiv_constants::CUSTOMER_ID_MAX_LENGTH {
+ Some(customer_id_str)
+ } else {
+ None
+ }
+ })
+}
+
+fn get_valid_transaction_id(
+ id: String,
+ error_field_name: &str,
+) -> Result<String, error_stack::Report<errors::ConnectorError>> {
+ if id.len() <= worldpayvantiv_constants::MAX_PAYMENT_REFERENCE_ID_LENGTH {
+ Ok(id.clone())
+ } else {
+ Err(errors::ConnectorError::MaxFieldLengthViolated {
+ connector: "Worldpayvantiv".to_string(),
+ field_name: error_field_name.to_string(),
+ max_length: worldpayvantiv_constants::MAX_PAYMENT_REFERENCE_ID_LENGTH,
+ received_length: id.len(),
+ }
+ .into())
+ }
+}
+
fn get_ship_to_address<T>(item: &T) -> Option<VantivAddressData>
where
T: UtilsRouterData,
@@ -672,11 +703,7 @@ impl TryFrom<&WorldpayvantivRouterData<&PaymentsAuthorizeRouterData>> for CnpOnl
user: worldpayvantiv_auth_type.user,
password: worldpayvantiv_auth_type.password,
};
- let customer_id = item
- .router_data
- .customer_id
- .clone()
- .map(|customer_id| customer_id.get_string_repr().to_string());
+ let customer_id = extract_customer_id(item.router_data);
let bill_to_address = get_bill_to_address(item.router_data);
let ship_to_address = get_ship_to_address(item.router_data);
@@ -686,16 +713,17 @@ impl TryFrom<&WorldpayvantivRouterData<&PaymentsAuthorizeRouterData>> for CnpOnl
item.router_data.request.payment_method_data.clone(),
item.router_data.request.payment_channel.clone(),
));
+
let (authorization, sale) =
if item.router_data.request.is_auto_capture()? && item.amount != MinorUnit::zero() {
+ let merchant_txn_id = get_valid_transaction_id(
+ item.router_data.connector_request_reference_id.clone(),
+ "sale.id",
+ )?;
(
None,
Some(Sale {
- id: format!(
- "{}_{}",
- OperationId::Sale,
- item.router_data.connector_request_reference_id
- ),
+ id: format!("{}_{merchant_txn_id}", OperationId::Sale),
report_group: report_group.clone(),
customer_id,
order_id: item.router_data.connector_request_reference_id.clone(),
@@ -723,12 +751,13 @@ impl TryFrom<&WorldpayvantivRouterData<&PaymentsAuthorizeRouterData>> for CnpOnl
} else {
OperationId::Auth
};
+ let merchant_txn_id = get_valid_transaction_id(
+ item.router_data.connector_request_reference_id.clone(),
+ "authorization.id",
+ )?;
(
Some(Authorization {
- id: format!(
- "{}_{}",
- operation_id, item.router_data.connector_request_reference_id
- ),
+ id: format!("{operation_id}_{merchant_txn_id}"),
report_group: report_group.clone(),
customer_id,
order_id: item.router_data.connector_request_reference_id.clone(),
@@ -811,10 +840,7 @@ impl TryFrom<&SetupMandateRouterData> for CnpOnlineRequest {
user: worldpayvantiv_auth_type.user,
password: worldpayvantiv_auth_type.password,
};
- let customer_id = item
- .customer_id
- .clone()
- .map(|customer_id| customer_id.get_string_repr().to_string());
+ let customer_id = extract_customer_id(item);
let bill_to_address = get_bill_to_address(item);
let ship_to_address = get_ship_to_address(item);
@@ -829,12 +855,12 @@ impl TryFrom<&SetupMandateRouterData> for CnpOnlineRequest {
item.request.payment_method_data.clone(),
item.request.payment_channel.clone(),
));
+ let merchant_txn_id = get_valid_transaction_id(
+ item.connector_request_reference_id.clone(),
+ "authorization.id",
+ )?;
let authorization_data = Authorization {
- id: format!(
- "{}_{}",
- OperationId::Sale,
- item.connector_request_reference_id
- ),
+ id: format!("{}_{merchant_txn_id}", OperationId::Sale),
report_group: report_group.clone(),
customer_id,
order_id: item.connector_request_reference_id.clone(),
@@ -934,8 +960,8 @@ where
Some(common_enums::TaxStatus::Taxable) => Some(false),
None => None,
};
-
- let customer_reference = get_vantiv_customer_reference(&l2_l3_data.customer_id);
+ let customer_reference =
+ get_vantiv_customer_reference(&l2_l3_data.merchant_order_reference_id);
let enhanced_data = EnhancedData {
customer_reference,
@@ -1018,12 +1044,12 @@ impl TryFrom<&WorldpayvantivRouterData<&PaymentsCaptureRouterData>> for CnpOnlin
"Failed to obtain report_group from metadata".to_string(),
),
)?;
+ let merchant_txn_id = get_valid_transaction_id(
+ item.router_data.connector_request_reference_id.clone(),
+ "capture.id",
+ )?;
let capture = Some(Capture {
- id: format!(
- "{}_{}",
- OperationId::Capture,
- item.router_data.connector_request_reference_id
- ),
+ id: format!("{}_{}", OperationId::Capture, merchant_txn_id),
report_group,
cnp_txn_id: item.router_data.request.connector_transaction_id.clone(),
amount: item.amount,
@@ -1066,19 +1092,14 @@ impl<F> TryFrom<&WorldpayvantivRouterData<&RefundsRouterData<F>>> for CnpOnlineR
"Failed to obtain report_group from metadata".to_string(),
),
)?;
-
- let customer_id = item
- .router_data
- .customer_id
- .clone()
- .map(|customer_id| customer_id.get_string_repr().to_string());
+ let customer_id = extract_customer_id(item.router_data);
+ let merchant_txn_id = get_valid_transaction_id(
+ item.router_data.connector_request_reference_id.clone(),
+ "credit.id",
+ )?;
let credit = Some(RefundRequest {
- id: format!(
- "{}_{}",
- OperationId::Refund,
- item.router_data.connector_request_reference_id
- ),
+ id: format!("{}_{merchant_txn_id}", OperationId::Refund),
report_group,
customer_id,
cnp_txn_id: item.router_data.request.connector_transaction_id.clone(),
@@ -1443,11 +1464,10 @@ where
}
}
-fn get_vantiv_customer_reference(customer_id: &Option<CustomerId>) -> Option<String> {
- customer_id.as_ref().and_then(|id| {
- let customer_id_str = id.get_string_repr().to_string();
- if customer_id_str.len() <= worldpayvantiv_constants::CUSTOMER_REFERENCE_MAX_LENGTH {
- Some(customer_id_str)
+fn get_vantiv_customer_reference(customer_id: &Option<String>) -> Option<String> {
+ customer_id.clone().and_then(|id| {
+ if id.len() <= worldpayvantiv_constants::CUSTOMER_REFERENCE_MAX_LENGTH {
+ Some(id)
} else {
None
}
@@ -1547,7 +1567,7 @@ impl<F> TryFrom<ResponseRouterData<F, CnpOnlineResponse, PaymentsCancelData, Pay
reason: Some(item.response.message.clone()),
status_code: item.http_code,
attempt_status: None,
- connector_transaction_id: None,
+ connector_transaction_id: None, // Transaction id not created at connector
network_advice_code: None,
network_decline_code,
network_error_message,
@@ -1739,12 +1759,12 @@ impl TryFrom<&PaymentsCancelRouterData> for CnpOnlineRequest {
"Failed to obtain report_group from metadata".to_string(),
),
)?;
+ let merchant_txn_id = get_valid_transaction_id(
+ item.connector_request_reference_id.clone(),
+ "authReversal.id",
+ )?;
let auth_reversal = Some(AuthReversal {
- id: format!(
- "{}_{}",
- OperationId::Void,
- item.connector_request_reference_id
- ),
+ id: format!("{}_{merchant_txn_id}", OperationId::Void),
report_group,
cnp_txn_id: item.request.connector_transaction_id.clone(),
});
@@ -1780,12 +1800,10 @@ impl TryFrom<&PaymentsCancelPostCaptureRouterData> for CnpOnlineRequest {
"Failed to obtain report_group from metadata".to_string(),
),
)?;
+ let merchant_txn_id =
+ get_valid_transaction_id(item.connector_request_reference_id.clone(), "void.id")?;
let void = Some(Void {
- id: format!(
- "{}_{}",
- OperationId::VoidPC,
- item.connector_request_reference_id
- ),
+ id: format!("{}_{merchant_txn_id}", OperationId::VoidPC,),
report_group,
cnp_txn_id: item.request.connector_transaction_id.clone(),
});
@@ -1847,7 +1865,7 @@ impl<F>
reason: Some(sale_response.message.clone()),
status_code: item.http_code,
attempt_status: None,
- connector_transaction_id: Some(sale_response.order_id.clone()),
+ connector_transaction_id: Some(sale_response.cnp_txn_id.clone()),
network_advice_code: None,
network_decline_code,
network_error_message,
@@ -1915,7 +1933,7 @@ impl<F>
reason: Some(auth_response.message.clone()),
status_code: item.http_code,
attempt_status: None,
- connector_transaction_id: Some(auth_response.order_id.clone()),
+ connector_transaction_id: Some(auth_response.cnp_txn_id.clone()),
network_advice_code: None,
network_decline_code,
network_error_message,
@@ -1968,7 +1986,7 @@ impl<F>
reason: Some(item.response.message.clone()),
status_code: item.http_code,
attempt_status: None,
- connector_transaction_id: None,
+ connector_transaction_id: None, // Transaction id not created at connector
network_advice_code: None,
network_decline_code: None,
network_error_message: None,
diff --git a/crates/hyperswitch_connectors/src/utils.rs b/crates/hyperswitch_connectors/src/utils.rs
index e273e499c3..1500da7656 100644
--- a/crates/hyperswitch_connectors/src/utils.rs
+++ b/crates/hyperswitch_connectors/src/utils.rs
@@ -499,6 +499,7 @@ pub trait RouterData {
fn is_three_ds(&self) -> bool;
fn get_payment_method_token(&self) -> Result<PaymentMethodToken, Error>;
fn get_customer_id(&self) -> Result<id_type::CustomerId, Error>;
+ fn get_optional_customer_id(&self) -> Option<id_type::CustomerId>;
fn get_connector_customer_id(&self) -> Result<String, Error>;
fn get_preprocessing_id(&self) -> Result<String, Error>;
fn get_recurring_mandate_payment_data(&self) -> Result<RecurringMandatePaymentData, Error>;
@@ -1026,6 +1027,10 @@ impl<Flow, Request, Response> RouterData
fn get_optional_l2_l3_data(&self) -> Option<L2L3Data> {
self.l2_l3_data.clone()
}
+
+ fn get_optional_customer_id(&self) -> Option<id_type::CustomerId> {
+ self.customer_id.clone()
+ }
}
pub trait AccessTokenRequestInfo {