/
stripe.go
104 lines (92 loc) · 2.27 KB
/
stripe.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
package commands
import (
"context"
"fmt"
"reflect"
"github.com/killbill/kbcli/v2/kbcmd/cmdlib/args"
"github.com/killbill/kbcli/v2/kbcmd/cmdlib"
stripe "github.com/stripe/stripe-go"
"github.com/stripe/stripe-go/client"
"github.com/urfave/cli"
)
var stripeSecret string
var createCardProperties = []args.Property{
{Name: "Name", Required: true},
{Name: "Number", Required: true},
{Name: "ExpMonth", Required: true},
{Name: "ExpYear", Required: true},
{Name: "CVC"},
{Name: "AddressCountry"},
{Name: "Currency"},
{Name: "AddressZip"},
}
// createStripeCardToken - new stripe credit card token
func createStripeCardToken(ctx context.Context, o *cmdlib.Options) error {
sc := &client.API{}
if stripeSecret == "" {
return fmt.Errorf("stripe_key flag must be specified")
}
stripe.LogLevel = 1
if o.PrintDebug {
stripe.LogLevel = 3
}
sc.Init(stripeSecret, nil)
params := &stripe.TokenParams{
Card: &stripe.CardParams{},
}
err := args.LoadProperties(params.Card, createCardProperties, o.Args)
if err != nil {
return err
}
t, err := sc.Tokens.New(params)
if err != nil {
return err
}
o.Print(t)
return nil
}
func registerStripeCommands(r *cmdlib.App) {
cmdlib.AddFormatter(reflect.TypeOf(&stripe.Token{}), cmdlib.Formatter{
Columns: []cmdlib.Column{
{
Name: "TOKEN",
Path: "$.id",
},
{
Name: "Name",
Path: "$.card.name",
},
{
Name: "MONTH",
Path: "$.card.exp_month",
},
{
Name: "YEAR",
Path: "$.card.exp_year",
},
},
})
// Register top level command
r.Register("", cli.Command{
Name: "stripe",
Usage: "Stripe utility methods",
Flags: []cli.Flag{
cli.StringFlag{
Name: "stripe_key",
Usage: "Stripe private key",
Destination: &stripeSecret,
EnvVar: "KB_STRIPE_KEY",
},
},
}, nil)
createTokenUsage := args.GenerateUsageString(&stripe.CardParams{}, createCardProperties)
r.Register("stripe", cli.Command{
Name: "new-card-token",
Usage: "create new stripe credit card token",
ArgsUsage: fmt.Sprintf(`%s
For e.g.,
stripe --stripe_key=sk_test_aT1UY1u211iUYI4LEL2tLFay new-card-token Name="Prem Ramanathan" Number=4242424242424242 Month=12 Year=2019 CVC=123
`, createTokenUsage),
Description: "Creates new stripe card token",
}, createStripeCardToken)
}