feat(connector): [BitPay] Currency Unit Conversion (#2736)

This commit is contained in:
Adarsh Jha
2023-10-31 19:28:58 +05:30
committed by GitHub
parent 0a44f5699e
commit e377279d9c
2 changed files with 57 additions and 12 deletions

View File

@ -82,6 +82,10 @@ impl ConnectorCommon for Bitpay {
"bitpay"
}
fn get_currency_unit(&self) -> api::CurrencyUnit {
api::CurrencyUnit::Minor
}
fn common_get_content_type(&self) -> &'static str {
"application/json"
}
@ -169,7 +173,13 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P
&self,
req: &types::PaymentsAuthorizeRouterData,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
let req_obj = bitpay::BitpayPaymentsRequest::try_from(req)?;
let connector_router_data = bitpay::BitpayRouterData::try_from((
&self.get_currency_unit(),
req.request.currency,
req.request.amount,
req,
))?;
let req_obj = bitpay::BitpayPaymentsRequest::try_from(&connector_router_data)?;
let bitpay_req = types::RequestBody::log_and_get_request_body(
&req_obj,

View File

@ -9,6 +9,37 @@ use crate::{
types::{self, api, storage::enums, ConnectorAuthType},
};
#[derive(Debug, Serialize)]
pub struct BitpayRouterData<T> {
pub amount: i64,
pub router_data: T,
}
impl<T>
TryFrom<(
&types::api::CurrencyUnit,
types::storage::enums::Currency,
i64,
T,
)> for BitpayRouterData<T>
{
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(
(_currency_unit, _currency, amount, router_data): (
&types::api::CurrencyUnit,
types::storage::enums::Currency,
i64,
T,
),
) -> Result<Self, Self::Error> {
Ok(Self {
amount,
router_data,
})
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum TransactionSpeed {
@ -31,9 +62,11 @@ pub struct BitpayPaymentsRequest {
token: Secret<String>,
}
impl TryFrom<&types::PaymentsAuthorizeRouterData> for BitpayPaymentsRequest {
impl TryFrom<&BitpayRouterData<&types::PaymentsAuthorizeRouterData>> for BitpayPaymentsRequest {
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(item: &types::PaymentsAuthorizeRouterData) -> Result<Self, Self::Error> {
fn try_from(
item: &BitpayRouterData<&types::PaymentsAuthorizeRouterData>,
) -> Result<Self, Self::Error> {
get_crypto_specific_payment_data(item)
}
}
@ -152,11 +185,13 @@ pub struct BitpayRefundRequest {
pub amount: i64,
}
impl<F> TryFrom<&types::RefundsRouterData<F>> for BitpayRefundRequest {
impl<F> TryFrom<&BitpayRouterData<&types::RefundsRouterData<F>>> for BitpayRefundRequest {
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(item: &types::RefundsRouterData<F>) -> Result<Self, Self::Error> {
fn try_from(
item: &BitpayRouterData<&types::RefundsRouterData<F>>,
) -> Result<Self, Self::Error> {
Ok(Self {
amount: item.request.refund_amount,
amount: item.router_data.request.refund_amount,
})
}
}
@ -232,14 +267,14 @@ pub struct BitpayErrorResponse {
}
fn get_crypto_specific_payment_data(
item: &types::PaymentsAuthorizeRouterData,
item: &BitpayRouterData<&types::PaymentsAuthorizeRouterData>,
) -> Result<BitpayPaymentsRequest, error_stack::Report<errors::ConnectorError>> {
let price = item.request.amount;
let currency = item.request.currency.to_string();
let redirect_url = item.request.get_return_url()?;
let notification_url = item.request.get_webhook_url()?;
let price = item.amount;
let currency = item.router_data.request.currency.to_string();
let redirect_url = item.router_data.request.get_return_url()?;
let notification_url = item.router_data.request.get_webhook_url()?;
let transaction_speed = TransactionSpeed::Medium;
let auth_type = item.connector_auth_type.clone();
let auth_type = item.router_data.connector_auth_type.clone();
let token = match auth_type {
ConnectorAuthType::HeaderKey { api_key } => api_key,
_ => String::default().into(),