Skip to content

Commit

Permalink
error_mapping comments resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
KiranKBR committed May 20, 2024
1 parent 4707994 commit f314bea
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 76 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/router/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ iso_currency = "0.4.4"
actix-http = "3.6.0"
events = { version = "0.1.0", path = "../events" }
totp-rs = { version = "5.5.1", features = ["gen_secret", "otpauth"]}
serde_repr = "0.1.19"

[build-dependencies]
router_env = { version = "0.1.0", path = "../router_env", default-features = false }
Expand Down
53 changes: 36 additions & 17 deletions crates/router/src/connector/payone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,25 +166,44 @@ impl ConnectorCommon for Payone {
event_builder.map(|i| i.set_response_body(&response));
router_env::logger::info!(connector_response=?response);

match response.errors.first() {
Some(error) => Ok(ErrorResponse {
status_code: error.http_status_code,
code: error.code.clone(),
message: error.message.clone(),
reason: None,
attempt_status: None,
connector_transaction_id: None,
}),
None => Ok(ErrorResponse {
status_code: res.status_code,
code: consts::NO_ERROR_CODE.to_string(),
message: consts::NO_ERROR_MESSAGE.to_string(),
reason: None,
attempt_status: None,
connector_transaction_id: None,
}),
match response.errors {
Some(errors) =>
{
let first_error = errors.first();
let code = first_error.map(|error| error.code.clone());
Ok(ErrorResponse {
status_code:res.status_code,
code: code.unwrap_or_else(|| consts::NO_ERROR_CODE.to_string()),
message:
errors
.iter()
.map(|error| format!("{} : {}", error.code, error.message))
.collect::<Vec<_>>()
.join(", "),
reason: Some(
errors
.iter()
.map(|error| format!("{} : {}", error.code, error.message))
.collect::<Vec<_>>()
.join(", "),
),
attempt_status: None,
connector_transaction_id: None,
})
},
None => {
Ok(ErrorResponse {
status_code:res.status_code,
code: consts::NO_ERROR_CODE.to_string(),
message: consts::NO_ERROR_MESSAGE.to_string(),
reason: None,
attempt_status: None,
connector_transaction_id: None,
}
)
}
}
}
}
impl ConnectorValidation for Payone {}

Expand Down
95 changes: 38 additions & 57 deletions crates/router/src/connector/payone/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ use cards::CardNumber;
use error_stack::ResultExt;
use masking::Secret;
use serde::{Deserialize, Serialize};
// #[cfg(feature = "payouts")]
use serde_repr::{Serialize_repr, Deserialize_repr};

#[cfg(not(feature = "payouts"))]
use crate::connector::utils;
use crate::connector::utils::{get_unimplemented_payment_method_error_message, CardIssuer};

#[cfg(feature = "payouts")]
type Error = error_stack::Report<errors::ConnectorError>;

#[cfg(feature = "payouts")]
use crate::{
connector::utils::{self, CardData, RouterData},
connector::utils::{ CardData, RouterData},
core::errors,
types::{self, api, storage::enums as storage_enums, transformers::ForeignFrom},
utils::OptionExt,
Expand All @@ -25,7 +25,7 @@ use crate::{
use crate::{core::errors, types::{self,api,storage::enums as storage_enums}};

pub struct PayoneRouterData<T> {
pub amount:String,
pub amount:i64,
pub router_data: T,
}

Expand All @@ -46,7 +46,6 @@ impl<T>
T,
),
) -> Result<Self, Self::Error> {
let amount = utils::get_amount_as_string(_currency_unit, amount, _currency)?;
Ok(Self {
amount,
router_data: item,
Expand All @@ -56,7 +55,7 @@ impl<T>

#[derive(Debug, Deserialize, Serialize)]
pub struct ErrorResponse {
pub errors: Vec<SubError>,
pub errors: Option<Vec<SubError>>,
pub error_id: Option<i32>,
}

Expand Down Expand Up @@ -114,7 +113,7 @@ pub struct AmountOfMoney {
#[serde(rename_all = "camelCase")]
pub struct CardPayoutMethodSpecificInput {
card: Card,
payment_product_id: i32,
payment_product_id: PaymentProductId,
}

#[cfg(feature = "payouts")]
Expand All @@ -132,8 +131,6 @@ pub struct Card {
pub struct OmnichannelPayoutSpecificInput {
payment_id: String,
}
#[cfg(feature = "payouts")]
pub struct CardAndCardIssuer(Card, CardIssuer);

#[cfg(feature = "payouts")]
impl TryFrom<PayoneRouterData<&types::PayoutsRouterData<api::PoFulfill>>>
Expand All @@ -147,20 +144,29 @@ impl TryFrom<PayoneRouterData<&types::PayoutsRouterData<api::PoFulfill>>>
match request.payout_type.to_owned() {
storage_enums::PayoutType::Card => {
let amount_of_money: AmountOfMoney = AmountOfMoney {
amount: item.amount.parse::<i64>().change_context(errors::ConnectorError::ParsingFailed)?,
amount: item.amount,
currency_code: item.router_data.request.destination_currency.to_string(),
};
let card_issuer =
CardAndCardIssuer::try_from(&item.router_data.get_payout_method_data()?)?;
let card = card_issuer.0;
let card_issuer = card_issuer.1;

let card_payout_method_specific_input: CardPayoutMethodSpecificInput =
CardPayoutMethodSpecificInput {
#[allow(clippy::as_conversions)]
payment_product_id: Gateway::try_from(card_issuer)? as i32,
card,
};
let card_payout_method_specific_input = match item.router_data.get_payout_method_data()?{
PayoutMethodData::Card(card_data) => {
CardPayoutMethodSpecificInput{
card: Card {
card_number: card_data.card_number.clone(),
card_holder_name: card_data
.card_holder_name
.clone()
.get_required_value("card_holder_name")
.change_context(errors::ConnectorError::MissingRequiredField {
field_name: "payout_method_data.card.holder_name",
})?,
expiry_date: card_data.get_card_expiry_month_year_2_digit_with_delimiter("".to_string())?,
},
payment_product_id: PaymentProductId::try_from(card_data.get_card_issuer()?)? ,
}
},
PayoutMethodData::Bank(_) => todo!(),
PayoutMethodData::Wallet(_) => todo!(),
};
Ok(Self {
amount_of_money,
card_payout_method_specific_input,
Expand All @@ -173,57 +179,32 @@ impl TryFrom<PayoneRouterData<&types::PayoutsRouterData<api::PoFulfill>>>
}
}

#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)]
pub enum Gateway {
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize_repr, Deserialize_repr)]
#[repr(i32)]
pub enum PaymentProductId {
Visa = 1,
MasterCard = 3,
MasterCard =3,
}

impl TryFrom<CardIssuer> for Gateway {
impl TryFrom<CardIssuer> for PaymentProductId {
type Error = error_stack::Report<errors::ConnectorError>;
fn try_from(issuer: CardIssuer) -> Result<Self, Self::Error> {
match issuer {
CardIssuer::Master => Ok(Self::MasterCard),
CardIssuer::Visa => Ok(Self::Visa),
_ => Err(errors::ConnectorError::NotImplemented(
CardIssuer::AmericanExpress |
CardIssuer::Maestro |
CardIssuer::Discover |
CardIssuer::DinersClub |
CardIssuer::JCB|
CardIssuer::CarteBlanche => Err(errors::ConnectorError::NotImplemented(
get_unimplemented_payment_method_error_message("payone"),
)
.into()),
}
}
}

#[cfg(feature = "payouts")]
impl TryFrom<&PayoutMethodData> for CardAndCardIssuer {
type Error = Error;
fn try_from(item: &PayoutMethodData) -> Result<Self, Self::Error> {
match item {
PayoutMethodData::Card(card) => Ok(Self(
Card {
card_number: card.card_number.clone(),
card_holder_name: card
.card_holder_name
.clone()
.get_required_value("card_holder_name")
.change_context(errors::ConnectorError::MissingRequiredField {
field_name: "payout_method_data.card.holder_name",
})?,
expiry_date: match card.get_card_expiry_month_year_2_digit_with_delimiter("".to_string()) {
Ok(date) => date,
Err(_) => Err(errors::ConnectorError::MissingRequiredField {
field_name: "payout_method_data.card.expiry_date",
})?,
},
},
card.get_card_issuer()?,
)),
_ => Err(errors::ConnectorError::MissingRequiredField {
field_name: "payout_method_data.card",
})?,
}
}
}

#[cfg(feature = "payouts")]
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
Expand Down

0 comments on commit f314bea

Please sign in to comment.