2024-04-03 17:47:23 +00:00
|
|
|
#![allow(
|
|
|
|
non_upper_case_globals,
|
|
|
|
clippy::large_enum_variant,
|
2024-04-03 18:03:28 +00:00
|
|
|
clippy::upper_case_acronyms,
|
|
|
|
dead_code
|
2024-04-03 17:47:23 +00:00
|
|
|
)]
|
2024-03-11 13:11:47 +00:00
|
|
|
#![feature(let_chains)]
|
|
|
|
#![deny(clippy::unwrap_used, clippy::expect_used)]
|
|
|
|
mod app;
|
|
|
|
mod queries;
|
|
|
|
mod routes;
|
|
|
|
|
|
|
|
use saleor_app_sdk::{
|
2024-03-21 15:21:01 +00:00
|
|
|
cargo_info,
|
2024-03-11 13:11:47 +00:00
|
|
|
config::Config,
|
2024-04-03 17:47:23 +00:00
|
|
|
manifest::{AppManifestBuilder, AppPermission},
|
|
|
|
webhooks::{SyncWebhookEventType, WebhookManifestBuilder},
|
2024-03-11 13:11:47 +00:00
|
|
|
SaleorApp,
|
|
|
|
};
|
|
|
|
use std::sync::Arc;
|
|
|
|
use tokio::sync::Mutex;
|
|
|
|
|
|
|
|
use crate::{
|
2024-04-03 17:47:23 +00:00
|
|
|
app::{get_active_payment_methods_from_env, trace_to_std, AppState},
|
2024-03-11 13:11:47 +00:00
|
|
|
queries::event_transactions::{
|
2024-04-03 17:47:23 +00:00
|
|
|
sub_payment_gateway_initialize_session, sub_transaction_cancelation_requested,
|
|
|
|
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()?;
|
2024-04-03 17:47:23 +00:00
|
|
|
trace_to_std(&config)?;
|
2024-03-11 13:11:47 +00:00
|
|
|
|
|
|
|
let saleor_app = SaleorApp::new(&config)?;
|
|
|
|
|
2024-04-03 17:47:23 +00:00
|
|
|
let app_manifest = AppManifestBuilder::new(&config, cargo_info!())
|
2024-03-11 13:11:47 +00:00
|
|
|
.add_webhook(
|
2024-04-03 17:47:23 +00:00
|
|
|
WebhookManifestBuilder::new(&config)
|
2024-03-11 13:11:47 +00:00
|
|
|
.set_query(sub_transaction_process_session)
|
|
|
|
.add_sync_event(SyncWebhookEventType::TransactionProcessSession)
|
|
|
|
.build(),
|
|
|
|
)
|
|
|
|
.add_webhook(
|
2024-04-03 17:47:23 +00:00
|
|
|
WebhookManifestBuilder::new(&config)
|
2024-03-11 13:11:47 +00:00
|
|
|
.set_query(sub_transaction_charge_requested)
|
|
|
|
.add_sync_event(SyncWebhookEventType::TransactionChargeRequested)
|
|
|
|
.build(),
|
|
|
|
)
|
|
|
|
.add_webhook(
|
2024-04-03 17:47:23 +00:00
|
|
|
WebhookManifestBuilder::new(&config)
|
2024-03-11 13:11:47 +00:00
|
|
|
.set_query(sub_transaction_refund_requested)
|
|
|
|
.add_sync_event(SyncWebhookEventType::TransactionRefundRequested)
|
|
|
|
.build(),
|
|
|
|
)
|
|
|
|
.add_webhook(
|
2024-04-03 17:47:23 +00:00
|
|
|
WebhookManifestBuilder::new(&config)
|
2024-03-11 13:11:47 +00:00
|
|
|
.set_query(sub_transaction_initialize_session)
|
|
|
|
.add_sync_event(SyncWebhookEventType::TransactionInitializeSession)
|
|
|
|
.build(),
|
|
|
|
)
|
|
|
|
.add_webhook(
|
2024-04-03 17:47:23 +00:00
|
|
|
WebhookManifestBuilder::new(&config)
|
2024-03-11 13:11:47 +00:00
|
|
|
.set_query(sub_payment_gateway_initialize_session)
|
|
|
|
.add_sync_event(SyncWebhookEventType::PaymentGatewayInitializeSession)
|
|
|
|
.build(),
|
|
|
|
)
|
2024-03-18 15:12:39 +00:00
|
|
|
.add_webhook(
|
2024-04-03 17:47:23 +00:00
|
|
|
WebhookManifestBuilder::new(&config)
|
2024-03-18 15:12:39 +00:00
|
|
|
.set_query(sub_transaction_cancelation_requested)
|
|
|
|
.add_sync_event(SyncWebhookEventType::TransactionCancelationRequested)
|
|
|
|
.build(),
|
|
|
|
)
|
2024-04-03 17:47:23 +00:00
|
|
|
// .add_webhook(
|
|
|
|
// WebhookManifestBuilder::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-04-03 17:47:23 +00:00
|
|
|
active_payment_methods: get_active_payment_methods_from_env()?,
|
2024-03-11 13:11:47 +00:00
|
|
|
manifest: app_manifest,
|
|
|
|
config: config.clone(),
|
|
|
|
saleor_app: Arc::new(Mutex::new(saleor_app)),
|
2024-07-03 14:07:04 +00:00
|
|
|
cod_extra_price_as_product_slug: std::env::var("COD_EXTRA_PRICE_AS_PRODUCT_SLUG").ok()
|
2024-03-11 13:11:47 +00:00
|
|
|
};
|
|
|
|
let app = create_routes(app_state);
|
|
|
|
|
|
|
|
let listener = tokio::net::TcpListener::bind(
|
2024-03-21 15:21:01 +00:00
|
|
|
"0.0.0.0:".to_owned()
|
|
|
|
+ config
|
|
|
|
.app_api_base_url
|
|
|
|
.split(':')
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.get(2)
|
|
|
|
.unwrap_or(&"3000"),
|
2024-03-11 13:11:47 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2024-04-03 17:47:23 +00:00
|
|
|
tracing::debug!("listening on {}", listener.local_addr()?);
|
2024-03-11 13:11:47 +00:00
|
|
|
match axum::serve(listener, app).await {
|
|
|
|
Ok(o) => Ok(o),
|
|
|
|
Err(e) => anyhow::bail!(e),
|
|
|
|
}
|
2024-03-09 18:59:15 +00:00
|
|
|
}
|