@@ -13,35 +13,39 @@ import (
13
13
14
14
"github.com/gitpod-io/gitpod/common-go/log"
15
15
"github.com/stripe/stripe-go/v72"
16
- "github.com/stripe/stripe-go/v72/customer"
17
- "github.com/stripe/stripe-go/v72/usagerecord"
16
+ "github.com/stripe/stripe-go/v72/client"
18
17
)
19
18
19
+ type Client struct {
20
+ sc * client.API
21
+ }
22
+
20
23
type stripeKeys struct {
21
24
PublishableKey string `json:"publishableKey"`
22
25
SecretKey string `json:"secretKey"`
23
26
}
24
27
25
28
// Authenticate authenticates the Stripe client using a provided file containing a Stripe secret key.
26
- func Authenticate (apiKeyFile string ) error {
29
+ func Authenticate (apiKeyFile string ) ( * Client , error ) {
27
30
bytes , err := os .ReadFile (apiKeyFile )
28
31
if err != nil {
29
- return err
32
+ return nil , err
30
33
}
31
34
32
35
var stripeKeys stripeKeys
33
36
err = json .Unmarshal (bytes , & stripeKeys )
34
37
if err != nil {
35
- return err
38
+ return nil , err
36
39
}
37
40
38
- stripe .Key = stripeKeys .SecretKey
39
- return nil
41
+ sc := & client.API {}
42
+ sc .Init (stripeKeys .SecretKey , nil )
43
+ return & Client {sc : sc }, nil
40
44
}
41
45
42
46
// UpdateUsage updates teams' Stripe subscriptions with usage data
43
47
// `usageForTeam` is a map from team name to total workspace seconds used within a billing period.
44
- func UpdateUsage (usageForTeam map [string ]int64 ) error {
48
+ func ( c * Client ) UpdateUsage (usageForTeam map [string ]int64 ) error {
45
49
teamIds := make ([]string , 0 , len (usageForTeam ))
46
50
for k := range usageForTeam {
47
51
teamIds = append (teamIds , k )
@@ -56,7 +60,7 @@ func UpdateUsage(usageForTeam map[string]int64) error {
56
60
Expand : []* string {stripe .String ("data.subscriptions" )},
57
61
},
58
62
}
59
- iter := customer .Search (params )
63
+ iter := c . sc . Customers .Search (params )
60
64
for iter .Next () {
61
65
customer := iter .Customer ()
62
66
log .Infof ("found customer %q for teamId %q" , customer .Name , customer .Metadata ["teamId" ])
@@ -77,7 +81,7 @@ func UpdateUsage(usageForTeam map[string]int64) error {
77
81
78
82
subscriptionItemId := subscription .Items .Data [0 ].ID
79
83
log .Infof ("registering usage against subscriptionItem %q" , subscriptionItemId )
80
- _ , err := usagerecord .New (& stripe.UsageRecordParams {
84
+ _ , err := c . sc . UsageRecords .New (& stripe.UsageRecordParams {
81
85
SubscriptionItem : stripe .String (subscriptionItemId ),
82
86
Quantity : stripe .Int64 (creditsUsed ),
83
87
})
0 commit comments