Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add usage data for stripe client usage #1982

Merged
merged 4 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions account/client_test.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
package account
package account_test

import (
"testing"

assert "github.com/stretchr/testify/require"
stripe "github.com/stripe/stripe-go/v81"
_ "github.com/stripe/stripe-go/v81/testing"
"github.com/stripe/stripe-go/v81/client"
. "github.com/stripe/stripe-go/v81/testing"
)

func TestAccountDel(t *testing.T) {
account, err := Del("acct_123", nil)
sc := client.New(TestAPIKey, nil)
account, err := sc.Accounts.Del("acct_123", nil)
assert.Nil(t, err)
assert.NotNil(t, account)
}

func TestAccountGet(t *testing.T) {
account, err := Get()
sc := client.New(TestAPIKey, nil)
account, err := sc.Accounts.Get()
assert.Nil(t, err)
assert.NotNil(t, account)
}

func TestAccountGetByID(t *testing.T) {
account, err := GetByID("acct_123", nil)
sc := client.New(TestAPIKey, nil)
account, err := sc.Accounts.GetByID("acct_123", nil)
assert.Nil(t, err)
assert.NotNil(t, account)
}

func TestAccountList(t *testing.T) {
i := List(&stripe.AccountListParams{})
sc := client.New(TestAPIKey, nil)
i := sc.Accounts.List(&stripe.AccountListParams{})

// Verify that we can get at least one account
assert.True(t, i.Next())
Expand All @@ -37,7 +42,8 @@ func TestAccountList(t *testing.T) {
}

func TestAccountNew(t *testing.T) {
account, err := New(&stripe.AccountParams{
sc := client.New(TestAPIKey, nil)
account, err := sc.Accounts.New(&stripe.AccountParams{
BusinessProfile: &stripe.AccountBusinessProfileParams{
Name: stripe.String("name"),
SupportEmail: stripe.String("[email protected]"),
Expand Down Expand Up @@ -103,15 +109,17 @@ func TestAccountNew(t *testing.T) {
}

func TestAccountReject(t *testing.T) {
account, err := Reject("acct_123", &stripe.AccountRejectParams{
sc := client.New(TestAPIKey, nil)
account, err := sc.Accounts.Reject("acct_123", &stripe.AccountRejectParams{
Reason: stripe.String("fraud"),
})
assert.Nil(t, err)
assert.NotNil(t, account)
}

func TestAccountUpdate(t *testing.T) {
account, err := Update("acct_123", &stripe.AccountParams{
sc := client.New(TestAPIKey, nil)
account, err := sc.Accounts.Update("acct_123", &stripe.AccountParams{
Company: &stripe.AccountCompanyParams{
Address: &stripe.AddressParams{
Country: stripe.String("CA"),
Expand Down
23 changes: 15 additions & 8 deletions charge/client_test.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
package charge
package charge_test

import (
"testing"

assert "github.com/stretchr/testify/require"
stripe "github.com/stripe/stripe-go/v81"
_ "github.com/stripe/stripe-go/v81/testing"
"github.com/stripe/stripe-go/v81/client"
. "github.com/stripe/stripe-go/v81/testing"
)

func TestChargeCapture(t *testing.T) {
charge, err := Capture("ch_123", &stripe.ChargeCaptureParams{
sc := client.New(TestAPIKey, nil)
charge, err := sc.Charges.Capture("ch_123", &stripe.ChargeCaptureParams{
Amount: stripe.Int64(123),
})
assert.Nil(t, err)
assert.NotNil(t, charge)
}

func TestChargeGet(t *testing.T) {
charge, err := Get("ch_123", nil)
sc := client.New(TestAPIKey, nil)
charge, err := sc.Charges.Get("ch_123", nil)
assert.Nil(t, err)
assert.NotNil(t, charge)
}

func TestChargeList(t *testing.T) {
i := List(&stripe.ChargeListParams{})
sc := client.New(TestAPIKey, nil)
i := sc.Charges.List(&stripe.ChargeListParams{})

// Verify that we can get at least one charge
assert.True(t, i.Next())
Expand All @@ -33,7 +37,8 @@ func TestChargeList(t *testing.T) {
}

func TestChargeSearch(t *testing.T) {
i := Search(&stripe.ChargeSearchParams{SearchParams: stripe.SearchParams{
sc := client.New(TestAPIKey, nil)
i := sc.Charges.Search(&stripe.ChargeSearchParams{SearchParams: stripe.SearchParams{
Query: "currency:\"USD\"",
}})

Expand All @@ -46,7 +51,8 @@ func TestChargeSearch(t *testing.T) {
}

func TestChargeNew(t *testing.T) {
charge, err := New(&stripe.ChargeParams{
sc := client.New(TestAPIKey, nil)
charge, err := sc.Charges.New(&stripe.ChargeParams{
Amount: stripe.Int64(11700),
Currency: stripe.String(string(stripe.CurrencyUSD)),
Source: &stripe.PaymentSourceSourceParams{Token: stripe.String("src_123")},
Expand All @@ -64,7 +70,8 @@ func TestChargeNew(t *testing.T) {
}

func TestChargeUpdate(t *testing.T) {
charge, err := Update("ch_123", &stripe.ChargeParams{
sc := client.New(TestAPIKey, nil)
charge, err := sc.Charges.Update("ch_123", &stripe.ChargeParams{
Description: stripe.String("Updated description"),
})
assert.Nil(t, err)
Expand Down
7 changes: 4 additions & 3 deletions client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,11 +425,12 @@ type API struct {

func (a *API) Init(key string, backends *stripe.Backends) {

usage := []string{"stripe_client"}
if backends == nil {
backends = &stripe.Backends{
API: stripe.GetBackend(stripe.APIBackend),
Connect: stripe.GetBackend(stripe.ConnectBackend),
Uploads: stripe.GetBackend(stripe.UploadsBackend),
API: &stripe.UsageBackend{B: stripe.GetBackend(stripe.APIBackend), Usage: usage},
Connect: &stripe.UsageBackend{B: stripe.GetBackend(stripe.ConnectBackend), Usage: usage},
Uploads: &stripe.UsageBackend{B: stripe.GetBackend(stripe.UploadsBackend), Usage: usage},
}
}

Expand Down
20 changes: 13 additions & 7 deletions coupon/client_test.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
package coupon
package coupon_test

import (
"testing"

assert "github.com/stretchr/testify/require"
stripe "github.com/stripe/stripe-go/v81"
_ "github.com/stripe/stripe-go/v81/testing"
"github.com/stripe/stripe-go/v81/client"
. "github.com/stripe/stripe-go/v81/testing"
)

func TestCouponDel(t *testing.T) {
coupon, err := Del("25OFF", nil)
sc := client.New(TestAPIKey, nil)
coupon, err := sc.Coupons.Del("25OFF", nil)
assert.Nil(t, err)
assert.NotNil(t, coupon)
}

func TestCouponGet(t *testing.T) {
coupon, err := Get("25OFF", nil)
sc := client.New(TestAPIKey, nil)
coupon, err := sc.Coupons.Get("25OFF", nil)
assert.Nil(t, err)
assert.NotNil(t, coupon)
}

func TestCouponList(t *testing.T) {
i := List(&stripe.CouponListParams{})
sc := client.New(TestAPIKey, nil)
i := sc.Coupons.List(&stripe.CouponListParams{})

// Verify that we can get at least one coupon
assert.True(t, i.Next())
Expand All @@ -31,7 +35,8 @@ func TestCouponList(t *testing.T) {
}

func TestCouponNew(t *testing.T) {
coupon, err := New(&stripe.CouponParams{
sc := client.New(TestAPIKey, nil)
coupon, err := sc.Coupons.New(&stripe.CouponParams{
AppliesTo: &stripe.CouponAppliesToParams{
Products: stripe.StringSlice([]string{
"prod_123",
Expand All @@ -49,7 +54,8 @@ func TestCouponNew(t *testing.T) {
}

func TestCouponUpdate(t *testing.T) {
coupon, err := Update("25OFF", &stripe.CouponParams{
sc := client.New(TestAPIKey, nil)
coupon, err := sc.Coupons.Update("25OFF", &stripe.CouponParams{
Params: stripe.Params{
Metadata: map[string]string{
"foo": "bar",
Expand Down
2 changes: 1 addition & 1 deletion params.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func (p *Params) AddExpand(f string) {
// InternalSetUsage sets the usage field on the Params struct.
// Unstable: for internal stripe-go usage only.
func (p *Params) InternalSetUsage(usage []string) {
p.usage = usage
p.usage = append(p.usage, usage...)
}

// AddExtra adds a new arbitrary key-value pair to the request data
Expand Down
34 changes: 34 additions & 0 deletions stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -1608,3 +1608,37 @@ func RawRequest(method, path string, content string, params *RawParams) (*APIRes
}
return nil, fmt.Errorf("Error: cannot call RawRequest if backends.API is initialized with a backend that doesn't implement RawRequestBackend")
}

// UsageBackend is a wrapper for stripe.Backend that sets the usage parameter
type UsageBackend struct {
B Backend
Usage []string
}

func (u *UsageBackend) Call(method, path, key string, params ParamsContainer, v LastResponseSetter) error {
if r := reflect.ValueOf(params); r.Kind() == reflect.Ptr && !r.IsNil() {
params.GetParams().InternalSetUsage(u.Usage)
}
return u.B.Call(method, path, key, params, v)
}

func (u *UsageBackend) CallRaw(method, path, key string, body *form.Values, params *Params, v LastResponseSetter) error {
params.GetParams().InternalSetUsage(u.Usage)
return u.B.CallRaw(method, path, key, body, params, v)
}

func (u *UsageBackend) CallMultipart(method, path, key, boundary string, body *bytes.Buffer, params *Params, v LastResponseSetter) error {
params.GetParams().InternalSetUsage(u.Usage)
return u.B.CallMultipart(method, path, key, boundary, body, params, v)
}

func (u *UsageBackend) CallStreaming(method, path, key string, params ParamsContainer, v StreamingLastResponseSetter) error {
if r := reflect.ValueOf(params); r.Kind() == reflect.Ptr && !r.IsNil() {
params.GetParams().InternalSetUsage(u.Usage)
}
return u.B.CallStreaming(method, path, key, params, v)
}

func (u *UsageBackend) SetMaxNetworkRetries(maxNetworkRetries int64) {
u.B.SetMaxNetworkRetries(maxNetworkRetries)
}
5 changes: 4 additions & 1 deletion testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ const (
// TestMerchantID is a token that can be used to represent a merchant ID in
// simple tests.
TestMerchantID = "acct_123"

// TestAPIKey is an API key that can be used against stripe-mock in tests.
TestAPIKey = "sk_test_myTestKey"
)

func init() {
Expand Down Expand Up @@ -82,7 +85,7 @@ func init() {
os.Exit(1)
}

stripe.Key = "sk_test_myTestKey"
stripe.Key = TestAPIKey

// Configure a backend for stripe-mock and set it for both the API and
// Uploads (unlike the real Stripe API, stripe-mock supports both these
Expand Down