77"strconv"
88"strings"
99
10+ "github.com/shopspring/decimal"
1011shopify "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
2324func printJSONL (charges []shopify.RecurringApplicationCharge ) {
2425for _ , 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 ) {
3540t := tabby .New ()
3641
3742for _ , charge := range charges {
@@ -49,7 +54,61 @@ func printFormatted(charges []shopify.RecurringApplicationCharge) {
4954
5055fmt .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
55114func listCharges (c * cli.Context ) error {
@@ -75,34 +134,61 @@ func listCharges(c *cli.Context) error {
75134if c .Bool ("jsonl" ) {
76135printJSONL (charges )
77136} else {
78- printFormatted (charges )
137+ printFormattedRecurringCharges (charges )
79138}
80139
81140return nil
82141}
83142
84143func init () {
85- chargeFlags := []cli.Flag {
144+ listFlags := []cli.Flag {
86145& cli.BoolFlag {
87146Name : "jsonl" ,
88147Aliases : []string {"j" },
89148Usage : "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
94171Cmd = cli.Command {
95172Name : "charges" ,
96- Usage : "Do things with charges (only recurring for now)" ,
173+ Aliases : []string {"c" , "ch" },
174+ Usage : "Do things with charges" ,
97175Subcommands : []* cli.Command {
98176{
99177Name : "ls" ,
100178Aliases : []string {"l" },
101179Usage : "List the shop's recurring charges or the recurring charges given by the specified IDs" ,
102180ArgsUsage : "[ID [ID ...]]" ,
103- Flags : append (cmd .Flags , chargeFlags ... ),
181+ Flags : append (cmd .Flags , listFlags ... ),
104182Action : 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