2024-03-12 19:48:20 +00:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
2024-03-11 13:11:47 +00:00
|
|
|
use anyhow::Context;
|
|
|
|
use axum::{
|
2024-03-12 19:48:20 +00:00
|
|
|
body::Body,
|
2024-03-11 13:11:47 +00:00
|
|
|
extract::State,
|
|
|
|
http::{HeaderMap, StatusCode},
|
2024-03-12 19:48:20 +00:00
|
|
|
response::Response,
|
|
|
|
Json,
|
2024-03-11 13:11:47 +00:00
|
|
|
};
|
|
|
|
use saleor_app_sdk::{
|
|
|
|
headers::SALEOR_API_URL_HEADER,
|
|
|
|
webhooks::{
|
|
|
|
utils::{get_webhook_event_type, EitherWebhookType},
|
|
|
|
SyncWebhookEventType,
|
|
|
|
},
|
|
|
|
};
|
2024-03-12 19:48:20 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use serde_json::Value;
|
2024-03-11 13:11:47 +00:00
|
|
|
use tracing::{debug, error, info};
|
|
|
|
|
2024-03-12 19:48:20 +00:00
|
|
|
use crate::{
|
|
|
|
app::{ActiveGateway, AppError, AppState},
|
|
|
|
queries::event_transactions::PaymentGatewayInitializeSession,
|
|
|
|
};
|
2024-03-11 13:11:47 +00:00
|
|
|
|
|
|
|
pub async fn webhooks(
|
|
|
|
headers: HeaderMap,
|
|
|
|
State(state): State<AppState>,
|
|
|
|
body: String,
|
2024-03-12 19:48:20 +00:00
|
|
|
) -> Result<Json<Value>, AppError> {
|
2024-03-11 13:11:47 +00:00
|
|
|
debug!("/api/webhooks");
|
|
|
|
debug!("req: {:?}", body);
|
|
|
|
debug!("headers: {:?}", headers);
|
|
|
|
|
|
|
|
let url = headers
|
|
|
|
.get(SALEOR_API_URL_HEADER)
|
|
|
|
.context("missing saleor api url header")?
|
|
|
|
.to_str()?
|
|
|
|
.to_owned();
|
|
|
|
let event_type = get_webhook_event_type(&headers)?;
|
2024-03-12 19:48:20 +00:00
|
|
|
let res: Json<Value> = match event_type {
|
2024-03-11 13:11:47 +00:00
|
|
|
EitherWebhookType::Sync(a) => match a {
|
|
|
|
SyncWebhookEventType::PaymentGatewayInitializeSession => {
|
2024-03-12 19:48:20 +00:00
|
|
|
Json::from(serde_json::to_value(JsonResponse {
|
|
|
|
data: JsonResponseData {
|
|
|
|
current_gateway: ActiveGateway::COD,
|
|
|
|
},
|
|
|
|
})?)
|
2024-03-11 13:11:47 +00:00
|
|
|
}
|
|
|
|
SyncWebhookEventType::TransactionProcessSession
|
|
|
|
| SyncWebhookEventType::TransactionChargeRequested
|
|
|
|
| SyncWebhookEventType::TransactionRefundRequested
|
|
|
|
| SyncWebhookEventType::TransactionInitializeSession => {
|
|
|
|
update_transaction_response(&state, &url).await?;
|
2024-03-12 19:48:20 +00:00
|
|
|
todo!()
|
2024-03-11 13:11:47 +00:00
|
|
|
}
|
2024-03-12 19:48:20 +00:00
|
|
|
_ => Json::from(Value::from_str("")?),
|
2024-03-11 13:11:47 +00:00
|
|
|
},
|
2024-03-12 19:48:20 +00:00
|
|
|
_ => Json::from(Value::from_str("")?),
|
|
|
|
};
|
2024-03-11 13:11:47 +00:00
|
|
|
|
|
|
|
info!("got webhooks!");
|
2024-03-12 19:48:20 +00:00
|
|
|
Ok(res)
|
2024-03-11 13:11:47 +00:00
|
|
|
}
|
|
|
|
|
2024-03-12 19:48:20 +00:00
|
|
|
#[derive(Serialize, Clone, Debug)]
|
|
|
|
pub struct JsonResponse {
|
|
|
|
data: JsonResponseData,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Clone, Debug)]
|
|
|
|
pub struct JsonResponseData {
|
|
|
|
current_gateway: ActiveGateway,
|
2024-03-11 13:11:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn update_transaction_response(state: &AppState, saleor_api_url: &str) -> anyhow::Result<()> {
|
|
|
|
todo!()
|
|
|
|
}
|
2024-03-12 19:48:20 +00:00
|
|
|
|
|
|
|
async fn new_transaction_response(state: &AppState, saleor_api_url: &str) -> anyhow::Result<()> {
|
|
|
|
debug!("Creating new transaction");
|
|
|
|
|
|
|
|
todo!()
|
|
|
|
}
|