Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

feat(connector): [PAYLOAD] add PostCaptureVoid support for cards (#11746)

awasthi21aa1248cb172d
diff --git a/crates/hyperswitch_connectors/src/connectors/payload.rs b/crates/hyperswitch_connectors/src/connectors/payload.rs
index 370cf28d95..c9a2e219fc 100644
--- a/crates/hyperswitch_connectors/src/connectors/payload.rs
+++ b/crates/hyperswitch_connectors/src/connectors/payload.rs
@@ -23,24 +23,25 @@ use hyperswitch_domain_models::{
router_flow_types::{
access_token_auth::AccessTokenAuth,
payments::{
- Authorize, Capture, CreateConnectorCustomer, PSync, PaymentMethodToken, Session,
- SetupMandate, Void,
+ Authorize, Capture, CreateConnectorCustomer, PSync, PaymentMethodToken,
+ PostCaptureVoid, Session, SetupMandate, Void,
},
refunds::{Execute, RSync},
},
router_request_types::{
AccessTokenRequestData, ConnectorCustomerData, PaymentMethodTokenizationData,
- PaymentsAuthorizeData, PaymentsCancelData, PaymentsCaptureData, PaymentsSessionData,
- PaymentsSyncData, RefundsData, SetupMandateRequestData,
+ PaymentsAuthorizeData, PaymentsCancelData, PaymentsCancelPostCaptureData,
+ PaymentsCaptureData, PaymentsSessionData, PaymentsSyncData, RefundsData,
+ SetupMandateRequestData,
},
router_response_types::{
ConnectorInfo, PaymentMethodDetails, PaymentsResponseData, RefundsResponseData,
SupportedPaymentMethods, SupportedPaymentMethodsExt,
},
types::{
- ConnectorCustomerRouterData, PaymentsAuthorizeRouterData, PaymentsCancelRouterData,
- PaymentsCaptureRouterData, PaymentsSyncRouterData, RefundSyncRouterData, RefundsRouterData,
- SetupMandateRouterData,
+ ConnectorCustomerRouterData, PaymentsAuthorizeRouterData,
+ PaymentsCancelPostCaptureRouterData, PaymentsCancelRouterData, PaymentsCaptureRouterData,
+ PaymentsSyncRouterData, RefundSyncRouterData, RefundsRouterData, SetupMandateRouterData,
},
};
use hyperswitch_interfaces::{
@@ -52,7 +53,10 @@ use hyperswitch_interfaces::{
disputes::DisputePayload,
errors,
events::connector_api_logs::ConnectorEvent,
- types::{self, ConnectorCustomerType, PaymentsVoidType, Response, SetupMandateType},
+ types::{
+ self, ConnectorCustomerType, PaymentsPostCaptureVoidType, PaymentsVoidType, Response,
+ SetupMandateType,
+ },
webhooks,
};
use hyperswitch_masking::{ExposeInterface, Mask};
@@ -85,6 +89,7 @@ impl api::PaymentAuthorize for Payload {}
impl api::PaymentSync for Payload {}
impl api::PaymentCapture for Payload {}
impl api::PaymentVoid for Payload {}
+impl api::PaymentPostCaptureVoid for Payload {}
impl api::Refund for Payload {}
impl api::RefundExecute for Payload {}
impl api::RefundSync for Payload {}
@@ -748,6 +753,98 @@ impl ConnectorIntegration<Void, PaymentsCancelData, PaymentsResponseData> for Pa
}
}
+impl ConnectorIntegration<PostCaptureVoid, PaymentsCancelPostCaptureData, PaymentsResponseData>
+ for Payload
+{
+ fn get_headers(
+ &self,
+ req: &PaymentsCancelPostCaptureRouterData,
+ connectors: &Connectors,
+ ) -> CustomResult<Vec<(String, hyperswitch_masking::Maskable<String>)>, errors::ConnectorError>
+ {
+ self.build_headers(req, connectors)
+ }
+
+ fn get_content_type(&self) -> &'static str {
+ self.common_get_content_type()
+ }
+
+ fn get_url(
+ &self,
+ req: &PaymentsCancelPostCaptureRouterData,
+ connectors: &Connectors,
+ ) -> CustomResult<String, errors::ConnectorError> {
+ let payment_id = &req.request.connector_transaction_id;
+ Ok(format!(
+ "{}/transactions/{}",
+ self.base_url(connectors),
+ payment_id
+ ))
+ }
+
+ fn get_request_body(
+ &self,
+ _req: &PaymentsCancelPostCaptureRouterData,
+ _connectors: &Connectors,
+ ) -> CustomResult<RequestContent, errors::ConnectorError> {
+ let connector_req = requests::PayloadCancelRequest {
+ status: responses::PayloadPaymentStatus::Voided,
+ };
+ Ok(RequestContent::Json(Box::new(connector_req)))
+ }
+
+ fn build_request(
+ &self,
+ req: &PaymentsCancelPostCaptureRouterData,
+ connectors: &Connectors,
+ ) -> CustomResult<Option<Request>, errors::ConnectorError> {
+ Ok(Some(
+ RequestBuilder::new()
+ .method(Method::Put)
+ .url(&PaymentsPostCaptureVoidType::get_url(
+ self, req, connectors,
+ )?)
+ .attach_default_headers()
+ .headers(PaymentsPostCaptureVoidType::get_headers(
+ self, req, connectors,
+ )?)
+ .set_body(PaymentsPostCaptureVoidType::get_request_body(
+ self, req, connectors,
+ )?)
+ .build(),
+ ))
+ }
+
+ fn handle_response(
+ &self,
+ data: &PaymentsCancelPostCaptureRouterData,
+ event_builder: Option<&mut ConnectorEvent>,
+ res: Response,
+ ) -> CustomResult<PaymentsCancelPostCaptureRouterData, errors::ConnectorError> {
+ let response: responses::PayloadPostCaptureVoidResponse = res
+ .response
+ .parse_struct("PayloadPostCaptureVoidResponse")
+ .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(
+ &self,
+ res: Response,
+ event_builder: Option<&mut ConnectorEvent>,
+ ) -> CustomResult<ErrorResponse, errors::ConnectorError> {
+ self.build_error_response(res, event_builder)
+ }
+}
+
impl ConnectorIntegration<Execute, RefundsData, RefundsResponseData> for Payload {
fn get_headers(
&self,
diff --git a/crates/hyperswitch_connectors/src/connectors/payload/responses.rs b/crates/hyperswitch_connectors/src/connectors/payload/responses.rs
index ef7187fec6..d42fdd3411 100644
--- a/crates/hyperswitch_connectors/src/connectors/payload/responses.rs
+++ b/crates/hyperswitch_connectors/src/connectors/payload/responses.rs
@@ -19,6 +19,12 @@ pub enum PayloadPaymentsResponse {
PayloadCardsResponse(PayloadCardsResponseData),
}
+/// Newtype wrapping `PayloadPaymentsResponse` for the PostCaptureVoid flow.
+/// A distinct type is required so its `TryFrom` impl does not conflict with
+/// the blanket `TryFrom<ResponseRouterData<F, PayloadPaymentsResponse, ...>>` impl.
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct PayloadPostCaptureVoidResponse(pub PayloadPaymentsResponse);
+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum AvsResponse {
diff --git a/crates/hyperswitch_connectors/src/connectors/payload/transformers.rs b/crates/hyperswitch_connectors/src/connectors/payload/transformers.rs
index cf85b76e1f..00c5513672 100644
--- a/crates/hyperswitch_connectors/src/connectors/payload/transformers.rs
+++ b/crates/hyperswitch_connectors/src/connectors/payload/transformers.rs
@@ -1,7 +1,7 @@
use std::collections::HashMap;
use api_models::webhooks::IncomingWebhookEvent;
-use common_enums::enums;
+use common_enums::{self as common_enums, enums};
use common_utils::{ext_traits::ValueExt, types::StringMajorUnit};
use error_stack::ResultExt;
use hyperswitch_domain_models::{
@@ -11,8 +11,11 @@ use hyperswitch_domain_models::{
AdditionalPaymentMethodConnectorResponse, ConnectorAuthType, ConnectorResponseData,
ErrorResponse, RouterData,
},
- router_flow_types::refunds::{Execute, RSync},
- router_request_types::ResponseId,
+ router_flow_types::{
+ payments::PostCaptureVoid,
+ refunds::{Execute, RSync},
+ },
+ router_request_types::{PaymentsCancelPostCaptureData, ResponseId},
router_response_types::{
ConnectorCustomerResponseData, MandateReference, PaymentsResponseData, RefundsResponseData,
},
@@ -588,6 +591,60 @@ impl<T> TryFrom<&PayloadRouterData<T>> for requests::PayloadCancelRequest {
}
}
+impl
+ TryFrom<
+ ResponseRouterData<
+ PostCaptureVoid,
+ responses::PayloadPostCaptureVoidResponse,
+ PaymentsCancelPostCaptureData,
+ PaymentsResponseData,
+ >,
+ > for RouterData<PostCaptureVoid, PaymentsCancelPostCaptureData, PaymentsResponseData>
+{
+ type Error = Error;
+ fn try_from(
+ item: ResponseRouterData<
+ PostCaptureVoid,
+ responses::PayloadPostCaptureVoidResponse,
+ PaymentsCancelPostCaptureData,
+ PaymentsResponseData,
+ >,
+ ) -> Result<Self, Self::Error> {
+ let responses::PayloadPaymentsResponse::PayloadCardsResponse(response) = item.response.0;
+
+ let post_capture_void_status = match response.status {
+ responses::PayloadPaymentStatus::Voided => {
+ common_enums::PostCaptureVoidStatus::Succeeded
+ }
+ responses::PayloadPaymentStatus::Processing => {
+ common_enums::PostCaptureVoidStatus::Pending
+ }
+ responses::PayloadPaymentStatus::Declined
+ | responses::PayloadPaymentStatus::Rejected => {
+ common_enums::PostCaptureVoidStatus::Failed
+ }
+ responses::PayloadPaymentStatus::Authorized
+ | responses::PayloadPaymentStatus::Processed => {
+ common_enums::PostCaptureVoidStatus::Failed
+ }
+ };
+
+ let description = post_capture_void_status
+ .is_post_capture_void_failure()
+ .then_some(response.status_message.clone())
+ .flatten();
+
+ Ok(Self {
+ response: Ok(PaymentsResponseData::PostCaptureVoidResponse {
+ post_capture_void_status,
+ connector_reference_id: Some(response.transaction_id.clone()),
+ description,
+ }),
+ ..item.data
+ })
+ }
+}
+
impl TryFrom<&PayloadRouterData<&PaymentsCaptureRouterData>> for requests::PayloadCaptureRequest {
type Error = Error;
fn try_from(
diff --git a/crates/hyperswitch_connectors/src/default_implementations.rs b/crates/hyperswitch_connectors/src/default_implementations.rs
index 601266353f..9b26b8f397 100644
--- a/crates/hyperswitch_connectors/src/default_implementations.rs
+++ b/crates/hyperswitch_connectors/src/default_implementations.rs
@@ -1393,7 +1393,6 @@ default_imp_for_cancel_post_capture!(
connectors::Payeezy,
connectors::Payjustnow,
connectors::Payjustnowinstore,
- connectors::Payload,
connectors::Payme,
connectors::Paystack,
connectors::Paytm,