Add Stripe billing, free trials, and cross-platform subscription guards
- Stripe integration: add StripeService with checkout sessions, customer portal, and webhook handling for subscription lifecycle events. - Free trials: auto-start configurable trial on first subscription check, with admin-controllable duration and enable/disable toggle. - Cross-platform guard: prevent duplicate subscriptions across iOS, Android, and Stripe by checking existing platform before allowing purchase. - Subscription model: add Stripe fields (customer_id, subscription_id, price_id), trial fields (trial_start, trial_end, trial_used), and SubscriptionSource/IsTrialActive helpers. - API: add trial and source fields to status response, update OpenAPI spec. - Clean up stale migration and audit docs. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -262,3 +262,59 @@ func (r *SubscriptionRepository) GetPromotionByID(promotionID string) (*models.P
|
||||
}
|
||||
return &promotion, nil
|
||||
}
|
||||
|
||||
// === Stripe Lookups ===
|
||||
|
||||
// FindByStripeCustomerID finds a subscription by Stripe customer ID
|
||||
func (r *SubscriptionRepository) FindByStripeCustomerID(customerID string) (*models.UserSubscription, error) {
|
||||
var sub models.UserSubscription
|
||||
err := r.db.Where("stripe_customer_id = ?", customerID).First(&sub).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &sub, nil
|
||||
}
|
||||
|
||||
// FindByStripeSubscriptionID finds a subscription by Stripe subscription ID
|
||||
func (r *SubscriptionRepository) FindByStripeSubscriptionID(subscriptionID string) (*models.UserSubscription, error) {
|
||||
var sub models.UserSubscription
|
||||
err := r.db.Where("stripe_subscription_id = ?", subscriptionID).First(&sub).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &sub, nil
|
||||
}
|
||||
|
||||
// UpdateStripeData updates all three Stripe fields (customer, subscription, price) in one call
|
||||
func (r *SubscriptionRepository) UpdateStripeData(userID uint, customerID, subscriptionID, priceID string) error {
|
||||
return r.db.Model(&models.UserSubscription{}).Where("user_id = ?", userID).Updates(map[string]interface{}{
|
||||
"stripe_customer_id": customerID,
|
||||
"stripe_subscription_id": subscriptionID,
|
||||
"stripe_price_id": priceID,
|
||||
}).Error
|
||||
}
|
||||
|
||||
// ClearStripeData clears the Stripe subscription and price IDs (customer ID stays for portal access)
|
||||
func (r *SubscriptionRepository) ClearStripeData(userID uint) error {
|
||||
return r.db.Model(&models.UserSubscription{}).Where("user_id = ?", userID).Updates(map[string]interface{}{
|
||||
"stripe_subscription_id": nil,
|
||||
"stripe_price_id": nil,
|
||||
}).Error
|
||||
}
|
||||
|
||||
// === Trial Management ===
|
||||
|
||||
// SetTrialDates sets the trial start, end, and marks trial as used
|
||||
func (r *SubscriptionRepository) SetTrialDates(userID uint, trialStart, trialEnd time.Time) error {
|
||||
return r.db.Model(&models.UserSubscription{}).Where("user_id = ?", userID).Updates(map[string]interface{}{
|
||||
"trial_start": trialStart,
|
||||
"trial_end": trialEnd,
|
||||
"trial_used": true,
|
||||
}).Error
|
||||
}
|
||||
|
||||
// UpdateExpiresAt updates the expires_at field for a user's subscription
|
||||
func (r *SubscriptionRepository) UpdateExpiresAt(userID uint, expiresAt time.Time) error {
|
||||
return r.db.Model(&models.UserSubscription{}).Where("user_id = ?", userID).
|
||||
Update("expires_at", expiresAt).Error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user