-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.go
More file actions
101 lines (86 loc) · 2.8 KB
/
functions.go
File metadata and controls
101 lines (86 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package p
import (
"context"
"fmt"
"io"
"log"
"net/http"
"strings"
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
"github.com/cloudevents/sdk-go/v2/event"
"github.com/odch/flightbox/functions-go/stripe-terminal/test"
"github.com/stripe/stripe-go/v74/webhook"
)
var config *test.Config
func init() {
var err error
config, err = test.LoadConfig()
if err != nil {
panic(err)
}
functions.HTTP("StripeWebhook", StripeWebhook)
functions.CloudEvent("CardPaymentsStripe", cardPaymentsStripe)
}
func StripeWebhook(w http.ResponseWriter, req *http.Request) {
// Protects against a malicious client streaming us an endless request
// body
const MaxBodyBytes = int64(65536)
req.Body = http.MaxBytesReader(w, req.Body, MaxBodyBytes)
body, err := io.ReadAll(req.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
// Pass the request body & Stripe-Signature header to ConstructEvent, along with the webhook signing key
event, err := webhook.ConstructEvent(body, req.Header.Get("Stripe-Signature"), config.WebHookSecret)
if err != nil {
w.WriteHeader(http.StatusBadRequest) // Return a 400 error on a bad signature
fmt.Fprintf(w, "%v", err)
return
}
ctx := context.Background()
fmt.Fprintf(w, "Received signed event: %v", event)
id := event.GetObjectValue("metadata", "external_id")
if id != "" {
if event.Type == "payment_intent.succeeded" {
test.UpdateStatus(config, ctx, id, "success")
} else if event.Type == "payment_intent.canceled" ||
event.Type == "payment_intent.payment_failed" {
test.UpdateStatus(config, ctx, id, "failure")
}
}
}
// RTDBEvent is the payload of a RTDB event.
type RTDBEvent struct {
Data interface{} `json:"data"`
Delta struct {
Amount int64 `json:"amount"` // cents
ArrivalReference string `json:"arrivalReference"`
RefNr string `json:"refNr"`
Currency string `json:"currency"`
Email string `json:"email"`
Registration string `json:"immatriculation"`
Method string `json:"method"`
} `json:"delta"`
}
func cardPaymentsStripe(ctx context.Context, e event.Event) error {
var rtdbEvent RTDBEvent
if err := e.DataAs(&rtdbEvent); err != nil {
return fmt.Errorf("event.DataAs: %w", err)
}
subject := e.Subject()
log.Printf("Function triggered by change to: %v", subject)
idx := strings.Split(subject, "/")
id := idx[len(idx)-1]
log.Printf("%+v", rtdbEvent)
var err error
if rtdbEvent.Delta.Method == "card" {
err = test.TerminalPayment(config, id, rtdbEvent.Delta.Amount, &rtdbEvent.Delta.Email, rtdbEvent.Delta.Registration)
} else {
err = test.CheckoutPayment(config, id, rtdbEvent.Delta.Amount, rtdbEvent.Delta.Email, rtdbEvent.Delta.Registration, rtdbEvent.Delta.ArrivalReference, rtdbEvent.Delta.RefNr)
}
if err != nil {
log.Println(err)
}
return nil
}