Skip to content

Commit f7b7000

Browse files
author
Rizal Widyarta Gowandy
committed
Invoices - Add List, Show, Create, Void, Resolve
https://commerce.coinbase.com/docs/api/\#invoices
1 parent 7f0d669 commit f7b7000

14 files changed

+975
-27
lines changed

.golangci.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ issues:
135135
- path: stub
136136
linters:
137137
- funlen
138+
- path: api
139+
linters:
140+
- dupl
138141

139142
# https://github.com/go-critic/go-critic/issues/926
140143
- linters:

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,9 @@ Version: 2018-03-22
140140
- [Show a checkout](https://commerce.coinbase.com/docs/api/#show-a-checkout)
141141
- [Create a checkout](https://commerce.coinbase.com/docs/api/#create-a-checkout)
142142
- [Update a checkout](https://commerce.coinbase.com/docs/api/#update-a-checkout)
143-
- [Delete a checkout](https://commerce.coinbase.com/docs/api/#delete-a-checkout)
143+
- [Delete a checkout](https://commerce.coinbase.com/docs/api/#delete-a-checkout)
144+
- [Invoices](https://commerce.coinbase.com/docs/api/#invoices)
145+
- [Create an invoice](https://commerce.coinbase.com/docs/api/#create-an-invoice)
146+
- [Show an invoice](https://commerce.coinbase.com/docs/api/#show-a-invoice)
147+
- [List invoices](https://commerce.coinbase.com/docs/api/#list-invoices)
148+
- [Cancel an invoice](https://commerce.coinbase.com/docs/api/#cancel-an-invoice)

main.go

+85-10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ func NewClient(cfg api.Config) (*Client, error) {
1919
chargesStub: stub.NewCharges(),
2020
checkouts: api.NewCheckouts(cfg),
2121
checkoutsStub: stub.NewCheckouts(),
22+
invoices: api.NewInvoices(cfg),
23+
invoicesStub: stub.NewInvoices(),
2224
}, nil
2325
}
2426

@@ -28,6 +30,8 @@ type Client struct {
2830
chargesStub api.ChargesItf
2931
checkouts api.CheckoutsItf
3032
checkoutsStub api.CheckoutsItf
33+
invoices api.InvoicesItf
34+
invoicesStub api.InvoicesItf
3135
}
3236

3337
// CreateCharge charge a customer with certain amount of currency.
@@ -36,7 +40,7 @@ type Client struct {
3640
// Once a charge is created a customer must broadcast a payment
3741
// to the blockchain before the charge expires.
3842
// Reference: https://commerce.coinbase.com/docs/api/#create-a-charge
39-
func (c Client) CreateCharge(ctx context.Context, req *entity.CreateChargeReq) (*entity.CreateChargeResp, error) {
43+
func (c *Client) CreateCharge(ctx context.Context, req *entity.CreateChargeReq) (*entity.CreateChargeResp, error) {
4044
if stub.Ok(ctx) {
4145
return c.chargesStub.Create(ctx, req)
4246
}
@@ -47,7 +51,7 @@ func (c Client) CreateCharge(ctx context.Context, req *entity.CreateChargeReq) (
4751
// Supply the unique charge code or id that was returned when the charge was created.
4852
// This information is also returned when a charge is first created.
4953
// Reference: https://commerce.coinbase.com/docs/api/#show-a-charge
50-
func (c Client) ShowCharge(ctx context.Context, req *entity.ShowChargeReq) (*entity.ShowChargeResp, error) {
54+
func (c *Client) ShowCharge(ctx context.Context, req *entity.ShowChargeReq) (*entity.ShowChargeResp, error) {
5155
if err := req.Validate(); err != nil {
5256
return nil, err
5357
}
@@ -59,7 +63,7 @@ func (c Client) ShowCharge(ctx context.Context, req *entity.ShowChargeReq) (*ent
5963

6064
// ListCharges lists all the charges.
6165
// Reference: https://commerce.coinbase.com/docs/api/#list-charges
62-
func (c Client) ListCharges(ctx context.Context, req *entity.ListChargesReq) (*entity.ListChargesResp, error) {
66+
func (c *Client) ListCharges(ctx context.Context, req *entity.ListChargesReq) (*entity.ListChargesResp, error) {
6367
if stub.Ok(ctx) {
6468
return c.chargesStub.List(ctx, req)
6569
}
@@ -75,7 +79,7 @@ func (c Client) ListCharges(ctx context.Context, req *entity.ListChargesReq) (*e
7579
// Once payment is detected, charge can no longer be canceled.
7680
//
7781
// Reference: https://commerce.coinbase.com/docs/api/#cancel-a-charge
78-
func (c Client) CancelCharge(ctx context.Context, req *entity.CancelChargeReq) (*entity.CancelChargeResp, error) {
82+
func (c *Client) CancelCharge(ctx context.Context, req *entity.CancelChargeReq) (*entity.CancelChargeResp, error) {
7983
if err := req.Validate(); err != nil {
8084
return nil, err
8185
}
@@ -93,7 +97,7 @@ func (c Client) CancelCharge(ctx context.Context, req *entity.CancelChargeReq) (
9397
// Only unresolved charges can be successfully resolved
9498
//
9599
// Reference: https://commerce.coinbase.com/docs/api/#resolve-a-charge
96-
func (c Client) ResolveCharge(ctx context.Context, req *entity.ResolveChargeReq) (*entity.ResolveChargeResp, error) {
100+
func (c *Client) ResolveCharge(ctx context.Context, req *entity.ResolveChargeReq) (*entity.ResolveChargeResp, error) {
97101
if err := req.Validate(); err != nil {
98102
return nil, err
99103
}
@@ -105,7 +109,7 @@ func (c Client) ResolveCharge(ctx context.Context, req *entity.ResolveChargeReq)
105109

106110
// ListCheckouts lists all the checkouts.
107111
// Reference: https://commerce.coinbase.com/docs/api/#list-checkouts
108-
func (c Client) ListCheckouts(ctx context.Context, req *entity.ListCheckoutsReq) (*entity.ListCheckoutsResp, error) {
112+
func (c *Client) ListCheckouts(ctx context.Context, req *entity.ListCheckoutsReq) (*entity.ListCheckoutsResp, error) {
109113
if stub.Ok(ctx) {
110114
return c.checkoutsStub.List(ctx, req)
111115
}
@@ -114,7 +118,7 @@ func (c Client) ListCheckouts(ctx context.Context, req *entity.ListCheckoutsReq)
114118

115119
// ShowCheckout show a single checkout.
116120
// Reference: https://commerce.coinbase.com/docs/api/#show-a-checkout
117-
func (c Client) ShowCheckout(ctx context.Context, req *entity.ShowCheckoutReq) (*entity.ShowCheckoutResp, error) {
121+
func (c *Client) ShowCheckout(ctx context.Context, req *entity.ShowCheckoutReq) (*entity.ShowCheckoutResp, error) {
118122
if err := req.Validate(); err != nil {
119123
return nil, err
120124
}
@@ -126,7 +130,7 @@ func (c Client) ShowCheckout(ctx context.Context, req *entity.ShowCheckoutReq) (
126130

127131
// CreateCheckout create a new checkout.
128132
// Reference: https://commerce.coinbase.com/docs/api/#create-a-checkout
129-
func (c Client) CreateCheckout(ctx context.Context, req *entity.CreateCheckoutReq) (*entity.CreateCheckoutResp, error) {
133+
func (c *Client) CreateCheckout(ctx context.Context, req *entity.CreateCheckoutReq) (*entity.CreateCheckoutResp, error) {
130134
if stub.Ok(ctx) {
131135
return c.checkoutsStub.Create(ctx, req)
132136
}
@@ -135,7 +139,7 @@ func (c Client) CreateCheckout(ctx context.Context, req *entity.CreateCheckoutRe
135139

136140
// UpdateCheckout update a checkout.
137141
// Reference: https://commerce.coinbase.com/docs/api/#update-a-checkout
138-
func (c Client) UpdateCheckout(ctx context.Context, req *entity.UpdateCheckoutReq) (*entity.UpdateCheckoutResp, error) {
142+
func (c *Client) UpdateCheckout(ctx context.Context, req *entity.UpdateCheckoutReq) (*entity.UpdateCheckoutResp, error) {
139143
if err := req.Validate(); err != nil {
140144
return nil, err
141145
}
@@ -147,7 +151,7 @@ func (c Client) UpdateCheckout(ctx context.Context, req *entity.UpdateCheckoutRe
147151

148152
// DeleteCheckout delete a checkout.
149153
// Reference: https://commerce.coinbase.com/docs/api/#delete-a-checkout
150-
func (c Client) DeleteCheckout(ctx context.Context, req *entity.DeleteCheckoutReq) (*entity.DeleteCheckoutResp, error) {
154+
func (c *Client) DeleteCheckout(ctx context.Context, req *entity.DeleteCheckoutReq) (*entity.DeleteCheckoutResp, error) {
151155
if err := req.Validate(); err != nil {
152156
return nil, err
153157
}
@@ -156,3 +160,74 @@ func (c Client) DeleteCheckout(ctx context.Context, req *entity.DeleteCheckoutRe
156160
}
157161
return c.checkouts.Delete(ctx, req)
158162
}
163+
164+
// ListInvoices lists all the invoices.
165+
// Reference: https://commerce.coinbase.com/docs/api/#list-invoices
166+
func (c *Client) ListInvoices(ctx context.Context, req *entity.ListInvoicesReq) (*entity.ListInvoicesResp, error) {
167+
if stub.Ok(ctx) {
168+
return c.invoicesStub.List(ctx, req)
169+
}
170+
return c.invoices.List(ctx, req)
171+
}
172+
173+
// ShowInvoice retrieves the details of an invoice that has been previously created.
174+
// Supply the unique invoice code or id that was returned when the charge was created.
175+
// This information is also returned when an invoice is first created.
176+
// Reference: https://commerce.coinbase.com/docs/api/#show-an-invoice
177+
func (c *Client) ShowInvoice(ctx context.Context, req *entity.ShowInvoiceReq) (*entity.ShowInvoiceResp, error) {
178+
if err := req.Validate(); err != nil {
179+
return nil, err
180+
}
181+
if stub.Ok(ctx) {
182+
return c.invoicesStub.Show(ctx, req)
183+
}
184+
return c.invoices.Show(ctx, req)
185+
}
186+
187+
// CreateInvoice to send an invoice in cryptocurrency,
188+
// you need to create an invoice object and provide the user
189+
// with the hosted url where they will be able to pay.
190+
// Once an invoice is viewed at the hosted url,
191+
// a charge will be generated on the invoice.
192+
// Reference: https://commerce.coinbase.com/docs/api/#create-an-invoice
193+
func (c *Client) CreateInvoice(ctx context.Context, req *entity.CreateInvoiceReq) (*entity.CreateInvoiceResp, error) {
194+
if stub.Ok(ctx) {
195+
return c.invoicesStub.Create(ctx, req)
196+
}
197+
return c.invoices.Create(ctx, req)
198+
}
199+
200+
// VoidInvoice voids an invoice that has been previously created.
201+
// Supply the unique invoice code or id that was returned when the charge was created.
202+
//
203+
// Note:
204+
// Only invoices with OPEN or VIEWED status can be voided.
205+
// Once a payment is detected, the invoice can no longer be voided.
206+
//
207+
// Reference: https://commerce.coinbase.com/docs/api/#void-an-invoice
208+
func (c *Client) VoidInvoice(ctx context.Context, req *entity.VoidInvoiceReq) (*entity.VoidInvoiceResp, error) {
209+
if err := req.Validate(); err != nil {
210+
return nil, err
211+
}
212+
if stub.Ok(ctx) {
213+
return c.invoicesStub.Void(ctx, req)
214+
}
215+
return c.invoices.Void(ctx, req)
216+
}
217+
218+
// ResolveInvoice resolve an invoice that has been previously marked as unresolved.
219+
// Supply the unique invoice code or id that was returned when the charge was created.
220+
//
221+
// Note:
222+
// Only invoices with an unresolved charge can be successfully resolved.
223+
//
224+
// Reference: https://commerce.coinbase.com/docs/api/#resolve-an-invoice
225+
func (c *Client) ResolveInvoice(ctx context.Context, req *entity.ResolveInvoiceReq) (*entity.ResolveInvoiceResp, error) {
226+
if err := req.Validate(); err != nil {
227+
return nil, err
228+
}
229+
if stub.Ok(ctx) {
230+
return c.invoicesStub.Resolve(ctx, req)
231+
}
232+
return c.invoices.Resolve(ctx, req)
233+
}

0 commit comments

Comments
 (0)