From e9826fe4f35c7783c73b5abebfd826dba4178c4d Mon Sep 17 00:00:00 2001 From: Mrudul Vajpayee Date: Sun, 10 Nov 2024 19:42:58 +0530 Subject: [PATCH] refactor NexixpayPaymentsResponse --- .../src/connectors/nexixpay.rs | 36 ++-- .../src/connectors/nexixpay/transformers.rs | 160 +++++++++--------- 2 files changed, 94 insertions(+), 102 deletions(-) diff --git a/crates/hyperswitch_connectors/src/connectors/nexixpay.rs b/crates/hyperswitch_connectors/src/connectors/nexixpay.rs index a39e86644f7d..bb6b7243f086 100644 --- a/crates/hyperswitch_connectors/src/connectors/nexixpay.rs +++ b/crates/hyperswitch_connectors/src/connectors/nexixpay.rs @@ -480,31 +480,17 @@ impl ConnectorIntegration, res: Response, ) -> CustomResult { - if data.request.connector_mandate_id().is_none() { - let response: nexixpay::NexixpayPaymentsResponse = res - .response - .parse_struct("NexixpayPaymentsResponse") - .change_context(errors::ConnectorError::ResponseDeserializationFailed)?; - event_builder.map(|i| i.set_response_body(&response)); - router_env::logger::info!(connector_response=?response); - RouterData::try_from(ResponseRouterData { - response, - data: data.clone(), - http_code: res.status_code, - }) - } else { - let response: nexixpay::NexixpayMandateResponse = res - .response - .parse_struct("NexixpayMandateResponse") - .change_context(errors::ConnectorError::ResponseDeserializationFailed)?; - event_builder.map(|i| i.set_response_body(&response)); - router_env::logger::info!(connector_response=?response); - RouterData::try_from(ResponseRouterData { - response, - data: data.clone(), - http_code: res.status_code, - }) - } + let response: nexixpay::NexixpayPaymentsResponse = res + .response + .parse_struct("NexixpayPaymentsResponse") + .change_context(errors::ConnectorError::ResponseDeserializationFailed)?; + event_builder.map(|i| i.set_response_body(&response)); + router_env::logger::info!(connector_response=?response); + RouterData::try_from(ResponseRouterData { + response, + data: data.clone(), + http_code: res.status_code, + }) } fn get_error_response( diff --git a/crates/hyperswitch_connectors/src/connectors/nexixpay/transformers.rs b/crates/hyperswitch_connectors/src/connectors/nexixpay/transformers.rs index 010e88dbf0f2..6458f760f2a8 100644 --- a/crates/hyperswitch_connectors/src/connectors/nexixpay/transformers.rs +++ b/crates/hyperswitch_connectors/src/connectors/nexixpay/transformers.rs @@ -187,7 +187,7 @@ struct Recurrence { #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] -pub struct NexixpayPaymentsResponse { +pub struct PaymentsResponse { operation: Operation, three_d_s_auth_request: String, three_d_s_auth_url: Secret, @@ -199,6 +199,14 @@ pub struct NexixpayMandateResponse { operation: Operation, } +#[derive(Debug, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +#[serde(untagged)] +pub enum NexixpayPaymentsResponse { + PaymentResponse(Box), + MandateResponse(Box), +} + #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct ThreeDSAuthResult { @@ -719,83 +727,81 @@ impl PaymentsResponseData, >, ) -> Result { - let complete_authorize_url = item.data.request.get_complete_authorize_url()?; - let operation_id: String = item.response.operation.operation_id; - let redirection_form = nexixpay_threeds_link(NexixpayRedirectionRequest { - three_d_s_auth_url: item.response.three_d_s_auth_url.expose().to_string(), - three_ds_request: item.response.three_d_s_auth_request.clone(), - return_url: complete_authorize_url.clone(), - transaction_id: operation_id.clone(), - })?; - let is_auto_capture = item.data.request.is_auto_capture()?; - let connector_metadata = Some(serde_json::json!(NexixpayConnectorMetaData { - three_d_s_auth_result: None, - three_d_s_auth_response: None, - authorization_operation_id: Some(operation_id.clone()), - cancel_operation_id: None, - capture_operation_id: { - if is_auto_capture { - Some(operation_id) - } else { - None - } - }, - psync_flow: NexixpayPaymentIntent::Authorize - })); - Ok(Self { - status: AttemptStatus::from(item.response.operation.operation_result), - response: Ok(PaymentsResponseData::TransactionResponse { - resource_id: ResponseId::ConnectorTransactionId( - item.response.operation.order_id.clone(), - ), - redirection_data: Box::new(Some(redirection_form.clone())), - mandate_reference: Box::new(Some(MandateReference { - connector_mandate_id: item.data.connector_mandate_request_reference_id.clone(), - payment_method_id: None, - mandate_metadata: None, - connector_mandate_request_reference_id: None, - })), - connector_metadata, - network_txn_id: None, - connector_response_reference_id: Some(item.response.operation.order_id), - incremental_authorization_allowed: None, - charge_id: None, - }), - ..item.data - }) - } -} - -impl - TryFrom< - ResponseRouterData, - > for RouterData -{ - type Error = error_stack::Report; - fn try_from( - item: ResponseRouterData< - F, - NexixpayMandateResponse, - PaymentsAuthorizeData, - PaymentsResponseData, - >, - ) -> Result { - Ok(Self { - status: AttemptStatus::from(item.response.operation.operation_result), - response: Ok(PaymentsResponseData::TransactionResponse { - resource_id: ResponseId::ConnectorTransactionId( - item.response.operation.order_id.clone(), - ), - redirection_data: Box::new(None), - mandate_reference: Box::new(None), - connector_metadata: None, - network_txn_id: None, - connector_response_reference_id: Some(item.response.operation.order_id), - incremental_authorization_allowed: None, - charge_id: None, + match item.response { + NexixpayPaymentsResponse::PaymentResponse(ref response_body) => { + let complete_authorize_url = item.data.request.get_complete_authorize_url()?; + let operation_id: String = response_body.operation.operation_id.clone(); + let redirection_form = nexixpay_threeds_link(NexixpayRedirectionRequest { + three_d_s_auth_url: response_body + .three_d_s_auth_url + .clone() + .expose() + .to_string(), + three_ds_request: response_body.three_d_s_auth_request.clone(), + return_url: complete_authorize_url.clone(), + transaction_id: operation_id.clone(), + })?; + let is_auto_capture = item.data.request.is_auto_capture()?; + let connector_metadata = Some(serde_json::json!(NexixpayConnectorMetaData { + three_d_s_auth_result: None, + three_d_s_auth_response: None, + authorization_operation_id: Some(operation_id.clone()), + cancel_operation_id: None, + capture_operation_id: { + if is_auto_capture { + Some(operation_id) + } else { + None + } + }, + psync_flow: NexixpayPaymentIntent::Authorize + })); + Ok(Self { + status: AttemptStatus::from(response_body.operation.operation_result.clone()), + response: Ok(PaymentsResponseData::TransactionResponse { + resource_id: ResponseId::ConnectorTransactionId( + response_body.operation.order_id.clone(), + ), + redirection_data: Box::new(Some(redirection_form.clone())), + mandate_reference: Box::new(Some(MandateReference { + connector_mandate_id: item + .data + .connector_mandate_request_reference_id + .clone(), + payment_method_id: None, + mandate_metadata: None, + connector_mandate_request_reference_id: None, + })), + connector_metadata, + network_txn_id: None, + connector_response_reference_id: Some( + response_body.operation.order_id.clone(), + ), + incremental_authorization_allowed: None, + charge_id: None, + }), + ..item.data + }) + } + NexixpayPaymentsResponse::MandateResponse(ref mandate_response) => Ok(Self { + status: AttemptStatus::from(mandate_response.operation.operation_result.clone()), + response: Ok(PaymentsResponseData::TransactionResponse { + resource_id: ResponseId::ConnectorTransactionId( + mandate_response.operation.order_id.clone(), + ), + redirection_data: Box::new(None), + mandate_reference: Box::new(None), + connector_metadata: None, + network_txn_id: None, + connector_response_reference_id: Some( + mandate_response.operation.order_id.clone(), + ), + incremental_authorization_allowed: None, + charge_id: None, + }), + ..item.data }), - ..item.data - }) + } } }