fix stripe subscription

This commit is contained in:
ntr 2020-01-05 12:55:32 +10:00
parent 54cbf20b54
commit 7b66ddd5c8

View File

@ -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<Uuid, Error> {
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<String, Error> {
_ => 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)?;
}
_ => {