This commit is contained in:
ntr
2019-07-11 22:11:47 +10:00
parent 6419fe55a0
commit d27786fa5a
5 changed files with 29 additions and 12 deletions
-2
View File
@@ -153,8 +153,6 @@ pub fn credit(tx: &mut Transaction, id: Uuid, credits: i64) -> Result<String, Er
let row = result.iter().next()
.ok_or(format_err!("account not updated {:?}", id))?;
println!("{:?}", row);
let db_credits: i64 = row.get(0);
let total = u32::try_from(db_credits)
.or(Err(format_err!("user {:?} has unparsable balance {:?}", id, db_credits)))?;
+1 -1
View File
@@ -38,7 +38,7 @@ mod mob;
mod mtx;
mod names;
mod net;
// mod payments;
mod payments;
mod pg;
mod player;
mod pubsub;
+6 -1
View File
@@ -14,7 +14,7 @@ use router::Router;
// use ws::{connect};
use account;
use pg::PgPool;
// use payments::{post_stripe_event};
use payments::{stripe};
pub const TOKEN_HEADER: &str = "x-auth-token";
@@ -207,10 +207,15 @@ impl Key for State { type Value = State; }
pub fn start(pool: PgPool) {
let mut router = Router::new();
// auth
router.post("/api/login", login, "login");
router.post("/api/logout", logout, "logout");
router.post("/api/register", register, "register");
// payments
router.post("/api/payments/stripe", stripe, "stripe");
let mut chain = Chain::new(router);
chain.link(Read::<State>::both(State { pool }));
chain.link_before(Read::<bodyparser::MaxBodyLength>::one(MAX_BODY_LENGTH));
+20 -6
View File
@@ -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<Uuid, Error> {
@@ -183,16 +192,21 @@ fn process_stripe_event(event: Event, pool: &PgPool) -> Result<String, Error> {
Ok(event.id.to_string())
}
pub fn post_stripe_event(state: web::Data<State>, body: web::Json::<Event>) -> Result<HttpResponse, MnmlHttpError> {
let event: Event = body.into_inner();
pub fn stripe(req: &mut Request) -> IronResult<Response> {
let state = req.get::<Readable<State>>().unwrap();
let event = match req.get::<bodyparser::Struct<Event>>() {
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<State>, body: web::Json::<Event>) -> R
#[cfg(test)]
mod tests {
use super::*;
use std::io::prelude::*;
use std::fs::File;
#[test]