Skip to content

Commit 1c68de9

Browse files
committed
Support for creating a one time charge
1 parent 55b6ef2 commit 1c68de9

File tree

2 files changed

+100
-13
lines changed

2 files changed

+100
-13
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Windows, macOS/OS X, and GNU/Linux are supported.
2020

2121
COMMANDS:
2222
admin, a Open admin pages
23-
charges Do things with charges (only recurring for now)
23+
charges, c, ch Do things with charges
2424
metafield, m, meta Metafield utilities
2525
orders, o Information about orders
2626
products, p Do things with products
@@ -135,8 +135,9 @@ Do things with charges (only recurring for now)
135135
sdt charges command [command options] [arguments...]
136136

137137
COMMANDS:
138-
ls, l List the shop's recurring charges or the recurring charges given by the specified IDs
139-
help, h Shows a list of commands or help for one command
138+
ls, l List the shop's recurring charges or the recurring charges given by the specified IDs
139+
create, c Create a one-time charge (Application Charge)
140+
help, h Shows a list of commands or help for one command
140141

141142
OPTIONS:
142143
--help, -h show help (default: false)

cmd/charges/charges.go

Lines changed: 96 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strconv"
88
"strings"
99

10+
"github.com/shopspring/decimal"
1011
shopify "github.com/bold-commerce/go-shopify/v3"
1112
"github.com/cheynewallace/tabby"
1213
"github.com/urfave/cli/v2"
@@ -22,16 +23,20 @@ type listChargesOptions struct {
2223

2324
func printJSONL(charges []shopify.RecurringApplicationCharge) {
2425
for _, charge := range charges {
25-
line, err := json.Marshal(charge)
26-
if err != nil {
27-
panic(err)
28-
}
26+
printChargeJSONL(charge);
27+
}
28+
}
2929

30-
fmt.Println(string(line))
30+
func printChargeJSONL(charge interface{}) {
31+
line, err := json.Marshal(charge)
32+
if err != nil {
33+
panic(err)
3134
}
35+
36+
fmt.Println(string(line))
3237
}
3338

34-
func printFormatted(charges []shopify.RecurringApplicationCharge) {
39+
func printFormattedRecurringCharges(charges []shopify.RecurringApplicationCharge) {
3540
t := tabby.New()
3641

3742
for _, charge := range charges {
@@ -49,7 +54,61 @@ func printFormatted(charges []shopify.RecurringApplicationCharge) {
4954

5055
fmt.Printf("%s\n", strings.Repeat("-", 20))
5156
}
57+
}
58+
59+
func printFormattedApplicationCharge(charge *shopify.ApplicationCharge) {
60+
t := tabby.New()
61+
62+
t.AddLine("Id", charge.ID)
63+
t.AddLine("Name", charge.Name)
64+
t.AddLine("Price", charge.Price)
65+
t.AddLine("Status", charge.Status)
66+
t.AddLine("Confirmation URL", charge.ConfirmationURL)
67+
t.AddLine("Return URL", charge.DecoratedReturnURL)
68+
t.AddLine("Test", *charge.Test)
69+
t.AddLine("Created At", charge.CreatedAt)
70+
t.AddLine("Updated At", charge.UpdatedAt)
71+
t.Print()
72+
73+
fmt.Printf("%s\n", strings.Repeat("-", 20))
74+
}
75+
76+
77+
func createCharge(c *cli.Context) error {
78+
var charge shopify.ApplicationCharge
79+
80+
if(c.Args().Len() < 2) {
81+
return fmt.Errorf("You must supply charge name and price")
82+
}
83+
84+
price, err := decimal.NewFromString(c.Args().Get(1))
85+
if err != nil {
86+
return fmt.Errorf("Cannot create charge: invalid price %s", err)
87+
}
5288

89+
charge.Price = &price
90+
charge.Name = c.Args().Get(0)
91+
92+
test := c.Bool("test")
93+
charge.Test = &test
94+
95+
returnURL := c.String("return-to")
96+
if len(returnURL) > 0 {
97+
charge.ReturnURL = returnURL
98+
}
99+
100+
result, err := cmd.NewShopifyClient(c).ApplicationCharge.Create(charge)
101+
if err != nil {
102+
return fmt.Errorf("Cannot create charge: %s", err)
103+
}
104+
105+
if(c.Bool("jsonl")) {
106+
printChargeJSONL(result)
107+
} else {
108+
printFormattedApplicationCharge(result)
109+
}
110+
111+
return nil
53112
}
54113

55114
func listCharges(c *cli.Context) error {
@@ -75,34 +134,61 @@ func listCharges(c *cli.Context) error {
75134
if c.Bool("jsonl") {
76135
printJSONL(charges)
77136
} else {
78-
printFormatted(charges)
137+
printFormattedRecurringCharges(charges)
79138
}
80139

81140
return nil
82141
}
83142

84143
func init() {
85-
chargeFlags := []cli.Flag{
144+
listFlags := []cli.Flag{
86145
&cli.BoolFlag{
87146
Name: "jsonl",
88147
Aliases: []string{"j"},
89148
Usage: "Output the charges in JSONL format",
90149
},
150+
}
91151

152+
createFlags := []cli.Flag{
153+
&cli.StringFlag{
154+
Name: "return-to",
155+
Aliases: []string{"r"},
156+
Usage: "URL to redirect user to after charge is accepted",
157+
},
158+
&cli.BoolFlag{
159+
Name: "test",
160+
Aliases: []string{"t"},
161+
Usage: "Make the charge a test charge",
162+
},
163+
// lib does not support
164+
// &cli.StringFlag{
165+
// Name: "currency",
166+
// Aliases: []string{"c"},
167+
// Usage: "Currency to use",
168+
// },
92169
}
93170

94171
Cmd = cli.Command{
95172
Name: "charges",
96-
Usage: "Do things with charges (only recurring for now)",
173+
Aliases: []string{"c", "ch"},
174+
Usage: "Do things with charges",
97175
Subcommands: []*cli.Command{
98176
{
99177
Name: "ls",
100178
Aliases: []string{"l"},
101179
Usage: "List the shop's recurring charges or the recurring charges given by the specified IDs",
102180
ArgsUsage: "[ID [ID ...]]",
103-
Flags: append(cmd.Flags, chargeFlags...),
181+
Flags: append(cmd.Flags, listFlags...),
104182
Action: listCharges,
105183
},
184+
{
185+
Name: "create",
186+
Aliases: []string{"c"},
187+
Usage: "Create a one-time charge (Application Charge)",
188+
ArgsUsage: "NAME PRICE",
189+
Flags: append(cmd.Flags, createFlags...),
190+
Action: createCharge,
191+
},
106192
},
107193
}
108194
}

0 commit comments

Comments
 (0)