Skip to content

Commit

Permalink
address pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ShankarSinghC committed Dec 22, 2024
1 parent 79759b1 commit 86b1e39
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ impl MerchantConnectorAccount {
.clone()
.parse_value("ConnectorAuthType")
}
pub fn get_connector_test_mode(&self) -> Option<bool> {
self.test_mode
}
}

#[cfg(feature = "v2")]
Expand Down Expand Up @@ -144,6 +147,9 @@ impl MerchantConnectorAccount {
.clone()
.parse_value("ConnectorAuthType")
}
pub fn get_connector_test_mode(&self) -> Option<bool> {
todo!()
}
}

#[cfg(feature = "v1")]
Expand Down
63 changes: 62 additions & 1 deletion crates/hyperswitch_domain_models/src/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use common_enums::enums;
use common_utils::{
self,
errors::{CustomResult, ValidationError},
id_type, pii,
id_type::{self, GenerateId},
pii,
types::{keymanager, MinorUnit},
};
use diesel_models::relay::RelayUpdateInternal;
Expand All @@ -11,6 +12,8 @@ use masking::{ExposeInterface, Secret};
use serde::{self, Deserialize, Serialize};
use time::PrimitiveDateTime;

use crate::{router_data::ErrorResponse, router_response_types};

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Relay {
pub id: id_type::RelayId,
Expand All @@ -31,6 +34,64 @@ pub struct Relay {
pub response_data: Option<pii::SecretSerdeValue>,
}

impl Relay {
pub fn new(
relay_request: &api_models::relay::RelayRequest,
merchant_id: &id_type::MerchantId,
profile_id: &id_type::ProfileId,
) -> Self {
let relay_id = id_type::RelayId::generate();
Self {
id: relay_id.clone(),
connector_resource_id: relay_request.connector_resource_id.clone(),
connector_id: relay_request.connector_id.clone(),
profile_id: profile_id.clone(),
merchant_id: merchant_id.clone(),
relay_type: common_enums::RelayType::Refund,
request_data: relay_request.data.clone().map(From::from),
status: common_enums::RelayStatus::Created,
connector_reference_id: None,
error_code: None,
error_message: None,
created_at: common_utils::date_time::now(),
modified_at: common_utils::date_time::now(),
response_data: None,
}
}
}

impl From<api_models::relay::RelayData> for RelayData {
fn from(relay: api_models::relay::RelayData) -> Self {
match relay {
api_models::relay::RelayData::Refund(relay_refund_request) => {
Self::Refund(RelayRefundData {
amount: relay_refund_request.amount,
currency: relay_refund_request.currency,
reason: relay_refund_request.reason,
})
}
}
}
}

impl RelayUpdate {
pub fn from(
response: Result<router_response_types::RefundsResponseData, ErrorResponse>,
) -> Self {
match response {
Err(error) => Self::ErrorUpdate {
error_code: error.code,
error_message: error.message,
status: common_enums::RelayStatus::Failure,
},
Ok(response) => Self::StatusUpdate {
connector_reference_id: Some(response.connector_refund_id),
status: common_enums::RelayStatus::from(response.refund_status),
},
}
}
}

impl From<Relay> for api_models::relay::RelayResponse {
fn from(value: Relay) -> Self {
let error = value
Expand Down
158 changes: 57 additions & 101 deletions crates/router/src/core/relay.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use api_models::relay as relay_models;
use common_utils::{
self,
ext_traits::OptionExt,
id_type::{self, GenerateId},
};
use common_utils::{self, ext_traits::OptionExt, id_type};
use error_stack::ResultExt;

use super::errors::{self, ConnectorErrorExt, RouterResponse, RouterResult, StorageErrorExt};
Expand All @@ -14,7 +10,6 @@ use crate::{
types::{
api::{self},
domain,
transformers::ForeignFrom,
},
};

Expand All @@ -30,6 +25,7 @@ pub async fn relay(
let db = state.store.as_ref();
let key_manager_state = &(&state).into();
let merchant_id = merchant_account.get_id();
let connector_id = &req.connector_id;

let profile_id_from_auth_layer = profile_id_optional
.get_required_value("ProfileId")
Expand All @@ -49,50 +45,72 @@ pub async fn relay(
id: profile_id_from_auth_layer.get_string_repr().to_owned(),
})?;

#[cfg(feature = "v1")]
let connector_account = db
.find_by_merchant_connector_account_merchant_id_merchant_connector_id(
key_manager_state,
merchant_id,
connector_id,
&key_store,
)
.await
.to_not_found_response(errors::ApiErrorResponse::MerchantConnectorAccountNotFound {
id: connector_id.get_string_repr().to_string(),
})?;

#[cfg(feature = "v2")]
let connector_account = db
.find_merchant_connector_account_by_id(key_manager_state, connector_id, &key_store)
.await
.to_not_found_response(errors::ApiErrorResponse::MerchantConnectorAccountNotFound {
id: connector_id.get_string_repr().to_string(),
})?;

validate_relay_refund_request(&req).attach_printable("Invalid relay refund request")?;

let relay_domain =
hyperswitch_domain_models::relay::Relay::new(&req, merchant_id, profile.get_id());

let relay_record = db
.insert_relay(key_manager_state, &key_store, relay_domain)
.await
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Failed to insert a relay record in db")?;

let relay_response = match req.relay_type {
common_enums::RelayType::Refund => {
Box::pin(relay_refund(
state,
&state,
merchant_account,
profile,
key_store,
&req,
connector_account,
&relay_record,
))
.await?
}
};

let relay_update_record = db
.update_relay(key_manager_state, &key_store, relay_record, relay_response)
.await
.change_context(errors::ApiErrorResponse::InternalServerError)?;

let response = relay_models::RelayResponse::from(relay_update_record);

Ok(hyperswitch_domain_models::api::ApplicationResponse::Json(
relay_response,
response,
))
}

pub async fn relay_refund(
state: SessionState,
state: &SessionState,
merchant_account: domain::MerchantAccount,
profile: domain::Profile,
key_store: domain::MerchantKeyStore,
req: &relay_models::RelayRequest,
) -> RouterResult<relay_models::RelayResponse> {
validate_relay_refund_request(req).attach_printable("Invalid relay refund request")?;

let db = state.store.as_ref();
let key_manager_state = &(&state).into();
let connector_id = &req.connector_id;
connector_account: domain::MerchantConnectorAccount,
relay_record: &hyperswitch_domain_models::relay::Relay,
) -> RouterResult<hyperswitch_domain_models::relay::RelayUpdate> {
let connector_id = &relay_record.connector_id;

let merchant_id = merchant_account.get_id();

let connector_account = db
.find_by_merchant_connector_account_merchant_id_merchant_connector_id(
key_manager_state,
merchant_account.get_id(),
connector_id,
&key_store,
)
.await
.to_not_found_response(errors::ApiErrorResponse::MerchantConnectorAccountNotFound {
id: connector_id.get_string_repr().to_string(),
})?;

let connector_data = api::ConnectorData::get_connector_by_name(
&state.conf.connectors,
&connector_account.connector_name,
Expand All @@ -106,25 +124,17 @@ pub async fn relay_refund(
hyperswitch_domain_models::router_response_types::RefundsResponseData,
> = connector_data.connector.get_connector_integration();

let relay_domain = get_relay_domain_model(req, merchant_account.get_id(), profile.get_id());

let relay_record = db
.insert_relay(key_manager_state, &key_store, relay_domain)
.await
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Failed to insert a relay record in db")?;

let router_data = utils::construct_relay_refund_router_data(
&state,
state,
&connector_account.connector_name,
merchant_id,
&connector_account,
&relay_record,
relay_record,
)
.await?;

let router_data_res = services::execute_connector_processing_step(
&state,
state,
connector_integration,
&router_data,
payments::CallConnectorAction::Trigger,
Expand All @@ -133,26 +143,10 @@ pub async fn relay_refund(
.await
.to_refund_failed_response()?;

let relay_response = match router_data_res.response {
Err(error) => hyperswitch_domain_models::relay::RelayUpdate::ErrorUpdate {
error_code: error.code,
error_message: error.message,
status: common_enums::RelayStatus::Failure,
},
Ok(response) => hyperswitch_domain_models::relay::RelayUpdate::StatusUpdate {
connector_reference_id: Some(response.connector_refund_id),
status: common_enums::RelayStatus::from(response.refund_status),
},
};

let relay_update = db
.update_relay(key_manager_state, &key_store, relay_record, relay_response)
.await
.change_context(errors::ApiErrorResponse::InternalServerError)?;

let response = relay_models::RelayResponse::from(relay_update);
let relay_response =
hyperswitch_domain_models::relay::RelayUpdate::from(router_data_res.response);

Ok(response)
Ok(relay_response)
}

// validate relay request
Expand Down Expand Up @@ -181,41 +175,3 @@ pub fn validate_relay_refund_data(
}
Ok(())
}

pub fn get_relay_domain_model(
relay_request: &relay_models::RelayRequest,
merchant_id: &id_type::MerchantId,
profile_id: &id_type::ProfileId,
) -> hyperswitch_domain_models::relay::Relay {
let relay_id = id_type::RelayId::generate();
hyperswitch_domain_models::relay::Relay {
id: relay_id.clone(),
connector_resource_id: relay_request.connector_resource_id.clone(),
connector_id: relay_request.connector_id.clone(),
profile_id: profile_id.clone(),
merchant_id: merchant_id.clone(),
relay_type: common_enums::RelayType::Refund,
request_data: relay_request.data.clone().map(ForeignFrom::foreign_from),
status: common_enums::RelayStatus::Created,
connector_reference_id: None,
error_code: None,
error_message: None,
created_at: common_utils::date_time::now(),
modified_at: common_utils::date_time::now(),
response_data: None,
}
}

impl ForeignFrom<relay_models::RelayData> for hyperswitch_domain_models::relay::RelayData {
fn foreign_from(relay: relay_models::RelayData) -> Self {
match relay {
relay_models::RelayData::Refund(relay_refund_request) => {
Self::Refund(hyperswitch_domain_models::relay::RelayRefundData {
amount: relay_refund_request.amount,
currency: relay_refund_request.currency,
reason: relay_refund_request.reason,
})
}
}
}
}
2 changes: 1 addition & 1 deletion crates/router/src/core/relay/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub async fn construct_relay_refund_router_data<'a, F>(
payout_method_data: None,
#[cfg(feature = "payouts")]
quote_id: None,
test_mode: connector_account.test_mode,
test_mode: connector_account.get_connector_test_mode(),
payment_method_balance: None,
connector_api_version,
connector_http_status_code: None,
Expand Down
27 changes: 18 additions & 9 deletions crates/router/src/db/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,29 @@ impl RelayInterface for MockDb {
impl RelayInterface for KafkaStore {
async fn insert_relay(
&self,
_key_manager_state: &KeyManagerState,
_merchant_key_store: &domain::MerchantKeyStore,
_new: hyperswitch_domain_models::relay::Relay,
key_manager_state: &KeyManagerState,
merchant_key_store: &domain::MerchantKeyStore,
new: hyperswitch_domain_models::relay::Relay,
) -> CustomResult<hyperswitch_domain_models::relay::Relay, errors::StorageError> {
Err(errors::StorageError::MockDbError)?
self.diesel_store
.insert_relay(key_manager_state, merchant_key_store, new)
.await
}

async fn update_relay(
&self,
_key_manager_state: &KeyManagerState,
_merchant_key_store: &domain::MerchantKeyStore,
_current_state: hyperswitch_domain_models::relay::Relay,
_relay_update: hyperswitch_domain_models::relay::RelayUpdate,
key_manager_state: &KeyManagerState,
merchant_key_store: &domain::MerchantKeyStore,
current_state: hyperswitch_domain_models::relay::Relay,
relay_update: hyperswitch_domain_models::relay::RelayUpdate,
) -> CustomResult<hyperswitch_domain_models::relay::Relay, errors::StorageError> {
Err(errors::StorageError::KafkaError)?
self.diesel_store
.update_relay(
key_manager_state,
merchant_key_store,
current_state,
relay_update,
)
.await
}
}
3 changes: 3 additions & 0 deletions crates/router/src/routes/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ pub async fn relay(
relay::relay(
state,
auth.merchant_account,
#[cfg(feature = "v1")]
auth.profile_id,
#[cfg(feature = "v2")]
Some(auth.profile.get_id().clone()),
auth.key_store,
req,
)
Expand Down

0 comments on commit 86b1e39

Please sign in to comment.