Skip to content

Commit

Permalink
refactor(connector): [noon] update and add recommended fields (#2381)
Browse files Browse the repository at this point in the history
Co-authored-by: Arjun Karthik <[email protected]>
  • Loading branch information
SamraatBansal and ArjunKarthik authored Oct 12, 2023
1 parent 53d7604 commit 751f16e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
56 changes: 54 additions & 2 deletions crates/router/src/connector/noon/transformers.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use common_utils::pii;
use error_stack::ResultExt;
use masking::Secret;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -36,6 +37,23 @@ pub struct NoonSubscriptionData {
name: String,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct NoonBillingAddress {
street: Option<Secret<String>>,
street2: Option<Secret<String>>,
city: Option<String>,
state_province: Option<Secret<String>>,
country: Option<api_models::enums::CountryAlpha2>,
postal_code: Option<Secret<String>>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct NoonBilling {
address: NoonBillingAddress,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct NoonOrder {
Expand All @@ -46,6 +64,7 @@ pub struct NoonOrder {
reference: String,
//Short description of the order.
name: String,
ip_address: Option<Secret<String, pii::IpAddress>>,
}

#[derive(Debug, Serialize)]
Expand Down Expand Up @@ -164,6 +183,7 @@ pub struct NoonPaymentsRequest {
configuration: NoonConfiguration,
payment_data: NoonPaymentData,
subscription: Option<NoonSubscriptionData>,
billing: Option<NoonBilling>,
}

impl TryFrom<&types::PaymentsAuthorizeRouterData> for NoonPaymentsRequest {
Expand Down Expand Up @@ -247,6 +267,27 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for NoonPaymentsRequest {
.take(50)
.collect();

let ip_address = item.request.get_ip_address_as_optional();

let channel = NoonChannels::Web;

let billing = item
.address
.billing
.clone()
.and_then(|billing_address| billing_address.address)
.map(|address| NoonBilling {
address: NoonBillingAddress {
street: address.line1,
street2: address.line2,
city: address.city,
// If state is passed in request, country becomes mandatory, keep a check while debugging failed payments
state_province: address.state,
country: address.country,
postal_code: address.zip,
},
});

let (subscription, tokenize_c_c) =
match item.request.setup_future_usage.is_some().then_some((
NoonSubscriptionData {
Expand All @@ -261,10 +302,11 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for NoonPaymentsRequest {
let order = NoonOrder {
amount: conn_utils::to_currency_base_unit(item.request.amount, item.request.currency)?,
currency,
channel: NoonChannels::Web,
channel,
category,
reference: item.connector_request_reference_id.clone(),
name,
ip_address,
};
let payment_action = if item.request.is_auto_capture()? {
NoonPaymentActions::Sale
Expand All @@ -274,6 +316,7 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for NoonPaymentsRequest {
Ok(Self {
api_operation: NoonApiOperations::Initiate,
order,
billing,
configuration: NoonConfiguration {
payment_action,
return_url: item.request.router_return_url.clone(),
Expand Down Expand Up @@ -333,7 +376,8 @@ impl From<NoonPaymentStatus> for enums::AttemptStatus {
fn from(item: NoonPaymentStatus) -> Self {
match item {
NoonPaymentStatus::Authorized => Self::Authorized,
NoonPaymentStatus::Captured | NoonPaymentStatus::PartiallyCaptured => Self::Charged,
NoonPaymentStatus::Captured => Self::Charged,
NoonPaymentStatus::PartiallyCaptured => Self::PartialCharged,
NoonPaymentStatus::Reversed => Self::Voided,
NoonPaymentStatus::Cancelled | NoonPaymentStatus::Expired => Self::AuthenticationFailed,
NoonPaymentStatus::ThreeDsEnrollInitiated | NoonPaymentStatus::ThreeDsEnrollChecked => {
Expand Down Expand Up @@ -444,6 +488,7 @@ pub struct NoonActionTransaction {
#[serde(rename_all = "camelCase")]
pub struct NoonActionOrder {
id: String,
cancellation_reason: Option<String>,
}

#[derive(Debug, Serialize)]
Expand All @@ -459,6 +504,7 @@ impl TryFrom<&types::PaymentsCaptureRouterData> for NoonPaymentsActionRequest {
fn try_from(item: &types::PaymentsCaptureRouterData) -> Result<Self, Self::Error> {
let order = NoonActionOrder {
id: item.request.connector_transaction_id.clone(),
cancellation_reason: None,
};
let transaction = NoonActionTransaction {
amount: conn_utils::to_currency_base_unit(
Expand Down Expand Up @@ -488,6 +534,11 @@ impl TryFrom<&types::PaymentsCancelRouterData> for NoonPaymentsCancelRequest {
fn try_from(item: &types::PaymentsCancelRouterData) -> Result<Self, Self::Error> {
let order = NoonActionOrder {
id: item.request.connector_transaction_id.clone(),
cancellation_reason: item
.request
.cancellation_reason
.clone()
.map(|reason| reason.chars().take(100).collect()), // Max 100 chars
};
Ok(Self {
api_operation: NoonApiOperations::Reverse,
Expand All @@ -501,6 +552,7 @@ impl<F> TryFrom<&types::RefundsRouterData<F>> for NoonPaymentsActionRequest {
fn try_from(item: &types::RefundsRouterData<F>) -> Result<Self, Self::Error> {
let order = NoonActionOrder {
id: item.request.connector_transaction_id.clone(),
cancellation_reason: None,
};
let transaction = NoonActionTransaction {
amount: conn_utils::to_currency_base_unit(
Expand Down
8 changes: 8 additions & 0 deletions crates/router/src/connector/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ pub trait PaymentsAuthorizeRequestData {
fn get_payment_method_type(&self) -> Result<diesel_models::enums::PaymentMethodType, Error>;
fn get_connector_mandate_id(&self) -> Result<String, Error>;
fn get_complete_authorize_url(&self) -> Result<String, Error>;
fn get_ip_address_as_optional(&self) -> Option<Secret<String, IpAddress>>;
}

impl PaymentsAuthorizeRequestData for types::PaymentsAuthorizeData {
Expand Down Expand Up @@ -370,6 +371,13 @@ impl PaymentsAuthorizeRequestData for types::PaymentsAuthorizeData {
self.connector_mandate_id()
.ok_or_else(missing_field_err("connector_mandate_id"))
}
fn get_ip_address_as_optional(&self) -> Option<Secret<String, IpAddress>> {
self.browser_info.clone().and_then(|browser_info| {
browser_info
.ip_address
.map(|ip| Secret::new(ip.to_string()))
})
}
}

pub trait ConnectorCustomerData {
Expand Down

0 comments on commit 751f16e

Please sign in to comment.