From 5cebec280a04b9389caaa725f664548d15e802f1 Mon Sep 17 00:00:00 2001 From: 0xZensh Date: Wed, 6 Sep 2023 21:09:24 +0800 Subject: [PATCH] Improve charge api. --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/api/charge.rs | 28 ++++++++++++++++++++-------- src/db/model_charge.rs | 2 ++ 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e43a0ed..1b24428 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2512,7 +2512,7 @@ dependencies = [ [[package]] name = "walletbase" -version = "0.3.0" +version = "0.3.1" dependencies = [ "aes-gcm", "anyhow", diff --git a/Cargo.toml b/Cargo.toml index 953f543..980168e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "walletbase" -version = "0.3.0" +version = "0.3.1" edition = "2021" rust-version = "1.64" description = "" diff --git a/src/api/charge.rs b/src/api/charge.rs index d0e2521..26a2d9a 100644 --- a/src/api/charge.rs +++ b/src/api/charge.rs @@ -20,13 +20,13 @@ use crate::db; #[derive(Debug, Deserialize, Validate)] pub struct ChargeInput { pub uid: PackObject, - pub currency: String, - #[validate(range(min = 10))] - pub amount: i64, - #[validate(range(min = 10, max = 1_000_000))] - pub quantity: i64, #[validate(length(min = 1), custom = "validate_provider")] pub provider: String, // stripe + #[validate(range(min = 10, max = 1_000_000))] + pub quantity: i64, + pub currency: Option, + #[validate(range(min = 1))] + pub amount: Option, pub charge_id: Option, pub charge_payload: Option>>, } @@ -103,7 +103,6 @@ pub async fn create( ) -> Result>, HTTPError> { let (to, input) = to.unpack(); input.validate()?; - let cur = Currency::from_str(&input.currency)?; let uid = input.uid.unwrap(); ctx.set_kvs(vec![ @@ -119,12 +118,20 @@ pub async fn create( let mut doc = db::Charge { uid, quantity: input.quantity, - currency: cur.alpha.to_lowercase(), - amount: input.amount, provider: input.provider, ..Default::default() }; + if let Some(amount) = input.amount { + let cur = Currency::from_str( + &input + .currency + .ok_or(HTTPError::new(400, "currency required".to_string()))?, + )?; + doc.amount = amount; + doc.currency = cur.alpha.to_lowercase(); + } + if let Some(charge_id) = input.charge_id { ctx.set("charge_id", charge_id.clone().into()).await; doc.status = 1; @@ -212,6 +219,11 @@ pub struct UpdateChargeInput { pub current_status: i8, #[validate(range(min = -2, max = 1))] pub status: Option, + pub currency: Option, + #[validate(range(min = 1))] + pub amount: Option, + #[validate(range(min = 1))] + pub amount_refunded: Option, pub charge_id: Option, pub charge_payload: Option>>, pub failure_code: Option, diff --git a/src/db/model_charge.rs b/src/db/model_charge.rs index 8ecea59..2de8a2a 100644 --- a/src/db/model_charge.rs +++ b/src/db/model_charge.rs @@ -123,6 +123,8 @@ impl Charge { ) -> anyhow::Result { let valid_fields = [ "status", + "currency", + "amount", "amount_refunded", "charge_id", "charge_payload",