refactor(connector): [Payme]Enhance currency Mapping with ConnectorCurrencyCommon Trait (#2194)

This commit is contained in:
chikke srujan
2023-09-27 16:50:23 +05:30
committed by GitHub
parent 22a8291ea6
commit 77b51d5cbe
2 changed files with 107 additions and 40 deletions

View File

@ -11,7 +11,7 @@ use transformers as payme;
use crate::{ use crate::{
configs::settings, configs::settings,
connector::utils as connector_utils, connector::{utils as connector_utils, utils::PaymentsPreProcessingData},
core::{ core::{
errors::{self, CustomResult}, errors::{self, CustomResult},
payments, payments,
@ -65,6 +65,10 @@ impl ConnectorCommon for Payme {
"payme" "payme"
} }
fn get_currency_unit(&self) -> api::CurrencyUnit {
api::CurrencyUnit::Minor
}
fn common_get_content_type(&self) -> &'static str { fn common_get_content_type(&self) -> &'static str {
"application/json" "application/json"
} }
@ -249,7 +253,11 @@ impl
&self, &self,
req: &types::PaymentsPreProcessingRouterData, req: &types::PaymentsPreProcessingRouterData,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> { ) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
let req_obj = payme::GenerateSaleRequest::try_from(req)?; let amount = req.request.get_amount()?;
let currency = req.request.get_currency()?;
let connector_router_data =
payme::PaymeRouterData::try_from((&self.get_currency_unit(), currency, amount, req))?;
let req_obj = payme::GenerateSaleRequest::try_from(&connector_router_data)?;
let payme_req = types::RequestBody::log_and_get_request_body( let payme_req = types::RequestBody::log_and_get_request_body(
&req_obj, &req_obj,
utils::Encode::<payme::GenerateSaleRequest>::encode_to_string_of_json, utils::Encode::<payme::GenerateSaleRequest>::encode_to_string_of_json,
@ -464,7 +472,13 @@ impl ConnectorIntegration<api::Authorize, types::PaymentsAuthorizeData, types::P
&self, &self,
req: &types::PaymentsAuthorizeRouterData, req: &types::PaymentsAuthorizeRouterData,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> { ) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
let req_obj = payme::PaymePaymentRequest::try_from(req)?; let connector_router_data = payme::PaymeRouterData::try_from((
&self.get_currency_unit(),
req.request.currency,
req.request.amount,
req,
))?;
let req_obj = payme::PaymePaymentRequest::try_from(&connector_router_data)?;
let payme_req = types::RequestBody::log_and_get_request_body( let payme_req = types::RequestBody::log_and_get_request_body(
&req_obj, &req_obj,
utils::Encode::<payme::PayRequest>::encode_to_string_of_json, utils::Encode::<payme::PayRequest>::encode_to_string_of_json,
@ -637,7 +651,13 @@ impl ConnectorIntegration<api::Capture, types::PaymentsCaptureData, types::Payme
&self, &self,
req: &types::PaymentsCaptureRouterData, req: &types::PaymentsCaptureRouterData,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> { ) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
let req_obj = payme::PaymentCaptureRequest::try_from(req)?; let connector_router_data = payme::PaymeRouterData::try_from((
&self.get_currency_unit(),
req.request.currency,
req.request.amount_to_capture,
req,
))?;
let req_obj = payme::PaymentCaptureRequest::try_from(&connector_router_data)?;
let payme_req = types::RequestBody::log_and_get_request_body( let payme_req = types::RequestBody::log_and_get_request_body(
&req_obj, &req_obj,
utils::Encode::<payme::PaymentCaptureRequest>::encode_to_string_of_json, utils::Encode::<payme::PaymentCaptureRequest>::encode_to_string_of_json,
@ -737,7 +757,13 @@ impl ConnectorIntegration<api::Execute, types::RefundsData, types::RefundsRespon
&self, &self,
req: &types::RefundsRouterData<api::Execute>, req: &types::RefundsRouterData<api::Execute>,
) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> { ) -> CustomResult<Option<types::RequestBody>, errors::ConnectorError> {
let req_obj = payme::PaymeRefundRequest::try_from(req)?; let connector_router_data = payme::PaymeRouterData::try_from((
&self.get_currency_unit(),
req.request.currency,
req.request.refund_amount,
req,
))?;
let req_obj = payme::PaymeRefundRequest::try_from(&connector_router_data)?;
let payme_req = types::RequestBody::log_and_get_request_body( let payme_req = types::RequestBody::log_and_get_request_body(
&req_obj, &req_obj,
utils::Encode::<payme::PaymeRefundRequest>::encode_to_string_of_json, utils::Encode::<payme::PaymeRefundRequest>::encode_to_string_of_json,

View File

@ -24,6 +24,36 @@ use crate::{
const LANGUAGE: &str = "en"; const LANGUAGE: &str = "en";
#[derive(Debug, Serialize)]
pub struct PaymeRouterData<T> {
pub amount: i64,
pub router_data: T,
}
impl<T>
TryFrom<(
&types::api::CurrencyUnit,
types::storage::enums::Currency,
i64,
T,
)> for PaymeRouterData<T>
{
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(
(_currency_unit, _currency, amount, item): (
&types::api::CurrencyUnit,
types::storage::enums::Currency,
i64,
T,
),
) -> Result<Self, Self::Error> {
Ok(Self {
amount,
router_data: item,
})
}
}
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub struct PayRequest { pub struct PayRequest {
buyer_name: Secret<String>, buyer_name: Secret<String>,
@ -311,33 +341,37 @@ pub enum SalePaymentMethod {
ApplePay, ApplePay,
} }
impl TryFrom<&types::PaymentsPreProcessingRouterData> for GenerateSaleRequest { impl TryFrom<&PaymeRouterData<&types::PaymentsPreProcessingRouterData>> for GenerateSaleRequest {
type Error = error_stack::Report<errors::ConnectorError>; type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(item: &types::PaymentsPreProcessingRouterData) -> Result<Self, Self::Error> { fn try_from(
let sale_type = SaleType::try_from(item)?; item: &PaymeRouterData<&types::PaymentsPreProcessingRouterData>,
let seller_payme_id = PaymeAuthType::try_from(&item.connector_auth_type)?.seller_payme_id; ) -> Result<Self, Self::Error> {
let order_details = item.request.get_order_details()?; let sale_type = SaleType::try_from(item.router_data)?;
let services = get_services(item); let seller_payme_id =
PaymeAuthType::try_from(&item.router_data.connector_auth_type)?.seller_payme_id;
let order_details = item.router_data.request.get_order_details()?;
let services = get_services(item.router_data);
let product_name = order_details let product_name = order_details
.first() .first()
.ok_or_else(missing_field_err("order_details"))? .ok_or_else(missing_field_err("order_details"))?
.product_name .product_name
.clone(); .clone();
let pmd = item let pmd = item
.router_data
.request .request
.payment_method_data .payment_method_data
.to_owned() .to_owned()
.ok_or_else(missing_field_err("payment_method_data"))?; .ok_or_else(missing_field_err("payment_method_data"))?;
Ok(Self { Ok(Self {
seller_payme_id, seller_payme_id,
sale_price: item.request.get_amount()?, sale_price: item.amount.to_owned(),
currency: item.request.get_currency()?, currency: item.router_data.request.get_currency()?,
product_name, product_name,
sale_payment_method: SalePaymentMethod::try_from(&pmd)?, sale_payment_method: SalePaymentMethod::try_from(&pmd)?,
sale_type, sale_type,
transaction_id: item.payment_id.clone(), transaction_id: item.router_data.payment_id.clone(),
sale_return_url: item.request.get_return_url()?, sale_return_url: item.router_data.request.get_return_url()?,
sale_callback_url: item.request.get_webhook_url()?, sale_callback_url: item.router_data.request.get_webhook_url()?,
language: LANGUAGE.to_string(), language: LANGUAGE.to_string(),
services, services,
}) })
@ -400,13 +434,15 @@ impl TryFrom<&PaymentMethodData> for SalePaymentMethod {
} }
} }
impl TryFrom<&types::PaymentsAuthorizeRouterData> for PaymePaymentRequest { impl TryFrom<&PaymeRouterData<&types::PaymentsAuthorizeRouterData>> for PaymePaymentRequest {
type Error = error_stack::Report<errors::ConnectorError>; type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(value: &types::PaymentsAuthorizeRouterData) -> Result<Self, Self::Error> { fn try_from(
let payme_request = if value.request.mandate_id.is_some() { value: &PaymeRouterData<&types::PaymentsAuthorizeRouterData>,
) -> Result<Self, Self::Error> {
let payme_request = if value.router_data.request.mandate_id.is_some() {
Self::MandateRequest(MandateRequest::try_from(value)?) Self::MandateRequest(MandateRequest::try_from(value)?)
} else { } else {
Self::PayRequest(PayRequest::try_from(value)?) Self::PayRequest(PayRequest::try_from(value.router_data)?)
}; };
Ok(payme_request) Ok(payme_request)
} }
@ -562,25 +598,28 @@ impl<F>
} }
} }
impl TryFrom<&types::PaymentsAuthorizeRouterData> for MandateRequest { impl TryFrom<&PaymeRouterData<&types::PaymentsAuthorizeRouterData>> for MandateRequest {
type Error = error_stack::Report<errors::ConnectorError>; type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(item: &types::PaymentsAuthorizeRouterData) -> Result<Self, Self::Error> { fn try_from(
let seller_payme_id = PaymeAuthType::try_from(&item.connector_auth_type)?.seller_payme_id; item: &PaymeRouterData<&types::PaymentsAuthorizeRouterData>,
let order_details = item.request.get_order_details()?; ) -> Result<Self, Self::Error> {
let seller_payme_id =
PaymeAuthType::try_from(&item.router_data.connector_auth_type)?.seller_payme_id;
let order_details = item.router_data.request.get_order_details()?;
let product_name = order_details let product_name = order_details
.first() .first()
.ok_or_else(missing_field_err("order_details"))? .ok_or_else(missing_field_err("order_details"))?
.product_name .product_name
.clone(); .clone();
Ok(Self { Ok(Self {
currency: item.request.currency, currency: item.router_data.request.currency,
sale_price: item.request.amount, sale_price: item.amount.to_owned(),
transaction_id: item.payment_id.clone(), transaction_id: item.router_data.payment_id.clone(),
product_name, product_name,
sale_return_url: item.request.get_return_url()?, sale_return_url: item.router_data.request.get_return_url()?,
seller_payme_id, seller_payme_id,
sale_callback_url: item.request.get_webhook_url()?, sale_callback_url: item.router_data.request.get_webhook_url()?,
buyer_key: Secret::new(item.request.get_connector_mandate_id()?), buyer_key: Secret::new(item.router_data.request.get_connector_mandate_id()?),
language: LANGUAGE.to_string(), language: LANGUAGE.to_string(),
}) })
} }
@ -851,18 +890,20 @@ pub struct PaymentCaptureRequest {
sale_price: i64, sale_price: i64,
} }
impl TryFrom<&types::PaymentsCaptureRouterData> for PaymentCaptureRequest { impl TryFrom<&PaymeRouterData<&types::PaymentsCaptureRouterData>> for PaymentCaptureRequest {
type Error = error_stack::Report<errors::ConnectorError>; type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(item: &types::PaymentsCaptureRouterData) -> Result<Self, Self::Error> { fn try_from(
if item.request.amount_to_capture != item.request.payment_amount { item: &PaymeRouterData<&types::PaymentsCaptureRouterData>,
) -> Result<Self, Self::Error> {
if item.router_data.request.amount_to_capture != item.router_data.request.payment_amount {
Err(errors::ConnectorError::NotSupported { Err(errors::ConnectorError::NotSupported {
message: "Partial Capture".to_string(), message: "Partial Capture".to_string(),
connector: "Payme", connector: "Payme",
})? })?
} }
Ok(Self { Ok(Self {
payme_sale_id: item.request.connector_transaction_id.clone(), payme_sale_id: item.router_data.request.connector_transaction_id.clone(),
sale_price: item.request.amount_to_capture, sale_price: item.amount,
}) })
} }
} }
@ -877,14 +918,14 @@ pub struct PaymeRefundRequest {
language: String, language: String,
} }
impl<F> TryFrom<&types::RefundsRouterData<F>> for PaymeRefundRequest { impl<F> TryFrom<&PaymeRouterData<&types::RefundsRouterData<F>>> for PaymeRefundRequest {
type Error = error_stack::Report<errors::ConnectorError>; type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(item: &types::RefundsRouterData<F>) -> Result<Self, Self::Error> { fn try_from(item: &PaymeRouterData<&types::RefundsRouterData<F>>) -> Result<Self, Self::Error> {
let auth_type = PaymeAuthType::try_from(&item.connector_auth_type)?; let auth_type = PaymeAuthType::try_from(&item.router_data.connector_auth_type)?;
Ok(Self { Ok(Self {
payme_sale_id: item.request.connector_transaction_id.clone(), payme_sale_id: item.router_data.request.connector_transaction_id.clone(),
seller_payme_id: auth_type.seller_payme_id, seller_payme_id: auth_type.seller_payme_id,
sale_refund_amount: item.request.refund_amount, sale_refund_amount: item.amount.to_owned(),
language: LANGUAGE.to_string(), language: LANGUAGE.to_string(),
}) })
} }