From d27786fa5a6a55bf1488ad38f8c4e9ba1eb603b6 Mon Sep 17 00:00:00 2001 From: ntr Date: Thu, 11 Jul 2019 22:11:47 +1000 Subject: [PATCH] stripe --- client/src/components/account.status.jsx | 4 ++-- server/src/account.rs | 2 -- server/src/main.rs | 2 +- server/src/net.rs | 7 ++++++- server/src/payments.rs | 26 ++++++++++++++++++------ 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/client/src/components/account.status.jsx b/client/src/components/account.status.jsx index beb2a844..a2e61b0b 100644 --- a/client/src/components/account.status.jsx +++ b/client/src/components/account.status.jsx @@ -19,8 +19,8 @@ function BitsBtn(args) { function subscribeClick(e) { stripe.redirectToCheckout({ items: [{plan: 'plan_FGmRwawcOJJ7Nv', quantity: 1}], - successUrl: 'http://localhost/payments/success', - cancelUrl: 'http://localhost/payments/cancel', + successUrl: 'http://localhost/api/payments/success', + cancelUrl: 'http://localhost/api/payments/cancel', clientReferenceId: account.id }); } diff --git a/server/src/account.rs b/server/src/account.rs index 4d0197ee..dd5f988b 100644 --- a/server/src/account.rs +++ b/server/src/account.rs @@ -153,8 +153,6 @@ pub fn credit(tx: &mut Transaction, id: Uuid, credits: i64) -> Result::both(State { pool })); chain.link_before(Read::::one(MAX_BODY_LENGTH)); diff --git a/server/src/payments.rs b/server/src/payments.rs index 78ed1f7f..6fd9529e 100644 --- a/server/src/payments.rs +++ b/server/src/payments.rs @@ -1,3 +1,11 @@ +use std::io::Read; + +use net::State; +use iron::prelude::*; +use iron::response::HttpResponse; +use iron::status; +use persistent::Read as Readable; + use uuid::Uuid; use postgres::transaction::Transaction; @@ -6,7 +14,8 @@ use failure::err_msg; use stripe::{Event, EventObject, CheckoutSession, SubscriptionStatus}; -use net::{State, PgPool, MnmlHttpError}; +use net::{MnmlHttpError}; +use pg::{PgPool}; use account; pub fn subscription_account(tx: &mut Transaction, sub: String) -> Result { @@ -183,16 +192,21 @@ fn process_stripe_event(event: Event, pool: &PgPool) -> Result { Ok(event.id.to_string()) } -pub fn post_stripe_event(state: web::Data, body: web::Json::) -> Result { - let event: Event = body.into_inner(); +pub fn stripe(req: &mut Request) -> IronResult { + let state = req.get::>().unwrap(); + let event = match req.get::>() { + Ok(Some(b)) => b, + _ => return Err(IronError::from(MnmlHttpError::BadRequest)), + }; + match process_stripe_event(event, &state.pool) { Ok(id)=> { info!("event processed successfully {:?}", id); - Ok(HttpResponse::Ok().finish()) + Ok(Response::with(status::Ok)) } Err(e) => { error!("{:?}", e); - Err(MnmlHttpError::ServerError) + Err(IronError::from(MnmlHttpError::ServerError)) } } } @@ -200,7 +214,7 @@ pub fn post_stripe_event(state: web::Data, body: web::Json::) -> R #[cfg(test)] mod tests { use super::*; - use std::io::prelude::*; + use std::fs::File; #[test]