@@ -2,71 +2,55 @@ package staticbackend
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
5
6
"fmt"
6
- "log "
7
+ "io "
7
8
"net/http"
9
+ "os"
8
10
"staticbackend/internal"
9
11
12
+ "github.com/stripe/stripe-go/v71"
10
13
"go.mongodb.org/mongo-driver/bson"
11
14
)
12
15
13
16
type stripeWebhook struct {}
14
17
15
- type CustomerSourceCreated struct {
16
- Data struct {
17
- CustomerID string `json:"customer"`
18
- } `json:"data"`
19
- }
20
-
21
18
func (wh * stripeWebhook ) process (w http.ResponseWriter , r * http.Request ) {
22
- fmt .Println ("inside stripe webhook" )
23
- var evt map [string ]interface {}
24
- if err := parseBody (r .Body , & evt ); err != nil {
25
- log .Println (err )
26
- http .Error (w , err .Error (), http .StatusBadRequest )
27
- return
28
- }
29
-
30
- typ , ok := evt ["type" ]
31
- if ! ok {
32
- log .Println ("no type specified" )
33
- http .Error (w , "no type specified in the event data" , http .StatusBadRequest )
19
+ const MaxBodyBytes = int64 (65536 )
20
+ r .Body = http .MaxBytesReader (w , r .Body , MaxBodyBytes )
21
+ payload , err := io .ReadAll (r .Body )
22
+ if err != nil {
23
+ fmt .Fprintf (os .Stderr , "Error reading request body: %v\n " , err )
24
+ w .WriteHeader (http .StatusServiceUnavailable )
34
25
return
35
26
}
36
27
37
- fmt . Println ( " event type" , typ )
28
+ event := stripe. Event {}
38
29
39
- var err error
40
-
41
- switch typ {
42
- case "customer.source.created" :
43
- err = wh .sourceCreated (evt ["data" ])
44
- }
45
-
46
- if err != nil {
47
- log .Println (err )
48
- http .Error (w , err .Error (), http .StatusInternalServerError )
30
+ if err := json .Unmarshal (payload , & event ); err != nil {
31
+ fmt .Fprintf (os .Stderr , "Failed to parse webhook body json: %v\n " , err .Error ())
32
+ w .WriteHeader (http .StatusBadRequest )
49
33
return
50
34
}
51
35
52
- respond (w , http .StatusOK , true )
53
- }
54
-
55
- func (wh * stripeWebhook ) sourceCreated (params interface {}) error {
56
- data , ok := params .(map [string ]interface {})
57
- if ! ok {
58
- return fmt .Errorf ("unable to cast params: %v into a map[string]interface{}" , params )
36
+ // Unmarshal the event data into an appropriate struct depending on its Type
37
+ switch event .Type {
38
+ case "payment_method.attached" :
39
+ var paymentMethod stripe.PaymentMethod
40
+ err := json .Unmarshal (event .Data .Raw , & paymentMethod )
41
+ if err != nil {
42
+ fmt .Fprintf (os .Stderr , "Error parsing webhook JSON: %v\n " , err )
43
+ w .WriteHeader (http .StatusBadRequest )
44
+ return
45
+ }
46
+ wh .handlePaymentMethodAttached (paymentMethod )
59
47
}
60
48
61
- obj , ok := data ["object" ].(map [string ]interface {})
62
- if ! ok {
63
- return fmt .Errorf ("unable to cast data[object] into a map[string]interface{}" )
64
- }
49
+ w .WriteHeader (http .StatusOK )
50
+ }
65
51
66
- stripeID , ok := obj ["customer" ].(string )
67
- if ! ok {
68
- return fmt .Errorf ("unable to convert %v to string" , data ["customer" ])
69
- }
52
+ func (wh * stripeWebhook ) handlePaymentMethodAttached (pm stripe.PaymentMethod ) error {
53
+ stripeID := pm .Customer .ID
70
54
71
55
db := client .Database ("sbsys" )
72
56
ctx := context .Background ()
0 commit comments