Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(NOBIDS) frontend: handle stripe-promo-code for when it does not apply #2942

Merged
merged 3 commits into from
Aug 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions handlers/stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/stripe/stripe-go/v72"
portalsession "github.com/stripe/stripe-go/v72/billingportal/session"
"github.com/stripe/stripe-go/v72/checkout/session"
"github.com/stripe/stripe-go/v72/price"
"github.com/stripe/stripe-go/v72/promotioncode"
"github.com/stripe/stripe-go/v72/webhook"
)
Expand Down Expand Up @@ -162,10 +163,24 @@ func StripeCreateCheckoutSession(w http.ResponseWriter, r *http.Request) {
}

if req.PromotionCode != "" {
it := promotioncode.List(&stripe.PromotionCodeListParams{
stripePrice, err := price.Get(req.Price, nil)
if err != nil || stripePrice == nil || stripePrice.Product == nil {
logger.WithError(err).WithField("stripePrice", stripePrice).Error("error retrieving stripe product for promotion code")
w.WriteHeader(http.StatusInternalServerError)
writeJSON(w, struct {
ErrorData string `json:"error"`
}{
ErrorData: "could not create a new stripe session, please try again later",
})
return
}

pcListParams := &stripe.PromotionCodeListParams{
Code: stripe.String(req.PromotionCode),
Active: stripe.Bool(true),
})
}
pcListParams.AddExpand("data.coupon.applies_to")
it := promotioncode.List(pcListParams)
if it.Err() != nil {
logger.WithError(it.Err()).Error("error retrieving stripe promotion code")
w.WriteHeader(http.StatusInternalServerError)
Expand All @@ -188,7 +203,15 @@ func StripeCreateCheckoutSession(w http.ResponseWriter, r *http.Request) {
if pc != nil && currPc.Created < pc.Created {
continue
}
pc = currPc
if currPc.Coupon == nil || currPc.Coupon.AppliesTo == nil {
continue
}
for _, p := range currPc.Coupon.AppliesTo.Products {
if stripePrice.Product.ID == p {
pc = currPc
break
}
}
}

// if promotion code is not found just ignore it
Expand Down
Loading