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): [PEACHPAYMENTS] Refunds, RSync and Cypress added (#9850)

sweta-sharma820f18311f8d
diff --git a/crates/hyperswitch_connectors/src/connectors/peachpayments.rs b/crates/hyperswitch_connectors/src/connectors/peachpayments.rs
index 2e95b9e8a6..4c9111a727 100644
--- a/crates/hyperswitch_connectors/src/connectors/peachpayments.rs
+++ b/crates/hyperswitch_connectors/src/connectors/peachpayments.rs
@@ -46,7 +46,11 @@ use hyperswitch_interfaces::{
use masking::{ExposeInterface, Mask, Secret};
use transformers as peachpayments;
-use crate::{constants::headers, types::ResponseRouterData, utils};
+use crate::{
+ constants::headers,
+ types::ResponseRouterData,
+ utils::{self, RefundsRequestData},
+};
#[derive(Clone)]
pub struct Peachpayments {
@@ -520,22 +524,148 @@ impl ConnectorIntegration<Void, PaymentsCancelData, PaymentsResponseData> for Pe
}
impl ConnectorIntegration<Execute, RefundsData, RefundsResponseData> for Peachpayments {
- fn build_request(
+ fn get_headers(
+ &self,
+ req: &RefundsRouterData<Execute>,
+ connectors: &Connectors,
+ ) -> CustomResult<Vec<(String, 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: &RefundsRouterData<Execute>,
+ req: &RefundsRouterData<Execute>,
+ connectors: &Connectors,
+ ) -> CustomResult<String, errors::ConnectorError> {
+ Ok(format!(
+ "{}/transactions/{}/refund",
+ self.base_url(connectors),
+ req.request.connector_transaction_id
+ ))
+ }
+
+ fn get_request_body(
+ &self,
+ req: &RefundsRouterData<Execute>,
_connectors: &Connectors,
+ ) -> CustomResult<RequestContent, errors::ConnectorError> {
+ let connector_req = peachpayments::PeachpaymentsRefundRequest::try_from(req)?;
+ Ok(RequestContent::Json(Box::new(connector_req)))
+ }
+
+ fn build_request(
+ &self,
+ req: &RefundsRouterData<Execute>,
+ connectors: &Connectors,
) -> CustomResult<Option<Request>, errors::ConnectorError> {
- Err(errors::ConnectorError::NotImplemented("Execute".to_string()).into())
+ let request = RequestBuilder::new()
+ .method(Method::Post)
+ .url(&types::RefundExecuteType::get_url(self, req, connectors)?)
+ .attach_default_headers()
+ .headers(types::RefundExecuteType::get_headers(
+ self, req, connectors,
+ )?)
+ .set_body(types::RefundExecuteType::get_request_body(
+ self, req, connectors,
+ )?)
+ .build();
+ Ok(Some(request))
+ }
+
+ fn handle_response(
+ &self,
+ data: &RefundsRouterData<Execute>,
+ event_builder: Option<&mut ConnectorEvent>,
+ res: Response,
+ ) -> CustomResult<RefundsRouterData<Execute>, errors::ConnectorError> {
+ let response: peachpayments::PeachpaymentsRefundResponse = res
+ .response
+ .parse_struct("PeachpaymentsRefundResponse")
+ .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<RSync, RefundsData, RefundsResponseData> for Peachpayments {
+ fn get_headers(
+ &self,
+ req: &RefundSyncRouterData,
+ connectors: &Connectors,
+ ) -> CustomResult<Vec<(String, masking::Maskable<String>)>, errors::ConnectorError> {
+ self.build_headers(req, connectors)
+ }
+
+ fn get_url(
+ &self,
+ req: &RefundSyncRouterData,
+ connectors: &Connectors,
+ ) -> CustomResult<String, errors::ConnectorError> {
+ let connector_refund_id = req.request.get_connector_refund_id()?;
+ Ok(format!(
+ "{}/transactions/{}",
+ self.base_url(connectors),
+ connector_refund_id
+ ))
+ }
+
fn build_request(
&self,
- _req: &RefundSyncRouterData,
- _connectors: &Connectors,
+ req: &RefundSyncRouterData,
+ connectors: &Connectors,
) -> CustomResult<Option<Request>, errors::ConnectorError> {
- Err(errors::ConnectorError::NotImplemented("Refunds Retrieve".to_string()).into())
+ Ok(Some(
+ RequestBuilder::new()
+ .method(Method::Get)
+ .url(&types::RefundSyncType::get_url(self, req, connectors)?)
+ .attach_default_headers()
+ .headers(types::RefundSyncType::get_headers(self, req, connectors)?)
+ .build(),
+ ))
+ }
+
+ fn handle_response(
+ &self,
+ data: &RefundSyncRouterData,
+ event_builder: Option<&mut ConnectorEvent>,
+ res: Response,
+ ) -> CustomResult<RefundSyncRouterData, errors::ConnectorError> {
+ let response: peachpayments::PeachpaymentsRsyncResponse = res
+ .response
+ .parse_struct("PeachpaymentsRsyncResponse")
+ .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)
}
}
@@ -650,7 +780,7 @@ static PEACHPAYMENTS_SUPPORTED_PAYMENT_METHODS: LazyLock<SupportedPaymentMethods
enums::PaymentMethodType::Credit,
PaymentMethodDetails {
mandates: enums::FeatureStatus::NotSupported,
- refunds: enums::FeatureStatus::NotSupported,
+ refunds: enums::FeatureStatus::Supported,
supported_capture_methods: supported_capture_methods.clone(),
specific_features: Some(
api_models::feature_matrix::PaymentMethodSpecificFeatures::Card({
@@ -669,7 +799,7 @@ static PEACHPAYMENTS_SUPPORTED_PAYMENT_METHODS: LazyLock<SupportedPaymentMethods
enums::PaymentMethodType::Debit,
PaymentMethodDetails {
mandates: enums::FeatureStatus::NotSupported,
- refunds: enums::FeatureStatus::NotSupported,
+ refunds: enums::FeatureStatus::Supported,
supported_capture_methods: supported_capture_methods.clone(),
specific_features: Some(
api_models::feature_matrix::PaymentMethodSpecificFeatures::Card({
diff --git a/crates/hyperswitch_connectors/src/connectors/peachpayments/transformers.rs b/crates/hyperswitch_connectors/src/connectors/peachpayments/transformers.rs
index 349af2ede6..a4685a73ec 100644
--- a/crates/hyperswitch_connectors/src/connectors/peachpayments/transformers.rs
+++ b/crates/hyperswitch_connectors/src/connectors/peachpayments/transformers.rs
@@ -8,9 +8,13 @@ use hyperswitch_domain_models::{
network_tokenization::NetworkTokenNumber,
payment_method_data::{Card, NetworkTokenData, PaymentMethodData},
router_data::{ConnectorAuthType, ErrorResponse, RouterData},
- router_request_types::ResponseId,
- router_response_types::PaymentsResponseData,
- types::{PaymentsAuthorizeRouterData, PaymentsCancelRouterData, PaymentsCaptureRouterData},
+ router_flow_types::refunds::{Execute, RSync},
+ router_request_types::{RefundsData, ResponseId},
+ router_response_types::{PaymentsResponseData, RefundsResponseData},
+ types::{
+ PaymentsAuthorizeRouterData, PaymentsCancelRouterData, PaymentsCaptureRouterData,
+ RefundsRouterData,
+ },
};
use hyperswitch_interfaces::{
consts::{NO_ERROR_CODE, NO_ERROR_MESSAGE},
@@ -94,7 +98,7 @@ pub struct CardOnFileData {
}
#[derive(Debug, Serialize, PartialEq)]
-#[serde[rename_all = "camelCase"]]
+#[serde(rename_all = "camelCase")]
pub struct EcommerceCardPaymentOnlyTransactionData {
pub merchant_information: MerchantInformation,
pub routing: Routing,
@@ -104,7 +108,7 @@ pub struct EcommerceCardPaymentOnlyTransactionData {
}
#[derive(Debug, Serialize, PartialEq)]
-#[serde[rename_all = "camelCase"]]
+#[serde(rename_all = "camelCase")]
pub struct EcommerceNetworkTokenPaymentOnlyTransactionData {
pub merchant_information: MerchantInformation,
pub routing: Routing,
@@ -121,7 +125,7 @@ pub enum EcommercePaymentOnlyTransactionData {
}
#[derive(Debug, Serialize, Deserialize, PartialEq)]
-#[serde[rename_all = "camelCase"]]
+#[serde(rename_all = "camelCase")]
pub struct MerchantInformation {
pub client_merchant_reference_id: Secret<String>,
pub name: Secret<String>,
@@ -147,7 +151,7 @@ pub struct MerchantInformation {
}
#[derive(Debug, Serialize, Deserialize, PartialEq)]
-#[serde[rename_all = "lowercase"]]
+#[serde(rename_all = "lowercase")]
pub enum MerchantType {
Standard,
Sub,
@@ -155,7 +159,7 @@ pub enum MerchantType {
}
#[derive(Debug, Serialize, Deserialize, PartialEq)]
-#[serde[rename_all = "camelCase"]]
+#[serde(rename_all = "camelCase")]
pub struct Routing {
pub route: Route,
pub mid: Secret<String>,
@@ -171,7 +175,7 @@ pub struct Routing {
}
#[derive(Debug, Serialize, Deserialize, PartialEq)]
-#[serde[rename_all = "snake_case"]]
+#[serde(rename_all = "snake_case")]
pub enum Route {
ExipayEmulator,
AbsaBase24,
@@ -187,7 +191,7 @@ pub enum Route {
}
#[derive(Debug, Serialize, PartialEq)]
-#[serde[rename_all = "camelCase"]]
+#[serde(rename_all = "camelCase")]
pub struct CardDetails {
pub pan: CardNumber,
#[serde(skip_serializing_if = "Option::is_none")]
@@ -199,7 +203,7 @@ pub struct CardDetails {
}
#[derive(Debug, Serialize, PartialEq)]
-#[serde[rename_all = "camelCase"]]
+#[serde(rename_all = "camelCase")]
pub struct NetworkTokenDetails {
pub token: NetworkTokenNumber,
pub expiry_year: Secret<String>,
@@ -210,7 +214,7 @@ pub struct NetworkTokenDetails {
}
#[derive(Debug, Serialize, PartialEq)]
-#[serde[rename_all = "lowercase"]]
+#[serde(rename_all = "lowercase")]
pub enum CardNetworkLowercase {
Visa,
Mastercard,
@@ -252,7 +256,7 @@ impl From<common_enums::CardNetwork> for CardNetworkLowercase {
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
-#[serde[rename_all = "camelCase"]]
+#[serde(rename_all = "camelCase")]
pub struct AmountDetails {
pub amount: MinorUnit,
pub currency_code: String,
@@ -262,11 +266,50 @@ pub struct AmountDetails {
// Confirm Transaction Request (for capture)
#[derive(Debug, Serialize, PartialEq)]
-#[serde[rename_all = "camelCase"]]
+#[serde(rename_all = "camelCase")]
pub struct PeachpaymentsConfirmRequest {
pub ecommerce_card_payment_only_confirmation_data: EcommerceCardPaymentOnlyConfirmationData,
}
+#[derive(Debug, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct PeachpaymentsRefundRequest {
+ pub reference_id: String,
+ pub ecommerce_card_payment_only_transaction_data: PeachpaymentsRefundTransactionData,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub pos_data: Option<PosData>,
+}
+
+#[derive(Debug, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct PosData {
+ pub referral: String,
+}
+
+#[derive(Debug, Serialize, PartialEq)]
+#[serde(rename_all = "camelCase")]
+pub struct PeachpaymentsRefundTransactionData {
+ pub amount: AmountDetails,
+}
+
+impl TryFrom<&RefundsRouterData<Execute>> for PeachpaymentsRefundRequest {
+ type Error = error_stack::Report<errors::ConnectorError>;
+ fn try_from(item: &RefundsRouterData<Execute>) -> Result<Self, Self::Error> {
+ let amount = AmountDetails {
+ amount: item.request.minor_refund_amount,
+ currency_code: item.request.currency.to_string(),
+ display_amount: None,
+ };
+ let ecommerce_card_payment_only_transaction_data =
+ PeachpaymentsRefundTransactionData { amount };
+ Ok(Self {
+ reference_id: item.request.refund_id.clone(),
+ ecommerce_card_payment_only_transaction_data,
+ pos_data: None,
+ })
+ }
+}
+
#[derive(Debug, Serialize, PartialEq)]
pub struct EcommerceCardPaymentOnlyConfirmationData {
pub amount: AmountDetails,
@@ -662,8 +705,19 @@ pub enum PeachpaymentsPaymentsResponse {
WebhookResponse(Box<PeachpaymentsIncomingWebhook>),
}
+impl From<PeachpaymentsRefundStatus> for common_enums::RefundStatus {
+ fn from(item: PeachpaymentsRefundStatus) -> Self {
+ match item {
+ PeachpaymentsRefundStatus::ApprovedConfirmed => Self::Success,
+ PeachpaymentsRefundStatus::Failed | PeachpaymentsRefundStatus::Declined => {
+ Self::Failure
+ }
+ }
+ }
+}
+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
-#[serde[rename_all = "camelCase"]]
+#[serde(rename_all = "camelCase")]
pub struct PeachpaymentsPaymentsData {
pub transaction_id: String,
pub response_code: Option<ResponseCode>,
@@ -671,9 +725,128 @@ pub struct PeachpaymentsPaymentsData {
pub ecommerce_card_payment_only_transaction_data: Option<EcommerceCardPaymentOnlyResponseData>,
}
+#[derive(Debug, Clone, Deserialize, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct PeachpaymentsRsyncResponse {
+ pub transaction_id: String,
+ pub transaction_result: PeachpaymentsRefundStatus,
+ pub response_code: Option<ResponseCode>,
+}
+
+#[derive(Debug, Clone, Deserialize, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct PeachpaymentsRefundResponse {
+ pub transaction_id: String,
+ pub original_transaction_id: Option<String>,
+ pub reference_id: String,
+ pub transaction_result: PeachpaymentsRefundStatus,
+ pub response_code: Option<ResponseCode>,
+ pub refund_balance_data: Option<RefundBalanceData>,
+}
+
+#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
+#[serde(rename_all = "snake_case")]
+pub enum PeachpaymentsRefundStatus {
+ ApprovedConfirmed,
+ Declined,
+ Failed,
+}
+
+#[derive(Debug, Clone, Deserialize, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct RefundBalanceData {
+ pub amount: AmountDetails,
+ pub balance: AmountDetails,
+ pub refund_history: Vec<RefundHistory>,
+}
+
+#[derive(Debug, Clone, Deserialize, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct RefundHistory {
+ pub transaction_id: String,
+ pub reference_id: String,
+ pub amount: AmountDetails,
+}
+
+impl<F>
+ TryFrom<ResponseRouterData<F, PeachpaymentsRefundResponse, RefundsData, RefundsResponseData>>
+ for RouterData<F, RefundsData, RefundsResponseData>
+{
+ type Error = error_stack::Report<errors::ConnectorError>;
+ fn try_from(
+ item: ResponseRouterData<F, PeachpaymentsRefundResponse, RefundsData, RefundsResponseData>,
+ ) -> Result<Self, Self::Error> {
+ let refund_status = common_enums::RefundStatus::from(item.response.transaction_result);
+ let response = if refund_status == storage_enums::RefundStatus::Failure {
+ Err(ErrorResponse {
+ code: get_error_code(item.response.response_code.as_ref()),
+ message: get_error_message(item.response.response_code.as_ref()),
+ reason: None,
+ status_code: item.http_code,
+ attempt_status: None,
+ connector_transaction_id: Some(item.response.transaction_id),
+ network_advice_code: None,
+ network_decline_code: None,
+ network_error_message: None,
+ connector_metadata: None,
+ })
+ } else {
+ Ok(RefundsResponseData {
+ connector_refund_id: item.response.transaction_id,
+ refund_status,
+ })
+ };
+ Ok(Self {
+ response,
+ ..item.data
+ })
+ }
+}
+
+impl
+ TryFrom<ResponseRouterData<RSync, PeachpaymentsRsyncResponse, RefundsData, RefundsResponseData>>
+ for RefundsRouterData<RSync>
+{
+ type Error = error_stack::Report<errors::ConnectorError>;
+ fn try_from(
+ item: ResponseRouterData<
+ RSync,
+ PeachpaymentsRsyncResponse,
+ RefundsData,
+ RefundsResponseData,
+ >,
+ ) -> Result<Self, Self::Error> {
+ let refund_status = item.response.transaction_result.into();
+ let response = if refund_status == storage_enums::RefundStatus::Failure {
+ Err(ErrorResponse {
+ code: get_error_code(item.response.response_code.as_ref()),
+ message: get_error_message(item.response.response_code.as_ref()),
+ reason: None,
+ status_code: item.http_code,
+ attempt_status: None,
+ connector_transaction_id: Some(item.response.transaction_id),
+ network_advice_code: None,
+ network_decline_code: None,
+ network_error_message: None,
+ connector_metadata: None,
+ })
+ } else {
+ Ok(RefundsResponseData {
+ connector_refund_id: item.response.transaction_id,
+ refund_status,
+ })
+ };
+
+ Ok(Self {
+ response,
+ ..item.data
+ })
+ }
+}
+
// Confirm Transaction Response
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
-#[serde[rename_all = "camelCase"]]
+#[serde(rename_all = "camelCase")]
pub struct PeachpaymentsConfirmResponse {
pub transaction_id: String,
pub response_code: Option<ResponseCode>,
@@ -682,7 +855,7 @@ pub struct PeachpaymentsConfirmResponse {
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
-#[serde[rename_all = "camelCase"]]
+#[serde(rename_all = "camelCase")]
#[serde(untagged)]
pub enum ResponseCode {
Text(String),
@@ -718,7 +891,7 @@ impl ResponseCode {
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
-#[serde[rename_all = "camelCase"]]
+#[serde(rename_all = "camelCase")]
pub struct EcommerceCardPaymentOnlyResponseData {
pub amount: Option<AmountDetails>,
pub stan: Option<Secret<String>>,
diff --git a/cypress-tests/cypress/e2e/configs/Payment/Peachpayments.js b/cypress-tests/cypress/e2e/configs/Payment/Peachpayments.js
index 73bfa95f6f..ecf55c6b0c 100644
--- a/cypress-tests/cypress/e2e/configs/Payment/Peachpayments.js
+++ b/cypress-tests/cypress/e2e/configs/Payment/Peachpayments.js
@@ -1,5 +1,4 @@
import { customerAcceptance } from "./Commons";
-import { getCustomExchange } from "./Modifiers";
const successfulNo3DSCardDetails = {
card_number: "4242424242424242",
@@ -238,117 +237,55 @@ export const connectorDetails = {
},
},
},
- Refund: getCustomExchange({
- Configs: {
- TRIGGER_SKIP: true,
- },
+ Refund: {
Request: {
amount: 6000,
},
Response: {
- status: 501,
- body: {
- error: {
- type: "invalid_request",
- message:
- "This Payment could not be refund because it has a status of requires_capture. The expected state is succeeded, partially_captured",
- code: "IR_14",
- },
- },
- },
- ResponseCustom: {
- status: 501,
+ status: 200,
body: {
- error: {
- type: "invalid_request",
- message: "Execute is not implemented",
- code: "IR_00",
- },
+ status: "succeeded",
},
},
- }),
- PartialRefund: getCustomExchange({
- Configs: {
- TRIGGER_SKIP: true,
- },
+ },
+ PartialRefund: {
Request: {
amount: 2000,
},
Response: {
- status: 501,
- body: {
- error: {
- type: "invalid_request",
- message:
- "This Payment could not be refund because it has a status of requires_capture. The expected state is succeeded, partially_captured",
- code: "IR_14",
- },
- },
- },
- ResponseCustom: {
- status: 501,
+ status: 200,
body: {
- error: {
- type: "invalid_request",
- message: "Execute is not implemented",
- code: "IR_00",
- },
+ status: "succeeded",
},
},
- }),
- manualPaymentRefund: getCustomExchange({
- Configs: {
- TRIGGER_SKIP: true,
- },
+ },
+ manualPaymentRefund: {
Request: {
amount: 6000,
},
Response: {
status: 200,
body: {
- status: "failed",
- },
- },
- ResponseCustom: {
- status: 501,
- body: {
- error: {
- type: "invalid_request",
- message: "Execute is not implemented",
- code: "IR_00",
- },
+ status: "succeeded",
},
},
- }),
- manualPaymentPartialRefund: getCustomExchange({
- Configs: {
- TRIGGER_SKIP: true,
- },
+ },
+ manualPaymentPartialRefund: {
Request: {
amount: 2000,
},
Response: {
status: 200,
body: {
- status: "failed",
- },
- },
- ResponseCustom: {
- status: 501,
- body: {
- error: {
- type: "invalid_request",
- message: "Execute is not implemented",
- code: "IR_00",
- },
+ status: "succeeded",
},
},
- }),
+ },
SyncRefund: {
Response: {
status: 200,
body: {
- status: "failed",
+ status: "succeeded",
},
},
},