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

feat(connector): [Worldpayxml] add payout webhook impl for worldpay wpg (#10346)

Sakil Mostak1628032263b1
diff --git a/crates/hyperswitch_connectors/src/connectors/worldpayxml.rs b/crates/hyperswitch_connectors/src/connectors/worldpayxml.rs
index 2040fce298..b828765c05 100644
--- a/crates/hyperswitch_connectors/src/connectors/worldpayxml.rs
+++ b/crates/hyperswitch_connectors/src/connectors/worldpayxml.rs
@@ -8,7 +8,9 @@ use common_utils::{
request::{Method, Request, RequestBuilder, RequestContent},
types::{AmountConvertor, StringMinorUnit, StringMinorUnitForConnector},
};
-use error_stack::{report, ResultExt};
+#[cfg(not(feature = "payouts"))]
+use error_stack::report;
+use error_stack::ResultExt;
use hyperswitch_domain_models::{
payment_method_data::PaymentMethodData,
router_data::{AccessToken, ConnectorAuthType, ErrorResponse, RouterData},
@@ -945,25 +947,76 @@ impl ConnectorIntegration<PoCancel, PayoutsData, PayoutsResponseData> for Worldp
#[async_trait::async_trait]
impl webhooks::IncomingWebhook for Worldpayxml {
- fn get_webhook_object_reference_id(
+ #[cfg(feature = "payouts")]
+ async fn verify_webhook_source(
&self,
_request: &webhooks::IncomingWebhookRequestDetails<'_>,
+ _merchant_id: &common_utils::id_type::MerchantId,
+ _connector_webhook_details: Option<common_utils::pii::SecretSerdeValue>,
+ _connector_account_details: common_utils::crypto::Encryptable<
+ masking::Secret<serde_json::Value>,
+ >,
+ _connector_label: &str,
+ ) -> CustomResult<bool, errors::ConnectorError> {
+ // Bypass Source Verification since it is done via MTLS
+ Ok(true)
+ }
+
+ fn get_webhook_object_reference_id(
+ &self,
+ #[cfg(feature = "payouts")] request: &webhooks::IncomingWebhookRequestDetails<'_>,
+ #[cfg(not(feature = "payouts"))] _request: &webhooks::IncomingWebhookRequestDetails<'_>,
) -> CustomResult<api_models::webhooks::ObjectReferenceId, errors::ConnectorError> {
- Err(report!(errors::ConnectorError::WebhooksNotImplemented))
+ #[cfg(feature = "payouts")]
+ {
+ let body: worldpayxml::WorldpayXmlWebhookBody =
+ utils::deserialize_xml_to_struct(request.body)?;
+ Ok(api_models::webhooks::ObjectReferenceId::PayoutId(
+ api_models::webhooks::PayoutIdType::ConnectorPayoutId(
+ body.notify.order_status_event.order_code,
+ ),
+ ))
+ }
+ #[cfg(not(feature = "payouts"))]
+ {
+ Err(report!(errors::ConnectorError::WebhooksNotImplemented))
+ }
}
fn get_webhook_event_type(
&self,
- _request: &webhooks::IncomingWebhookRequestDetails<'_>,
+ #[cfg(feature = "payouts")] request: &webhooks::IncomingWebhookRequestDetails<'_>,
+ #[cfg(not(feature = "payouts"))] _request: &webhooks::IncomingWebhookRequestDetails<'_>,
) -> CustomResult<api_models::webhooks::IncomingWebhookEvent, errors::ConnectorError> {
- Err(report!(errors::ConnectorError::WebhooksNotImplemented))
+ #[cfg(feature = "payouts")]
+ {
+ let body: worldpayxml::WorldpayXmlWebhookBody =
+ utils::deserialize_xml_to_struct(request.body)?;
+ Ok(worldpayxml::get_payout_webhook_event(
+ body.notify.order_status_event.payment.last_event,
+ ))
+ }
+ #[cfg(not(feature = "payouts"))]
+ {
+ Err(report!(errors::ConnectorError::WebhooksNotImplemented))
+ }
}
fn get_webhook_resource_object(
&self,
- _request: &webhooks::IncomingWebhookRequestDetails<'_>,
+ #[cfg(feature = "payouts")] request: &webhooks::IncomingWebhookRequestDetails<'_>,
+ #[cfg(not(feature = "payouts"))] _request: &webhooks::IncomingWebhookRequestDetails<'_>,
) -> CustomResult<Box<dyn masking::ErasedMaskSerialize>, errors::ConnectorError> {
- Err(report!(errors::ConnectorError::WebhooksNotImplemented))
+ #[cfg(feature = "payouts")]
+ {
+ let body: worldpayxml::WorldpayXmlWebhookBody =
+ utils::deserialize_xml_to_struct(request.body)?;
+ Ok(Box::new(body))
+ }
+ #[cfg(not(feature = "payouts"))]
+ {
+ Err(report!(errors::ConnectorError::WebhooksNotImplemented))
+ }
}
}
diff --git a/crates/hyperswitch_connectors/src/connectors/worldpayxml/transformers.rs b/crates/hyperswitch_connectors/src/connectors/worldpayxml/transformers.rs
index 83a4dd4f8b..9622f4f112 100644
--- a/crates/hyperswitch_connectors/src/connectors/worldpayxml/transformers.rs
+++ b/crates/hyperswitch_connectors/src/connectors/worldpayxml/transformers.rs
@@ -195,10 +195,10 @@ struct OrderStatus {
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
-struct Payment {
+pub struct Payment {
payment_method: String,
amount: WorldpayXmlAmount,
- last_event: LastEvent,
+ pub last_event: LastEvent,
#[serde(rename = "AuthorisationId")]
authorisation_id: Option<AuthorisationId>,
scheme_response: Option<SchemeResponse>,
@@ -270,7 +270,7 @@ struct AuthorisationId {
#[derive(Debug, Clone, Copy, Deserialize, Serialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
-enum LastEvent {
+pub enum LastEvent {
Authorised,
Refused,
Cancelled,
@@ -278,9 +278,11 @@ enum LastEvent {
Settled,
SentForAuthorisation,
SentForRefund,
+ SentForFastRefund,
Refunded,
RefundRequested,
RefundFailed,
+ RefundedByMerchant,
}
#[derive(Debug, Deserialize, Serialize)]
@@ -739,6 +741,8 @@ fn get_attempt_status(
LastEvent::Refunded
| LastEvent::SentForRefund
| LastEvent::RefundRequested
+ | LastEvent::SentForFastRefund
+ | LastEvent::RefundedByMerchant
| LastEvent::RefundFailed => Err(errors::ConnectorError::UnexpectedResponseError(
bytes::Bytes::from("Invalid LastEvent".to_string()),
)),
@@ -748,7 +752,10 @@ fn get_attempt_status(
fn get_refund_status(last_event: LastEvent) -> Result<enums::RefundStatus, errors::ConnectorError> {
match last_event {
LastEvent::Refunded => Ok(enums::RefundStatus::Success),
- LastEvent::SentForRefund | LastEvent::RefundRequested => Ok(enums::RefundStatus::Pending),
+ LastEvent::SentForRefund
+ | LastEvent::RefundRequested
+ | LastEvent::SentForFastRefund
+ | LastEvent::RefundedByMerchant => Ok(enums::RefundStatus::Pending),
LastEvent::RefundFailed => Ok(enums::RefundStatus::Failure),
LastEvent::Captured | LastEvent::Settled => Ok(enums::RefundStatus::Pending),
LastEvent::Authorised
@@ -1629,3 +1636,49 @@ impl CardAddress {
}
}
}
+
+#[derive(Debug, Serialize, Deserialize)]
+#[serde(rename = "paymentService")]
+pub struct WorldpayXmlWebhookBody {
+ #[serde(rename = "@version")]
+ pub version: String,
+ #[serde(rename = "@merchantCode")]
+ pub merchant_code: Secret<String>,
+ pub notify: Notify,
+}
+
+#[derive(Debug, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct Notify {
+ pub order_status_event: OrderStatusEvent,
+}
+
+#[derive(Debug, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct OrderStatusEvent {
+ #[serde(rename = "@orderCode")]
+ pub order_code: String,
+ pub payment: Payment,
+}
+
+pub fn get_payout_webhook_event(status: LastEvent) -> api_models::webhooks::IncomingWebhookEvent {
+ match status {
+ LastEvent::SentForRefund
+ | LastEvent::RefundedByMerchant
+ | LastEvent::SentForFastRefund
+ | LastEvent::RefundRequested => {
+ api_models::webhooks::IncomingWebhookEvent::PayoutProcessing
+ }
+ LastEvent::Refunded => api_models::webhooks::IncomingWebhookEvent::PayoutSuccess,
+ LastEvent::Cancelled => api_models::webhooks::IncomingWebhookEvent::PayoutCancelled,
+ LastEvent::Refused | LastEvent::RefundFailed => {
+ api_models::webhooks::IncomingWebhookEvent::PayoutFailure
+ }
+ LastEvent::Authorised
+ | LastEvent::Settled
+ | LastEvent::Captured
+ | LastEvent::SentForAuthorisation => {
+ api_models::webhooks::IncomingWebhookEvent::EventNotSupported
+ }
+ }
+}