saleor-apps-rs/simple-payment-gateway/src/app.rs

131 lines
4.2 KiB
Rust
Raw Normal View History

2024-03-11 13:11:47 +00:00
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
};
2024-03-12 19:48:20 +00:00
use enum_iterator::{all, Sequence};
use std::sync::Arc;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::EnvFilter;
2024-03-11 13:11:47 +00:00
2024-03-12 19:48:20 +00:00
use saleor_app_sdk::{config::Config, locales::LocaleCode, manifest::AppManifest, SaleorApp};
use serde::Serialize;
2024-03-11 13:11:47 +00:00
// Make our own error that wraps `anyhow::Error`.
pub struct AppError(anyhow::Error);
// Tell axum how to convert `AppError` into a response.
impl IntoResponse for AppError {
fn into_response(self) -> Response {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Something went wrong: {}", self.0),
)
.into_response()
}
}
// This enables using `?` on functions that return `Result<_, anyhow::Error>` to turn them into
// `Result<_, AppError>`. That way you don't need to do that manually.
impl<E> From<E> for AppError
where
E: Into<anyhow::Error>,
{
fn from(err: E) -> Self {
Self(err.into())
}
}
pub fn trace_to_std(config: &Config) {
let filter = EnvFilter::builder()
.with_default_directive(LevelFilter::DEBUG.into())
.from_env()
.unwrap()
.add_directive(
format!("{}={}", env!("CARGO_PKG_NAME"), config.log_level)
.parse()
.unwrap(),
);
2024-03-11 13:11:47 +00:00
tracing_subscriber::fmt()
.with_max_level(config.log_level)
.with_env_filter(filter)
.with_target(true)
.compact()
2024-03-11 13:11:47 +00:00
.init();
}
2024-03-12 19:48:20 +00:00
#[derive(Debug, Clone, Sequence, Serialize)]
2024-03-14 13:33:22 +00:00
#[serde(rename_all = "lowercase")]
2024-03-12 19:48:20 +00:00
pub enum GatewayType {
Accreditation,
Cash,
/**
Acronym for Cash on Delivery
*/
COD,
Inkaso,
Other,
Transfer,
}
2024-03-11 13:11:47 +00:00
#[derive(Debug, Clone)]
pub struct AppState {
pub saleor_app: Arc<tokio::sync::Mutex<SaleorApp>>,
pub config: Config,
pub manifest: AppManifest,
2024-03-12 19:48:20 +00:00
pub active_gateways: Vec<ActiveGateway>,
}
pub fn get_active_gateways_from_env() -> anyhow::Result<Vec<ActiveGateway>> {
_ = dotenvy::dotenv();
2024-03-12 19:48:20 +00:00
//eg: "accreditation,cod,other,transfer"
let env_types = std::env::var("ACTIVE_GATEWAYS")?;
let locale = std::env::var("LOCALE")?;
let locale = match locale.as_str() {
"SK" => LocaleCode::Sk,
"EN" => LocaleCode::En,
l => unimplemented!("Locale {l} not implemented"),
};
2024-03-14 13:51:55 +00:00
let str_types: Vec<_> = env_types.split(',').collect();
2024-03-12 19:48:20 +00:00
let gateway_types = str_types
.iter()
.zip(all::<GatewayType>())
.filter_map(|(s, g)| match format!("{:?}", g).to_lowercase() == *s {
true => Some(g),
false => None,
})
2024-03-14 13:33:22 +00:00
.map(|g| ActiveGateway {
gateway_type: g.clone(),
currencies: vec!["EUR".to_owned()],
id: format!("{:?}", &g).to_lowercase(),
config: [],
name: match (g, &locale) {
(GatewayType::COD, LocaleCode::Sk) => "Dobierka".to_owned(),
(GatewayType::Cash, LocaleCode::Sk) => "Hotovosť".to_owned(),
(GatewayType::Transfer, LocaleCode::Sk) => "Bankový prevod".to_owned(),
(GatewayType::Inkaso, LocaleCode::Sk) => "Inkaso".to_owned(),
(GatewayType::Accreditation, LocaleCode::Sk) => "Vzajomný zápočet".to_owned(),
(GatewayType::Other, LocaleCode::Sk) => "Iné".to_owned(),
(GatewayType::COD, LocaleCode::En) => "Cash on delivery".to_owned(),
(GatewayType::Cash, LocaleCode::En) => "Cash".to_owned(),
(GatewayType::Transfer, LocaleCode::En) => "Bank transfer".to_owned(),
(GatewayType::Inkaso, LocaleCode::En) => "Encashment".to_owned(),
(GatewayType::Accreditation, LocaleCode::En) => "Mutual credit".to_owned(),
(GatewayType::Other, LocaleCode::En) => "Other".to_owned(),
(g, l) => unimplemented!("Gateway {:?} in locale {:?} not implemented", g, l),
},
})
2024-03-12 19:48:20 +00:00
.collect::<Vec<_>>();
2024-03-14 13:33:22 +00:00
Ok(gateway_types)
2024-03-12 19:48:20 +00:00
}
#[derive(Debug, Clone, Serialize)]
pub struct ActiveGateway {
pub gateway_type: GatewayType,
pub id: String,
pub name: String,
pub currencies: Vec<String>,
//don't need this one yet
pub config: [(); 0],
}