mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-29 17:19:15 +08:00
feat(router): add integrity check for refund refund sync and capture flow with stripe as connector (#5187)
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com> Co-authored-by: Hrithikesh <61539176+hrithikesh026@users.noreply.github.com> Co-authored-by: Narayan Bhat <narayan.bhat@juspay.in> Co-authored-by: Sahkal Poddar <sahkalpoddar@Sahkals-MacBook-Air.local>
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
use common_utils::errors::IntegrityCheckError;
|
||||
use hyperswitch_domain_models::router_request_types::{
|
||||
AuthoriseIntegrityObject, PaymentsAuthorizeData, PaymentsSyncData, SyncIntegrityObject,
|
||||
AuthoriseIntegrityObject, CaptureIntegrityObject, PaymentsAuthorizeData, PaymentsCaptureData,
|
||||
PaymentsSyncData, RefundIntegrityObject, RefundsData, SyncIntegrityObject,
|
||||
};
|
||||
|
||||
/// Connector Integrity trait to check connector data integrity
|
||||
@ -33,6 +34,30 @@ pub trait CheckIntegrity<Request, T> {
|
||||
) -> Result<(), IntegrityCheckError>;
|
||||
}
|
||||
|
||||
impl<T, Request> CheckIntegrity<Request, T> for RefundsData
|
||||
where
|
||||
T: FlowIntegrity,
|
||||
Request: GetIntegrityObject<T>,
|
||||
{
|
||||
fn check_integrity(
|
||||
&self,
|
||||
request: &Request,
|
||||
connector_refund_id: Option<String>,
|
||||
) -> Result<(), IntegrityCheckError> {
|
||||
match request.get_response_integrity_object() {
|
||||
Some(res_integrity_object) => {
|
||||
let req_integrity_object = request.get_request_integrity_object();
|
||||
T::compare(
|
||||
req_integrity_object,
|
||||
res_integrity_object,
|
||||
connector_refund_id,
|
||||
)
|
||||
}
|
||||
None => Ok(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, Request> CheckIntegrity<Request, T> for PaymentsAuthorizeData
|
||||
where
|
||||
T: FlowIntegrity,
|
||||
@ -57,6 +82,30 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, Request> CheckIntegrity<Request, T> for PaymentsCaptureData
|
||||
where
|
||||
T: FlowIntegrity,
|
||||
Request: GetIntegrityObject<T>,
|
||||
{
|
||||
fn check_integrity(
|
||||
&self,
|
||||
request: &Request,
|
||||
connector_transaction_id: Option<String>,
|
||||
) -> Result<(), IntegrityCheckError> {
|
||||
match request.get_response_integrity_object() {
|
||||
Some(res_integrity_object) => {
|
||||
let req_integrity_object = request.get_request_integrity_object();
|
||||
T::compare(
|
||||
req_integrity_object,
|
||||
res_integrity_object,
|
||||
connector_transaction_id,
|
||||
)
|
||||
}
|
||||
None => Ok(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, Request> CheckIntegrity<Request, T> for PaymentsSyncData
|
||||
where
|
||||
T: FlowIntegrity,
|
||||
@ -81,6 +130,36 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl FlowIntegrity for RefundIntegrityObject {
|
||||
type IntegrityObject = Self;
|
||||
fn compare(
|
||||
req_integrity_object: Self,
|
||||
res_integrity_object: Self,
|
||||
connector_transaction_id: Option<String>,
|
||||
) -> Result<(), IntegrityCheckError> {
|
||||
let mut mismatched_fields = Vec::new();
|
||||
|
||||
if req_integrity_object.currency != res_integrity_object.currency {
|
||||
mismatched_fields.push("currency".to_string());
|
||||
}
|
||||
|
||||
if req_integrity_object.refund_amount != res_integrity_object.refund_amount {
|
||||
mismatched_fields.push("refund_amount".to_string());
|
||||
}
|
||||
|
||||
if mismatched_fields.is_empty() {
|
||||
Ok(())
|
||||
} else {
|
||||
let field_names = mismatched_fields.join(", ");
|
||||
|
||||
Err(IntegrityCheckError {
|
||||
field_names,
|
||||
connector_transaction_id,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FlowIntegrity for AuthoriseIntegrityObject {
|
||||
type IntegrityObject = Self;
|
||||
fn compare(
|
||||
@ -120,9 +199,63 @@ impl FlowIntegrity for SyncIntegrityObject {
|
||||
) -> Result<(), IntegrityCheckError> {
|
||||
let mut mismatched_fields = Vec::new();
|
||||
|
||||
if req_integrity_object.amount != res_integrity_object.amount {
|
||||
mismatched_fields.push("amount".to_string());
|
||||
res_integrity_object
|
||||
.captured_amount
|
||||
.zip(req_integrity_object.captured_amount)
|
||||
.map(|tup| {
|
||||
if tup.0 != tup.1 {
|
||||
mismatched_fields.push("captured_amount".to_string());
|
||||
}
|
||||
});
|
||||
|
||||
res_integrity_object
|
||||
.amount
|
||||
.zip(req_integrity_object.amount)
|
||||
.map(|tup| {
|
||||
if tup.0 != tup.1 {
|
||||
mismatched_fields.push("amount".to_string());
|
||||
}
|
||||
});
|
||||
|
||||
res_integrity_object
|
||||
.currency
|
||||
.zip(req_integrity_object.currency)
|
||||
.map(|tup| {
|
||||
if tup.0 != tup.1 {
|
||||
mismatched_fields.push("currency".to_string());
|
||||
}
|
||||
});
|
||||
|
||||
if mismatched_fields.is_empty() {
|
||||
Ok(())
|
||||
} else {
|
||||
let field_names = mismatched_fields.join(", ");
|
||||
|
||||
Err(IntegrityCheckError {
|
||||
field_names,
|
||||
connector_transaction_id,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FlowIntegrity for CaptureIntegrityObject {
|
||||
type IntegrityObject = Self;
|
||||
fn compare(
|
||||
req_integrity_object: Self,
|
||||
res_integrity_object: Self,
|
||||
connector_transaction_id: Option<String>,
|
||||
) -> Result<(), IntegrityCheckError> {
|
||||
let mut mismatched_fields = Vec::new();
|
||||
|
||||
res_integrity_object
|
||||
.capture_amount
|
||||
.zip(req_integrity_object.capture_amount)
|
||||
.map(|tup| {
|
||||
if tup.0 != tup.1 {
|
||||
mismatched_fields.push("capture_amount".to_string());
|
||||
}
|
||||
});
|
||||
|
||||
if req_integrity_object.currency != res_integrity_object.currency {
|
||||
mismatched_fields.push("currency".to_string());
|
||||
@ -141,6 +274,32 @@ impl FlowIntegrity for SyncIntegrityObject {
|
||||
}
|
||||
}
|
||||
|
||||
impl GetIntegrityObject<CaptureIntegrityObject> for PaymentsCaptureData {
|
||||
fn get_response_integrity_object(&self) -> Option<CaptureIntegrityObject> {
|
||||
self.integrity_object.clone()
|
||||
}
|
||||
|
||||
fn get_request_integrity_object(&self) -> CaptureIntegrityObject {
|
||||
CaptureIntegrityObject {
|
||||
capture_amount: Some(self.minor_amount_to_capture),
|
||||
currency: self.currency,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl GetIntegrityObject<RefundIntegrityObject> for RefundsData {
|
||||
fn get_response_integrity_object(&self) -> Option<RefundIntegrityObject> {
|
||||
self.integrity_object.clone()
|
||||
}
|
||||
|
||||
fn get_request_integrity_object(&self) -> RefundIntegrityObject {
|
||||
RefundIntegrityObject {
|
||||
currency: self.currency,
|
||||
refund_amount: self.minor_refund_amount,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl GetIntegrityObject<AuthoriseIntegrityObject> for PaymentsAuthorizeData {
|
||||
fn get_response_integrity_object(&self) -> Option<AuthoriseIntegrityObject> {
|
||||
self.integrity_object.clone()
|
||||
@ -163,6 +322,7 @@ impl GetIntegrityObject<SyncIntegrityObject> for PaymentsSyncData {
|
||||
SyncIntegrityObject {
|
||||
amount: Some(self.amount),
|
||||
currency: Some(self.currency),
|
||||
captured_amount: self.captured_amount,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user