stripe
This commit is contained in:
parent
6419fe55a0
commit
d27786fa5a
@ -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
|
||||
});
|
||||
}
|
||||
|
||||
@ -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)))?;
|
||||
|
||||
@ -38,7 +38,7 @@ mod mob;
|
||||
mod mtx;
|
||||
mod names;
|
||||
mod net;
|
||||
// mod payments;
|
||||
mod payments;
|
||||
mod pg;
|
||||
mod player;
|
||||
mod pubsub;
|
||||
|
||||
@ -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));
|
||||
|
||||
@ -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]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user