From 92f7918e6f98460fb739d50b908ae33fda2f80b8 Mon Sep 17 00:00:00 2001 From: DEEPANSHU BANSAL <41580413+deepanshu-iiitu@users.noreply.github.com> Date: Fri, 13 Oct 2023 16:35:00 +0530 Subject: [PATCH] feat(router): Add Cancel Event in Webhooks and Mapping it in Stripe (#2573) --- crates/api_models/src/webhooks.rs | 4 +++- crates/common_enums/src/enums.rs | 1 + crates/router/src/compatibility/stripe/webhooks.rs | 1 + crates/router/src/connector/stripe.rs | 4 +++- crates/router/src/core/webhooks/utils.rs | 1 + crates/router/src/types/transformers.rs | 4 ++-- .../down.sql | 2 ++ .../2023-10-13-100447_add-payment-cancelled-event-type/up.sql | 2 ++ openapi/openapi_spec.json | 1 + 9 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 migrations/2023-10-13-100447_add-payment-cancelled-event-type/down.sql create mode 100644 migrations/2023-10-13-100447_add-payment-cancelled-event-type/up.sql diff --git a/crates/api_models/src/webhooks.rs b/crates/api_models/src/webhooks.rs index da5d8a6567d7..bc8e75f6d479 100644 --- a/crates/api_models/src/webhooks.rs +++ b/crates/api_models/src/webhooks.rs @@ -12,6 +12,7 @@ pub enum IncomingWebhookEvent { PaymentIntentSuccess, PaymentIntentProcessing, PaymentIntentPartiallyFunded, + PaymentIntentCancelled, PaymentActionRequired, EventNotSupported, SourceChargeable, @@ -84,7 +85,8 @@ impl From for WebhookFlow { | IncomingWebhookEvent::PaymentIntentSuccess | IncomingWebhookEvent::PaymentIntentProcessing | IncomingWebhookEvent::PaymentActionRequired - | IncomingWebhookEvent::PaymentIntentPartiallyFunded => Self::Payment, + | IncomingWebhookEvent::PaymentIntentPartiallyFunded + | IncomingWebhookEvent::PaymentIntentCancelled => Self::Payment, IncomingWebhookEvent::EventNotSupported => Self::ReturnResponse, IncomingWebhookEvent::RefundSuccess | IncomingWebhookEvent::RefundFailure => { Self::Refund diff --git a/crates/common_enums/src/enums.rs b/crates/common_enums/src/enums.rs index 39e18483963e..a44725bc91be 100644 --- a/crates/common_enums/src/enums.rs +++ b/crates/common_enums/src/enums.rs @@ -794,6 +794,7 @@ pub enum EventType { PaymentSucceeded, PaymentFailed, PaymentProcessing, + PaymentCancelled, ActionRequired, RefundSucceeded, RefundFailed, diff --git a/crates/router/src/compatibility/stripe/webhooks.rs b/crates/router/src/compatibility/stripe/webhooks.rs index 1b5f8969d56b..c44e265a9657 100644 --- a/crates/router/src/compatibility/stripe/webhooks.rs +++ b/crates/router/src/compatibility/stripe/webhooks.rs @@ -168,6 +168,7 @@ fn get_stripe_event_type(event_type: api_models::enums::EventType) -> &'static s api_models::enums::EventType::PaymentSucceeded => "payment_intent.succeeded", api_models::enums::EventType::PaymentFailed => "payment_intent.payment_failed", api_models::enums::EventType::PaymentProcessing => "payment_intent.processing", + api_models::enums::EventType::PaymentCancelled => "payment_intent.canceled", // the below are not really stripe compatible because stripe doesn't provide this api_models::enums::EventType::ActionRequired => "action.required", diff --git a/crates/router/src/connector/stripe.rs b/crates/router/src/connector/stripe.rs index ff8988254305..59700ecf353a 100644 --- a/crates/router/src/connector/stripe.rs +++ b/crates/router/src/connector/stripe.rs @@ -1844,6 +1844,9 @@ impl api::IncomingWebhook for Stripe { stripe::WebhookEventType::PaymentIntentSucceed => { api::IncomingWebhookEvent::PaymentIntentSuccess } + stripe::WebhookEventType::PaymentIntentCanceled => { + api::IncomingWebhookEvent::PaymentIntentCancelled + } stripe::WebhookEventType::ChargeSucceeded => { if let Some(stripe::WebhookPaymentMethodDetails { payment_method: @@ -1898,7 +1901,6 @@ impl api::IncomingWebhook for Stripe { | stripe::WebhookEventType::ChargePending | stripe::WebhookEventType::ChargeUpdated | stripe::WebhookEventType::ChargeRefunded - | stripe::WebhookEventType::PaymentIntentCanceled | stripe::WebhookEventType::PaymentIntentCreated | stripe::WebhookEventType::PaymentIntentProcessing | stripe::WebhookEventType::PaymentIntentAmountCapturableUpdated diff --git a/crates/router/src/core/webhooks/utils.rs b/crates/router/src/core/webhooks/utils.rs index ac7e5081c823..b187001e10e6 100644 --- a/crates/router/src/core/webhooks/utils.rs +++ b/crates/router/src/core/webhooks/utils.rs @@ -17,6 +17,7 @@ fn default_webhook_config() -> api::MerchantWebhookConfig { api::IncomingWebhookEvent::PaymentIntentSuccess, api::IncomingWebhookEvent::PaymentIntentFailure, api::IncomingWebhookEvent::PaymentIntentProcessing, + api::IncomingWebhookEvent::PaymentIntentCancelled, api::IncomingWebhookEvent::PaymentActionRequired, api::IncomingWebhookEvent::RefundSuccess, ]) diff --git a/crates/router/src/types/transformers.rs b/crates/router/src/types/transformers.rs index 8686729ea2bb..31ec31cfa988 100644 --- a/crates/router/src/types/transformers.rs +++ b/crates/router/src/types/transformers.rs @@ -257,8 +257,8 @@ impl ForeignFrom for Option { | api_enums::IntentStatus::RequiresCustomerAction => { Some(storage_enums::EventType::ActionRequired) } - api_enums::IntentStatus::Cancelled - | api_enums::IntentStatus::RequiresPaymentMethod + api_enums::IntentStatus::Cancelled => Some(storage_enums::EventType::PaymentCancelled), + api_enums::IntentStatus::RequiresPaymentMethod | api_enums::IntentStatus::RequiresConfirmation | api_enums::IntentStatus::RequiresCapture | api_enums::IntentStatus::PartiallyCaptured => None, diff --git a/migrations/2023-10-13-100447_add-payment-cancelled-event-type/down.sql b/migrations/2023-10-13-100447_add-payment-cancelled-event-type/down.sql new file mode 100644 index 000000000000..87e4e81e047a --- /dev/null +++ b/migrations/2023-10-13-100447_add-payment-cancelled-event-type/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +Select 1; \ No newline at end of file diff --git a/migrations/2023-10-13-100447_add-payment-cancelled-event-type/up.sql b/migrations/2023-10-13-100447_add-payment-cancelled-event-type/up.sql new file mode 100644 index 000000000000..5854491a6ecb --- /dev/null +++ b/migrations/2023-10-13-100447_add-payment-cancelled-event-type/up.sql @@ -0,0 +1,2 @@ +-- Your SQL goes here +ALTER TYPE "EventType" ADD VALUE 'payment_cancelled'; \ No newline at end of file diff --git a/openapi/openapi_spec.json b/openapi/openapi_spec.json index 971a573b8e4b..3f1f16803379 100644 --- a/openapi/openapi_spec.json +++ b/openapi/openapi_spec.json @@ -5076,6 +5076,7 @@ "payment_succeeded", "payment_failed", "payment_processing", + "payment_cancelled", "action_required", "refund_succeeded", "refund_failed",