feat(admin): add payment_connector_list API (#160)

This commit is contained in:
Nishant Joshi
2022-12-15 19:43:44 +05:30
committed by GitHub
parent bc2f13ab07
commit e5ea411fb0
6 changed files with 104 additions and 18 deletions

20
Cargo.lock generated
View File

@ -1825,6 +1825,16 @@ dependencies = [
"pkg-config",
]
[[package]]
name = "libmimalloc-sys"
version = "0.1.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04d1c67deb83e6b75fa4fe3309e09cfeade12e7721d95322af500d3814ea60c9"
dependencies = [
"cc",
"libc",
]
[[package]]
name = "libz-sys"
version = "1.1.8"
@ -1943,6 +1953,15 @@ version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "mimalloc"
version = "0.1.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b2374e2999959a7b583e1811a1ddbf1d3a4b9496eceb9746f1192a59d871eca"
dependencies = [
"libmimalloc-sys",
]
[[package]]
name = "mime"
version = "0.3.16"
@ -2712,6 +2731,7 @@ dependencies = [
"literally",
"masking",
"maud",
"mimalloc",
"mime",
"nanoid",
"once_cell",

View File

@ -10,7 +10,7 @@ use crate::{
types::{
self, api,
storage::{self, MerchantAccount},
transformers::ForeignInto,
transformers::{ForeignInto, ForeignTryInto},
},
utils::{self, OptionExt, ValueExt},
};
@ -356,22 +356,35 @@ pub async fn retrieve_payment_connector(
.map_err(|error| {
error.to_not_found_response(errors::ApiErrorResponse::MerchantConnectorAccountNotFound)
})?;
let payment_methods_enabled = match mca.payment_methods_enabled {
Some(val) => serde_json::Value::Array(val)
.parse_value("PaymentMethods")
.change_context(errors::ApiErrorResponse::InternalServerError)?,
None => None,
};
let response = api::PaymentConnectorCreate {
connector_type: mca.connector_type.foreign_into(),
connector_name: mca.connector_name,
merchant_connector_id: Some(mca.merchant_connector_id),
connector_account_details: Some(Secret::new(mca.connector_account_details)),
test_mode: mca.test_mode,
disabled: mca.disabled,
payment_methods_enabled,
metadata: None,
};
Ok(service_api::BachResponse::Json(mca.foreign_try_into()?))
}
pub async fn list_payment_connectors(
store: &dyn StorageInterface,
merchant_id: String,
) -> RouterResponse<Vec<api::PaymentConnectorCreate>> {
// Validate merchant account
store
.find_merchant_account_by_merchant_id(&merchant_id)
.await
.map_err(|err| {
err.to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)
})?;
let merchant_connector_accounts = store
.find_merchant_connector_account_by_merchant_id_list(&merchant_id)
.await
.map_err(|error| {
error.to_not_found_response(errors::ApiErrorResponse::MerchantConnectorAccountNotFound)
})?;
let mut response = vec![];
// The can be eliminated once [#79711](https://github.com/rust-lang/rust/issues/79711) is stabilized
for mca in merchant_connector_accounts.into_iter() {
response.push(mca.foreign_try_into()?);
}
Ok(service_api::BachResponse::Json(response))
}

View File

@ -130,6 +130,24 @@ pub async fn payment_connector_retrieve(
.await
}
#[instrument(skip_all, fields(flow = ?Flow::PaymentConnectorsList))]
pub async fn payment_connector_list(
state: web::Data<AppState>,
req: HttpRequest,
path: web::Path<String>,
) -> HttpResponse {
let merchant_id = path.into_inner();
api::server_wrap(
&state,
&req,
merchant_id,
|state, _, merchant_id| list_payment_connectors(&*state.store, merchant_id),
api::MerchantAuthentication::AdminApiKey,
)
.await
}
#[instrument(skip_all, fields(flow = ?Flow::PaymentConnectorsUpdate))]
// #[post("/{merchant_id}/connectors/{merchant_connector_id}")]
pub async fn payment_connector_update(

View File

@ -177,7 +177,8 @@ impl MerchantConnectorAccount {
.app_data(web::Data::new(config))
.service(
web::resource("/{merchant_id}/connectors")
.route(web::post().to(payment_connector_create)),
.route(web::post().to(payment_connector_create))
.route(web::get().to(payment_connector_list)),
)
.service(
web::resource("/{merchant_id}/payment_methods")

View File

@ -1,6 +1,8 @@
use std::convert::TryInto;
use api_models::enums as api_enums;
use common_utils::ext_traits::ValueExt;
use error_stack::ResultExt;
use storage_models::enums as storage_enums;
use crate::{
@ -321,3 +323,33 @@ impl<'a> From<F<&'a storage::Address>> for F<api_types::Address> {
.into()
}
}
impl TryFrom<F<storage::MerchantConnectorAccount>>
for F<api_models::admin::PaymentConnectorCreate>
{
type Error = error_stack::Report<errors::ApiErrorResponse>;
fn try_from(item: F<storage::MerchantConnectorAccount>) -> Result<Self, Self::Error> {
let merchant_ca = item.0;
let payment_methods_enabled = match merchant_ca.payment_methods_enabled {
Some(val) => serde_json::Value::Array(val)
.parse_value("PaymentMethods")
.change_context(errors::ApiErrorResponse::InternalServerError)?,
None => None,
};
Ok(api_models::admin::PaymentConnectorCreate {
connector_type: merchant_ca.connector_type.foreign_into(),
connector_name: merchant_ca.connector_name,
merchant_connector_id: Some(merchant_ca.merchant_connector_id),
connector_account_details: Some(masking::Secret::new(
merchant_ca.connector_account_details,
)),
test_mode: merchant_ca.test_mode,
disabled: merchant_ca.disabled,
metadata: None,
payment_methods_enabled,
}
.into())
}
}

View File

@ -70,6 +70,8 @@ pub enum Flow {
PaymentConnectorsUpdate,
/// Payment connectors delete flow.
PaymentConnectorsDelete,
/// Payment connectors list flow.
PaymentConnectorsList,
/// Customers create flow.
CustomersCreate,
/// Customers retrieve flow.