@@ -1178,11 +1178,7 @@ pub async fn stripe_webhook(
1178
1178
price_id,
1179
1179
interval,
1180
1180
created : Utc :: now ( ) ,
1181
- status : if charge_status == ChargeStatus :: Succeeded {
1182
- SubscriptionStatus :: Provisioned
1183
- } else {
1184
- SubscriptionStatus :: Unprovisioned
1185
- } ,
1181
+ status : SubscriptionStatus :: Unprovisioned ,
1186
1182
metadata : None ,
1187
1183
} ;
1188
1184
@@ -1342,16 +1338,18 @@ pub async fn stripe_webhook(
1342
1338
. json :: < PyroServerResponse > ( )
1343
1339
. await ?;
1344
1340
1345
- if let Some ( ref mut subscription) = metadata. user_subscription_item {
1346
- subscription. metadata = Some ( SubscriptionMetadata :: Pyro { id : res. uuid } ) ;
1347
- subscription. upsert ( & mut transaction) . await ?;
1341
+ if let Some ( ref mut subscription) =
1342
+ metadata. user_subscription_item
1343
+ {
1344
+ subscription. metadata =
1345
+ Some ( SubscriptionMetadata :: Pyro { id : res. uuid } ) ;
1348
1346
}
1349
1347
}
1350
1348
}
1351
1349
}
1352
1350
}
1353
1351
1354
- if let Some ( subscription) = metadata. user_subscription_item {
1352
+ if let Some ( mut subscription) = metadata. user_subscription_item {
1355
1353
let open_charge =
1356
1354
ChargeItem :: get_open_subscription ( subscription. id , & mut * transaction)
1357
1355
. await ?;
@@ -1382,7 +1380,11 @@ pub async fn stripe_webhook(
1382
1380
amount : new_price as i64 ,
1383
1381
currency_code : metadata. product_price_item . currency_code ,
1384
1382
status : ChargeStatus :: Open ,
1385
- due : Utc :: now ( ) + subscription. interval . duration ( ) ,
1383
+ due : if subscription. status == SubscriptionStatus :: Unprovisioned {
1384
+ Utc :: now ( ) + subscription. interval . duration ( )
1385
+ } else {
1386
+ metadata. charge_item . due + subscription. interval . duration ( )
1387
+ } ,
1386
1388
last_attempt : None ,
1387
1389
type_ : ChargeType :: Subscription ,
1388
1390
subscription_id : Some ( subscription. id ) ,
@@ -1391,6 +1393,9 @@ pub async fn stripe_webhook(
1391
1393
. upsert ( & mut transaction)
1392
1394
. await ?;
1393
1395
} ;
1396
+
1397
+ subscription. status = SubscriptionStatus :: Provisioned ;
1398
+ subscription. upsert ( & mut transaction) . await ?;
1394
1399
}
1395
1400
1396
1401
transaction. commit ( ) . await ?;
@@ -1538,8 +1543,18 @@ pub async fn subscription_task(pool: PgPool, redis: RedisPool) {
1538
1543
let mut clear_cache_users = Vec :: new ( ) ;
1539
1544
1540
1545
// If an active subscription has a canceled charge OR a failed charge more than two days ago, it should be cancelled
1541
- let all_subscriptions =
1542
- user_subscription_item:: UserSubscriptionItem :: get_all_unprovision ( & pool) . await ?;
1546
+ let all_charges = ChargeItem :: get_unprovision ( & pool) . await ?;
1547
+
1548
+ let mut all_subscriptions = user_subscription_item:: UserSubscriptionItem :: get_many (
1549
+ & all_charges
1550
+ . iter ( )
1551
+ . filter_map ( |x| x. subscription_id )
1552
+ . collect :: < HashSet < _ > > ( )
1553
+ . into_iter ( )
1554
+ . collect :: < Vec < _ > > ( ) ,
1555
+ & pool,
1556
+ )
1557
+ . await ?;
1543
1558
let subscription_prices = product_item:: ProductPriceItem :: get_many (
1544
1559
& all_subscriptions
1545
1560
. iter ( )
@@ -1572,7 +1587,20 @@ pub async fn subscription_task(pool: PgPool, redis: RedisPool) {
1572
1587
)
1573
1588
. await ?;
1574
1589
1575
- for mut subscription in all_subscriptions {
1590
+ for charge in all_charges {
1591
+ let subscription = if let Some ( subscription) = all_subscriptions
1592
+ . iter_mut ( )
1593
+ . find ( |x| Some ( x. id ) == charge. subscription_id )
1594
+ {
1595
+ subscription
1596
+ } else {
1597
+ continue ;
1598
+ } ;
1599
+
1600
+ if subscription. status == SubscriptionStatus :: Unprovisioned {
1601
+ continue ;
1602
+ }
1603
+
1576
1604
let product_price = if let Some ( product_price) = subscription_prices
1577
1605
. iter ( )
1578
1606
. find ( |x| x. id == subscription. price_id )
@@ -1624,7 +1652,11 @@ pub async fn subscription_task(pool: PgPool, redis: RedisPool) {
1624
1652
) )
1625
1653
. header ( "X-Master-Key" , dotenvy:: var ( "PYRO_API_KEY" ) ?)
1626
1654
. json ( & serde_json:: json!( {
1627
- "reason" : "cancelled"
1655
+ "reason" : if charge. status == ChargeStatus :: Cancelled {
1656
+ "cancelled"
1657
+ } else {
1658
+ "paymentfailed"
1659
+ }
1628
1660
} ) )
1629
1661
. send ( )
1630
1662
. await ;
0 commit comments