-
Notifications
You must be signed in to change notification settings - Fork 13
/
checkout_sdk.go
213 lines (184 loc) · 5.71 KB
/
checkout_sdk.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package checkout
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/checkout/checkout-sdk-go"
"github.com/checkout/checkout-sdk-go/instruments"
"github.com/checkout/checkout-sdk-go/payments"
"github.com/checkout/checkout-sdk-go/tokens"
)
type Checkout struct {
ctx context.Context
environment string
secretKey string
publicKey *string
webhookSecrets map[string]string
}
func NewCheckout(secretKey string, publicKey *string, environment string, webhookSecrets map[string]string) *Checkout {
return &Checkout{
ctx: context.Background(),
environment: environment,
secretKey: secretKey,
publicKey: publicKey,
webhookSecrets: webhookSecrets,
}
}
func (c *Checkout) RequestPayment(request *payments.Request) *payments.Response {
config, err := checkout.Create(c.secretKey, c.publicKey)
idempotencyKey := checkout.NewIdempotencyKey()
params := checkout.Params{
IdempotencyKey: &idempotencyKey,
}
if err != nil {
panic("failed creating checkout client: " + err.Error())
}
var client = payments.NewClient(*config)
response, err := client.Request(request, ¶ms)
if err != nil {
panic("checkout.com new payment request error: " + err.Error())
}
return response
}
func (c *Checkout) RequestRefunds(amount uint64, paymentID, reference string, metadata map[string]string) *payments.RefundsResponse {
config, err := checkout.Create(c.secretKey, c.publicKey)
if err != nil {
panic("failed creating checkout client: " + err.Error())
}
idempotencyKey := checkout.NewIdempotencyKey()
params := checkout.Params{
IdempotencyKey: &idempotencyKey,
}
var client = payments.NewClient(*config)
request := &payments.RefundsRequest{
Amount: amount,
Reference: reference,
Metadata: metadata,
}
response, err := client.Refunds(paymentID, request, ¶ms)
if err != nil {
panic("checkout.com refund error: " + err.Error())
}
return response
}
func (c *Checkout) CheckWebhookKey(keyCode, key string) bool {
if value, found := c.webhookSecrets[keyCode]; found {
if value == key {
return true
}
}
return false
}
func (c *Checkout) DeleteCustomerInstrument(instrumentID string) bool {
config, err := checkout.Create(c.secretKey, nil)
if err != nil {
panic("failed creating checkout client: " + err.Error())
}
client := &http.Client{}
req, _ := http.NewRequest("DELETE", *config.URI+"/instruments/"+instrumentID, nil)
req.Header.Set("Authorization", c.secretKey)
resp, _ := client.Do(req)
if resp.StatusCode == 404 {
return false
}
if resp.StatusCode != 204 {
panic(fmt.Sprintf("error calling delete instrument api checkout : %s", resp.Body))
}
return true
}
func (c *Checkout) GetCustomer(idOrEmail string) (bool, *CustomerResponse) {
config, err := checkout.Create(c.secretKey, c.publicKey)
if err != nil {
panic("failed creating checkout client: " + err.Error())
}
client := &http.Client{}
req, _ := http.NewRequest("GET", *config.URI+"/customers/"+idOrEmail, nil)
req.Header.Set("Authorization", c.secretKey)
req.Header.Set("Content-Type", "application/json")
resp, _ := client.Do(req)
if resp.StatusCode == 404 {
return false, nil
} else if resp.StatusCode == 200 {
res := &CustomerResponse{}
err := json.NewDecoder(resp.Body).Decode(res)
if err != nil {
panic(err)
}
return true, res
}
data, _ := ioutil.ReadAll(resp.Body)
panic(fmt.Sprintf("wrong status checkout get customer code: %d, body %s", resp.StatusCode, string(data)))
}
func (c *Checkout) SaveGetClient(customerData *SaveCustomerRequest) (created bool, customer *CustomerResponse) {
exists, customerRes := c.GetCustomer(customerData.Email)
if exists {
return false, customerRes
}
config, err := checkout.Create(c.secretKey, c.publicKey)
if err != nil {
panic("failed creating checkout client: " + err.Error())
}
client := &http.Client{}
jsonReq, _ := json.Marshal(customerData)
req, _ := http.NewRequest("POST", *config.URI+"/customers/", bytes.NewBuffer(jsonReq))
req.Header.Set("Authorization", c.secretKey)
req.Header.Set("Content-Type", "application/json")
resp, _ := client.Do(req)
if resp.StatusCode == 201 {
_, customerRes := c.GetCustomer(customerData.Email)
return true, customerRes
}
data, _ := ioutil.ReadAll(resp.Body)
panic(fmt.Sprintf("wrong status checkout create customer code: %d, body %s", resp.StatusCode, string(data)))
}
func (c *Checkout) CreateToken(request *tokens.Request) (string, error) {
config, err := checkout.Create(c.secretKey, c.publicKey)
if err != nil {
panic("failed creating checkout client: " + err.Error())
}
token := tokens.NewClient(*config)
resp, err := token.Request(request)
if err != nil {
return "", err
}
return resp.Created.Token, nil
}
func (c *Checkout) CreateInstrument(request *instruments.Request) (*instruments.Response, error) {
config, err := checkout.Create(c.secretKey, c.publicKey)
if err != nil {
panic("failed creating checkout client: " + err.Error())
}
client := instruments.NewClient(*config)
res, err := client.Create(request)
if err != nil {
return nil, err
}
return res, nil
}
func (c *Checkout) GetInstrument(sourceID string) (*instruments.Response, error) {
config, err := checkout.Create(c.secretKey, c.publicKey)
if err != nil {
panic("failed creating checkout client: " + err.Error())
}
client := instruments.NewClient(*config)
res, err := client.Get(sourceID)
if err != nil {
return nil, err
}
return res, nil
}
func (c *Checkout) GetPaymentDetail(paymentID string) (*payments.PaymentResponse, error) {
config, err := checkout.Create(c.secretKey, c.publicKey)
if err != nil {
return nil, err
}
var client = payments.NewClient(*config)
response, err := client.Get(paymentID)
if err != nil {
return nil, err
}
return response, nil
}