Skip to content

Commit

Permalink
Merge pull request #1036 from stripe/cjavilla/list-checkout-sessions
Browse files Browse the repository at this point in the history
Add support for listing Checkout `Session` and passing tax rate information
  • Loading branch information
remi-stripe committed Feb 24, 2020
2 parents 6b8041f + 0dd12dc commit 2d56cd6
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
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
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
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
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"`
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
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

0 comments on commit 2d56cd6

Please sign in to comment.