From 5eff9d47d3e53d380ef792a8fbdf06ecf78d3d16 Mon Sep 17 00:00:00 2001 From: DEEPANSHU BANSAL <41580413+deepanshu-iiitu@users.noreply.github.com> Date: Mon, 4 Mar 2024 19:06:12 +0530 Subject: [PATCH] feat(connector): [PLACETOPAY] Fix refund request and status mapping (#3894) --- crates/router/src/connector/placetopay.rs | 6 +- .../src/connector/placetopay/transformers.rs | 136 ++++++++++++------ 2 files changed, 98 insertions(+), 44 deletions(-) diff --git a/crates/router/src/connector/placetopay.rs b/crates/router/src/connector/placetopay.rs index 81255f450e..c28fed1fed 100644 --- a/crates/router/src/connector/placetopay.rs +++ b/crates/router/src/connector/placetopay.rs @@ -119,8 +119,8 @@ impl ConnectorValidation for Placetopay { ) -> CustomResult<(), errors::ConnectorError> { let capture_method = capture_method.unwrap_or_default(); match capture_method { - enums::CaptureMethod::Manual => Ok(()), - enums::CaptureMethod::Automatic + enums::CaptureMethod::Automatic => Ok(()), + enums::CaptureMethod::Manual | enums::CaptureMethod::ManualMultiple | enums::CaptureMethod::Scheduled => Err(utils::construct_not_supported_error_report( capture_method, @@ -615,7 +615,7 @@ impl ConnectorIntegration CustomResult, errors::ConnectorError> { Ok(Some( services::RequestBuilder::new() - .method(services::Method::Get) + .method(services::Method::Post) .url(&types::RefundSyncType::get_url(self, req, connectors)?) .attach_default_headers() .headers(types::RefundSyncType::get_headers(self, req, connectors)?) diff --git a/crates/router/src/connector/placetopay/transformers.rs b/crates/router/src/connector/placetopay/transformers.rs index f1c054b960..e5dedf6ded 100644 --- a/crates/router/src/connector/placetopay/transformers.rs +++ b/crates/router/src/connector/placetopay/transformers.rs @@ -206,30 +206,43 @@ impl TryFrom<&types::ConnectorAuthType> for PlacetopayAuthType { #[derive(Debug, Clone, Deserialize, Serialize)] #[serde(rename_all = "SCREAMING_SNAKE_CASE")] -pub enum PlacetopayStatus { +pub enum PlacetopayTransactionStatus { Ok, Failed, Approved, + // ApprovedPartial, + // PartialExpired, Rejected, Pending, PendingValidation, PendingProcess, + // Refunded, + // Reversed, + Error, + // Unknown, + // Manual, + // Dispute, + //The statuses that are commented out are awaiting clarification on the connector. } #[derive(Debug, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct PlacetopayStatusResponse { - status: PlacetopayStatus, + status: PlacetopayTransactionStatus, } -impl From for enums::AttemptStatus { - fn from(item: PlacetopayStatus) -> Self { +impl From for enums::AttemptStatus { + fn from(item: PlacetopayTransactionStatus) -> Self { match item { - PlacetopayStatus::Approved | PlacetopayStatus::Ok => Self::Authorized, - PlacetopayStatus::Failed | PlacetopayStatus::Rejected => Self::Failure, - PlacetopayStatus::Pending - | PlacetopayStatus::PendingValidation - | PlacetopayStatus::PendingProcess => Self::Authorizing, + PlacetopayTransactionStatus::Approved | PlacetopayTransactionStatus::Ok => { + Self::Charged + } + PlacetopayTransactionStatus::Failed + | PlacetopayTransactionStatus::Rejected + | PlacetopayTransactionStatus::Error => Self::Failure, + PlacetopayTransactionStatus::Pending + | PlacetopayTransactionStatus::PendingValidation + | PlacetopayTransactionStatus::PendingProcess => Self::Pending, } } } @@ -239,6 +252,7 @@ impl From for enums::AttemptStatus { pub struct PlacetopayPaymentsResponse { status: PlacetopayStatusResponse, internal_reference: u64, + authorization: Option, } impl @@ -263,7 +277,11 @@ impl ), redirection_data: None, mandate_reference: None, - connector_metadata: None, + connector_metadata: item + .response + .authorization + .clone() + .map(|authorization| serde_json::json!(authorization)), network_txn_id: None, connector_response_reference_id: None, incremental_authorization_allowed: None, @@ -281,55 +299,90 @@ pub struct PlacetopayRefundRequest { auth: PlacetopayAuth, internal_reference: u64, action: PlacetopayNextAction, + authorization: Option, } impl TryFrom<&types::RefundsRouterData> for PlacetopayRefundRequest { type Error = error_stack::Report; fn try_from(item: &types::RefundsRouterData) -> Result { - let auth = PlacetopayAuth::try_from(&item.connector_auth_type)?; - let internal_reference = item - .request - .connector_transaction_id - .parse::() - .into_report() - .change_context(errors::ConnectorError::RequestEncodingFailed)?; - let action = PlacetopayNextAction::Refund; + if item.request.refund_amount == item.request.payment_amount { + let auth = PlacetopayAuth::try_from(&item.connector_auth_type)?; - Ok(Self { - auth, - internal_reference, - action, - }) + let internal_reference = item + .request + .connector_transaction_id + .parse::() + .into_report() + .change_context(errors::ConnectorError::RequestEncodingFailed)?; + let action = PlacetopayNextAction::Reverse; + let authorization = match item.request.connector_metadata.clone() { + Some(metadata) => metadata.as_str().map(|auth| auth.to_string()), + None => None, + }; + Ok(Self { + auth, + internal_reference, + action, + authorization, + }) + } else { + Err(errors::ConnectorError::NotSupported { + message: "Partial Refund".to_string(), + connector: "placetopay", + } + .into()) + } } } impl From for enums::RefundStatus { fn from(item: PlacetopayRefundStatus) -> Self { match item { - PlacetopayRefundStatus::Refunded => Self::Success, - PlacetopayRefundStatus::Failed | PlacetopayRefundStatus::Rejected => Self::Failure, - PlacetopayRefundStatus::Pending | PlacetopayRefundStatus::PendingProcess => { - Self::Pending - } + PlacetopayRefundStatus::Ok + | PlacetopayRefundStatus::Approved + | PlacetopayRefundStatus::Refunded => Self::Success, + PlacetopayRefundStatus::Failed + | PlacetopayRefundStatus::Rejected + | PlacetopayRefundStatus::Error => Self::Failure, + PlacetopayRefundStatus::Pending + | PlacetopayRefundStatus::PendingProcess + | PlacetopayRefundStatus::PendingValidation => Self::Pending, } } } #[derive(Debug, Deserialize, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct PlacetopayRefundResponse { - status: PlacetopayRefundStatus, - internal_reference: u64, +#[serde(rename_all = "SCREAMING_SNAKE_CASE")] +pub enum PlacetopayRefundStatus { + Ok, + Failed, + Approved, + // ApprovedPartial, + // PartialExpired, + Rejected, + Pending, + PendingValidation, + PendingProcess, + Refunded, + // Reversed, + Error, + // Unknown, + // Manual, + // Dispute, + //The statuses that are commented out are awaiting clarification on the connector. } #[derive(Debug, Deserialize, Serialize)] -#[serde(rename_all = "SCREAMING_SNAKE_CASE")] -pub enum PlacetopayRefundStatus { - Refunded, - Rejected, - Failed, - Pending, - PendingProcess, +#[serde(rename_all = "camelCase")] +pub struct PlacetopayRefundStatusResponse { + status: PlacetopayRefundStatus, +} + +#[derive(Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct PlacetopayRefundResponse { + status: PlacetopayRefundStatusResponse, + internal_reference: u64, } impl TryFrom> @@ -342,7 +395,7 @@ impl TryFrom