diff --git a/Cargo.lock b/Cargo.lock index 7bf0747..c52371e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -71,6 +71,17 @@ dependencies = [ "opaque-debug", ] +[[package]] +name = "ahash" +version = "0.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "891477e0c6a8957309ee5c45a6368af3ae14bb510732d2684ffa19af310920f9" +dependencies = [ + "getrandom 0.2.12", + "once_cell", + "version_check", +] + [[package]] name = "aho-corasick" version = "1.1.2" @@ -113,6 +124,12 @@ version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bddcadddf5e9015d310179a59bb28c4d4b9920ad0f11e8e14dbadf654890c9a6" +[[package]] +name = "arrayvec" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" + [[package]] name = "ascii" version = "0.9.3" @@ -386,6 +403,18 @@ version = "2.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" +[[package]] +name = "bitvec" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c" +dependencies = [ + "funty", + "radium", + "tap", + "wyz", +] + [[package]] name = "block-buffer" version = "0.9.0" @@ -420,6 +449,30 @@ dependencies = [ "tracing", ] +[[package]] +name = "borsh" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f58b559fd6448c6e2fd0adb5720cd98a2506594cafa4737ff98c396f3e82f667" +dependencies = [ + "borsh-derive", + "cfg_aliases", +] + +[[package]] +name = "borsh-derive" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7aadb5b6ccbd078890f6d7003694e33816e6b784358f18e15e7e6d9f065a57cd" +dependencies = [ + "once_cell", + "proc-macro-crate", + "proc-macro2", + "quote", + "syn 2.0.48", + "syn_derive", +] + [[package]] name = "bstr" version = "1.9.1" @@ -436,6 +489,28 @@ version = "3.15.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ea184aa71bb362a1157c896979544cc23974e08fd265f29ea96b59f0b4a555b" +[[package]] +name = "bytecheck" +version = "0.6.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23cdc57ce23ac53c931e88a43d06d070a6fd142f2617be5855eb75efc9beb1c2" +dependencies = [ + "bytecheck_derive", + "ptr_meta", + "simdutf8", +] + +[[package]] +name = "bytecheck_derive" +version = "0.6.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3db406d29fbcd95542e92559bed4d8ad92636d1ca8b3b72ede10b4bcc010e659" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "byteorder" version = "1.5.0" @@ -469,6 +544,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "cfg_aliases" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" + [[package]] name = "chrono" version = "0.4.34" @@ -1011,6 +1092,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "funty" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" + [[package]] name = "futures" version = "0.3.30" @@ -1268,6 +1355,15 @@ version = "1.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b43ede17f21864e81be2fa654110bf1e793774238d86ef8555c37e6519c0403" +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +dependencies = [ + "ahash", +] + [[package]] name = "hashbrown" version = "0.14.3" @@ -1550,7 +1646,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "233cf39063f058ea2caae4091bf4a3ef70a653afbc026f5c4a4135d114e3c177" dependencies = [ "equivalent", - "hashbrown", + "hashbrown 0.14.3", ] [[package]] @@ -2139,6 +2235,38 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" +[[package]] +name = "proc-macro-crate" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d37c51ca738a55da99dc0c4a34860fd675453b8b36209178c2249bb13651284" +dependencies = [ + "toml_edit", +] + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + [[package]] name = "proc-macro-hack" version = "0.5.20+deprecated" @@ -2167,6 +2295,26 @@ dependencies = [ "yansi", ] +[[package]] +name = "ptr_meta" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0738ccf7ea06b608c10564b31debd4f5bc5e197fc8bfe088f68ae5ce81e7a4f1" +dependencies = [ + "ptr_meta_derive", +] + +[[package]] +name = "ptr_meta_derive" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16b845dbfca988fa33db069c0e230574d15a3088f147a87b64c7589eb662c9ac" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "quick-xml" version = "0.31.0" @@ -2186,6 +2334,12 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "radium" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" + [[package]] name = "rand" version = "0.7.3" @@ -2354,6 +2508,15 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" +[[package]] +name = "rend" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71fe3824f5629716b1589be05dacd749f6aa084c87e00e016714a8cdfccc997c" +dependencies = [ + "bytecheck", +] + [[package]] name = "reqwest" version = "0.11.24" @@ -2409,6 +2572,51 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "rkyv" +version = "0.7.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cba464629b3394fc4dbc6f940ff8f5b4ff5c7aef40f29166fd4ad12acbc99c0" +dependencies = [ + "bitvec", + "bytecheck", + "bytes 1.5.0", + "hashbrown 0.12.3", + "ptr_meta", + "rend", + "rkyv_derive", + "seahash", + "tinyvec", + "uuid", +] + +[[package]] +name = "rkyv_derive" +version = "0.7.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7dddfff8de25e6f62b9d64e6e432bf1c6736c57d20323e15ee10435fbda7c65" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "rust_decimal" +version = "1.34.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b39449a79f45e8da28c57c341891b69a183044b29518bb8f86dbac9df60bb7df" +dependencies = [ + "arrayvec", + "borsh", + "bytes 1.5.0", + "num-traits", + "rand 0.8.5", + "rkyv", + "serde", + "serde_json", +] + [[package]] name = "rustc-demangle" version = "0.1.23" @@ -2485,6 +2693,7 @@ dependencies = [ "jsonwebtoken", "redis", "reqwest", + "rust_decimal", "serde", "serde_json", "strum", @@ -2542,6 +2751,12 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "seahash" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c107b6f4780854c8b126e228ea8869f4d7b71260f962fefb57b996b8959ba6b" + [[package]] name = "security-framework" version = "2.9.2" @@ -2711,6 +2926,12 @@ dependencies = [ "libc", ] +[[package]] +name = "simdutf8" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f27f6278552951f1f2b8cf9da965d10969b2efdea95a6ec47987ab46edfe263a" + [[package]] name = "simple-payment-gateway" version = "0.1.0" @@ -2724,6 +2945,7 @@ dependencies = [ "enum-iterator", "envy", "redis", + "rust_decimal", "saleor-app-sdk", "serde", "serde_json", @@ -2990,6 +3212,18 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "syn_derive" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1329189c02ff984e9736652b1631330da25eaa6bc639089ed4915d25446cbe7b" +dependencies = [ + "proc-macro-error", + "proc-macro2", + "quote", + "syn 2.0.48", +] + [[package]] name = "sync_wrapper" version = "0.1.2" @@ -3017,6 +3251,12 @@ dependencies = [ "libc", ] +[[package]] +name = "tap" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" + [[package]] name = "tempfile" version = "3.10.1" @@ -3234,6 +3474,23 @@ dependencies = [ "tracing", ] +[[package]] +name = "toml_datetime" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1" + +[[package]] +name = "toml_edit" +version = "0.21.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a8534fd7f78b5405e860340ad6575217ce99f38d4d5c8f2442cb5ecb50090e1" +dependencies = [ + "indexmap", + "toml_datetime", + "winnow", +] + [[package]] name = "tower" version = "0.4.13" @@ -3510,6 +3767,12 @@ dependencies = [ "serde", ] +[[package]] +name = "uuid" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f00cc9702ca12d3c81455259621e676d0f7251cec66a21e98fe2e9a37db93b2a" + [[package]] name = "valuable" version = "0.1.0" @@ -3825,6 +4088,15 @@ version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8" +[[package]] +name = "winnow" +version = "0.5.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f593a95398737aeed53e489c785df13f3618e41dbcd6718c6addbf1395aa6876" +dependencies = [ + "memchr", +] + [[package]] name = "winreg" version = "0.50.0" @@ -3835,6 +4107,15 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "wyz" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" +dependencies = [ + "tap", +] + [[package]] name = "xml-builder" version = "0.5.2" diff --git a/Cargo.toml b/Cargo.toml index 35a17f0..d53637e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,3 +24,4 @@ saleor-app-sdk = { path = "sdk" } tower = { version = "0.4.13", features = ["util"] } tower-http = { version = "0.5.2", features = ["fs", "trace"] } cynic-codegen = "3.4.3" +rust_decimal = { version = "1.34.3", features = ["serde-float"] } diff --git a/sdk/Cargo.toml b/sdk/Cargo.toml index 964e816..09728bc 100644 --- a/sdk/Cargo.toml +++ b/sdk/Cargo.toml @@ -26,6 +26,7 @@ serde_json.workspace = true envy.workspace = true dotenvy.workspace = true tower = { workspace = true } +rust_decimal = { workspace = true, features = ["serde-float"] } reqwest = { version = "0.11.24", features = ["json"] } jsonwebtoken = "9.2.0" async-trait = "0.1.77" diff --git a/sdk/src/webhooks/sync_response.rs b/sdk/src/webhooks/sync_response.rs index 477e448..6107020 100644 --- a/sdk/src/webhooks/sync_response.rs +++ b/sdk/src/webhooks/sync_response.rs @@ -1,19 +1,26 @@ +use rust_decimal::Decimal; use serde::Serialize; //Why are these few in snake_case but rest is camelCase? #[derive(Serialize, Debug, Clone)] pub struct CheckoutCalculateTaxesResponse { - pub shipping_price_gross_amount: f32, - pub shipping_price_net_amount: f32, - pub shipping_tax_rate: f32, + #[serde(with = "rust_decimal::serde::float")] + pub shipping_price_gross_amount: Decimal, + #[serde(with = "rust_decimal::serde::float")] + pub shipping_price_net_amount: Decimal, + #[serde(with = "rust_decimal::serde::float")] + pub shipping_tax_rate: Decimal, pub lines: Vec, } #[derive(Serialize, Debug, Clone)] pub struct LinesResponse { - pub total_gross_amount: f32, - pub total_net_amount: f32, - pub tax_rate: f32, + #[serde(with = "rust_decimal::serde::float")] + pub total_gross_amount: Decimal, + #[serde(with = "rust_decimal::serde::float")] + pub total_net_amount: Decimal, + #[serde(with = "rust_decimal::serde::float")] + pub tax_rate: Decimal, } #[derive(Serialize, Debug, Clone)] @@ -40,7 +47,8 @@ pub struct ShippingListMethodsForCheckout(Vec struct ShippingListMethodsForCheckoutVec { pub id: String, pub name: Option, - pub amount: f32, + #[serde(with = "rust_decimal::serde::float")] + pub amount: Decimal, pub currency: String, pub maximum_delivery_days: Option, } @@ -56,7 +64,8 @@ pub enum ChargeRequestedResult { pub struct TransactionChargeRequestedResponse { pub psp_reference: String, pub result: Option, - pub amount: Option, + #[serde(with = "rust_decimal::serde::float_option")] + pub amount: Option, pub time: Option, pub external_url: Option, pub message: Option, @@ -74,7 +83,8 @@ pub enum RefundRequestedResult { pub struct TransactionRefundRequestedResponse { pub psp_reference: String, pub result: Option, - pub amount: Option, + #[serde(with = "rust_decimal::serde::float_option")] + pub amount: Option, pub time: Option, pub external_url: Option, pub message: Option, @@ -92,7 +102,8 @@ pub enum CancelationRequestedResult { pub struct TransactionCancelationRequestedResponse { pub psp_reference: String, pub result: Option, - pub amount: Option, + #[serde(with = "rust_decimal::serde::float_option")] + pub amount: Option, pub time: Option, pub external_url: Option, pub message: Option, @@ -100,7 +111,7 @@ pub struct TransactionCancelationRequestedResponse { #[derive(Serialize, Debug, Clone)] pub struct PaymentGatewayInitializeSessionResponse { - pub data: T, + pub data: Option, } #[derive(Serialize, Debug, Clone)] @@ -122,7 +133,8 @@ pub struct TransactionInitializeSessionResponse { pub psp_reference: Option, pub data: Option, pub result: TransactionSessionResult, - pub amount: f32, + #[serde(with = "rust_decimal::serde::float")] + pub amount: Decimal, pub time: Option, pub external_url: Option, pub message: Option, @@ -134,7 +146,8 @@ pub struct TransactionProcessSessionResponse { pub psp_reference: Option, pub data: Option, pub result: TransactionSessionResult, - pub amount: f32, + #[serde(with = "rust_decimal::serde::float")] + pub amount: Decimal, pub time: Option, pub external_url: Option, pub message: Option, diff --git a/simple-payment-gateway/Cargo.toml b/simple-payment-gateway/Cargo.toml index 1595898..4a2a98e 100644 --- a/simple-payment-gateway/Cargo.toml +++ b/simple-payment-gateway/Cargo.toml @@ -33,6 +33,7 @@ tower-http = { workspace = true, features = ["fs", "trace"] } surf.workspace = true cynic = { workspace = true, features = ["http-surf"] } cynic-codegen.workspace = true +rust_decimal = { workspace = true, features = ["serde-float"] } const_format = "0.2.32" enum-iterator = "2.0.0" diff --git a/simple-payment-gateway/src/app.rs b/simple-payment-gateway/src/app.rs index 6c3d883..e804487 100644 --- a/simple-payment-gateway/src/app.rs +++ b/simple-payment-gateway/src/app.rs @@ -43,6 +43,7 @@ pub fn trace_to_std(config: &Config) { } #[derive(Debug, Clone, Sequence, Serialize)] +#[serde(rename_all = "lowercase")] pub enum GatewayType { Accreditation, Cash, @@ -82,10 +83,30 @@ pub fn get_active_gateways_from_env() -> anyhow::Result> { true => Some(g), false => None, }) - .map(|g| ) + .map(|g| ActiveGateway { + gateway_type: g.clone(), + currencies: vec!["EUR".to_owned()], + id: format!("{:?}", &g).to_lowercase(), + config: [], + name: match (g, &locale) { + (GatewayType::COD, LocaleCode::Sk) => "Dobierka".to_owned(), + (GatewayType::Cash, LocaleCode::Sk) => "Hotovosť".to_owned(), + (GatewayType::Transfer, LocaleCode::Sk) => "Bankový prevod".to_owned(), + (GatewayType::Inkaso, LocaleCode::Sk) => "Inkaso".to_owned(), + (GatewayType::Accreditation, LocaleCode::Sk) => "Vzajomný zápočet".to_owned(), + (GatewayType::Other, LocaleCode::Sk) => "Iné".to_owned(), + (GatewayType::COD, LocaleCode::En) => "Cash on delivery".to_owned(), + (GatewayType::Cash, LocaleCode::En) => "Cash".to_owned(), + (GatewayType::Transfer, LocaleCode::En) => "Bank transfer".to_owned(), + (GatewayType::Inkaso, LocaleCode::En) => "Encashment".to_owned(), + (GatewayType::Accreditation, LocaleCode::En) => "Mutual credit".to_owned(), + (GatewayType::Other, LocaleCode::En) => "Other".to_owned(), + (g, l) => unimplemented!("Gateway {:?} in locale {:?} not implemented", g, l), + }, + }) .collect::>(); - todo!() + Ok(gateway_types) } #[derive(Debug, Clone, Serialize)] @@ -97,11 +118,3 @@ pub struct ActiveGateway { //don't need this one yet pub config: [(); 0], } -impl ActiveGateway{ - fn from_gateway_type(ty: &GatewayType) -> Self { - all_currencies = - match type { - - } - } -} diff --git a/simple-payment-gateway/src/queries/mutation_transaction_update.rs b/simple-payment-gateway/src/queries/mutation_transaction_update.rs index 02e162e..e2a8b23 100644 --- a/simple-payment-gateway/src/queries/mutation_transaction_update.rs +++ b/simple-payment-gateway/src/queries/mutation_transaction_update.rs @@ -70,7 +70,7 @@ pub enum TransactionUpdateErrorCode { Unique, } -#[derive(cynic::InputObject, Debug)] +#[derive(cynic::InputObject, Debug, Default)] pub struct TransactionUpdateInput<'a> { pub name: Option<&'a str>, pub message: Option<&'a str>, diff --git a/simple-payment-gateway/src/routes/webhooks.rs b/simple-payment-gateway/src/routes/webhooks.rs index 758e792..a28d198 100644 --- a/simple-payment-gateway/src/routes/webhooks.rs +++ b/simple-payment-gateway/src/routes/webhooks.rs @@ -1,5 +1,3 @@ -use std::str::FromStr; - use anyhow::Context; use axum::{ body::Body, @@ -8,20 +6,38 @@ use axum::{ response::Response, Json, }; +use cynic::{http::SurfExt, MutationBuilder}; +use rust_decimal::Decimal; use saleor_app_sdk::{ headers::SALEOR_API_URL_HEADER, webhooks::{ + sync_response::{ + ChargeRequestedResult, PaymentGatewayInitializeSessionResponse, RefundRequestedResult, + TransactionChargeRequestedResponse, TransactionInitializeSessionResponse, + TransactionProcessSessionResponse, TransactionRefundRequestedResponse, + TransactionSessionResult, + }, utils::{get_webhook_event_type, EitherWebhookType}, SyncWebhookEventType, }, }; use serde::{Deserialize, Serialize}; use serde_json::Value; +use std::str::FromStr; use tracing::{debug, error, info}; use crate::{ - app::{ActiveGateway, AppError, AppState}, - queries::event_transactions::PaymentGatewayInitializeSession, + app::{ActiveGateway, AppError, AppState, GatewayType}, + queries::{ + event_transactions::{ + PaymentGatewayInitializeSession, TransactionActionEnum, TransactionChargeRequested2, + TransactionFlowStrategyEnum, TransactionInitializeSession2, TransactionProcessSession, + TransactionProcessSession2, TransactionRefundRequested2, + }, + mutation_transaction_update::{ + TransactionUpdate, TransactionUpdateInput, TransactionUpdateVariables, + }, + }, }; pub async fn webhooks( @@ -33,7 +49,7 @@ pub async fn webhooks( debug!("req: {:?}", body); debug!("headers: {:?}", headers); - let url = headers + let saleor_api_url = headers .get(SALEOR_API_URL_HEADER) .context("missing saleor api url header")? .to_str()? @@ -41,19 +57,117 @@ pub async fn webhooks( let event_type = get_webhook_event_type(&headers)?; let res: Json = match event_type { EitherWebhookType::Sync(a) => match a { - SyncWebhookEventType::PaymentGatewayInitializeSession => { - Json::from(serde_json::to_value(JsonResponse { - data: JsonResponseData { - current_gateway: ActiveGateway::COD, + SyncWebhookEventType::PaymentGatewayInitializeSession => Json::from( + serde_json::to_value(PaymentGatewayInitializeSessionResponse:: { data: None })?, + ), + SyncWebhookEventType::TransactionProcessSession => { + let data = serde_json::from_str::(&body)?; + Json::from(serde_json::to_value(TransactionProcessSessionResponse::< + u8, + > { + data: None, + time: None, + psp_reference: None, + external_url: None, + message: None, + amount: Decimal::from_str(&data.action.amount.0)?, + result: match data.action.action_type { + TransactionFlowStrategyEnum::Charge => { + TransactionSessionResult::ChargeSuccess + } + TransactionFlowStrategyEnum::Authorization => { + TransactionSessionResult::AuthorizationSuccess + } }, })?) } - SyncWebhookEventType::TransactionProcessSession - | SyncWebhookEventType::TransactionChargeRequested - | SyncWebhookEventType::TransactionRefundRequested - | SyncWebhookEventType::TransactionInitializeSession => { - update_transaction_response(&state, &url).await?; - todo!() + SyncWebhookEventType::TransactionChargeRequested => { + let data = serde_json::from_str::(&body)?; + Json::from(serde_json::to_value(TransactionChargeRequestedResponse { + time: None, + psp_reference: "".to_owned(), + external_url: None, + message: None, + amount: data + .action + .amount + .and_then(|a| Decimal::from_str(&a.0).ok()), + result: Some(ChargeRequestedResult::ChargeSuccess), + })?) + } + SyncWebhookEventType::TransactionRefundRequested => { + let data = serde_json::from_str::(&body)?; + Json::from(serde_json::to_value(TransactionRefundRequestedResponse { + time: None, + psp_reference: "".to_owned(), + external_url: None, + message: None, + amount: data + .action + .amount + .and_then(|a| Decimal::from_str(&a.0).ok()), + result: Some(RefundRequestedResult::RefundSuccess), + })?) + } + SyncWebhookEventType::TransactionInitializeSession => { + let data = serde_json::from_str::(&body)?; + let app = state.saleor_app.lock().await; + let auth_data = app.apl.get(&saleor_api_url).await?; + let operation = TransactionUpdate::build(TransactionUpdateVariables { + id: &data.transaction.id, + transaction: Some(TransactionUpdateInput { + message: Some(""), + ..Default::default() + }), + }); + let mut res = surf::post(&saleor_api_url).run_graphql(operation).await; + + let mut webhook_result = WebhookResult::Failiure; + if let Ok(r) = &mut res + && let Some(data) = &mut r.data + && let Some(q_res) = &mut data.transaction_update + { + if !q_res.errors.is_empty() { + q_res + .errors + .iter() + .for_each(|e| error!("failed update transaction, {:?}", e)); + } else { + if let Some(tr) = &mut q_res.transaction { + tr.message = serde_json::to_string(&PaymentMethod { + payment_method: GatewayType::COD, + })?; + webhook_result = WebhookResult::Success; + } + } + } + + Json::from(serde_json::to_value( + TransactionInitializeSessionResponse:: { + data: None, + time: None, + psp_reference: None, + external_url: None, + message: None, + amount: Decimal::from_str(&data.action.amount.0)?, + result: match (data.action.action_type, webhook_result) { + (TransactionFlowStrategyEnum::Charge, WebhookResult::Success) => { + TransactionSessionResult::ChargeSuccess + } + ( + TransactionFlowStrategyEnum::Authorization, + WebhookResult::Success, + ) => TransactionSessionResult::AuthorizationSuccess, + (TransactionFlowStrategyEnum::Charge, WebhookResult::Failiure) => { + TransactionSessionResult::ChargeFailiure + } + ( + TransactionFlowStrategyEnum::Authorization, + WebhookResult::Failiure, + ) => TransactionSessionResult::AuthorizationFailure, + }, + }, + )?) } _ => Json::from(Value::from_str("")?), }, @@ -73,13 +187,12 @@ pub struct JsonResponse { pub struct JsonResponseData { current_gateway: ActiveGateway, } - -async fn update_transaction_response(state: &AppState, saleor_api_url: &str) -> anyhow::Result<()> { - todo!() +enum WebhookResult { + Success, + Failiure, } -async fn new_transaction_response(state: &AppState, saleor_api_url: &str) -> anyhow::Result<()> { - debug!("Creating new transaction"); - - todo!() +#[derive(Serialize, Clone, Debug)] +struct PaymentMethod { + payment_method: GatewayType, }