-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
74 lines (68 loc) · 1.96 KB
/
main.go
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
package main
import (
"context"
"encoding/json"
"log"
"net/http"
"github.com/kokizzu/gotro/L"
"github.com/rizalgowandy/coinbase-commerce-go"
"github.com/rizalgowandy/coinbase-commerce-go/pkg/api"
"github.com/rizalgowandy/coinbase-commerce-go/pkg/entity"
"github.com/rizalgowandy/coinbase-commerce-go/pkg/enum"
"github.com/rizalgowandy/coinbase-commerce-go/pkg/stub"
)
func main() {
client, err := coinbase.NewClient(api.Config{
Key: "API_KEY",
Debug: true,
})
if err != nil {
log.Fatal(err)
}
// Set stub to send webhook response
ctx := stub.SetWebhookReq(context.Background(), &entity.WebhookReq{
URL: "http://127.0.0.1:9000/test",
SharedSecretKey: "WEBHOOK_KEY",
Resource: stub.CreateWebhooksResource(),
})
resp, err := client.CreateCharge(ctx, &entity.CreateChargeReq{
Name: "The Sovereign Individual",
Description: "Mastering the Transition to the Information Age",
LocalPrice: entity.CreateChargePrice{
Amount: "100.00",
Currency: "USD",
},
PricingType: enum.PricingTypeFixedPrice,
Metadata: map[string]string{
"customer_id": "id_1005",
"customer_name": "Satoshi Nakamoto",
},
RedirectURL: "https://charge/completed/page",
CancelURL: "https://charge/canceled/page",
})
if err != nil {
log.Fatal(err)
}
L.Describe(resp.Data.ID)
// Create server.
http.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
var req entity.WebhookResource
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
receivedSignature := r.Header.Get("X-CC-Webhook-Signature")
sharedSecretKey := "WEBHOOK_KEY"
err = coinbase.CompareWebhookSignature(r.Context(), &req, receivedSignature, sharedSecretKey)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
// Success.
L.Describe(r.Header)
L.Describe(req)
w.WriteHeader(http.StatusOK)
})
_ = http.ListenAndServe(":9000", nil)
}