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

View File

@ -19,8 +19,8 @@ function BitsBtn(args) {
function subscribeClick(e) { function subscribeClick(e) {
stripe.redirectToCheckout({ stripe.redirectToCheckout({
items: [{plan: 'plan_FGmRwawcOJJ7Nv', quantity: 1}], items: [{plan: 'plan_FGmRwawcOJJ7Nv', quantity: 1}],
successUrl: 'http://localhost/payments/success', successUrl: 'http://localhost/api/payments/success',
cancelUrl: 'http://localhost/payments/cancel', cancelUrl: 'http://localhost/api/payments/cancel',
clientReferenceId: account.id clientReferenceId: account.id
}); });
} }

View File

@ -153,8 +153,6 @@ pub fn credit(tx: &mut Transaction, id: Uuid, credits: i64) -> Result<String, Er
let row = result.iter().next() let row = result.iter().next()
.ok_or(format_err!("account not updated {:?}", id))?; .ok_or(format_err!("account not updated {:?}", id))?;
println!("{:?}", row);
let db_credits: i64 = row.get(0); let db_credits: i64 = row.get(0);
let total = u32::try_from(db_credits) let total = u32::try_from(db_credits)
.or(Err(format_err!("user {:?} has unparsable balance {:?}", id, db_credits)))?; .or(Err(format_err!("user {:?} has unparsable balance {:?}", id, db_credits)))?;

View File

@ -38,7 +38,7 @@ mod mob;
mod mtx; mod mtx;
mod names; mod names;
mod net; mod net;
// mod payments; mod payments;
mod pg; mod pg;
mod player; mod player;
mod pubsub; mod pubsub;

View File

@ -14,7 +14,7 @@ use router::Router;
// use ws::{connect}; // use ws::{connect};
use account; use account;
use pg::PgPool; use pg::PgPool;
// use payments::{post_stripe_event}; use payments::{stripe};
pub const TOKEN_HEADER: &str = "x-auth-token"; pub const TOKEN_HEADER: &str = "x-auth-token";
@ -207,10 +207,15 @@ impl Key for State { type Value = State; }
pub fn start(pool: PgPool) { pub fn start(pool: PgPool) {
let mut router = Router::new(); let mut router = Router::new();
// auth
router.post("/api/login", login, "login"); router.post("/api/login", login, "login");
router.post("/api/logout", logout, "logout"); router.post("/api/logout", logout, "logout");
router.post("/api/register", register, "register"); router.post("/api/register", register, "register");
// payments
router.post("/api/payments/stripe", stripe, "stripe");
let mut chain = Chain::new(router); let mut chain = Chain::new(router);
chain.link(Read::<State>::both(State { pool })); chain.link(Read::<State>::both(State { pool }));
chain.link_before(Read::<bodyparser::MaxBodyLength>::one(MAX_BODY_LENGTH)); chain.link_before(Read::<bodyparser::MaxBodyLength>::one(MAX_BODY_LENGTH));

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 uuid::Uuid;
use postgres::transaction::Transaction; use postgres::transaction::Transaction;
@ -6,7 +14,8 @@ use failure::err_msg;
use stripe::{Event, EventObject, CheckoutSession, SubscriptionStatus}; use stripe::{Event, EventObject, CheckoutSession, SubscriptionStatus};
use net::{State, PgPool, MnmlHttpError}; use net::{MnmlHttpError};
use pg::{PgPool};
use account; use account;
pub fn subscription_account(tx: &mut Transaction, sub: String) -> Result<Uuid, Error> { 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()) Ok(event.id.to_string())
} }
pub fn post_stripe_event(state: web::Data<State>, body: web::Json::<Event>) -> Result<HttpResponse, MnmlHttpError> { pub fn stripe(req: &mut Request) -> IronResult<Response> {
let event: Event = body.into_inner(); 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) { match process_stripe_event(event, &state.pool) {
Ok(id)=> { Ok(id)=> {
info!("event processed successfully {:?}", id); info!("event processed successfully {:?}", id);
Ok(HttpResponse::Ok().finish()) Ok(Response::with(status::Ok))
} }
Err(e) => { Err(e) => {
error!("{:?}", 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)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use std::io::prelude::*;
use std::fs::File; use std::fs::File;
#[test] #[test]