Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

Billing tweaks for stripe.

Matt Roberts51b2b8f86819
diff --git a/app/services/sqlstore/postgres/billing.go b/app/services/sqlstore/postgres/billing.go
index 999ad1a99..583a855d7 100644
--- a/app/services/sqlstore/postgres/billing.go
+++ b/app/services/sqlstore/postgres/billing.go
@@ -2,14 +2,17 @@ package postgres
import (
"context"
+ "database/sql"
"github.com/getfider/fider/app"
"github.com/getfider/fider/app/models/cmd"
+ "github.com/getfider/fider/app/models/dto"
"github.com/getfider/fider/app/models/entity"
"github.com/getfider/fider/app/models/enum"
"github.com/getfider/fider/app/models/query"
"github.com/getfider/fider/app/pkg/dbx"
"github.com/getfider/fider/app/pkg/errors"
+ "github.com/getfider/fider/app/pkg/log"
"github.com/getfider/fider/app/services/sqlstore/dbEntities"
)
@@ -72,7 +75,23 @@ func getStripeBillingState(ctx context.Context, q *query.GetStripeBillingState)
func activateStripeSubscription(ctx context.Context, c *cmd.ActivateStripeSubscription) error {
return using(ctx, func(trx *dbx.Trx, _ *entity.Tenant, _ *entity.User) error {
- _, err := trx.Execute(`
+ // Check for existing Paddle subscription before migrating to Stripe
+ var paddleSubscriptionID sql.NullString
+ err := trx.Scalar(&paddleSubscriptionID, `
+ SELECT paddle_subscription_id FROM tenants_billing WHERE tenant_id = $1
+ `, c.TenantID)
+ if err != nil && errors.Cause(err) != app.ErrNotFound {
+ return errors.Wrap(err, "failed to check existing paddle subscription")
+ }
+ if paddleSubscriptionID.Valid && paddleSubscriptionID.String != "" {
+ log.Warnf(ctx, "PADDLE_TO_STRIPE_MIGRATION: Tenant @{TenantID} switching from Paddle (subscription: @{PaddleSubscriptionID}) to Stripe (subscription: @{StripeSubscriptionID})", dto.Props{
+ "TenantID": c.TenantID,
+ "PaddleSubscriptionID": paddleSubscriptionID.String,
+ "StripeSubscriptionID": c.SubscriptionID,
+ })
+ }
+
+ _, err = trx.Execute(`
INSERT INTO tenants_billing (tenant_id, stripe_customer_id, stripe_subscription_id, license_key)
VALUES ($1, $2, $3, $4)
ON CONFLICT (tenant_id) DO UPDATE
@@ -84,9 +103,9 @@ func activateStripeSubscription(ctx context.Context, c *cmd.ActivateStripeSubscr
_, err = trx.Execute(`
UPDATE tenants
- SET is_pro = true
+ SET is_pro = true, status = $2
WHERE id = $1
- `, c.TenantID)
+ `, c.TenantID, enum.TenantActive)
if err != nil {
return errors.Wrap(err, "failed to set tenant to pro plan")
}