Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Prevent stale subscription verification

Neeraj Guptabc9b153ff26b
diff --git a/server/pkg/controller/billing.go b/server/pkg/controller/billing.go
index 062d3b07a9..4dba916b65 100644
--- a/server/pkg/controller/billing.go
+++ b/server/pkg/controller/billing.go
@@ -209,12 +209,9 @@ func (c *BillingController) VerifySubscription(
if err != nil {
return ente.Subscription{}, stacktrace.Propagate(err, "")
}
- newSubscriptionExpiresSooner := newSubscription.ExpiryTime < currentSubscription.ExpiryTime
isUpgradingFromFreePlan := currentSubscription.ProductID == ente.FreePlanProductID
- hasChangedProductID := currentSubscription.ProductID != newSubscription.ProductID
- isOutdatedPurchase := !isUpgradingFromFreePlan && !hasChangedProductID && newSubscriptionExpiresSooner
- if isOutdatedPurchase {
- log.Info("Outdated purchase reported")
+ if shouldSkipVerifiedSubscriptionReplacement(currentSubscription, newSubscription, time.Microseconds()) {
+ log.Info("Outdated or expired purchase reported")
return currentSubscription, nil
}
if newSubscription.Storage < currentSubscription.Storage {
@@ -287,6 +284,15 @@ func (c *BillingController) VerifySubscription(
return newSubscription, nil
}
+func shouldSkipVerifiedSubscriptionReplacement(currentSubscription ente.Subscription, verifiedSubscription ente.Subscription, now int64) bool {
+ effectiveExpiry := verifiedSubscription.ExpiryTime + billing.ProviderToExpiryGracePeriodMap[verifiedSubscription.PaymentProvider]
+ return effectiveExpiry < now ||
+ (currentSubscription.ProductID != ente.FreePlanProductID &&
+ currentSubscription.PaymentProvider == verifiedSubscription.PaymentProvider &&
+ currentSubscription.OriginalTransactionID == verifiedSubscription.OriginalTransactionID &&
+ verifiedSubscription.ExpiryTime < currentSubscription.ExpiryTime)
+}
+
func (c *BillingController) getAllPlans(countryCode string, stripeAccountCountry ente.StripeAccountCountry) []ente.BillingPlan {
if slices.Contains(billing.CountriesInEU, countryCode) {
countryCode = "EU"
diff --git a/server/pkg/controller/billing_test.go b/server/pkg/controller/billing_test.go
new file mode 100644
index 0000000000..73d65ffa51
--- /dev/null
+++ b/server/pkg/controller/billing_test.go
@@ -0,0 +1,64 @@
+package controller
+
+import (
+ "testing"
+
+ "github.com/ente/museum/ente"
+ "github.com/ente/museum/pkg/utils/billing"
+)
+
+func TestShouldSkipVerifiedSubscriptionReplacement(t *testing.T) {
+ now := int64(10_000_000_000_000)
+ appStoreGrace := billing.ProviderToExpiryGracePeriodMap[ente.AppStore]
+ paid := func(provider ente.PaymentProvider, transactionID string, expiry int64) ente.Subscription {
+ return ente.Subscription{
+ ProductID: "paid",
+ PaymentProvider: provider,
+ OriginalTransactionID: transactionID,
+ ExpiryTime: expiry,
+ }
+ }
+
+ tests := []struct {
+ name string
+ current ente.Subscription
+ verified ente.Subscription
+ want bool
+ }{
+ {
+ name: "rejects receipt beyond provider grace",
+ current: paid(ente.Stripe, "stripe", now+15),
+ verified: paid(ente.AppStore, "apple", now-appStoreGrace-1),
+ want: true,
+ },
+ {
+ name: "accepts receipt within provider grace for free account",
+ current: ente.Subscription{ProductID: ente.FreePlanProductID},
+ verified: paid(ente.AppStore, "apple", now-appStoreGrace+1),
+ },
+ {
+ name: "accepts cross-provider switch within provider grace",
+ current: paid(ente.Stripe, "stripe", now+45),
+ verified: paid(ente.AppStore, "apple", now-appStoreGrace+1),
+ },
+ {
+ name: "accepts active Play purchase with a new token",
+ current: paid(ente.PlayStore, "old-token", now+45),
+ verified: paid(ente.PlayStore, "new-token", now+30),
+ },
+ {
+ name: "rejects older version of the same subscription",
+ current: paid(ente.AppStore, "apple", now+45),
+ verified: paid(ente.AppStore, "apple", now+30),
+ want: true,
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ if got := shouldSkipVerifiedSubscriptionReplacement(test.current, test.verified, now); got != test.want {
+ t.Fatalf("shouldSkipVerifiedSubscriptionReplacement() = %t, want %t", got, test.want)
+ }
+ })
+ }
+}