Skip to content

Commit

Permalink
Add support for the LineItem resource and APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-stripe committed May 12, 2020
1 parent a130c55 commit b74a522
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ cache:
env:
global:
# If changing this number, please also change it in `testing/testing.go`.
- STRIPE_MOCK_VERSION=0.89.0
- STRIPE_MOCK_VERSION=0.90.0

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

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

// Client is used to invoke /checkout_sessions APIs.
Expand Down Expand Up @@ -59,6 +60,27 @@ func (c Client) List(listParams *stripe.CheckoutSessionListParams) *Iter {
})}
}

// ListLineItems returns a list of line items on a session.
func ListLineItems(params *stripe.CheckoutSessionListLineItemParams) *lineitem.Iter {
return getC().ListLineItems(params)
}

// ListLineItems returns a list of line items on a session.
func (c Client) ListLineItems(listParams *stripe.CheckoutSessionListLineItemParams) *lineitem.Iter {
path := stripe.FormatURLPath("/v1/checkout/sessions/%s/line_items", stripe.StringValue(listParams.Session))
return &lineitem.Iter{stripe.GetIter(listParams, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.ListMeta, error) {
list := &stripe.LineItemList{}
err := c.B.CallRaw(http.MethodGet, path, 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
Expand Down
44 changes: 37 additions & 7 deletions checkout_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,38 @@ const (
CheckoutSessionModeSubscription CheckoutSessionMode = "subscription"
)

// CheckoutSessionLineItemPriceDataRecurringParams is the set of parameters for the recurring
// components of a price created inline for a line item
type CheckoutSessionLineItemPriceDataRecurringParams struct {
AggregateUsage *string `form:"aggregate_usage"`
Interval *string `form:"interval"`
IntervalCount *int64 `form:"interval_count"`
TrialPeriodDays *int64 `form:"trial_period_days"`
UsageType *string `form:"usage_type"`
}

// CheckoutSessionLineItemPriceDataParams is a structure representing the parameters to create
// an inline price for a line item.
type CheckoutSessionLineItemPriceDataParams struct {
Currency *string `form:"currency"`
Product *string `form:"product"`
Recurring *CheckoutSessionLineItemPriceDataRecurringParams `form:"recurring"`
UnitAmount *int64 `form:"unit_amount"`
UnitAmountDecimal *float64 `form:"unit_amount_decimal,high_precision"`
}

// CheckoutSessionLineItemParams is the set of parameters allowed for a line item
// on a checkout session.
type CheckoutSessionLineItemParams struct {
Amount *int64 `form:"amount"`
Currency *string `form:"currency"`
Description *string `form:"description"`
Images []*string `form:"images"`
Name *string `form:"name"`
Quantity *int64 `form:"quantity"`
TaxRates []*string `form:"tax_rates"`
Amount *int64 `form:"amount"`
Currency *string `form:"currency"`
Description *string `form:"description"`
Images []*string `form:"images"`
Name *string `form:"name"`
Price *string `form:"price"`
PriceData *CheckoutSessionLineItemPriceDataParams `form:"price_data"`
Quantity *int64 `form:"quantity"`
TaxRates []*string `form:"tax_rates"`
}

// CheckoutSessionPaymentIntentDataTransferDataParams is the set of parameters allowed for the
Expand Down Expand Up @@ -130,6 +152,13 @@ type CheckoutSessionParams struct {
SuccessURL *string `form:"success_url"`
}

// CheckoutSessionListLineItemParams is the set of parameters that can be
// used when listing line items on a session.
type CheckoutSessionListLineItemParams struct {
ListParams `form:"*"`
Session *string `form:"-"` // Included in 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
Expand Down Expand Up @@ -174,6 +203,7 @@ type CheckoutSession struct {
Deleted bool `json:"deleted"`
DisplayItems []*CheckoutSessionDisplayItem `json:"display_items"`
ID string `json:"id"`
LineItems []*LineItem `json:"line_items"`
Livemode bool `json:"livemode"`
Locale string `json:"locale"`
Metadata map[string]string `json:"metadata"`
Expand Down
51 changes: 51 additions & 0 deletions lineitem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package stripe

import (
"encoding/json"
)

// LineItemTax represent the details of one tax rate applied to a line item.
type LineItemTax struct {
Amount int64 `json:"amount"`
TaxRate *TaxRate `json:"tax_rate"`
}

// LineItem is the resource representing a line item.
type LineItem struct {
APIResource
AmountSubtotal int64 `json:"amount_subtotal"`
AmountTotal int64 `json:"amount_total"`
Currency Currency `json:"currency"`
Description string `json:"description"`
ID string `json:"id"`
Object string `json:"object"`
Price *Price `json:"price"`
Quantity int64 `json:"quantity"`
Taxes []*LineItemTax `json:"taxes"`
}

// LineItemList is a list of prices as returned from a list endpoint.
type LineItemList struct {
APIResource
ListMeta
Data []*LineItem `json:"data"`
}

// UnmarshalJSON handles deserialization of a LineItem.
// This custom unmarshaling is needed because the resulting
// property may be an id or the full struct if it was expanded.
func (s *LineItem) UnmarshalJSON(data []byte) error {
if id, ok := ParseID(data); ok {
s.ID = id
return nil
}

type price LineItem
var v price
if err := json.Unmarshal(data, &v); err != nil {
return err
}

*s = LineItem(v)
return nil
}
16 changes: 16 additions & 0 deletions lineitem/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Package lineitem provides the tools needs to interact with the LineItem resource.
package lineitem

import (
stripe "github.com/stripe/stripe-go/v71"
)

// Iter is an iterator for line items across various resources.
type Iter struct {
*stripe.Iter
}

// LineItem returns the line item which the iterator is currently pointing to.
func (i *Iter) LineItem() *stripe.LineItem {
return i.Current().(*stripe.LineItem)
}
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.89.0"
MockMinimumVersion = "0.90.0"

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

0 comments on commit b74a522

Please sign in to comment.