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): add dispute and refund webhooks for Airwallex (#1021)

Shankar Singh C8c3411413847
diff --git a/crates/router/src/connector/airwallex.rs b/crates/router/src/connector/airwallex.rs
index 3f7d4734b4..36fef0c5de 100644
--- a/crates/router/src/connector/airwallex.rs
+++ b/crates/router/src/connector/airwallex.rs
@@ -2,7 +2,7 @@ mod transformers;
use std::fmt::Debug;
-use common_utils::ext_traits::ByteSliceExt;
+use common_utils::ext_traits::{ByteSliceExt, ValueExt};
use error_stack::{IntoReport, ResultExt};
use transformers as airwallex;
@@ -964,9 +964,37 @@ impl api::IncomingWebhook for Airwallex {
.parse_struct("airwallexWebhookData")
.change_context(errors::ConnectorError::WebhookReferenceIdNotFound)?;
- Ok(api_models::webhooks::ObjectReferenceId::PaymentId(
- api_models::payments::PaymentIdType::ConnectorTransactionId(details.source_id),
- ))
+ if airwallex::is_transaction_event(&details.name) {
+ Ok(api_models::webhooks::ObjectReferenceId::PaymentId(
+ api_models::payments::PaymentIdType::ConnectorTransactionId(
+ details
+ .source_id
+ .ok_or(errors::ConnectorError::WebhookReferenceIdNotFound)?,
+ ),
+ ))
+ } else if airwallex::is_refund_event(&details.name) {
+ Ok(api_models::webhooks::ObjectReferenceId::RefundId(
+ api_models::webhooks::RefundIdType::ConnectorRefundId(
+ details
+ .source_id
+ .ok_or(errors::ConnectorError::WebhookReferenceIdNotFound)?,
+ ),
+ ))
+ } else if airwallex::is_dispute_event(&details.name) {
+ let dispute_details: airwallex::AirwallexDisputeObject = details
+ .data
+ .object
+ .parse_value("AirwallexDisputeObject")
+ .change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?;
+
+ Ok(api_models::webhooks::ObjectReferenceId::PaymentId(
+ api_models::payments::PaymentIdType::ConnectorTransactionId(
+ dispute_details.payment_intent_id,
+ ),
+ ))
+ } else {
+ Err(errors::ConnectorError::WebhookEventTypeNotFound).into_report()
+ }
}
fn get_webhook_event_type(
@@ -977,12 +1005,7 @@ impl api::IncomingWebhook for Airwallex {
.body
.parse_struct("airwallexWebhookData")
.change_context(errors::ConnectorError::WebhookReferenceIdNotFound)?;
-
- Ok(match details.name.as_str() {
- "payment_attempt.failed_to_process" => api::IncomingWebhookEvent::PaymentIntentFailure,
- "payment_attempt.authorized" => api::IncomingWebhookEvent::PaymentIntentSuccess,
- _ => Err(errors::ConnectorError::WebhookEventTypeNotFound).into_report()?,
- })
+ Ok(api::IncomingWebhookEvent::try_from(details.name)?)
}
fn get_webhook_resource_object(
@@ -996,6 +1019,33 @@ impl api::IncomingWebhook for Airwallex {
Ok(details.data.object)
}
+
+ fn get_dispute_details(
+ &self,
+ request: &api::IncomingWebhookRequestDetails<'_>,
+ ) -> CustomResult<api::disputes::DisputePayload, errors::ConnectorError> {
+ let details: airwallex::AirwallexWebhookData = request
+ .body
+ .parse_struct("airwallexWebhookData")
+ .change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?;
+ let dispute_details: airwallex::AirwallexDisputeObject = details
+ .data
+ .object
+ .parse_value("AirwallexDisputeObject")
+ .change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?;
+ Ok(api::disputes::DisputePayload {
+ amount: dispute_details.dispute_amount.to_string(),
+ currency: dispute_details.dispute_currency,
+ dispute_stage: api_models::enums::DisputeStage::from(dispute_details.stage.clone()),
+ connector_dispute_id: dispute_details.dispute_id,
+ connector_reason: dispute_details.dispute_reason_type,
+ connector_reason_code: dispute_details.dispute_original_reason_code,
+ challenge_required_by: None,
+ connector_status: dispute_details.status.to_string(),
+ created_at: dispute_details.created_at,
+ updated_at: dispute_details.updated_at,
+ })
+ }
}
impl services::ConnectorRedirectResponse for Airwallex {
diff --git a/crates/router/src/connector/airwallex/transformers.rs b/crates/router/src/connector/airwallex/transformers.rs
index ca3df4aff9..5d4c2ec71d 100644
--- a/crates/router/src/connector/airwallex/transformers.rs
+++ b/crates/router/src/connector/airwallex/transformers.rs
@@ -483,8 +483,128 @@ impl TryFrom<types::RefundsResponseRouterData<api::RSync, RefundResponse>>
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AirwallexWebhookData {
- pub source_id: String,
- pub name: String,
+ pub source_id: Option<String>,
+ pub name: AirwallexWebhookEventType,
+ pub data: AirwallexObjectData,
+}
+
+#[derive(Debug, Deserialize, strum::Display, PartialEq)]
+pub enum AirwallexWebhookEventType {
+ #[serde(rename = "payment_intent.created")]
+ PaymentIntentCreated,
+ #[serde(rename = "payment_intent.requires_payment_method")]
+ PaymentIntentRequiresPaymentMethod,
+ #[serde(rename = "payment_intent.cancelled")]
+ PaymentIntentCancelled,
+ #[serde(rename = "payment_intent.succeeded")]
+ PaymentIntentSucceeded,
+ #[serde(rename = "payment_intent.requires_capture")]
+ PaymentIntentRequiresCapture,
+ #[serde(rename = "payment_intent.requires_customer_action")]
+ PaymentIntentRequiresCustomerAction,
+ #[serde(rename = "payment_attempt.authorized")]
+ PaymentAttemptAuthorized,
+ #[serde(rename = "payment_attempt.authorization_failed")]
+ PaymentAttemptAuthorizationFailed,
+ #[serde(rename = "payment_attempt.capture_requested")]
+ PaymentAttemptCaptureRequested,
+ #[serde(rename = "payment_attempt.capture_failed")]
+ PaymentAttemptCaptureFailed,
+ #[serde(rename = "payment_attempt.authentication_redirected")]
+ PaymentAttemptAuthenticationRedirected,
+ #[serde(rename = "payment_attempt.authentication_failed")]
+ PaymentAttemptAuthenticationFailed,
+ #[serde(rename = "payment_attempt.failed_to_process")]
+ PaymentAttemptFailedToProcess,
+ #[serde(rename = "payment_attempt.cancelled")]
+ PaymentAttemptCancelled,
+ #[serde(rename = "payment_attempt.expired")]
+ PaymentAttemptExpired,
+ #[serde(rename = "payment_attempt.risk_declined")]
+ PaymentAttemptRiskDeclined,
+ #[serde(rename = "payment_attempt.settled")]
+ PaymentAttemptSettled,
+ #[serde(rename = "payment_attempt.paid")]
+ PaymentAttemptPaid,
+ #[serde(rename = "refund.received")]
+ RefundReceived,
+ #[serde(rename = "refund.accepted")]
+ RefundAccepted,
+ #[serde(rename = "refund.succeeded")]
+ RefundSucceeded,
+ #[serde(rename = "refund.failed")]
+ RefundFailed,
+ #[serde(rename = "dispute.rfi_responded_by_merchant")]
+ DisputeRfiRespondedByMerchant,
+ #[serde(rename = "dispute.dispute.pre_chargeback_accepted")]
+ DisputePreChargebackAccepted,
+ #[serde(rename = "dispute.accepted")]
+ DisputeAccepted,
+ #[serde(rename = "dispute.dispute_received_by_merchant")]
+ DisputeReceivedByMerchant,
+ #[serde(rename = "dispute.dispute_responded_by_merchant")]
+ DisputeRespondedByMerchant,
+ #[serde(rename = "dispute.won")]
+ DisputeWon,
+ #[serde(rename = "dispute.lost")]
+ DisputeLost,
+ #[serde(rename = "dispute.dispute_reversed")]
+ DisputeReversed,
+}
+
+pub fn is_transaction_event(event_code: &AirwallexWebhookEventType) -> bool {
+ matches!(
+ event_code,
+ AirwallexWebhookEventType::PaymentAttemptFailedToProcess
+ | AirwallexWebhookEventType::PaymentAttemptAuthorized
+ )
+}
+
+pub fn is_refund_event(event_code: &AirwallexWebhookEventType) -> bool {
+ matches!(
+ event_code,
+ AirwallexWebhookEventType::RefundSucceeded | AirwallexWebhookEventType::RefundFailed
+ )
+}
+
+pub fn is_dispute_event(event_code: &AirwallexWebhookEventType) -> bool {
+ matches!(
+ event_code,
+ AirwallexWebhookEventType::DisputeAccepted
+ | AirwallexWebhookEventType::DisputePreChargebackAccepted
+ | AirwallexWebhookEventType::DisputeRespondedByMerchant
+ | AirwallexWebhookEventType::DisputeWon
+ | AirwallexWebhookEventType::DisputeLost
+ )
+}
+
+#[derive(Debug, Deserialize)]
+pub struct AirwallexObjectData {
+ pub object: serde_json::Value,
+}
+
+#[derive(Debug, Deserialize)]
+pub struct AirwallexDisputeObject {
+ pub payment_intent_id: String,
+ pub dispute_amount: i64,
+ pub dispute_currency: String,
+ pub stage: AirwallexDisputeStage,
+ pub dispute_id: String,
+ pub dispute_reason_type: Option<String>,
+ pub dispute_original_reason_code: Option<String>,
+ pub status: String,
+ #[serde(with = "common_utils::custom_serde::iso8601::option")]
+ pub created_at: Option<PrimitiveDateTime>,
+ #[serde(with = "common_utils::custom_serde::iso8601::option")]
+ pub updated_at: Option<PrimitiveDateTime>,
+}
+
+#[derive(Debug, Deserialize, strum::Display, Clone)]
+#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
+pub enum AirwallexDisputeStage {
+ Rfi,
+ Dispute,
+ Arbitration,
}
#[derive(Debug, Deserialize)]
@@ -503,3 +623,33 @@ pub struct AirwallexErrorResponse {
pub message: String,
pub source: Option<String>,
}
+
+impl TryFrom<AirwallexWebhookEventType> for api_models::webhooks::IncomingWebhookEvent {
+ type Error = errors::ConnectorError;
+ fn try_from(value: AirwallexWebhookEventType) -> Result<Self, Self::Error> {
+ Ok(match value {
+ AirwallexWebhookEventType::PaymentAttemptFailedToProcess => Self::PaymentIntentFailure,
+ AirwallexWebhookEventType::PaymentAttemptAuthorized => Self::PaymentIntentSuccess,
+ AirwallexWebhookEventType::RefundSucceeded => Self::RefundSuccess,
+ AirwallexWebhookEventType::RefundFailed => Self::RefundFailure,
+ AirwallexWebhookEventType::DisputeAccepted
+ | AirwallexWebhookEventType::DisputePreChargebackAccepted => Self::DisputeAccepted,
+ AirwallexWebhookEventType::DisputeRespondedByMerchant => Self::DisputeChallenged,
+ AirwallexWebhookEventType::DisputeWon | AirwallexWebhookEventType::DisputeReversed => {
+ Self::DisputeWon
+ }
+ AirwallexWebhookEventType::DisputeLost => Self::DisputeLost,
+ _ => Err(errors::ConnectorError::WebhookEventTypeNotFound)?,
+ })
+ }
+}
+
+impl From<AirwallexDisputeStage> for api_models::enums::DisputeStage {
+ fn from(code: AirwallexDisputeStage) -> Self {
+ match code {
+ AirwallexDisputeStage::Rfi => Self::PreDispute,
+ AirwallexDisputeStage::Dispute => Self::Dispute,
+ AirwallexDisputeStage::Arbitration => Self::PreArbitration,
+ }
+ }
+}