From 7b66ddd5c84d5533f805cee84802bafb4416a6a9 Mon Sep 17 00:00:00 2001 From: ntr Date: Sun, 5 Jan 2020 12:55:32 +1000 Subject: [PATCH] fix stripe subscription --- server/src/payments.rs | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/server/src/payments.rs b/server/src/payments.rs index 702aec57..6d1f722e 100644 --- a/server/src/payments.rs +++ b/server/src/payments.rs @@ -20,6 +20,18 @@ use pg::{Db, PgPool}; use account; use account::Account; +fn amount_to_credits(amount: i64) -> i64 { + match amount { + 500 => 50, + 1000 => 110, + 2000 => 250, + 5000 => 660, + _ => amount + .checked_div(CREDITS_COST_CENTS) + .expect("credits cost 0"), + } +} + pub fn subscription_account(tx: &mut Transaction, sub: String) -> Result { let query = " SELECT account, customer, checkout, subscription @@ -164,16 +176,7 @@ impl StripeData { Ok(self) }, StripeData::Purchase { account, customer: _, amount, checkout: _ } => { - let credits = match amount { - 500 => 50, - 1000 => 110, - 2000 => 250, - 5000 => 660, - _ => amount - .checked_div(CREDITS_COST_CENTS) - .expect("credits cost 0"), - }; - + let credits = amount_to_credits(*amount); account::credit(tx, *account, credits)?; Ok(self) @@ -250,6 +253,19 @@ fn process_stripe_event(event: Event, pool: &PgPool) -> Result { _ => false, }; + if subbed { + let amount = match s.plan { + None => { + error!("subscription missing plan {:?}", s); + return Err(err_msg("PlanMissing")); + }, + Some(p) => p.amount.unwrap(), + }; + + account::credit(&mut tx, account, amount_to_credits(amount))?; + account::credit(&mut tx, account, CREDITS_SUB_BONUS)?; + } + account::set_subscribed(&mut tx, account, subbed)?; } _ => {