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

138 lines
4.8 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};
2024-03-18 17:52:05 +00:00
use iso_currency::Currency;
use std::{str::FromStr, sync::Arc};
use tracing::{debug, level_filters::LevelFilter};
use tracing_subscriber::EnvFilter;
2024-03-11 13:11:47 +00:00
use saleor_app_sdk::{config::Config, locales::LocaleCode, manifest::AppManifest, SaleorApp};
use serde::{Deserialize, Serialize};
2024-03-11 13:11:47 +00:00
// Make our own error that wraps `anyhow::Error`.
pub struct AppError(pub anyhow::Error);
2024-03-11 13:11:47 +00:00
// 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) -> anyhow::Result<()> {
let filter = EnvFilter::builder()
.with_default_directive(LevelFilter::DEBUG.into())
.from_env()?
.add_directive(format!("{}={}", env!("CARGO_PKG_NAME"), config.log_level).parse()?);
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();
Ok(())
2024-03-11 13:11:47 +00:00
}
2024-04-15 17:51:15 +00:00
#[derive(Debug, Clone, Copy, Sequence, Serialize, Deserialize, PartialEq)]
2024-03-14 13:33:22 +00:00
#[serde(rename_all = "lowercase")]
pub enum PaymentMethodType {
2024-03-12 19:48:20 +00:00
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,
pub active_payment_methods: Vec<ActivePaymentMethod>,
2024-07-03 14:07:04 +00:00
pub cod_extra_price_as_product_slug: Option<String>,
2024-03-12 19:48:20 +00:00
}
pub fn get_active_payment_methods_from_env() -> anyhow::Result<Vec<ActivePaymentMethod>> {
_ = dotenvy::dotenv();
2024-03-12 19:48:20 +00:00
//eg: "accreditation,cod,other,transfer"
let env_methods = std::env::var("ACTIVE_PAYMENT_METHODS")?;
2024-03-12 19:48:20 +00:00
let locale = std::env::var("LOCALE")?;
2024-03-18 17:52:05 +00:00
let currencies = std::env::var("CURRENCIES")?;
let locale = LocaleCode::from_str(&locale)?;
2024-03-21 15:21:01 +00:00
let currencies = currencies.split(',').collect::<Vec<_>>();
2024-03-18 17:52:05 +00:00
let currencies = currencies
.iter()
2024-03-21 15:21:01 +00:00
.map(|c| Currency::from_str(c))
2024-03-18 17:52:05 +00:00
.collect::<Result<Vec<_>, _>>()
.map_err(|e| anyhow::anyhow!(format!("{:?}", e)))?;
2024-03-12 19:48:20 +00:00
let str_types: Vec<_> = env_methods.split(',').collect();
let payment_methods = str_types
2024-03-12 19:48:20 +00:00
.iter()
.flat_map(|s| all::<PaymentMethodType>().map(move |g| (s, g)))
2024-03-12 19:48:20 +00:00
.filter_map(|(s, g)| match format!("{:?}", g).to_lowercase() == *s {
true => Some(g),
false => None,
})
.map(|g| ActivePaymentMethod {
2024-04-15 17:51:15 +00:00
typ: g,
name: match (g, &locale) {
(PaymentMethodType::COD, LocaleCode::Sk) => "Dobierka".to_owned(),
(PaymentMethodType::Cash, LocaleCode::Sk) => "Hotovosť".to_owned(),
(PaymentMethodType::Transfer, LocaleCode::Sk) => "Bankový prevod".to_owned(),
(PaymentMethodType::Inkaso, LocaleCode::Sk) => "Inkaso".to_owned(),
(PaymentMethodType::Accreditation, LocaleCode::Sk) => "Vzajomný zápočet".to_owned(),
(PaymentMethodType::Other, LocaleCode::Sk) => "Iné".to_owned(),
(PaymentMethodType::COD, LocaleCode::En) => "Cash on delivery".to_owned(),
(PaymentMethodType::Cash, LocaleCode::En) => "Cash".to_owned(),
(PaymentMethodType::Transfer, LocaleCode::En) => "Bank transfer".to_owned(),
(PaymentMethodType::Inkaso, LocaleCode::En) => "Encashment".to_owned(),
(PaymentMethodType::Accreditation, LocaleCode::En) => "Mutual credit".to_owned(),
(PaymentMethodType::Other, LocaleCode::En) => "Other".to_owned(),
(g, l) => unimplemented!("Gateway {:?} in locale {:?} not implemented", g, l),
2024-03-14 13:33:22 +00:00
},
})
2024-03-12 19:48:20 +00:00
.collect::<Vec<_>>();
debug!(
"active gateway types:{:?}\ncurrencies:{:?}\nlocale:{:?}",
&payment_methods, &currencies, &locale
);
Ok(payment_methods)
2024-03-12 19:48:20 +00:00
}
#[derive(Debug, Clone, Serialize)]
pub struct ActivePaymentMethod {
pub typ: PaymentMethodType,
pub name: String,
}
#[derive(Serialize)]
pub struct PaymentGatewayInitializeSessionData {
pub payment_methods: Vec<ActivePaymentMethod>,
}
#[derive(Deserialize, Serialize)]
pub struct TransactionInitializeSessionData {
pub payment_method: PaymentMethodType,
2024-03-12 19:48:20 +00:00
}