feat(connector): [CRYPTOPAY] Report underpaid/overpaid amount in outgoing webhooks (#4468)

This commit is contained in:
DEEPANSHU BANSAL
2024-04-26 16:56:14 +05:30
committed by GitHub
parent b2b9fab31d
commit cc1051da99
10 changed files with 59 additions and 23 deletions

View File

@ -287,11 +287,14 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
event_builder.map(|i| i.set_response_body(&response));
router_env::logger::info!(connector_response=?response);
types::RouterData::try_from(types::ResponseRouterData {
response,
data: data.clone(),
http_code: res.status_code,
})
types::RouterData::try_from((
types::ResponseRouterData {
response,
data: data.clone(),
http_code: res.status_code,
},
data.request.currency,
))
}
fn get_error_response(
@ -371,11 +374,14 @@ impl ConnectorIntegration<api::PSync, types::PaymentsSyncData, types::PaymentsRe
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
event_builder.map(|i| i.set_response_body(&response));
router_env::logger::info!(connector_response=?response);
types::RouterData::try_from(types::ResponseRouterData {
response,
data: data.clone(),
http_code: res.status_code,
})
types::RouterData::try_from((
types::ResponseRouterData {
response,
data: data.clone(),
http_code: res.status_code,
},
data.request.currency,
))
}
fn get_error_response(

View File

@ -1,8 +1,10 @@
use common_utils::pii;
use error_stack::ResultExt;
use masking::Secret;
use reqwest::Url;
use serde::{Deserialize, Serialize};
use super::utils as connector_utils;
use crate::{
connector::utils::{self, is_payment_failure, CryptoData, PaymentsAuthorizeRequestData},
consts,
@ -146,17 +148,17 @@ pub struct CryptopayPaymentsResponse {
}
impl<F, T>
TryFrom<types::ResponseRouterData<F, CryptopayPaymentsResponse, T, types::PaymentsResponseData>>
for types::RouterData<F, T, types::PaymentsResponseData>
TryFrom<(
types::ResponseRouterData<F, CryptopayPaymentsResponse, T, types::PaymentsResponseData>,
diesel_models::enums::Currency,
)> for types::RouterData<F, T, types::PaymentsResponseData>
{
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(
item: types::ResponseRouterData<
F,
CryptopayPaymentsResponse,
T,
types::PaymentsResponseData,
>,
(item, currency): (
types::ResponseRouterData<F, CryptopayPaymentsResponse, T, types::PaymentsResponseData>,
diesel_models::enums::Currency,
),
) -> Result<Self, Self::Error> {
let status = enums::AttemptStatus::from(item.response.data.status.clone());
let response = if is_payment_failure(status) {
@ -197,11 +199,28 @@ impl<F, T>
incremental_authorization_allowed: None,
})
};
Ok(Self {
status,
response,
..item.data
})
match item.response.data.price_amount {
Some(price_amount) => {
let amount_captured = Some(
connector_utils::to_currency_lower_unit(price_amount, currency)?
.parse::<i64>()
.change_context(errors::ConnectorError::ParsingFailed)?,
);
Ok(Self {
status,
response,
amount_captured,
..item.data
})
}
None => Ok(Self {
status,
response,
..item.data
}),
}
}
}

View File

@ -1220,6 +1220,7 @@ impl<F: Clone> TryFrom<PaymentAdditionalData<'_, F>> for types::PaymentsSyncData
None => types::SyncRequestType::SinglePaymentSync,
},
payment_method_type: payment_data.payment_attempt.payment_method_type,
currency: payment_data.currency,
})
}
}

View File

@ -565,6 +565,7 @@ pub struct PaymentsSyncData {
pub sync_type: SyncRequestType,
pub mandate_id: Option<api_models::payments::MandateIds>,
pub payment_method_type: Option<storage_enums::PaymentMethodType>,
pub currency: storage_enums::Currency,
}
#[derive(Debug, Default, Clone)]

View File

@ -109,6 +109,7 @@ async fn should_sync_authorized_payment() {
sync_type: types::SyncRequestType::SinglePaymentSync,
connector_meta: None,
payment_method_type: None,
currency: enums::Currency::USD,
}),
None,
)
@ -224,6 +225,7 @@ async fn should_sync_auto_captured_payment() {
sync_type: types::SyncRequestType::SinglePaymentSync,
connector_meta: None,
payment_method_type: None,
currency: enums::Currency::USD,
}),
None,
)

View File

@ -157,6 +157,7 @@ async fn should_sync_authorized_payment() {
connector_meta: None,
mandate_id: None,
payment_method_type: None,
currency: enums::Currency::USD,
}),
get_default_payment_info(),
)

View File

@ -125,6 +125,7 @@ async fn should_sync_authorized_payment() {
connector_meta,
mandate_id: None,
payment_method_type: None,
currency: enums::Currency::EUR,
}),
None,
)

View File

@ -142,6 +142,7 @@ async fn should_sync_authorized_payment() {
sync_type: types::SyncRequestType::SinglePaymentSync,
connector_meta,
payment_method_type: None,
currency: enums::Currency::USD,
}),
get_default_payment_info(),
)
@ -339,6 +340,7 @@ async fn should_sync_auto_captured_payment() {
sync_type: types::SyncRequestType::SinglePaymentSync,
connector_meta,
payment_method_type: None,
currency: enums::Currency::USD,
}),
get_default_payment_info(),
)

View File

@ -997,6 +997,7 @@ impl Default for PaymentSyncType {
sync_type: types::SyncRequestType::SinglePaymentSync,
connector_meta: None,
payment_method_type: None,
currency: enums::Currency::USD,
};
Self(data)
}

View File

@ -104,6 +104,7 @@ async fn should_sync_authorized_payment() {
connector_meta: None,
mandate_id: None,
payment_method_type: None,
currency: enums::Currency::USD,
}),
None,
)
@ -219,6 +220,7 @@ async fn should_sync_auto_captured_payment() {
connector_meta: None,
mandate_id: None,
payment_method_type: None,
currency: enums::Currency::USD,
}),
None,
)