Skip to content

Commit 7f0d669

Browse files
authored
Merge pull request #8 from benalucorp/arwego/feat/add_checkouts
Add Show Checkout
2 parents 8397621 + 79a7560 commit 7f0d669

File tree

6 files changed

+144
-0
lines changed

6 files changed

+144
-0
lines changed

main.go

+12
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,18 @@ func (c Client) ListCheckouts(ctx context.Context, req *entity.ListCheckoutsReq)
112112
return c.checkouts.List(ctx, req)
113113
}
114114

115+
// ShowCheckout show a single checkout.
116+
// Reference: https://commerce.coinbase.com/docs/api/#show-a-checkout
117+
func (c Client) ShowCheckout(ctx context.Context, req *entity.ShowCheckoutReq) (*entity.ShowCheckoutResp, error) {
118+
if err := req.Validate(); err != nil {
119+
return nil, err
120+
}
121+
if stub.Ok(ctx) {
122+
return c.checkoutsStub.Show(ctx, req)
123+
}
124+
return c.checkouts.Show(ctx, req)
125+
}
126+
115127
// CreateCheckout create a new checkout.
116128
// Reference: https://commerce.coinbase.com/docs/api/#create-a-checkout
117129
func (c Client) CreateCheckout(ctx context.Context, req *entity.CreateCheckoutReq) (*entity.CreateCheckoutResp, error) {

main_integration_test.go

+70
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,76 @@ func TestClient_ListCheckouts_Integration(t *testing.T) {
561561
}
562562
}
563563

564+
func TestClient_ShowCheckout_Integration(t *testing.T) {
565+
if !integration {
566+
return
567+
}
568+
569+
type args struct {
570+
ctx context.Context
571+
req *entity.ShowCheckoutReq
572+
}
573+
tests := []struct {
574+
name string
575+
args args
576+
wantErr bool
577+
}{
578+
{
579+
name: "Missing identifier",
580+
args: args{
581+
ctx: context.Background(),
582+
req: &entity.ShowCheckoutReq{
583+
CheckoutID: "",
584+
},
585+
},
586+
wantErr: true,
587+
},
588+
{
589+
name: "Show using checkout id",
590+
args: args{
591+
ctx: context.Background(),
592+
req: &entity.ShowCheckoutReq{
593+
CheckoutID: func() string {
594+
c := client
595+
got, err := c.checkouts.Create(context.Background(), &entity.CreateCheckoutReq{
596+
Name: "The Sovereign Individual",
597+
Description: "Mastering the Transition to the Information Age",
598+
LocalPrice: entity.CreateCheckoutPrice{
599+
Amount: "100.00",
600+
Currency: "USD",
601+
},
602+
PricingType: enum.PricingTypeFixedPrice,
603+
RequestedInfo: []enum.CheckoutRequestedInfo{
604+
enum.CheckoutRequestedInfoName,
605+
enum.CheckoutRequestedInfoEmail,
606+
},
607+
})
608+
if err != nil {
609+
return ""
610+
}
611+
return got.Data.ID
612+
}(),
613+
},
614+
},
615+
wantErr: false,
616+
},
617+
}
618+
for _, tt := range tests {
619+
t.Run(tt.name, func(t *testing.T) {
620+
c := client
621+
got, err := c.ShowCheckout(tt.args.ctx, tt.args.req)
622+
if (err != nil) != tt.wantErr {
623+
t.Errorf("ShowCheckout() error = %v, wantErr %v", err, tt.wantErr)
624+
return
625+
}
626+
L.Describe(got, err)
627+
if !tt.wantErr {
628+
assert.NotNil(t, got)
629+
}
630+
})
631+
}
632+
}
633+
564634
func TestClient_CreateCheckout_Integration(t *testing.T) {
565635
if !integration {
566636
return

pkg/api/api.go

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type ChargesItf interface {
2727

2828
type CheckoutsItf interface {
2929
List(ctx context.Context, req *entity.ListCheckoutsReq) (*entity.ListCheckoutsResp, error)
30+
Show(ctx context.Context, req *entity.ShowCheckoutReq) (*entity.ShowCheckoutResp, error)
3031
Create(ctx context.Context, req *entity.CreateCheckoutReq) (*entity.CreateCheckoutResp, error)
3132
Update(ctx context.Context, req *entity.UpdateCheckoutReq) (*entity.UpdateCheckoutResp, error)
3233
Delete(ctx context.Context, req *entity.DeleteCheckoutReq) (*entity.DeleteCheckoutResp, error)

pkg/api/checkouts.go

+25
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,31 @@ func (c *Checkouts) List(ctx context.Context, req *entity.ListCheckoutsReq) (*en
4242
return &content, nil
4343
}
4444

45+
func (c *Checkouts) Show(ctx context.Context, req *entity.ShowCheckoutReq) (*entity.ShowCheckoutResp, error) {
46+
url := "/checkouts/{identifier}"
47+
48+
var (
49+
content entity.ShowCheckoutResp
50+
contentErr entity.ErrResp
51+
)
52+
53+
_, err := c.client.R().
54+
SetContext(ctx).
55+
SetPathParam("identifier", req.Identifier()).
56+
SetResult(&content).
57+
SetError(&contentErr).
58+
Get(url)
59+
if err != nil {
60+
return nil, err
61+
}
62+
63+
if contentErr.Valid() {
64+
return nil, contentErr.Error
65+
}
66+
67+
return &content, nil
68+
}
69+
4570
func (c *Checkouts) Create(ctx context.Context, req *entity.CreateCheckoutReq) (*entity.CreateCheckoutResp, error) {
4671
url := "/checkouts"
4772

pkg/api/stub/checkouts.go

+13
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@ func NewCheckouts() *Checkouts {
1212

1313
type Checkouts struct{}
1414

15+
func (c *Checkouts) Show(ctx context.Context, req *entity.ShowCheckoutReq) (*entity.ShowCheckoutResp, error) {
16+
if err := CreateErrResp(ctx); err.Valid() {
17+
return nil, err.Error
18+
}
19+
20+
data := CreateCheckoutResource()
21+
data.ID = req.CheckoutID
22+
23+
return &entity.ShowCheckoutResp{
24+
Data: data,
25+
}, nil
26+
}
27+
1528
func (c *Checkouts) Create(ctx context.Context, req *entity.CreateCheckoutReq) (*entity.CreateCheckoutResp, error) {
1629
if err := CreateErrResp(ctx); err.Valid() {
1730
return nil, err.Error

pkg/entity/checkouts.go

+23
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,29 @@ type ListCheckoutsResp struct {
1717
Data []CheckoutResource `json:"data"`
1818
}
1919

20+
// Reference: https://commerce.coinbase.com/docs/api/#show-a-checkout
21+
22+
type ShowCheckoutReq struct {
23+
CheckoutID string `json:"checkout_id"`
24+
}
25+
26+
func (s ShowCheckoutReq) Validate() error {
27+
if s.CheckoutID == "" {
28+
return errors.New("payload: at least one of [id] must be supplied")
29+
}
30+
return nil
31+
}
32+
33+
// Identifier returns identifier for current request.
34+
// Checkout code has higher priority than id.
35+
func (s ShowCheckoutReq) Identifier() string {
36+
return s.CheckoutID
37+
}
38+
39+
type ShowCheckoutResp struct {
40+
Data CheckoutResource `json:"data"`
41+
}
42+
2043
// Reference: https://commerce.coinbase.com/docs/api/#create-a-checkout
2144

2245
type CreateCheckoutReq struct {

0 commit comments

Comments
 (0)