Skip to content

Commit

Permalink
refactor: add a fn in PSyncData for fetching MandateReference and rem…
Browse files Browse the repository at this point in the history
…ove redundant code
  • Loading branch information
kashif-m committed Nov 6, 2024
1 parent 992e9d4 commit 394d075
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 40 deletions.
39 changes: 8 additions & 31 deletions crates/hyperswitch_connectors/src/connectors/worldpay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ mod requests;
mod response;
pub mod transformers;

use api_models::{
payments::{MandateReferenceId, PaymentIdType},
webhooks::IncomingWebhookEvent,
};
use api_models::{payments::PaymentIdType, webhooks::IncomingWebhookEvent};
use common_enums::{enums, PaymentAction};
use common_utils::{
crypto,
Expand All @@ -29,7 +26,7 @@ use hyperswitch_domain_models::{
PaymentsAuthorizeData, PaymentsCancelData, PaymentsCaptureData, PaymentsSessionData,
PaymentsSyncData, RefundsData, ResponseId, SetupMandateRequestData,
},
router_response_types::{MandateReference, PaymentsResponseData, RefundsResponseData},
router_response_types::{PaymentsResponseData, RefundsResponseData},
types::{
PaymentsAuthorizeRouterData, PaymentsCancelRouterData, PaymentsCaptureRouterData,
PaymentsCompleteAuthorizeRouterData, PaymentsSyncRouterData, RefundExecuteRouterData,
Expand All @@ -54,16 +51,18 @@ use requests::{
use response::{
EventType, ResponseIdStr, WorldpayErrorResponse, WorldpayEventResponse,
WorldpayPaymentsResponse, WorldpayWebhookEventType, WorldpayWebhookTransactionId,
WP_CORRELATION_ID,
};
use ring::hmac;
use transformers::{self as worldpay, WP_CORRELATION_ID};
use transformers::{self as worldpay};

use crate::{
constants::headers,
types::ResponseRouterData,
utils::{
construct_not_implemented_error_report, convert_amount, get_header_key_value,
is_mandate_supported, ForeignTryFrom, PaymentMethodDataType, RefundsRequestData,
is_mandate_supported, ForeignTryFrom, PaymentMethodDataType, PaymentsSyncRequestData,
RefundsRequestData,
},
};

Expand Down Expand Up @@ -525,29 +524,7 @@ impl ConnectorIntegration<PSync, PaymentsSyncData, PaymentsResponseData> for Wor
response: Ok(PaymentsResponseData::TransactionResponse {
resource_id: data.request.connector_transaction_id.clone(),
redirection_data: Box::new(None),
mandate_reference: Box::new(data.request.mandate_id.as_ref().and_then(
|mandate_ids| {
mandate_ids
.mandate_reference_id
.as_ref()
.and_then(|mandate_ref_id| match mandate_ref_id {
MandateReferenceId::ConnectorMandateId(connector_mandate_id) => {
Some(MandateReference {
connector_mandate_id: connector_mandate_id
.get_connector_mandate_id(),
payment_method_id: connector_mandate_id
.get_payment_method_id(),
mandate_metadata: connector_mandate_id
.get_mandate_metadata(),
connector_mandate_request_reference_id:
connector_mandate_id
.get_connector_mandate_request_reference_id(),
})
}
_ => None,
})
},
)),
mandate_reference: Box::new(data.request.get_connector_mandate_reference()),
connector_metadata: None,
network_txn_id: None,
connector_response_reference_id: optional_correlation_id,
Expand Down Expand Up @@ -874,7 +851,7 @@ impl ConnectorIntegration<CompleteAuthorize, CompleteAuthorizeData, PaymentsResp
router_env::logger::info!(connector_response=?response);
let optional_correlation_id = res.headers.and_then(|headers| {
headers
.get("WP-CorrelationId")
.get(WP_CORRELATION_ID)
.and_then(|header_value| header_value.to_str().ok())
.map(|id| id.to_string())
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,6 @@ pub struct WorldpayCompleteAuthorizationRequest {
#[serde(skip_serializing_if = "Option::is_none")]
pub collection_reference: Option<String>,
}

pub const THREE_DS_MODE: &str = "always";
pub const THREE_DS_TYPE: &str = "integrated";
Original file line number Diff line number Diff line change
Expand Up @@ -455,3 +455,6 @@ pub enum WorldpayWebhookStatus {
SentForRefund,
RefundFailed,
}

/// Worldpay's unique reference ID for a request
pub const WP_CORRELATION_ID: &str = "WP-CorrelationId";
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ impl<T> TryFrom<(&api::CurrencyUnit, enums::Currency, MinorUnit, T)> for Worldpa
}
}

/// Worldpay's unique reference ID for a request
pub const WP_CORRELATION_ID: &str = "WP-CorrelationId";

#[derive(Debug, Default, Serialize, Deserialize)]
pub struct WorldpayConnectorMetadataObject {
pub merchant_name: Option<Secret<String>>,
Expand Down Expand Up @@ -406,8 +403,8 @@ fn create_three_ds_request<T: WorldpayPaymentsRequestData>(
})?;

Ok(Some(ThreeDSRequest {
three_ds_type: "integrated".to_string(),
mode: "always".to_string(),
three_ds_type: THREE_DS_TYPE.to_string(),
mode: THREE_DS_MODE.to_string(),
device_data: ThreeDSRequestDeviceData {
accept_header,
user_agent_header,
Expand Down
20 changes: 20 additions & 0 deletions crates/hyperswitch_connectors/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use hyperswitch_domain_models::{
PaymentsCaptureData, PaymentsPreProcessingData, PaymentsSyncData, RefundsData, ResponseId,
SetupMandateRequestData,
},
router_response_types::MandateReference,
};
use hyperswitch_interfaces::{api, consts, errors, types::Response};
use image::Luma;
Expand Down Expand Up @@ -1341,6 +1342,7 @@ impl PaymentsCaptureRequestData for PaymentsCaptureData {

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

Expand All @@ -1352,6 +1354,24 @@ impl PaymentsSyncRequestData for PaymentsSyncData {
Some(_) => Err(errors::ConnectorError::CaptureMethodNotSupported.into()),
}
}
fn get_connector_mandate_reference(&self) -> Option<MandateReference> {
self.mandate_id.as_ref().and_then(|mandate_ids| {
mandate_ids.mandate_reference_id.as_ref().and_then(
|mandate_ref_id| match mandate_ref_id {
payments::MandateReferenceId::ConnectorMandateId(conn_mandate_id) => {
Some(MandateReference {
connector_mandate_id: conn_mandate_id.get_connector_mandate_id(),
payment_method_id: conn_mandate_id.get_payment_method_id(),
mandate_metadata: conn_mandate_id.get_mandate_metadata(),
connector_mandate_request_reference_id: conn_mandate_id
.get_connector_mandate_request_reference_id(),
})
}
_ => None,
},
)
})
}
fn get_connector_transaction_id(&self) -> CustomResult<String, errors::ConnectorError> {
match self.connector_transaction_id.clone() {
ResponseId::ConnectorTransactionId(txn_id) => Ok(txn_id),
Expand Down
3 changes: 0 additions & 3 deletions crates/router/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,3 @@ pub const VAULT_DELETE_FLOW_TYPE: &str = "delete_from_vault";
/// Vault Fingerprint fetch flow type
#[cfg(all(feature = "v2", feature = "payment_methods_v2"))]
pub const VAULT_GET_FINGERPRINT_FLOW_TYPE: &str = "get_fingerprint_vault";

/// Worldpay's unique reference ID for a request TODO: Move to hyperswitch_connectors/constants once Worldpay is moved to connectors crate
pub const WP_CORRELATION_ID: &str = "WP-CorrelationId";
1 change: 0 additions & 1 deletion crates/router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ pub mod headers {
pub const X_REDIRECT_URI: &str = "x-redirect-uri";
pub const X_TENANT_ID: &str = "x-tenant-id";
pub const X_CLIENT_SECRET: &str = "X-Client-Secret";
pub const X_WP_API_VERSION: &str = "WP-Api-Version";
}

pub mod pii {
Expand Down

0 comments on commit 394d075

Please sign in to comment.