You've already forked AstralRinth
forked from didirus/AstralRinth
Fix billing setup
This commit is contained in:
@@ -206,8 +206,8 @@ impl ChargeItem {
|
||||
Ok(res.and_then(|r| r.try_into().ok()))
|
||||
}
|
||||
|
||||
pub async fn get_chargeable(
|
||||
exec: impl sqlx::Executor<'_, Database = sqlx::Postgres>,
|
||||
pub async fn get_chargeable_lock(
|
||||
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
||||
) -> Result<Vec<ChargeItem>, DatabaseError> {
|
||||
let charge_type = ChargeType::Subscription.as_str();
|
||||
let res = select_charges_with_predicate!(
|
||||
@@ -218,10 +218,11 @@ impl ChargeItem {
|
||||
(status = 'open' AND due < NOW()) OR
|
||||
(status = 'failed' AND last_attempt < NOW() - INTERVAL '2 days')
|
||||
)
|
||||
FOR UPDATE SKIP LOCKED
|
||||
"#,
|
||||
charge_type
|
||||
)
|
||||
.fetch_all(exec)
|
||||
.fetch_all(&mut **transaction)
|
||||
.await?;
|
||||
|
||||
Ok(res
|
||||
|
||||
@@ -2271,9 +2271,11 @@ pub async fn index_billing(
|
||||
info!("Indexing billing queue");
|
||||
let res = async {
|
||||
// If a charge is open and due or has been attempted more than two days ago, it should be processed
|
||||
let mut transaction = pool.begin().await?;
|
||||
|
||||
let charges_to_do =
|
||||
crate::database::models::charge_item::ChargeItem::get_chargeable(
|
||||
&pool,
|
||||
crate::database::models::charge_item::ChargeItem::get_chargeable_lock(
|
||||
&mut transaction,
|
||||
)
|
||||
.await?;
|
||||
|
||||
@@ -2396,8 +2398,11 @@ pub async fn index_billing(
|
||||
|
||||
charge.status = ChargeStatus::Processing;
|
||||
|
||||
stripe::PaymentIntent::create(&stripe_client, intent)
|
||||
.await?;
|
||||
if let Err(e) = stripe::PaymentIntent::create(&stripe_client, intent).await {
|
||||
tracing::error!("Failed to create payment intent: {:?}", e);
|
||||
charge.status = ChargeStatus::Failed;
|
||||
charge.last_attempt = Some(Utc::now());
|
||||
}
|
||||
} else {
|
||||
charge.status = ChargeStatus::Failed;
|
||||
charge.last_attempt = Some(Utc::now());
|
||||
|
||||
Reference in New Issue
Block a user