Skip to content

Commit 394d075

Browse files
committed
refactor: add a fn in PSyncData for fetching MandateReference and remove redundant code
1 parent 992e9d4 commit 394d075

File tree

7 files changed

+36
-40
lines changed

7 files changed

+36
-40
lines changed

crates/hyperswitch_connectors/src/connectors/worldpay.rs

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ mod requests;
22
mod response;
33
pub mod transformers;
44

5-
use api_models::{
6-
payments::{MandateReferenceId, PaymentIdType},
7-
webhooks::IncomingWebhookEvent,
8-
};
5+
use api_models::{payments::PaymentIdType, webhooks::IncomingWebhookEvent};
96
use common_enums::{enums, PaymentAction};
107
use common_utils::{
118
crypto,
@@ -29,7 +26,7 @@ use hyperswitch_domain_models::{
2926
PaymentsAuthorizeData, PaymentsCancelData, PaymentsCaptureData, PaymentsSessionData,
3027
PaymentsSyncData, RefundsData, ResponseId, SetupMandateRequestData,
3128
},
32-
router_response_types::{MandateReference, PaymentsResponseData, RefundsResponseData},
29+
router_response_types::{PaymentsResponseData, RefundsResponseData},
3330
types::{
3431
PaymentsAuthorizeRouterData, PaymentsCancelRouterData, PaymentsCaptureRouterData,
3532
PaymentsCompleteAuthorizeRouterData, PaymentsSyncRouterData, RefundExecuteRouterData,
@@ -54,16 +51,18 @@ use requests::{
5451
use response::{
5552
EventType, ResponseIdStr, WorldpayErrorResponse, WorldpayEventResponse,
5653
WorldpayPaymentsResponse, WorldpayWebhookEventType, WorldpayWebhookTransactionId,
54+
WP_CORRELATION_ID,
5755
};
5856
use ring::hmac;
59-
use transformers::{self as worldpay, WP_CORRELATION_ID};
57+
use transformers::{self as worldpay};
6058

6159
use crate::{
6260
constants::headers,
6361
types::ResponseRouterData,
6462
utils::{
6563
construct_not_implemented_error_report, convert_amount, get_header_key_value,
66-
is_mandate_supported, ForeignTryFrom, PaymentMethodDataType, RefundsRequestData,
64+
is_mandate_supported, ForeignTryFrom, PaymentMethodDataType, PaymentsSyncRequestData,
65+
RefundsRequestData,
6766
},
6867
};
6968

@@ -525,29 +524,7 @@ impl ConnectorIntegration<PSync, PaymentsSyncData, PaymentsResponseData> for Wor
525524
response: Ok(PaymentsResponseData::TransactionResponse {
526525
resource_id: data.request.connector_transaction_id.clone(),
527526
redirection_data: Box::new(None),
528-
mandate_reference: Box::new(data.request.mandate_id.as_ref().and_then(
529-
|mandate_ids| {
530-
mandate_ids
531-
.mandate_reference_id
532-
.as_ref()
533-
.and_then(|mandate_ref_id| match mandate_ref_id {
534-
MandateReferenceId::ConnectorMandateId(connector_mandate_id) => {
535-
Some(MandateReference {
536-
connector_mandate_id: connector_mandate_id
537-
.get_connector_mandate_id(),
538-
payment_method_id: connector_mandate_id
539-
.get_payment_method_id(),
540-
mandate_metadata: connector_mandate_id
541-
.get_mandate_metadata(),
542-
connector_mandate_request_reference_id:
543-
connector_mandate_id
544-
.get_connector_mandate_request_reference_id(),
545-
})
546-
}
547-
_ => None,
548-
})
549-
},
550-
)),
527+
mandate_reference: Box::new(data.request.get_connector_mandate_reference()),
551528
connector_metadata: None,
552529
network_txn_id: None,
553530
connector_response_reference_id: optional_correlation_id,
@@ -874,7 +851,7 @@ impl ConnectorIntegration<CompleteAuthorize, CompleteAuthorizeData, PaymentsResp
874851
router_env::logger::info!(connector_response=?response);
875852
let optional_correlation_id = res.headers.and_then(|headers| {
876853
headers
877-
.get("WP-CorrelationId")
854+
.get(WP_CORRELATION_ID)
878855
.and_then(|header_value| header_value.to_str().ok())
879856
.map(|id| id.to_string())
880857
});

crates/hyperswitch_connectors/src/connectors/worldpay/requests.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,3 +330,6 @@ pub struct WorldpayCompleteAuthorizationRequest {
330330
#[serde(skip_serializing_if = "Option::is_none")]
331331
pub collection_reference: Option<String>,
332332
}
333+
334+
pub const THREE_DS_MODE: &str = "always";
335+
pub const THREE_DS_TYPE: &str = "integrated";

crates/hyperswitch_connectors/src/connectors/worldpay/response.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,3 +455,6 @@ pub enum WorldpayWebhookStatus {
455455
SentForRefund,
456456
RefundFailed,
457457
}
458+
459+
/// Worldpay's unique reference ID for a request
460+
pub const WP_CORRELATION_ID: &str = "WP-CorrelationId";

crates/hyperswitch_connectors/src/connectors/worldpay/transformers.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ impl<T> TryFrom<(&api::CurrencyUnit, enums::Currency, MinorUnit, T)> for Worldpa
5252
}
5353
}
5454

55-
/// Worldpay's unique reference ID for a request
56-
pub const WP_CORRELATION_ID: &str = "WP-CorrelationId";
57-
5855
#[derive(Debug, Default, Serialize, Deserialize)]
5956
pub struct WorldpayConnectorMetadataObject {
6057
pub merchant_name: Option<Secret<String>>,
@@ -406,8 +403,8 @@ fn create_three_ds_request<T: WorldpayPaymentsRequestData>(
406403
})?;
407404

408405
Ok(Some(ThreeDSRequest {
409-
three_ds_type: "integrated".to_string(),
410-
mode: "always".to_string(),
406+
three_ds_type: THREE_DS_TYPE.to_string(),
407+
mode: THREE_DS_MODE.to_string(),
411408
device_data: ThreeDSRequestDeviceData {
412409
accept_header,
413410
user_agent_header,

crates/hyperswitch_connectors/src/utils.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use hyperswitch_domain_models::{
2626
PaymentsCaptureData, PaymentsPreProcessingData, PaymentsSyncData, RefundsData, ResponseId,
2727
SetupMandateRequestData,
2828
},
29+
router_response_types::MandateReference,
2930
};
3031
use hyperswitch_interfaces::{api, consts, errors, types::Response};
3132
use image::Luma;
@@ -1341,6 +1342,7 @@ impl PaymentsCaptureRequestData for PaymentsCaptureData {
13411342

13421343
pub trait PaymentsSyncRequestData {
13431344
fn is_auto_capture(&self) -> Result<bool, Error>;
1345+
fn get_connector_mandate_reference(&self) -> Option<MandateReference>;
13441346
fn get_connector_transaction_id(&self) -> CustomResult<String, errors::ConnectorError>;
13451347
}
13461348

@@ -1352,6 +1354,24 @@ impl PaymentsSyncRequestData for PaymentsSyncData {
13521354
Some(_) => Err(errors::ConnectorError::CaptureMethodNotSupported.into()),
13531355
}
13541356
}
1357+
fn get_connector_mandate_reference(&self) -> Option<MandateReference> {
1358+
self.mandate_id.as_ref().and_then(|mandate_ids| {
1359+
mandate_ids.mandate_reference_id.as_ref().and_then(
1360+
|mandate_ref_id| match mandate_ref_id {
1361+
payments::MandateReferenceId::ConnectorMandateId(conn_mandate_id) => {
1362+
Some(MandateReference {
1363+
connector_mandate_id: conn_mandate_id.get_connector_mandate_id(),
1364+
payment_method_id: conn_mandate_id.get_payment_method_id(),
1365+
mandate_metadata: conn_mandate_id.get_mandate_metadata(),
1366+
connector_mandate_request_reference_id: conn_mandate_id
1367+
.get_connector_mandate_request_reference_id(),
1368+
})
1369+
}
1370+
_ => None,
1371+
},
1372+
)
1373+
})
1374+
}
13551375
fn get_connector_transaction_id(&self) -> CustomResult<String, errors::ConnectorError> {
13561376
match self.connector_transaction_id.clone() {
13571377
ResponseId::ConnectorTransactionId(txn_id) => Ok(txn_id),

crates/router/src/consts.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,3 @@ pub const VAULT_DELETE_FLOW_TYPE: &str = "delete_from_vault";
180180
/// Vault Fingerprint fetch flow type
181181
#[cfg(all(feature = "v2", feature = "payment_methods_v2"))]
182182
pub const VAULT_GET_FINGERPRINT_FLOW_TYPE: &str = "get_fingerprint_vault";
183-
184-
/// Worldpay's unique reference ID for a request TODO: Move to hyperswitch_connectors/constants once Worldpay is moved to connectors crate
185-
pub const WP_CORRELATION_ID: &str = "WP-CorrelationId";

crates/router/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ pub mod headers {
8989
pub const X_REDIRECT_URI: &str = "x-redirect-uri";
9090
pub const X_TENANT_ID: &str = "x-tenant-id";
9191
pub const X_CLIENT_SECRET: &str = "X-Client-Secret";
92-
pub const X_WP_API_VERSION: &str = "WP-Api-Version";
9392
}
9493

9594
pub mod pii {

0 commit comments

Comments
 (0)