Skip to content

Commit

Permalink
feat(connector): [CRYPTOPAY] Report underpaid/overpaid amount in outg…
Browse files Browse the repository at this point in the history
…oing webhooks (#4468)
  • Loading branch information
deepanshu-iiitu authored Apr 26, 2024
1 parent b2b9fab commit cc1051d
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 23 deletions.
26 changes: 16 additions & 10 deletions crates/router/src/connector/cryptopay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
45 changes: 32 additions & 13 deletions crates/router/src/connector/cryptopay/transformers.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}),
}
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/router/src/core/payments/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/router/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
2 changes: 2 additions & 0 deletions crates/router/tests/connectors/bambora.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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,
)
Expand Down
1 change: 1 addition & 0 deletions crates/router/tests/connectors/forte.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
Expand Down
1 change: 1 addition & 0 deletions crates/router/tests/connectors/nexinets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ async fn should_sync_authorized_payment() {
connector_meta,
mandate_id: None,
payment_method_type: None,
currency: enums::Currency::EUR,
}),
None,
)
Expand Down
2 changes: 2 additions & 0 deletions crates/router/tests/connectors/paypal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
Expand Down Expand Up @@ -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(),
)
Expand Down
1 change: 1 addition & 0 deletions crates/router/tests/connectors/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 2 additions & 0 deletions crates/router/tests/connectors/zen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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,
)
Expand Down

0 comments on commit cc1051d

Please sign in to comment.