From f1d8eecb4cc67ec4ebca7e87fdb4d726805192b2 Mon Sep 17 00:00:00 2001 From: Sk Sakil Mostak Date: Fri, 5 Jan 2024 19:25:38 +0530 Subject: [PATCH 1/2] fix: handle deserialization issue of webhook body --- crates/router/src/connector/stripe.rs | 18 +++++++++++++++--- .../src/connector/stripe/transformers.rs | 6 +++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/crates/router/src/connector/stripe.rs b/crates/router/src/connector/stripe.rs index 65fd652629fb..789e5885f644 100644 --- a/crates/router/src/connector/stripe.rs +++ b/crates/router/src/connector/stripe.rs @@ -1861,7 +1861,11 @@ impl api::IncomingWebhook for Stripe { match details.event_data.event_object.metadata { // if order_id is present Some(meta_data) => api_models::webhooks::ObjectReferenceId::PaymentId( - api_models::payments::PaymentIdType::PaymentAttemptId(meta_data.order_id), + api_models::payments::PaymentIdType::PaymentAttemptId( + meta_data + .order_id + .ok_or(errors::ConnectorError::WebhookReferenceIdNotFound)?, + ), ), // else used connector_transaction_id None => api_models::webhooks::ObjectReferenceId::PaymentId( @@ -1875,7 +1879,11 @@ impl api::IncomingWebhook for Stripe { match details.event_data.event_object.metadata { // if order_id is present Some(meta_data) => api_models::webhooks::ObjectReferenceId::PaymentId( - api_models::payments::PaymentIdType::PaymentAttemptId(meta_data.order_id), + api_models::payments::PaymentIdType::PaymentAttemptId( + meta_data + .order_id + .ok_or(errors::ConnectorError::WebhookReferenceIdNotFound)?, + ), ), // else used connector_transaction_id None => api_models::webhooks::ObjectReferenceId::PaymentId( @@ -1915,7 +1923,11 @@ impl api::IncomingWebhook for Stripe { match meta_data.is_refund_id_as_reference { // if the order_id is refund_id Some(_) => api_models::webhooks::ObjectReferenceId::RefundId( - api_models::webhooks::RefundIdType::RefundId(meta_data.order_id), + api_models::webhooks::RefundIdType::RefundId( + meta_data.order_id.ok_or( + errors::ConnectorError::WebhookReferenceIdNotFound, + )?, + ), ), // if the order_id is payment_id // since payment_id was being passed before the deployment of this pr diff --git a/crates/router/src/connector/stripe/transformers.rs b/crates/router/src/connector/stripe/transformers.rs index 3a28f777907f..666fc146f580 100644 --- a/crates/router/src/connector/stripe/transformers.rs +++ b/crates/router/src/connector/stripe/transformers.rs @@ -135,7 +135,7 @@ pub struct PaymentIntentRequest { pub struct StripeMetadata { // merchant_reference_id #[serde(rename(serialize = "metadata[order_id]"))] - pub order_id: String, + pub order_id: Option, // to check whether the order_id is refund_id or payemnt_id // before deployment, order id is set to payemnt_id in refunds but now it is set as refund_id // it is set as string instead of bool because stripe pass it as string even if we set it as bool @@ -1861,7 +1861,7 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for PaymentIntentRequest { statement_descriptor_suffix: item.request.statement_descriptor_suffix.clone(), statement_descriptor: item.request.statement_descriptor.clone(), meta_data: StripeMetadata { - order_id, + order_id: Some(order_id), is_refund_id_as_reference: None, }, return_url: item @@ -2696,7 +2696,7 @@ impl TryFrom<&types::RefundsRouterData> for RefundRequest { amount: Some(amount), payment_intent, meta_data: StripeMetadata { - order_id: item.request.refund_id.clone(), + order_id: Some(item.request.refund_id.clone()), is_refund_id_as_reference: Some("true".to_string()), }, }) From 824e22551899b40edf88ffaaa282e4e40ed15307 Mon Sep 17 00:00:00 2001 From: Sk Sakil Mostak Date: Fri, 5 Jan 2024 20:42:38 +0530 Subject: [PATCH 2/2] refactor: comments --- crates/router/src/connector/stripe.rs | 53 +++++++++++-------- .../src/connector/stripe/transformers.rs | 8 +-- 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/crates/router/src/connector/stripe.rs b/crates/router/src/connector/stripe.rs index 789e5885f644..8c43e2c16a25 100644 --- a/crates/router/src/connector/stripe.rs +++ b/crates/router/src/connector/stripe.rs @@ -1858,14 +1858,15 @@ impl api::IncomingWebhook for Stripe { Ok(match details.event_data.event_object.object { stripe::WebhookEventObjectType::PaymentIntent => { - match details.event_data.event_object.metadata { + match details + .event_data + .event_object + .metadata + .and_then(|meta_data| meta_data.order_id) + { // if order_id is present - Some(meta_data) => api_models::webhooks::ObjectReferenceId::PaymentId( - api_models::payments::PaymentIdType::PaymentAttemptId( - meta_data - .order_id - .ok_or(errors::ConnectorError::WebhookReferenceIdNotFound)?, - ), + Some(order_id) => api_models::webhooks::ObjectReferenceId::PaymentId( + api_models::payments::PaymentIdType::PaymentAttemptId(order_id), ), // else used connector_transaction_id None => api_models::webhooks::ObjectReferenceId::PaymentId( @@ -1876,14 +1877,15 @@ impl api::IncomingWebhook for Stripe { } } stripe::WebhookEventObjectType::Charge => { - match details.event_data.event_object.metadata { + match details + .event_data + .event_object + .metadata + .and_then(|meta_data| meta_data.order_id) + { // if order_id is present - Some(meta_data) => api_models::webhooks::ObjectReferenceId::PaymentId( - api_models::payments::PaymentIdType::PaymentAttemptId( - meta_data - .order_id - .ok_or(errors::ConnectorError::WebhookReferenceIdNotFound)?, - ), + Some(order_id) => api_models::webhooks::ObjectReferenceId::PaymentId( + api_models::payments::PaymentIdType::PaymentAttemptId(order_id), ), // else used connector_transaction_id None => api_models::webhooks::ObjectReferenceId::PaymentId( @@ -1916,18 +1918,25 @@ impl api::IncomingWebhook for Stripe { ) } stripe::WebhookEventObjectType::Refund => { - match details.event_data.event_object.metadata { + match details + .event_data + .event_object + .metadata + .clone() + .and_then(|meta_data| meta_data.order_id) + { // if meta_data is present - Some(meta_data) => { + Some(order_id) => { // Issue: 2076 - match meta_data.is_refund_id_as_reference { + match details + .event_data + .event_object + .metadata + .and_then(|meta_data| meta_data.is_refund_id_as_reference) + { // if the order_id is refund_id Some(_) => api_models::webhooks::ObjectReferenceId::RefundId( - api_models::webhooks::RefundIdType::RefundId( - meta_data.order_id.ok_or( - errors::ConnectorError::WebhookReferenceIdNotFound, - )?, - ), + api_models::webhooks::RefundIdType::RefundId(order_id), ), // if the order_id is payment_id // since payment_id was being passed before the deployment of this pr diff --git a/crates/router/src/connector/stripe/transformers.rs b/crates/router/src/connector/stripe/transformers.rs index d81231d90284..4338e8f9ff28 100644 --- a/crates/router/src/connector/stripe/transformers.rs +++ b/crates/router/src/connector/stripe/transformers.rs @@ -3203,7 +3203,7 @@ pub struct WebhookPaymentMethodDetails { pub payment_method: WebhookPaymentMethodType, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Clone, Deserialize)] pub struct WebhookEventObjectData { pub id: String, pub object: WebhookEventObjectType, @@ -3218,7 +3218,7 @@ pub struct WebhookEventObjectData { pub metadata: Option, } -#[derive(Debug, Deserialize, strum::Display)] +#[derive(Debug, Clone, Deserialize, strum::Display)] #[serde(rename_all = "snake_case")] pub enum WebhookEventObjectType { PaymentIntent, @@ -3280,7 +3280,7 @@ pub enum WebhookEventType { Unknown, } -#[derive(Debug, Serialize, strum::Display, Deserialize, PartialEq)] +#[derive(Debug, Clone, Serialize, strum::Display, Deserialize, PartialEq)] #[serde(rename_all = "snake_case")] pub enum WebhookEventStatus { WarningNeedsResponse, @@ -3304,7 +3304,7 @@ pub enum WebhookEventStatus { Unknown, } -#[derive(Debug, Deserialize, PartialEq)] +#[derive(Debug, Clone, Deserialize, PartialEq)] pub struct EvidenceDetails { #[serde(with = "common_utils::custom_serde::timestamp")] pub due_by: PrimitiveDateTime,