Skip to content

Commit

Permalink
refactor: add mapping for ConnectorError in payouts flow
Browse files Browse the repository at this point in the history
  • Loading branch information
Kashif committed Oct 16, 2023
1 parent 92ee1db commit e922165
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 37 deletions.
8 changes: 6 additions & 2 deletions crates/router/src/connector/adyen/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3999,8 +3999,12 @@ impl<F> TryFrom<&AdyenRouterData<&types::PayoutsRouterData<F>>> for AdyenPayoutC
iban: Some(b.iban),
tax_id: None,
},
_ => Err(errors::ConnectorError::NotSupported {
message: "Bank transfers via ACH or Bacs are not supported".to_string(),
payouts::BankPayout::Ach(..) => Err(errors::ConnectorError::NotSupported {
message: "Bank transfer via ACH is not supported".to_string(),
connector: "Adyen",
})?,
payouts::BankPayout::Bacs(..) => Err(errors::ConnectorError::NotSupported {
message: "Bank transfer via Bacs is not supported".to_string(),
connector: "Adyen",
})?,
};
Expand Down
5 changes: 5 additions & 0 deletions crates/router/src/core/errors/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,11 @@ impl<T> ConnectorErrorExt<T> for error_stack::Result<T, errors::ConnectorError>
field_names: field_names.to_vec(),
}
}
errors::ConnectorError::NotSupported { message, connector } => {
errors::ApiErrorResponse::NotSupported {
message: format!("{} by {}", message, connector),
}
}
_ => errors::ApiErrorResponse::InternalServerError,
};
err.change_context(error)
Expand Down
34 changes: 19 additions & 15 deletions crates/router/src/core/payouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub struct PayoutData {
pub payout_attempt: storage::PayoutAttempt,
pub payout_method_data: Option<payouts::PayoutMethodData>,
pub merchant_connector_account: Option<payment_helpers::MerchantConnectorAccountType>,
pub profile_id: String,
}

// ********************************************** CORE FLOWS **********************************************
Expand Down Expand Up @@ -96,9 +97,7 @@ pub async fn payouts_create_core(
merchant_account: domain::MerchantAccount,
key_store: domain::MerchantKeyStore,
req: payouts::PayoutCreateRequest,
) -> RouterResponse<payouts::PayoutCreateResponse>
where
{
) -> RouterResponse<payouts::PayoutCreateResponse> {
// Form connector data
let connector_data = get_connector_data(
&state,
Expand All @@ -111,7 +110,7 @@ where
.await?;

// Validate create request
let (payout_id, payout_method_data) =
let (payout_id, payout_method_data, profile_id) =
validator::validate_create_request(&state, &merchant_account, &req).await?;

// Create DB entries
Expand All @@ -121,6 +120,7 @@ where
&key_store,
&req,
&payout_id,
&profile_id,
&connector_data.connector_name,
payout_method_data.as_ref(),
)
Expand Down Expand Up @@ -559,18 +559,8 @@ pub async fn create_recipient(
let customer_details = payout_data.customer_details.to_owned();
let connector_name = connector_data.connector_name.to_string();

let profile_id = core_utils::get_profile_id_from_business_details(
payout_data.payout_attempt.business_country,
payout_data.payout_attempt.business_label.as_ref(),
merchant_account,
payout_data.payout_attempt.profile_id.as_ref(),
&*state.store,
false,
)
.await?;

// Create the connector label using {profile_id}_{connector_name}
let connector_label = format!("{profile_id}_{}", connector_name);
let connector_label = format!("{}_{}", payout_data.profile_id, connector_name);

let (should_call_connector, _connector_customer_id) =
helpers::should_call_payout_connector_create_customer(
Expand Down Expand Up @@ -1122,13 +1112,15 @@ pub async fn response_handler(
}

// DB entries
#[allow(clippy::too_many_arguments)]
#[cfg(feature = "payouts")]
pub async fn payout_create_db_entries(
state: &AppState,
merchant_account: &domain::MerchantAccount,
key_store: &domain::MerchantKeyStore,
req: &payouts::PayoutCreateRequest,
payout_id: &String,
profile_id: &String,
connector_name: &api_enums::PayoutConnectors,
stored_payout_method_data: Option<&payouts::PayoutMethodData>,
) -> RouterResult<PayoutData> {
Expand Down Expand Up @@ -1267,6 +1259,7 @@ pub async fn payout_create_db_entries(
.cloned()
.or(stored_payout_method_data.cloned()),
merchant_connector_account: None,
profile_id: profile_id.to_owned(),
})
}

Expand All @@ -1277,6 +1270,8 @@ pub async fn make_payout_data(
key_store: &domain::MerchantKeyStore,
req: &payouts::PayoutRequest,
) -> RouterResult<PayoutData> {
use error_stack::IntoReport;

let db = &*state.store;
let merchant_id = &merchant_account.merchant_id;
let payout_id = match req {
Expand Down Expand Up @@ -1316,12 +1311,21 @@ pub async fn make_payout_data(
.await
.map_or(None, |c| c);

let profile_id = payout_attempt
.profile_id
.clone()
.ok_or(errors::ApiErrorResponse::MissingRequiredField {
field_name: "profile_id or business_country, business_label",
})
.into_report()?;

Ok(PayoutData {
billing_address,
customer_details,
payouts,
payout_attempt,
payout_method_data: None,
merchant_connector_account: None,
profile_id,
})
}
15 changes: 13 additions & 2 deletions crates/router/src/core/payouts/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub async fn validate_create_request(
state: &AppState,
merchant_account: &domain::MerchantAccount,
req: &payouts::PayoutCreateRequest,
) -> RouterResult<(String, Option<payouts::PayoutMethodData>)> {
) -> RouterResult<(String, Option<payouts::PayoutMethodData>, String)> {
let merchant_id = &merchant_account.merchant_id;

// Merchant ID
Expand Down Expand Up @@ -109,5 +109,16 @@ pub async fn validate_create_request(
None => None,
};

Ok((payout_id, payout_method_data))
// Profile ID
let profile_id = core_utils::get_profile_id_from_business_details(
req.business_country,
req.business_label.as_ref(),
merchant_account,
req.profile_id.as_ref(),
&*state.store,
false,
)
.await?;

Ok((payout_id, payout_method_data, profile_id))
}
24 changes: 6 additions & 18 deletions crates/router/src/core/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,20 @@ pub async fn get_mca_for_payout<'a>(
merchant_account: &domain::MerchantAccount,
key_store: &domain::MerchantKeyStore,
payout_data: &PayoutData,
) -> RouterResult<(helpers::MerchantConnectorAccountType, String)> {
let payout_attempt = &payout_data.payout_attempt;
let profile_id = get_profile_id_from_business_details(
payout_attempt.business_country,
payout_attempt.business_label.as_ref(),
merchant_account,
payout_attempt.profile_id.as_ref(),
&*state.store,
false,
)
.await
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("profile_id is not set in payout_attempt")?;
) -> RouterResult<helpers::MerchantConnectorAccountType> {
match payout_data.merchant_connector_account.to_owned() {
Some(mca) => Ok((mca, profile_id)),
Some(mca) => Ok(mca),
None => {
let merchant_connector_account = helpers::get_merchant_connector_account(
state,
merchant_account.merchant_id.as_str(),
None,
key_store,
&profile_id,
&payout_data.profile_id,
connector_id,
)
.await?;
Ok((merchant_connector_account, profile_id))
Ok(merchant_connector_account)
}
}
}
Expand All @@ -80,7 +68,7 @@ pub async fn construct_payout_router_data<'a, F>(
_request: &api_models::payouts::PayoutRequest,
payout_data: &mut PayoutData,
) -> RouterResult<types::PayoutsRouterData<F>> {
let (merchant_connector_account, profile_id) = get_mca_for_payout(
let merchant_connector_account = get_mca_for_payout(
state,
connector_id,
merchant_account,
Expand Down Expand Up @@ -126,7 +114,7 @@ pub async fn construct_payout_router_data<'a, F>(
let payouts = &payout_data.payouts;
let payout_attempt = &payout_data.payout_attempt;
let customer_details = &payout_data.customer_details;
let connector_label = format!("{profile_id}_{}", payout_attempt.connector);
let connector_label = format!("{}_{}", payout_data.profile_id, payout_attempt.connector);
let connector_customer_id = customer_details
.as_ref()
.and_then(|c| c.connector_customer.as_ref())
Expand Down

0 comments on commit e922165

Please sign in to comment.