|
@@ -7,12 +7,13 @@ use uuid::Uuid;
|
|
|
use crate::{
|
|
use crate::{
|
|
|
http_error::HttpError,
|
|
http_error::HttpError,
|
|
|
auth::user_auth,
|
|
auth::user_auth,
|
|
|
- STRIPE_KEY, TOKEN_PACK_PRICE
|
|
|
|
|
|
|
+ models::purchase::{Purchase},
|
|
|
|
|
+ STRIPE_KEY, TOKEN_PACK_PRICE, TOKENS_PER_PACK
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
#[derive(Deserialize)]
|
|
|
struct Body {
|
|
struct Body {
|
|
|
- token_packs: u32
|
|
|
|
|
|
|
+ token_packs: i32
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#[post("/user/payment-intent")]
|
|
#[post("/user/payment-intent")]
|
|
@@ -25,11 +26,13 @@ pub async fn route(
|
|
|
return Err(HttpError::InvalidInput("Must buy minimum 5 token packs".into()));
|
|
return Err(HttpError::InvalidInput("Must buy minimum 5 token packs".into()));
|
|
|
}
|
|
}
|
|
|
let user = user_auth(&db, &req).await?;
|
|
let user = user_auth(&db, &req).await?;
|
|
|
- let client_secret = request_payment_intent(body.token_packs, user.id).await?;
|
|
|
|
|
|
|
+ let (pi_id, client_secret) = request_payment_intent(body.token_packs, user.id).await?;
|
|
|
|
|
+ let purchase = create_purchase(user.id, pi_id, body.token_packs)?;
|
|
|
|
|
+ purchase.insert_one(&db).await?;
|
|
|
Ok(HttpResponse::Ok().json(json!({"client_secret": client_secret})))
|
|
Ok(HttpResponse::Ok().json(json!({"client_secret": client_secret})))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-async fn request_payment_intent(token_packs: u32, user_id: Uuid) -> Result<String, HttpError> {
|
|
|
|
|
|
|
+async fn request_payment_intent(token_packs: i32, user_id: Uuid) -> Result<(String, String), HttpError> {
|
|
|
let client = Client::new();
|
|
let client = Client::new();
|
|
|
let amount = (token_packs * TOKEN_PACK_PRICE.get().unwrap()).to_string();
|
|
let amount = (token_packs * TOKEN_PACK_PRICE.get().unwrap()).to_string();
|
|
|
|
|
|
|
@@ -48,8 +51,30 @@ async fn request_payment_intent(token_packs: u32, user_id: Uuid) -> Result<Strin
|
|
|
|
|
|
|
|
let body = res.json::<Value>().await
|
|
let body = res.json::<Value>().await
|
|
|
.map_err(|_| HttpError::InternalError("Failed to convert stripe response to json".into()))?;
|
|
.map_err(|_| HttpError::InternalError("Failed to convert stripe response to json".into()))?;
|
|
|
- Ok(body["client_secret"]
|
|
|
|
|
- .as_str()
|
|
|
|
|
- .ok_or(HttpError::InternalError("Bad client secret".into()))?
|
|
|
|
|
- .to_string())
|
|
|
|
|
|
|
+
|
|
|
|
|
+ Ok((
|
|
|
|
|
+ body["id"]
|
|
|
|
|
+ .as_str()
|
|
|
|
|
+ .ok_or(HttpError::InternalError("Bad Payment Intent ID".into()))?
|
|
|
|
|
+ .to_string(),
|
|
|
|
|
+ body["client_secret"]
|
|
|
|
|
+ .as_str()
|
|
|
|
|
+ .ok_or(HttpError::InternalError("Bad client secret".into()))?
|
|
|
|
|
+ .to_string()
|
|
|
|
|
+ ))
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+fn create_purchase(user_id: Uuid, pi_id: String, pack_count: i32) -> Result<Purchase, HttpError> {
|
|
|
|
|
+ Purchase::new(
|
|
|
|
|
+ user_id,
|
|
|
|
|
+ pi_id,
|
|
|
|
|
+ "Pending".to_string(),
|
|
|
|
|
+ pack_count,
|
|
|
|
|
+ *TOKEN_PACK_PRICE
|
|
|
|
|
+ .get()
|
|
|
|
|
+ .unwrap(),
|
|
|
|
|
+ *TOKENS_PER_PACK
|
|
|
|
|
+ .get()
|
|
|
|
|
+ .unwrap()
|
|
|
|
|
+ )
|
|
|
}
|
|
}
|