Skip to content

Commit

Permalink
Fix subscriptions edge case (#952)
Browse files Browse the repository at this point in the history
* Fix subscriptions edge case

* prep
  • Loading branch information
Geometrically authored Aug 15, 2024
1 parent 1d0d8d7 commit 7dd340f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/database/models/user_subscription_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ impl UserSubscriptionItem {
SET interval = EXCLUDED.interval,
expires = EXCLUDED.expires,
last_charge = EXCLUDED.last_charge,
status = EXCLUDED.status
status = EXCLUDED.status,
price_id = EXCLUDED.price_id
",
self.id.0,
self.user_id.0,
Expand Down
18 changes: 18 additions & 0 deletions src/routes/internal/billing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,20 @@ pub async fn initiate_payment(
..Default::default()
};

let mut metadata = HashMap::new();
metadata.insert("modrinth_user_id".to_string(), to_base62(user.id.0));
metadata.insert(
"modrinth_price_id".to_string(),
to_base62(price_item.id.0 as u64),
);
if let Some(interval) = payment_request.interval {
metadata.insert(
"modrinth_subscription_interval".to_string(),
interval.as_str().to_string(),
);
}
update_payment_intent.metadata = Some(metadata);

if let PaymentRequestType::PaymentMethod { .. } = payment_request.type_ {
update_payment_intent.payment_method = Some(payment_method.id.clone());
}
Expand Down Expand Up @@ -897,6 +911,7 @@ pub async fn stripe_webhook(
user_subscription.expires += duration;
user_subscription.status = SubscriptionStatus::Active;
user_subscription.interval = interval;
user_subscription.price_id = metadata.product_price.id;
user_subscription.upsert(&mut transaction).await?;
} else {
user_subscription_item::UserSubscriptionItem {
Expand Down Expand Up @@ -952,6 +967,7 @@ pub async fn stripe_webhook(
if let Some(mut user_subscription) = metadata.user_subscription {
user_subscription.status = SubscriptionStatus::PaymentProcessing;
user_subscription.interval = interval;
user_subscription.price_id = metadata.product_price.id;
user_subscription.upsert(&mut transaction).await?;
} else {
user_subscription_item::UserSubscriptionItem {
Expand Down Expand Up @@ -988,6 +1004,8 @@ pub async fn stripe_webhook(
if let Some(mut user_subscription) = metadata.user_subscription {
user_subscription.last_charge = Some(Utc::now());
user_subscription.status = SubscriptionStatus::PaymentFailed;
user_subscription.price_id = metadata.product_price.id;
user_subscription.interval = interval;
user_subscription.upsert(&mut transaction).await?;
} else {
user_subscription_item::UserSubscriptionItem {
Expand Down

0 comments on commit 7dd340f

Please sign in to comment.