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 support for listing Checkout Session and passing tax rate information #1036

Merged
merged 2 commits into from
Feb 24, 2020
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ cache:
env:
global:
# If changing this number, please also change it in `testing/testing.go`.
- STRIPE_MOCK_VERSION=0.79.0
- STRIPE_MOCK_VERSION=0.82.0

go:
- "1.9.x"
Expand Down
31 changes: 31 additions & 0 deletions checkout/session/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"

stripe "github.com/stripe/stripe-go"
"github.com/stripe/stripe-go/form"
)

// Client is used to invoke /checkout_sessions APIs.
Expand Down Expand Up @@ -38,6 +39,36 @@ func (c Client) Get(id string, params *stripe.CheckoutSessionParams) (*stripe.Ch
return session, err
}

// List returns a list of sessions.
func List(params *stripe.CheckoutSessionListParams) *Iter {
return getC().List(params)
}

// List returns a list of sessions.
func (c Client) List(listParams *stripe.CheckoutSessionListParams) *Iter {
return &Iter{stripe.GetIter(listParams, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.ListMeta, error) {
list := &stripe.CheckoutSessionList{}
err := c.B.CallRaw(http.MethodGet, "/v1/checkout/sessions", c.Key, b, p, list)

ret := make([]interface{}, len(list.Data))
for i, v := range list.Data {
ret[i] = v
}

return ret, list.ListMeta, err
})}
}

// Iter is an iterator for sessions.
type Iter struct {
*stripe.Iter
}

// CheckoutSession returns the session which the iterator is currently pointing to.
func (i *Iter) CheckoutSession() *stripe.CheckoutSession {
return i.Current().(*stripe.CheckoutSession)
}

func getC() Client {
return Client{stripe.GetBackend(stripe.APIBackend), stripe.Key}
}
9 changes: 9 additions & 0 deletions checkout/session/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,12 @@ func TestCheckoutSessionNew(t *testing.T) {
assert.Nil(t, err)
assert.NotNil(t, session)
}

func TestCheckoutSessionList(t *testing.T) {
i := List(&stripe.CheckoutSessionListParams{})

// Verify that we can get at least one session.
assert.True(t, i.Next())
assert.Nil(t, i.Err())
assert.NotNil(t, i.CheckoutSession())
}
22 changes: 20 additions & 2 deletions checkout_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type CheckoutSessionLineItemParams struct {
Images []*string `form:"images"`
Name *string `form:"name"`
Quantity *int64 `form:"quantity"`
TaxRates []*string `form:"tax_rates"`
}

// CheckoutSessionPaymentIntentDataTransferDataParams is the set of parameters allowed for the
Expand Down Expand Up @@ -81,15 +82,17 @@ type CheckoutSessionSetupIntentDataParams struct {
// CheckoutSessionSubscriptionDataItemsParams is the set of parameters allowed for one item on a
// checkout session associated with a subscription.
type CheckoutSessionSubscriptionDataItemsParams struct {
Plan *string `form:"plan"`
Quantity *int64 `form:"quantity"`
Plan *string `form:"plan"`
Quantity *int64 `form:"quantity"`
TaxRates []*string `form:"tax_rates"`
}

// CheckoutSessionSubscriptionDataParams is the set of parameters allowed for the subscription
// creation on a checkout session.
type CheckoutSessionSubscriptionDataParams struct {
Params `form:"*"`
ApplicationFeePercent *float64 `form:"application_fee_percent"`
DefaultTaxRates []*string `form:"default_tax_rates"`
Items []*CheckoutSessionSubscriptionDataItemsParams `form:"items"`
TrialEnd *int64 `form:"trial_end"`
TrialFromPlan *bool `form:"trial_from_plan"`
Expand Down Expand Up @@ -117,6 +120,15 @@ type CheckoutSessionParams struct {
SuccessURL *string `form:"success_url"`
}

// CheckoutSessionListParams is the set of parameters that can be
// used when listing sessions.
// For more details see: https://stripe.com/docs/api/checkout/sessions/list
type CheckoutSessionListParams struct {
ListParams `form:"*"`
PaymentIntent *string `form:"payment_intent"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you alphabetize those?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

woops! good catch. Fixing

Subscription *string `form:"subscription"`
}

// CheckoutSessionDisplayItemCustom represents an item of type custom in a checkout session
type CheckoutSessionDisplayItemCustom struct {
Description string `json:"description"`
Expand Down Expand Up @@ -158,6 +170,12 @@ type CheckoutSession struct {
SuccessURL string `json:"success_url"`
}

// CheckoutSessionList is a list of sessions as retrieved from a list endpoint.
type CheckoutSessionList struct {
ListMeta
Data []*CheckoutSession `json:"data"`
}

// UnmarshalJSON handles deserialization of a checkout session.
// This custom unmarshaling is needed because the resulting
// property may be an id or the full struct if it was expanded.
Expand Down
2 changes: 1 addition & 1 deletion testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const (
// added in a more recent version of stripe-mock, we can show people a
// better error message instead of the test suite crashing with a bunch of
// confusing 404 errors or the like.
MockMinimumVersion = "0.79.0"
MockMinimumVersion = "0.82.0"

// TestMerchantID is a token that can be used to represent a merchant ID in
// simple tests.
Expand Down