forked from processout/applepay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
96 lines (85 loc) · 2.03 KB
/
app.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/davecgh/go-spew/spew"
"github.com/gin-gonic/gin"
"github.com/processout/applepay"
)
var (
ap *applepay.Merchant
)
func init() {
var err error
applepay.AppleRootCertificatePath = "AppleRootCA-G3.crt"
ap, err = applepay.New(
"merchant.com.processout.test",
applepay.MerchantDisplayName("ProcessOut Development Store"),
applepay.MerchantDomainName("applepay.processout.com"),
applepay.MerchantCertificateLocation(
"certs/cert-merchant.crt",
"certs/cert-merchant-key.pem",
),
applepay.ProcessingCertificateLocation(
"certs/cert-processing.crt",
"certs/cert-processing-key.pem",
),
)
if err != nil {
panic(err)
}
log.Println("Apple Pay test app starting")
}
func main() {
r := gin.Default()
r.StaticFile("/", "./static/index.html")
r.Static("/.well-known", "./static/.well-known")
r.Static("/public", "./static")
r.POST("/getApplePaySession", getApplePaySession)
r.POST("/processApplePayResponse", processApplePayResponse)
port := "8000"
if envPort := os.Getenv("PORT"); envPort != "" {
port = envPort
}
r.Run("localhost:" + port)
}
func getApplePaySession(c *gin.Context) {
r := &struct{ URL string }{}
if err := c.BindJSON(r); err != nil {
log.Println(err)
c.Status(http.StatusBadRequest)
return
}
payload, err := ap.Session(r.URL)
if err != nil {
log.Println(err)
c.Status(http.StatusInternalServerError)
return
}
c.Status(http.StatusOK)
c.Header("Content-Type", "application/json")
c.Writer.Write(payload)
}
func processApplePayResponse(c *gin.Context) {
r := &applepay.Response{}
if err := c.BindJSON(r); err != nil {
log.Println(err)
c.Status(http.StatusBadRequest)
return
}
// Optional: select merchant credentials to use based on the hash of the
// public key:
// h, err := r.Token.PublicKeyHash()
token, err := ap.DecryptResponse(r)
if err != nil {
log.Println(err)
c.Status(http.StatusBadRequest)
return
}
fmt.Println("Token received!")
spew.Dump(token)
// TODO: check price…
c.Status(http.StatusOK)
}