-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
121 lines (97 loc) · 2.51 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/joho/godotenv"
"github.com/stripe/stripe-go/v76"
"github.com/stripe/stripe-go/v76/paymentintent"
"io"
"log"
"net/http"
"os"
)
func init() {
if err := godotenv.Load(); err != nil {
log.Print("No .env file found")
}
}
func main() {
stripeKey, exists := os.LookupEnv("STRIPE_KEY")
if !exists {
log.Print("There is no Stripe key in .env file")
}
stripe.Key = stripeKey
http.HandleFunc("/about", handleAbout)
http.HandleFunc("/create-payment-intent", handleCreatePaymentIntent)
err := http.ListenAndServe(":4242", nil) // run the server
if err != nil {
log.Fatal(err)
}
}
func handleAbout(w http.ResponseWriter, r *http.Request) {
response := []byte("Test shop server working with Stripe API")
_, err := w.Write(response)
if err != nil {
log.Println(err)
}
}
func handleCreatePaymentIntent(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
var request struct {
ProductId string `json:"product_id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Address1 string `json:"address_1"`
Address2 string `json:"address_2"`
City string `json:"city"`
State string `json:"state"`
Zip string `json:"zip"`
Country string `json:"country"`
}
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Stripe api
params := &stripe.PaymentIntentParams{
Amount: stripe.Int64(calculateOrderAmount(request.ProductId)),
Currency: stripe.String(string(stripe.CurrencyUSD)),
AutomaticPaymentMethods: &stripe.PaymentIntentAutomaticPaymentMethodsParams{
Enabled: stripe.Bool(true),
},
}
paymentIntent, err := paymentintent.New(params)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
var response struct {
ClientSecret string `json:"clientSecret"`
}
response.ClientSecret = paymentIntent.ClientSecret
var buf bytes.Buffer
err = json.NewEncoder(&buf).Encode(response)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "application/json")
_, err = io.Copy(w, &buf)
if err != nil {
fmt.Println(err)
}
}
func calculateOrderAmount(productId string) int64 {
switch productId {
case "Forever Pants":
return 26000
case "ForeverShirt":
return 15500
case "Forever Shorts":
return 30000
}
return 0
}