mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-03 05:17:02 +08:00
refactor(connector): added amount conversion framework for bitpay (#4973)
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
This commit is contained in:
@ -1,13 +1,17 @@
|
||||
pub mod transformers;
|
||||
|
||||
use std::fmt::Debug;
|
||||
|
||||
use common_utils::{errors::ReportSwitchExt, ext_traits::ByteSliceExt, request::RequestContent};
|
||||
use common_utils::{
|
||||
errors::ReportSwitchExt,
|
||||
ext_traits::ByteSliceExt,
|
||||
request::RequestContent,
|
||||
types::{AmountConvertor, MinorUnit, MinorUnitForConnector},
|
||||
};
|
||||
use error_stack::ResultExt;
|
||||
use masking::PeekInterface;
|
||||
use transformers as bitpay;
|
||||
|
||||
use self::bitpay::BitpayWebhookDetails;
|
||||
use super::utils as connector_utils;
|
||||
use crate::{
|
||||
configs::settings,
|
||||
consts,
|
||||
@ -27,8 +31,18 @@ use crate::{
|
||||
utils::BytesExt,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Bitpay;
|
||||
#[derive(Clone)]
|
||||
pub struct Bitpay {
|
||||
amount_converter: &'static (dyn AmountConvertor<Output = MinorUnit> + Sync),
|
||||
}
|
||||
|
||||
impl Bitpay {
|
||||
pub fn new() -> &'static Self {
|
||||
&Self {
|
||||
amount_converter: &MinorUnitForConnector,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl api::Payment for Bitpay {}
|
||||
impl api::PaymentToken for Bitpay {}
|
||||
@ -195,12 +209,13 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P
|
||||
req: &types::PaymentsAuthorizeRouterData,
|
||||
_connectors: &settings::Connectors,
|
||||
) -> CustomResult<RequestContent, errors::ConnectorError> {
|
||||
let connector_router_data = bitpay::BitpayRouterData::try_from((
|
||||
&self.get_currency_unit(),
|
||||
let amount = connector_utils::convert_amount(
|
||||
self.amount_converter,
|
||||
req.request.minor_amount,
|
||||
req.request.currency,
|
||||
req.request.amount,
|
||||
req,
|
||||
))?;
|
||||
)?;
|
||||
|
||||
let connector_router_data = bitpay::BitpayRouterData::from((amount, req));
|
||||
let connector_req = bitpay::BitpayPaymentsRequest::try_from(&connector_router_data)?;
|
||||
|
||||
Ok(RequestContent::Json(Box::new(connector_req)))
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
use common_utils::types::MinorUnit;
|
||||
use masking::Secret;
|
||||
use reqwest::Url;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@ -11,25 +12,16 @@ use crate::{
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct BitpayRouterData<T> {
|
||||
pub amount: i64,
|
||||
pub amount: MinorUnit,
|
||||
pub router_data: T,
|
||||
}
|
||||
|
||||
impl<T> TryFrom<(&api::CurrencyUnit, enums::Currency, i64, T)> for BitpayRouterData<T> {
|
||||
type Error = error_stack::Report<errors::ConnectorError>;
|
||||
|
||||
fn try_from(
|
||||
(_currency_unit, _currency, amount, router_data): (
|
||||
&api::CurrencyUnit,
|
||||
enums::Currency,
|
||||
i64,
|
||||
T,
|
||||
),
|
||||
) -> Result<Self, Self::Error> {
|
||||
Ok(Self {
|
||||
impl<T> From<(MinorUnit, T)> for BitpayRouterData<T> {
|
||||
fn from((amount, router_data): (MinorUnit, T)) -> Self {
|
||||
Self {
|
||||
amount,
|
||||
router_data,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -45,7 +37,7 @@ pub enum TransactionSpeed {
|
||||
#[derive(Default, Debug, Serialize, Eq, PartialEq)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct BitpayPaymentsRequest {
|
||||
price: i64,
|
||||
price: MinorUnit,
|
||||
currency: String,
|
||||
#[serde(rename = "redirectURL")]
|
||||
redirect_url: String,
|
||||
@ -119,10 +111,10 @@ pub enum ExceptionStatus {
|
||||
pub struct BitpayPaymentResponseData {
|
||||
pub url: Option<Url>,
|
||||
pub status: BitpayPaymentStatus,
|
||||
pub price: i64,
|
||||
pub price: MinorUnit,
|
||||
pub currency: String,
|
||||
pub amount_paid: i64,
|
||||
pub invoice_time: Option<i64>,
|
||||
pub amount_paid: MinorUnit,
|
||||
pub invoice_time: Option<MinorUnit>,
|
||||
pub rate_refresh_time: Option<i64>,
|
||||
pub expiration_time: Option<i64>,
|
||||
pub current_time: Option<i64>,
|
||||
@ -183,7 +175,7 @@ impl<F, T>
|
||||
// Type definition for RefundRequest
|
||||
#[derive(Default, Debug, Serialize)]
|
||||
pub struct BitpayRefundRequest {
|
||||
pub amount: i64,
|
||||
pub amount: MinorUnit,
|
||||
}
|
||||
|
||||
impl<F> TryFrom<&BitpayRouterData<&types::RefundsRouterData<F>>> for BitpayRefundRequest {
|
||||
@ -192,7 +184,7 @@ impl<F> TryFrom<&BitpayRouterData<&types::RefundsRouterData<F>>> for BitpayRefun
|
||||
item: &BitpayRouterData<&types::RefundsRouterData<F>>,
|
||||
) -> Result<Self, Self::Error> {
|
||||
Ok(Self {
|
||||
amount: item.router_data.request.refund_amount,
|
||||
amount: item.amount,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -367,7 +367,9 @@ impl ConnectorData {
|
||||
enums::Connector::Billwerk => {
|
||||
Ok(ConnectorEnum::Old(Box::new(connector::Billwerk::new())))
|
||||
}
|
||||
enums::Connector::Bitpay => Ok(ConnectorEnum::Old(Box::new(&connector::Bitpay))),
|
||||
enums::Connector::Bitpay => {
|
||||
Ok(ConnectorEnum::Old(Box::new(connector::Bitpay::new())))
|
||||
}
|
||||
enums::Connector::Bluesnap => {
|
||||
Ok(ConnectorEnum::Old(Box::new(connector::Bluesnap::new())))
|
||||
}
|
||||
|
||||
@ -13,7 +13,7 @@ impl utils::Connector for BitpayTest {
|
||||
fn get_data(&self) -> api::ConnectorData {
|
||||
use router::connector::Bitpay;
|
||||
utils::construct_connector_data_old(
|
||||
Box::new(&Bitpay),
|
||||
Box::new(Bitpay::new()),
|
||||
types::Connector::Bitpay,
|
||||
api::GetToken::Connector,
|
||||
None,
|
||||
|
||||
Reference in New Issue
Block a user