-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
272 lines (249 loc) · 9.63 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package coinbase
import (
"context"
"github.com/rizalgowandy/coinbase-commerce-go/pkg/api"
"github.com/rizalgowandy/coinbase-commerce-go/pkg/entity"
"github.com/rizalgowandy/coinbase-commerce-go/pkg/stub"
)
// NewClient creates a client to interact with Coinbase Commerce API.
func NewClient(cfg api.Config) (*Client, error) {
if err := cfg.Validate(); err != nil {
return nil, err
}
return &Client{
charges: api.NewCharges(cfg),
chargesStub: stub.NewCharges(cfg),
checkouts: api.NewCheckouts(cfg),
checkoutsStub: stub.NewCheckouts(),
invoices: api.NewInvoices(cfg),
invoicesStub: stub.NewInvoices(),
events: api.NewEvents(cfg),
eventsStub: stub.NewEvents(),
}, nil
}
// Client is the main client to interact with Coinbase Commerce API.
type Client struct {
charges api.ChargesItf
chargesStub api.ChargesItf
checkouts api.CheckoutsItf
checkoutsStub api.CheckoutsItf
invoices api.InvoicesItf
invoicesStub api.InvoicesItf
events api.EventsItf
eventsStub api.EventsItf
}
// CreateCharge charge a customer with certain amount of currency.
// To get paid in cryptocurrency, you need to create a charge object and
// provide the user with a cryptocurrency address to which they must send cryptocurrency.
// Once a charge is created a customer must broadcast a payment
// to the blockchain before the charge expires.
// Reference: https://commerce.coinbase.com/docs/api/#create-a-charge
func (c *Client) CreateCharge(ctx context.Context, req *entity.CreateChargeReq) (*entity.CreateChargeResp, error) {
if stub.Ok(ctx) {
return c.chargesStub.Create(ctx, req)
}
return c.charges.Create(ctx, req)
}
// ShowCharge retrieves the details of a charge that has been previously created.
// Supply the unique charge code or id that was returned when the charge was created.
// This information is also returned when a charge is first created.
// Reference: https://commerce.coinbase.com/docs/api/#show-a-charge
func (c *Client) ShowCharge(ctx context.Context, req *entity.ShowChargeReq) (*entity.ShowChargeResp, error) {
if err := req.Validate(); err != nil {
return nil, err
}
if stub.Ok(ctx) {
return c.chargesStub.Show(ctx, req)
}
return c.charges.Show(ctx, req)
}
// ListCharges lists all the charges.
// Reference: https://commerce.coinbase.com/docs/api/#list-charges
func (c *Client) ListCharges(ctx context.Context, req *entity.ListChargesReq) (*entity.ListChargesResp, error) {
if stub.Ok(ctx) {
return c.chargesStub.List(ctx, req)
}
return c.charges.List(ctx, req)
}
// CancelCharge cancels a charge that has been previously created.
// Supply the unique charge code or id that was returned when the charge was created.
// This information is also returned when a charge is first created.
//
// Note:
// Only new charges can be successfully canceled.
// Once payment is detected, charge can no longer be canceled.
//
// Reference: https://commerce.coinbase.com/docs/api/#cancel-a-charge
func (c *Client) CancelCharge(ctx context.Context, req *entity.CancelChargeReq) (*entity.CancelChargeResp, error) {
if err := req.Validate(); err != nil {
return nil, err
}
if stub.Ok(ctx) {
return c.chargesStub.Cancel(ctx, req)
}
return c.charges.Cancel(ctx, req)
}
// ResolveCharge resolves a charge that has been previously marked as unresolved.
// Supply the unique charge code or id that was returned when the charge was created.
// This information is also returned when a charge is first created.
//
// Note:
// Only unresolved charges can be successfully resolved
//
// Reference: https://commerce.coinbase.com/docs/api/#resolve-a-charge
func (c *Client) ResolveCharge(ctx context.Context, req *entity.ResolveChargeReq) (*entity.ResolveChargeResp, error) {
if err := req.Validate(); err != nil {
return nil, err
}
if stub.Ok(ctx) {
return c.chargesStub.Resolve(ctx, req)
}
return c.charges.Resolve(ctx, req)
}
// ListCheckouts lists all the checkouts.
// Reference: https://commerce.coinbase.com/docs/api/#list-checkouts
func (c *Client) ListCheckouts(ctx context.Context, req *entity.ListCheckoutsReq) (*entity.ListCheckoutsResp, error) {
if stub.Ok(ctx) {
return c.checkoutsStub.List(ctx, req)
}
return c.checkouts.List(ctx, req)
}
// ShowCheckout show a single checkout.
// Reference: https://commerce.coinbase.com/docs/api/#show-a-checkout
func (c *Client) ShowCheckout(ctx context.Context, req *entity.ShowCheckoutReq) (*entity.ShowCheckoutResp, error) {
if err := req.Validate(); err != nil {
return nil, err
}
if stub.Ok(ctx) {
return c.checkoutsStub.Show(ctx, req)
}
return c.checkouts.Show(ctx, req)
}
// CreateCheckout create a new checkout.
// Reference: https://commerce.coinbase.com/docs/api/#create-a-checkout
func (c *Client) CreateCheckout(ctx context.Context, req *entity.CreateCheckoutReq) (*entity.CreateCheckoutResp, error) {
if stub.Ok(ctx) {
return c.checkoutsStub.Create(ctx, req)
}
return c.checkouts.Create(ctx, req)
}
// UpdateCheckout update a checkout.
// Reference: https://commerce.coinbase.com/docs/api/#update-a-checkout
func (c *Client) UpdateCheckout(ctx context.Context, req *entity.UpdateCheckoutReq) (*entity.UpdateCheckoutResp, error) {
if err := req.Validate(); err != nil {
return nil, err
}
if stub.Ok(ctx) {
return c.checkoutsStub.Update(ctx, req)
}
return c.checkouts.Update(ctx, req)
}
// DeleteCheckout delete a checkout.
// Reference: https://commerce.coinbase.com/docs/api/#delete-a-checkout
func (c *Client) DeleteCheckout(ctx context.Context, req *entity.DeleteCheckoutReq) (*entity.DeleteCheckoutResp, error) {
if err := req.Validate(); err != nil {
return nil, err
}
if stub.Ok(ctx) {
return c.checkoutsStub.Delete(ctx, req)
}
return c.checkouts.Delete(ctx, req)
}
// ListInvoices lists all the invoices.
// Reference: https://commerce.coinbase.com/docs/api/#list-invoices
func (c *Client) ListInvoices(ctx context.Context, req *entity.ListInvoicesReq) (*entity.ListInvoicesResp, error) {
if stub.Ok(ctx) {
return c.invoicesStub.List(ctx, req)
}
return c.invoices.List(ctx, req)
}
// ShowInvoice retrieves the details of an invoice that has been previously created.
// Supply the unique invoice code or id that was returned when the charge was created.
// This information is also returned when an invoice is first created.
// Reference: https://commerce.coinbase.com/docs/api/#show-an-invoice
func (c *Client) ShowInvoice(ctx context.Context, req *entity.ShowInvoiceReq) (*entity.ShowInvoiceResp, error) {
if err := req.Validate(); err != nil {
return nil, err
}
if stub.Ok(ctx) {
return c.invoicesStub.Show(ctx, req)
}
return c.invoices.Show(ctx, req)
}
// CreateInvoice to send an invoice in cryptocurrency,
// you need to create an invoice object and provide the user
// with the hosted url where they will be able to pay.
// Once an invoice is viewed at the hosted url,
// a charge will be generated on the invoice.
// Reference: https://commerce.coinbase.com/docs/api/#create-an-invoice
func (c *Client) CreateInvoice(ctx context.Context, req *entity.CreateInvoiceReq) (*entity.CreateInvoiceResp, error) {
if stub.Ok(ctx) {
return c.invoicesStub.Create(ctx, req)
}
return c.invoices.Create(ctx, req)
}
// VoidInvoice voids an invoice that has been previously created.
// Supply the unique invoice code or id that was returned when the charge was created.
//
// Note:
// Only invoices with OPEN or VIEWED status can be voided.
// Once a payment is detected, the invoice can no longer be voided.
//
// Reference: https://commerce.coinbase.com/docs/api/#void-an-invoice
func (c *Client) VoidInvoice(ctx context.Context, req *entity.VoidInvoiceReq) (*entity.VoidInvoiceResp, error) {
if err := req.Validate(); err != nil {
return nil, err
}
if stub.Ok(ctx) {
return c.invoicesStub.Void(ctx, req)
}
return c.invoices.Void(ctx, req)
}
// ResolveInvoice resolve an invoice that has been previously marked as unresolved.
// Supply the unique invoice code or id that was returned when the charge was created.
//
// Note:
// Only invoices with an unresolved charge can be successfully resolved.
//
// Reference: https://commerce.coinbase.com/docs/api/#resolve-an-invoice
func (c *Client) ResolveInvoice(ctx context.Context, req *entity.ResolveInvoiceReq) (*entity.ResolveInvoiceResp, error) {
if err := req.Validate(); err != nil {
return nil, err
}
if stub.Ok(ctx) {
return c.invoicesStub.Resolve(ctx, req)
}
return c.invoices.Resolve(ctx, req)
}
// ListEvents lists all the events.
// Reference: https://commerce.coinbase.com/docs/api/#list-events
func (c *Client) ListEvents(ctx context.Context, req *entity.ListEventsReq) (*entity.ListEventsResp, error) {
if stub.Ok(ctx) {
return c.eventsStub.List(ctx, req)
}
return c.events.List(ctx, req)
}
// ShowEvent retrieves the details of an event.
// Supply the unique identifier of the event, which you might have received in a webhook.
// Reference: https://commerce.coinbase.com/docs/api/#show-an-event
func (c *Client) ShowEvent(ctx context.Context, req *entity.ShowEventReq) (*entity.ShowEventResp, error) {
if err := req.Validate(); err != nil {
return nil, err
}
if stub.Ok(ctx) {
return c.eventsStub.Show(ctx, req)
}
return c.events.Show(ctx, req)
}
// CompareWebhookSignature used for the handler middleware to verify webhook.
// Mismatch signature will return an error.
//
// Payload:
// req => from request body.
// receivedSignature => from "X-CC-Webhook-Signature" header.
// sharedSecretKey => from webhook setting page secret key.
//
// Reference: https://commerce.coinbase.com/docs/api/#securing-webhooks
func CompareWebhookSignature(ctx context.Context, req *entity.WebhookResource, receivedSignature, sharedSecretKey string) error {
return api.CompareWebhookSignature(ctx, req, receivedSignature, sharedSecretKey)
}