fix stripe subscription
This commit is contained in:
parent
54cbf20b54
commit
7b66ddd5c8
@ -20,6 +20,18 @@ use pg::{Db, PgPool};
|
|||||||
use account;
|
use account;
|
||||||
use account::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> {
|
pub fn subscription_account(tx: &mut Transaction, sub: String) -> Result<Uuid, Error> {
|
||||||
let query = "
|
let query = "
|
||||||
SELECT account, customer, checkout, subscription
|
SELECT account, customer, checkout, subscription
|
||||||
@ -164,16 +176,7 @@ impl StripeData {
|
|||||||
Ok(self)
|
Ok(self)
|
||||||
},
|
},
|
||||||
StripeData::Purchase { account, customer: _, amount, checkout: _ } => {
|
StripeData::Purchase { account, customer: _, amount, checkout: _ } => {
|
||||||
let credits = match amount {
|
let credits = amount_to_credits(*amount);
|
||||||
500 => 50,
|
|
||||||
1000 => 110,
|
|
||||||
2000 => 250,
|
|
||||||
5000 => 660,
|
|
||||||
_ => amount
|
|
||||||
.checked_div(CREDITS_COST_CENTS)
|
|
||||||
.expect("credits cost 0"),
|
|
||||||
};
|
|
||||||
|
|
||||||
account::credit(tx, *account, credits)?;
|
account::credit(tx, *account, credits)?;
|
||||||
|
|
||||||
Ok(self)
|
Ok(self)
|
||||||
@ -250,6 +253,19 @@ fn process_stripe_event(event: Event, pool: &PgPool) -> Result<String, Error> {
|
|||||||
_ => false,
|
_ => 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)?;
|
account::set_subscribed(&mut tx, account, subbed)?;
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user