Skip to content

Commit

Permalink
Adds locks arround the functions that check wether a payment was made…
Browse files Browse the repository at this point in the history
… or not
  • Loading branch information
emmdim committed Nov 12, 2024
1 parent b44692c commit bbe3656
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
5 changes: 4 additions & 1 deletion stripehandler/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ func (s *StripeHandler) RegisterHandlers(api *apirest.API) {
// createCheckoutSession creates a new Stripe Checkout session
func (s *StripeHandler) createCheckoutSession(msg *apirest.APIdata, ctx *httprouter.HTTPContext) error {
to := ctx.URLParam("to")
// referral := ctx.URLParam("referral")
defaultAmount := s.DefaultAmount
if amount := ctx.URLParam("amount"); amount != "" {
var err error
Expand Down Expand Up @@ -89,6 +88,8 @@ func (s *StripeHandler) createCheckoutSession(msg *apirest.APIdata, ctx *httprou
}

func (s *StripeHandler) retrieveCheckoutSession(_ *apirest.APIdata, ctx *httprouter.HTTPContext) error {
s.SessionLock.Lock()
defer s.SessionLock.Unlock()
sessionId := ctx.URLParam("session_id")
status, err := s.RetrieveCheckoutSession(sessionId)
if err != nil {
Expand All @@ -110,6 +111,8 @@ func (s *StripeHandler) retrieveCheckoutSession(_ *apirest.APIdata, ctx *httprou
}

func (s *StripeHandler) handleWebhook(apiData *apirest.APIdata, ctx *httprouter.HTTPContext) error {
s.SessionLock.Lock()
defer s.SessionLock.Unlock()
sig := ctx.Request.Header.Get("Stripe-Signature")
// Pass the request body and Stripe-Signature header to ConstructEvent, along with the webhook signing key
sessionId, err := s.HandleWebhook(apiData, sig)
Expand Down
2 changes: 2 additions & 0 deletions stripehandler/stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"sync"

"github.com/stripe/stripe-go/v81"
"github.com/stripe/stripe-go/v81/checkout/session"
Expand All @@ -24,6 +25,7 @@ type StripeHandler struct {
WebhookSecret string // The secret used to verify Stripe webhook events.
Storage *storage.Storage // The storage instance for the faucet.
Faucet *faucet.Faucet // The faucet instance.
SessionLock sync.RWMutex // The lock for the session.
}

// ReturnStatus represents the response status and data returned by the client.
Expand Down

0 comments on commit bbe3656

Please sign in to comment.