Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
emmdim committed Oct 15, 2024
1 parent 6f5c526 commit 8d058e4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
59 changes: 59 additions & 0 deletions api/subscriptions.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package api

import (
"encoding/json"
"fmt"
"io/ioutil"

Check failure on line 6 in api/subscriptions.go

View workflow job for this annotation

GitHub Actions / lint

SA1019: "io/ioutil" has been deprecated since Go 1.19: As of Go 1.16, the same functionality is now provided by package [io] or package [os], and those implementations should be preferred in new code. See the specific function documentation for details. (staticcheck)

Check failure on line 6 in api/subscriptions.go

View workflow job for this annotation

GitHub Actions / lint

SA1019: "io/ioutil" has been deprecated since Go 1.19: As of Go 1.16, the same functionality is now provided by package [io] or package [os], and those implementations should be preferred in new code. See the specific function documentation for details. (staticcheck)
"net/http"
"os"

"github.com/stripe/stripe-go/v80"
"github.com/stripe/stripe-go/v80/webhook"
"go.vocdoni.io/dvote/log"
)

// getSubscriptionsHandler handles the request to get the subscriptions of an organization.
Expand All @@ -16,3 +24,54 @@ func (a *API) getSubscriptionsHandler(w http.ResponseWriter, r *http.Request) {
// send the subscriptions back to the user
httpWriteJSON(w, subscriptions)
}

// HandleWebhook handles the incoming webhook event from Stripe.
// It takes the API data and signature as input parameters and returns the session ID and an error (if any).
// The request body and Stripe-Signature header are passed to ConstructEvent, along with the webhook signing key.
// If the event type is "checkout.session.completed", it unmarshals the event data into a CheckoutSession struct
// and returns the session ID. Otherwise, it returns an empty string.
func (a *API) HandleWebhook(w http.ResponseWriter, r *http.Request) {

const MaxBodyBytes = int64(65536)
r.Body = http.MaxBytesReader(w, r.Body, MaxBodyBytes)
payload, err := ioutil.ReadAll(r.Body)
if err != nil {

log.Warnw("Stripe Webhook: Error reading request body: %v\n", err)
w.WriteHeader(http.StatusServiceUnavailable)
return
}

event := stripe.Event{}

if err := json.Unmarshal(payload, &event); err != nil {
log.Warnw("Stripe Webhook: error while parsing basic request. %v\n", err.Error())
w.WriteHeader(http.StatusBadRequest)
return
}

endpointSecret := "whsec_QHZlw9fOPECQKGsHeXFTLpu36G4ko75b"
signatureHeader := r.Header.Get("Stripe-Signature")
event, err = webhook.ConstructEvent(payload, signatureHeader, endpointSecret)
if err != nil {
log.Warnw("Stripe Webhook: Webhook signature verification failed. %v\n", err)
w.WriteHeader(http.StatusBadRequest) // Return a 400 error on a bad signature
return
}

// Unmarshal the event data into an appropriate struct depending on its Type
switch event.Type {
case "customer.subscription.created":
var subscription stripe.Subscription
err := json.Unmarshal(event.Data.Raw, &subscription)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing webhook JSON: %v\n", err)
w.WriteHeader(http.StatusBadRequest)
return
}
log.Warnf("Subscription created for %d.", subscription.ID)

Check failure on line 72 in api/subscriptions.go

View workflow job for this annotation

GitHub Actions / lint

printf: go.vocdoni.io/dvote/log.Warnf format %d has arg subscription.ID of wrong type string (govet)

Check failure on line 72 in api/subscriptions.go

View workflow job for this annotation

GitHub Actions / lint

printf: go.vocdoni.io/dvote/log.Warnf format %d has arg subscription.ID of wrong type string (govet)
log.Warnf("Subscription plan for %s.", subscription.Items.Data[0].Plan.ID)
// Then define and call a func to handle the successful attachment of a PaymentMethod.
// handleSubscriptionCreated(subscription)
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ require (
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/stripe/stripe-go/v80 v80.2.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/supranational/blst v0.3.11 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1485,6 +1485,8 @@ github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXl
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stripe/stripe-go/v80 v80.2.0 h1:rCl1PyIAG+gi7tj9prOuWt6XNjKK0BjMoZSvtdiQwUc=
github.com/stripe/stripe-go/v80 v80.2.0/go.mod h1:n7tsDvdltYlzOLGXlseMSJM6ik5uv3guptqtae/VSak=
github.com/stvp/go-udp-testing v0.0.0-20201019212854-469649b16807/go.mod h1:7jxmlfBCDBXRzr0eAQJ48XC1hBu1np4CS5+cHEYfwpc=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
Expand Down Expand Up @@ -1793,6 +1795,7 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
golang.org/x/net v0.0.0-20210520170846-37e1c6afe023/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
Expand Down

0 comments on commit 8d058e4

Please sign in to comment.