From 5c2232b737f5430a68fdf6cba9aa5f4c1d6cf3e2 Mon Sep 17 00:00:00 2001 From: ItsMeShashank Date: Wed, 7 Jun 2023 12:54:18 +0530 Subject: [PATCH] fix(router): hotfixes for stripe webhook event mapping and reference id retrieval (#1368) --- crates/router/src/connector/stripe.rs | 40 ++++++++----- .../src/connector/stripe/transformers.rs | 58 +++++++++++++++++-- 2 files changed, 78 insertions(+), 20 deletions(-) diff --git a/crates/router/src/connector/stripe.rs b/crates/router/src/connector/stripe.rs index 0aea295b17..12d38fc1c2 100644 --- a/crates/router/src/connector/stripe.rs +++ b/crates/router/src/connector/stripe.rs @@ -1666,9 +1666,15 @@ impl api::IncomingWebhook for Stripe { .change_context(errors::ConnectorError::WebhookReferenceIdNotFound)?; Ok(match details.event_data.event_object.object { - stripe::WebhookEventObjectType::PaymentIntent - | stripe::WebhookEventObjectType::Charge - | stripe::WebhookEventObjectType::Dispute => { + stripe::WebhookEventObjectType::PaymentIntent => { + api_models::webhooks::ObjectReferenceId::PaymentId( + api_models::payments::PaymentIdType::ConnectorTransactionId( + details.event_data.event_object.id, + ), + ) + } + + stripe::WebhookEventObjectType::Charge | stripe::WebhookEventObjectType::Dispute => { api_models::webhooks::ObjectReferenceId::PaymentId( api_models::payments::PaymentIdType::ConnectorTransactionId( details @@ -1693,7 +1699,7 @@ impl api::IncomingWebhook for Stripe { &self, request: &api::IncomingWebhookRequestDetails<'_>, ) -> CustomResult { - let details: stripe::WebhookEvent = request + let details: stripe::WebhookEventTypeBody = request .body .parse_struct("WebhookEvent") .change_context(errors::ConnectorError::WebhookReferenceIdNotFound)?; @@ -1705,21 +1711,27 @@ impl api::IncomingWebhook for Stripe { stripe::WebhookEventType::PaymentIntentSucceed => { api::IncomingWebhookEvent::PaymentIntentSuccess } + stripe::WebhookEventType::ChargeSucceeded => { + if let Some(stripe::WebhookPaymentMethodDetails { + payment_method: stripe::WebhookPaymentMethodType::AchCreditTransfer, + }) = details.event_data.event_object.payment_method_details + { + api::IncomingWebhookEvent::PaymentIntentSuccess + } else { + api::IncomingWebhookEvent::EventNotSupported + } + } stripe::WebhookEventType::SourceChargeable => { api::IncomingWebhookEvent::SourceChargeable } - stripe::WebhookEventType::ChargeSucceeded => { - api::IncomingWebhookEvent::PaymentIntentSuccess - } stripe::WebhookEventType::DisputeCreated => api::IncomingWebhookEvent::DisputeOpened, stripe::WebhookEventType::DisputeClosed => api::IncomingWebhookEvent::DisputeCancelled, - stripe::WebhookEventType::DisputeUpdated => api::IncomingWebhookEvent::try_from( - details - .event_data - .event_object - .status - .ok_or(errors::ConnectorError::WebhookEventTypeNotFound)?, - )?, + stripe::WebhookEventType::DisputeUpdated => details + .event_data + .event_object + .status + .map(Into::into) + .unwrap_or(api::IncomingWebhookEvent::EventNotSupported), stripe::WebhookEventType::PaymentIntentPartiallyFunded => { api::IncomingWebhookEvent::PaymentIntentPartiallyFunded } diff --git a/crates/router/src/connector/stripe/transformers.rs b/crates/router/src/connector/stripe/transformers.rs index 911cd9f593..caa695837e 100644 --- a/crates/router/src/connector/stripe/transformers.rs +++ b/crates/router/src/connector/stripe/transformers.rs @@ -566,17 +566,27 @@ pub enum StripeBankNames { Boz, } -impl TryFrom for api_models::webhooks::IncomingWebhookEvent { - type Error = errors::ConnectorError; - fn try_from(value: WebhookEventStatus) -> Result { - Ok(match value { +impl From for api_models::webhooks::IncomingWebhookEvent { + fn from(value: WebhookEventStatus) -> Self { + match value { WebhookEventStatus::WarningNeedsResponse => Self::DisputeOpened, WebhookEventStatus::WarningClosed => Self::DisputeCancelled, WebhookEventStatus::WarningUnderReview => Self::DisputeChallenged, WebhookEventStatus::Won => Self::DisputeWon, WebhookEventStatus::Lost => Self::DisputeLost, - _ => Err(errors::ConnectorError::WebhookEventTypeNotFound)?, - }) + WebhookEventStatus::NeedsResponse + | WebhookEventStatus::UnderReview + | WebhookEventStatus::ChargeRefunded + | WebhookEventStatus::Succeeded + | WebhookEventStatus::RequiresPaymentMethod + | WebhookEventStatus::RequiresConfirmation + | WebhookEventStatus::RequiresAction + | WebhookEventStatus::Processing + | WebhookEventStatus::RequiresCapture + | WebhookEventStatus::Canceled + | WebhookEventStatus::Chargeable + | WebhookEventStatus::Unknown => Self::EventNotSupported, + } } } @@ -2297,12 +2307,46 @@ pub struct WebhookEvent { pub event_data: WebhookEventData, } +#[derive(Debug, Deserialize)] +pub struct WebhookEventTypeBody { + #[serde(rename = "type")] + pub event_type: WebhookEventType, + #[serde(rename = "data")] + pub event_data: WebhookStatusData, +} + #[derive(Debug, Deserialize)] pub struct WebhookEventData { #[serde(rename = "object")] pub event_object: WebhookEventObjectData, } +#[derive(Debug, Deserialize)] +pub struct WebhookStatusData { + #[serde(rename = "object")] + pub event_object: WebhookStatusObjectData, +} + +#[derive(Debug, Deserialize)] +pub struct WebhookStatusObjectData { + pub status: Option, + pub payment_method_details: Option, +} + +#[derive(Debug, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum WebhookPaymentMethodType { + AchCreditTransfer, + #[serde(other)] + Unknown, +} + +#[derive(Debug, Deserialize)] +pub struct WebhookPaymentMethodDetails { + #[serde(rename = "type")] + pub payment_method: WebhookPaymentMethodType, +} + #[derive(Debug, Deserialize)] pub struct WebhookEventObjectData { pub id: String, @@ -2397,6 +2441,8 @@ pub enum WebhookEventStatus { RequiresCapture, Canceled, Chargeable, + #[serde(other)] + Unknown, } #[derive(Debug, Deserialize, PartialEq)]