Skip to content

Commit

Permalink
Updates Stripe checkout session creation using price pack IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
emmdim committed Sep 10, 2024
1 parent 7cf56ba commit dff74d0
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions stripehandler/stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package stripehandler
import (
"encoding/json"
"errors"
"fmt"

"github.com/stripe/stripe-go/v78"
"github.com/stripe/stripe-go/v78/checkout/session"
"github.com/stripe/stripe-go/v78/price"
"github.com/stripe/stripe-go/v78/webhook"
"github.com/vocdoni/vocfaucet/faucet"
"github.com/vocdoni/vocfaucet/storage"
Expand Down Expand Up @@ -60,17 +62,25 @@ func NewStripeClient(key, priceId, webhookSecret string, minQuantity, maxQuantit
// The function constructs a stripe.CheckoutSessionParams object with the provided parameters and creates a new session using the session.New function.
// If the session creation is successful, it returns the session pointer, otherwise it returns an error.
func (s *StripeHandler) CreateCheckoutSession(defaultAmount int64, to, returnURL, referral string) (*stripe.CheckoutSession, error) {
// search corresponding price tokens package
packName := fmt.Sprintf("pack_%d", defaultAmount)
priceParams := &stripe.PriceListParams{LookupKeys: []*string{&packName}}
priceList := price.List(priceParams).PriceList()
// iterate price result
if len(priceList.Data) == 0 {
return nil, nil
}
params := &stripe.CheckoutSessionParams{
ClientReferenceID: stripe.String(to),
UIMode: stripe.String("embedded"),
Mode: stripe.String(string(stripe.CheckoutSessionModePayment)),
ReturnURL: stripe.String(returnURL + "/{CHECKOUT_SESSION_ID}"),
LineItems: []*stripe.CheckoutSessionLineItemParams{
{
Price: stripe.String(s.PriceId),
AdjustableQuantity: &stripe.CheckoutSessionLineItemAdjustableQuantityParams{
Enabled: stripe.Bool(true),
Minimum: stripe.Int64(s.MinQuantity),
Maximum: stripe.Int64(s.MaxQuantity),
PriceData: &stripe.CheckoutSessionLineItemPriceDataParams{
Product: &priceList.Data[0].Product.ID,
Currency: (*string)(&priceList.Data[0].Currency),
UnitAmountDecimal: &priceList.Data[0].UnitAmountDecimal,
},
Quantity: stripe.Int64(defaultAmount),
},
Expand All @@ -79,7 +89,6 @@ func (s *StripeHandler) CreateCheckoutSession(defaultAmount int64, to, returnURL
"to": to,
"referral": referral,
},
Mode: stripe.String(string(stripe.CheckoutSessionModePayment)),
}
ses, err := session.New(params)
if err != nil {
Expand Down

0 comments on commit dff74d0

Please sign in to comment.