From f12a43817787faedfdca26ec7f956bf5734c5ee3 Mon Sep 17 00:00:00 2001 From: SamraatBansal <55536657+SamraatBansal@users.noreply.github.com> Date: Tue, 26 Sep 2023 14:06:02 +0530 Subject: [PATCH] fix(connector): [noon] Create psync struct from webhook resource object (#2370) --- crates/router/src/connector/noon.rs | 12 ++++-- .../router/src/connector/noon/transformers.rs | 38 ++++++++++++++++--- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/crates/router/src/connector/noon.rs b/crates/router/src/connector/noon.rs index 625f080d2953..deda24bfbd66 100644 --- a/crates/router/src/connector/noon.rs +++ b/crates/router/src/connector/noon.rs @@ -705,9 +705,15 @@ impl api::IncomingWebhook for Noon { &self, request: &api::IncomingWebhookRequestDetails<'_>, ) -> CustomResult { - let reference_object: serde_json::Value = serde_json::from_slice(request.body) - .into_report() + let resource: noon::NoonWebhookObject = request + .body + .parse_struct("NoonWebhookObject") .change_context(errors::ConnectorError::WebhookResourceObjectNotFound)?; - Ok(reference_object) + + let res_json = serde_json::to_value(noon::NoonPaymentsResponse::from(resource)) + .into_report() + .change_context(errors::ConnectorError::WebhookBodyDecodingFailed)?; + + Ok(res_json) } } diff --git a/crates/router/src/connector/noon/transformers.rs b/crates/router/src/connector/noon/transformers.rs index a27ae6d9b70e..eff626189103 100644 --- a/crates/router/src/connector/noon/transformers.rs +++ b/crates/router/src/connector/noon/transformers.rs @@ -301,7 +301,7 @@ impl TryFrom<&types::ConnectorAuthType> for NoonAuthType { } } } -#[derive(Default, Debug, Deserialize, strum::Display)] +#[derive(Default, Debug, Deserialize, Serialize, strum::Display)] #[serde(rename_all = "SCREAMING_SNAKE_CASE")] #[strum(serialize_all = "UPPERCASE")] pub enum NoonPaymentStatus { @@ -335,12 +335,12 @@ impl From for enums::AttemptStatus { } } -#[derive(Debug, Deserialize)] +#[derive(Debug, Serialize, Deserialize)] pub struct NoonSubscriptionResponse { identifier: String, } -#[derive(Default, Debug, Deserialize)] +#[derive(Default, Debug, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct NoonPaymentsOrderResponse { status: NoonPaymentStatus, @@ -349,13 +349,13 @@ pub struct NoonPaymentsOrderResponse { error_message: Option, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct NoonCheckoutData { post_url: url::Url, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct NoonPaymentsResponseResult { order: NoonPaymentsOrderResponse, @@ -363,7 +363,7 @@ pub struct NoonPaymentsResponseResult { subscription: Option, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Serialize, Deserialize)] pub struct NoonPaymentsResponse { result: NoonPaymentsResponseResult, } @@ -633,6 +633,32 @@ pub struct NoonWebhookEvent { pub event_type: NoonWebhookEventTypes, } +#[derive(Debug, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct NoonWebhookObject { + pub order_status: NoonPaymentStatus, + pub order_id: u64, +} + +/// This from will ensure that webhook body would be properly parsed into PSync response +impl From for NoonPaymentsResponse { + fn from(value: NoonWebhookObject) -> Self { + Self { + result: NoonPaymentsResponseResult { + order: NoonPaymentsOrderResponse { + status: value.order_status, + id: value.order_id, + //For successful payments Noon Always populates error_code as 0. + error_code: 0, + error_message: None, + }, + checkout_data: None, + subscription: None, + }, + } + } +} + #[derive(Debug, Deserialize)] #[serde(rename_all = "camelCase")] pub struct NoonErrorResponse {