// use rumqttc::QoS; pub use payment_adapter::*; // use crate::AsyncClient; pub static CLIENT_NAME: &str = "payments"; #[derive(Debug, thiserror::Error, serde::Serialize, serde::Deserialize)] pub enum Error { #[error("Something went wrong")] InternalServerError, } #[derive(Debug, Copy, Clone)] pub enum Topic { Pending, WaitingForConfirmation, Completed, Canceled, } impl Topic { pub fn to_str(self) -> &'static str { match self { Topic::Completed => "payments/completed", Topic::Canceled => "payments/canceled", Topic::Pending => "payments/pending", Topic::WaitingForConfirmation => "payments/waiting_for_confirmation", } } } impl Into for Topic { fn into(self) -> String { self.to_str().into() } } impl<'s> PartialEq<&'s str> for Topic { fn eq(&self, other: &&'s str) -> bool { self.to_str() == *other } } impl PartialEq for Topic { fn eq(&self, other: &String) -> bool { self.to_str() == other.as_str() } } pub mod start_payment { use super::Error; #[derive(Debug, serde::Serialize, serde::Deserialize)] pub struct Input { pub adapter_name: String, pub create_payment: payment_adapter::CreatePayment, } #[derive(Debug, serde::Serialize, serde::Deserialize)] pub struct Details { pub redirect_url: String, } pub type Output = Result; } pub mod notification { use super::Error; #[derive(Debug, serde::Serialize, serde::Deserialize)] pub struct Input { pub adapter_name: String, pub status: String, } #[derive(Debug, serde::Serialize, serde::Deserialize)] pub struct Details {} pub type Output = Result; } pub mod cancel { use model::Price; use super::Error; #[derive(Debug, serde::Serialize, serde::Deserialize)] pub enum RefundType { /// Refund entire payment Full, /// Refund only part given in enum Partial(Price), } #[derive(Debug, serde::Serialize, serde::Deserialize)] pub struct Input { pub adapter_name: String, pub refund: RefundType, pub provider_order_id: String, pub description: String, } #[derive(Debug, serde::Serialize, serde::Deserialize)] pub struct Details {} pub type Output = Result; } /// List of available payment adapters pub mod adapters { use super::Error; #[derive(Debug, serde::Serialize, serde::Deserialize)] pub struct Input {} #[derive(Debug, serde::Serialize, serde::Deserialize)] pub struct Details { pub names: Vec, } pub type Output = Result; } pub mod rpc { use config::SharedAppConfig; use crate::payments::{adapters, cancel, notification, start_payment}; #[tarpc::service] pub trait Payments { /// User manually started payment procedure async fn start_payment(input: start_payment::Input) -> start_payment::Output; /// User manually cancelled order async fn cancel(input: cancel::Input) -> cancel::Output; /// Received update status notification from payment service async fn notification(input: notification::Input) -> notification::Output; /// List of available adapters async fn adapters(input: adapters::Input) -> adapters::Output; } pub async fn create_client(config: SharedAppConfig) -> PaymentsClient { use tarpc::client; use tarpc::tokio_serde::formats::Bincode; let l = config.lock(); let addr = l.orders_manager().rpc_addr(); let transport = tarpc::serde_transport::tcp::connect(addr, Bincode::default); let client = PaymentsClient::new( client::Config::default(), transport.await.expect("Failed to connect to server"), ) .spawn(); client } } pub mod mqtt { use config::SharedAppConfig; use rumqttc::EventLoop; use super::CLIENT_NAME; use crate::AsyncClient; pub fn create_client(config: SharedAppConfig) -> (AsyncClient, EventLoop) { crate::mqtt::create_client(CLIENT_NAME, config.lock().payments_manager().mqtt_addr()) } }