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