Skip to content

Commit

Permalink
(NOBIDS) frontend: handle stripe-promo-code for when it does not apply
Browse files Browse the repository at this point in the history
  • Loading branch information
guybrush committed Aug 19, 2024
1 parent 5ded67e commit da3d6f4
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 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,34 @@ 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 {
logger.WithError(err).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
}
if stripePrice == nil || stripePrice.Product == nil {
logger.WithError(fmt.Errorf("product is nil")).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.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 +213,18 @@ func StripeCreateCheckoutSession(w http.ResponseWriter, r *http.Request) {
if pc != nil && currPc.Created < pc.Created {
continue
}
pc = currPc
if currPc.Coupon == nil {
continue
}
if 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

0 comments on commit da3d6f4

Please sign in to comment.