Skip to content

Commit

Permalink
updates checkout session to include billing cycle anchor
Browse files Browse the repository at this point in the history
  • Loading branch information
nickzelei committed Sep 21, 2024
1 parent 9e9dcea commit a99f621
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions internal/billing/stripe-billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package billing
import (
"fmt"
"log/slog"
"time"

"github.com/stripe/stripe-go/v79"
stripeapiclient "github.com/stripe/stripe-go/v79/client"
Expand Down Expand Up @@ -101,9 +102,22 @@ func (c *Client) NewCheckoutSession(customerId, accountSlug, userId string, logg
CancelURL: stripe.String(fmt.Sprintf("%s/%s/settings/billing", c.cfg.AppBaseUrl, accountSlug)),
Customer: stripe.String(customerId),
Metadata: map[string]string{"userId": userId},
SubscriptionData: &stripe.CheckoutSessionSubscriptionDataParams{
BillingCycleAnchor: stripe.Int64(getNextMonthBillingCycleAnchor()),
},
})

Check warning on line 108 in internal/billing/stripe-billing.go

View check run for this annotation

Codecov / codecov/patch

internal/billing/stripe-billing.go#L97-L108

Added lines #L97 - L108 were not covered by tests
}

func getNextMonthBillingCycleAnchor() int64 {
now := time.Now().UTC()

Check warning on line 112 in internal/billing/stripe-billing.go

View check run for this annotation

Codecov / codecov/patch

internal/billing/stripe-billing.go#L111-L112

Added lines #L111 - L112 were not covered by tests

// Calculate the first day of the next month
nextMonth := now.AddDate(0, 1, 0)
firstOfNextMonth := time.Date(nextMonth.Year(), nextMonth.Month(), 1, 0, 0, 0, 0, time.UTC)

Check warning on line 116 in internal/billing/stripe-billing.go

View check run for this annotation

Codecov / codecov/patch

internal/billing/stripe-billing.go#L115-L116

Added lines #L115 - L116 were not covered by tests

return firstOfNextMonth.Unix()

Check warning on line 118 in internal/billing/stripe-billing.go

View check run for this annotation

Codecov / codecov/patch

internal/billing/stripe-billing.go#L118

Added line #L118 was not covered by tests
}

func (c *Client) getPricesFromLookupKeys() (map[string]*stripe.Price, error) {
output := map[string]*stripe.Price{}
pricelistParams := &stripe.PriceListParams{
Expand Down

0 comments on commit a99f621

Please sign in to comment.