2024-03-11 13:11:47 +00:00
|
|
|
#![allow(non_upper_case_globals)]
|
|
|
|
#![feature(let_chains)]
|
|
|
|
#![deny(clippy::unwrap_used, clippy::expect_used)]
|
|
|
|
mod app;
|
|
|
|
mod queries;
|
|
|
|
mod routes;
|
|
|
|
|
|
|
|
use anyhow::Context;
|
|
|
|
use saleor_app_sdk::{
|
|
|
|
config::Config,
|
|
|
|
manifest::{AppManifest, AppPermission},
|
|
|
|
webhooks::{SyncWebhookEventType, WebhookManifest},
|
|
|
|
SaleorApp,
|
|
|
|
};
|
|
|
|
use std::sync::Arc;
|
|
|
|
use tokio::sync::Mutex;
|
|
|
|
|
|
|
|
use crate::{
|
2024-03-12 19:48:20 +00:00
|
|
|
app::{get_active_gateways_from_env, trace_to_std, AppState},
|
2024-03-11 13:11:47 +00:00
|
|
|
queries::event_transactions::{
|
2024-03-12 19:48:20 +00:00
|
|
|
sub_list_payment_gateways, sub_payment_gateway_initialize_session,
|
|
|
|
sub_transaction_charge_requested, sub_transaction_initialize_session,
|
|
|
|
sub_transaction_process_session, sub_transaction_refund_requested,
|
2024-03-11 13:11:47 +00:00
|
|
|
},
|
|
|
|
routes::create_routes,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> anyhow::Result<()> {
|
|
|
|
let config = Config::load()?;
|
|
|
|
trace_to_std(&config);
|
|
|
|
|
|
|
|
let saleor_app = SaleorApp::new(&config)?;
|
|
|
|
|
|
|
|
let app_manifest = AppManifest::new(&config)
|
|
|
|
.add_webhook(
|
|
|
|
WebhookManifest::new(&config)
|
|
|
|
.set_query(sub_transaction_process_session)
|
|
|
|
.add_sync_event(SyncWebhookEventType::TransactionProcessSession)
|
|
|
|
.build(),
|
|
|
|
)
|
|
|
|
.add_webhook(
|
|
|
|
WebhookManifest::new(&config)
|
|
|
|
.set_query(sub_transaction_charge_requested)
|
|
|
|
.add_sync_event(SyncWebhookEventType::TransactionChargeRequested)
|
|
|
|
.build(),
|
|
|
|
)
|
|
|
|
.add_webhook(
|
|
|
|
WebhookManifest::new(&config)
|
|
|
|
.set_query(sub_transaction_refund_requested)
|
|
|
|
.add_sync_event(SyncWebhookEventType::TransactionRefundRequested)
|
|
|
|
.build(),
|
|
|
|
)
|
|
|
|
.add_webhook(
|
|
|
|
WebhookManifest::new(&config)
|
|
|
|
.set_query(sub_transaction_initialize_session)
|
|
|
|
.add_sync_event(SyncWebhookEventType::TransactionInitializeSession)
|
|
|
|
.build(),
|
|
|
|
)
|
|
|
|
.add_webhook(
|
|
|
|
WebhookManifest::new(&config)
|
|
|
|
.set_query(sub_payment_gateway_initialize_session)
|
|
|
|
.add_sync_event(SyncWebhookEventType::PaymentGatewayInitializeSession)
|
|
|
|
.build(),
|
|
|
|
)
|
2024-03-12 19:48:20 +00:00
|
|
|
.add_webhook(
|
|
|
|
WebhookManifest::new(&config)
|
|
|
|
.set_query(sub_list_payment_gateways)
|
|
|
|
.add_sync_event(SyncWebhookEventType::PaymentListGateways)
|
|
|
|
.build(),
|
|
|
|
)
|
2024-03-11 13:11:47 +00:00
|
|
|
.add_permissions(vec![
|
|
|
|
AppPermission::HandlePayments,
|
|
|
|
AppPermission::ManageOrders,
|
|
|
|
AppPermission::ManageCheckouts,
|
|
|
|
AppPermission::HandleCheckouts,
|
|
|
|
])
|
|
|
|
.build();
|
2024-03-12 19:48:20 +00:00
|
|
|
|
2024-03-11 13:11:47 +00:00
|
|
|
let app_state = AppState {
|
2024-03-12 19:48:20 +00:00
|
|
|
active_gateways: get_active_gateways_from_env()?,
|
2024-03-11 13:11:47 +00:00
|
|
|
manifest: app_manifest,
|
|
|
|
config: config.clone(),
|
|
|
|
saleor_app: Arc::new(Mutex::new(saleor_app)),
|
|
|
|
};
|
|
|
|
let app = create_routes(app_state);
|
|
|
|
|
|
|
|
let listener = tokio::net::TcpListener::bind(
|
|
|
|
&config
|
|
|
|
.app_api_base_url
|
|
|
|
.split("//")
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.get(1)
|
|
|
|
.context("APP_API_BASE_URL invalid format")?,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
tracing::debug!("listening on {}", listener.local_addr().unwrap());
|
|
|
|
match axum::serve(listener, app).await {
|
|
|
|
Ok(o) => Ok(o),
|
|
|
|
Err(e) => anyhow::bail!(e),
|
|
|
|
}
|
2024-03-09 18:59:15 +00:00
|
|
|
}
|