Skip to content

Commit

Permalink
Merge pull request #518 from pantera-stripe/pantera-product-and-plan-…
Browse files Browse the repository at this point in the history
…support

Product and plan support
  • Loading branch information
ob-stripe authored Feb 15, 2018
2 parents c7c700f + 9bb1c27 commit 8dbdb3e
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ cache:

env:
global:
- STRIPE_MOCK_VERSION=0.4.0
- STRIPE_MOCK_VERSION=0.7.0

go:
- 1.7
Expand Down
2 changes: 1 addition & 1 deletion ephemeralkey/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestEphemeralKeyDel(t *testing.T) {
func TestEphemeralKeyNew(t *testing.T) {
key, err := New(&stripe.EphemeralKeyParams{
Customer: "cus_123",
StripeVersion: "2017-05-25",
StripeVersion: "2018-02-06",
})
assert.Nil(t, err)
assert.NotNil(t, key)
Expand Down
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func ExamplePlan_list() {

it := plan.List(params)
for it.Next() {
log.Printf("%v ", it.Plan().Name)
log.Printf("%v ", it.Plan().Nickname)
}
if err := it.Err(); err != nil {
log.Fatal(err)
Expand Down
23 changes: 12 additions & 11 deletions plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type Plan struct {
IntervalCount uint64 `json:"interval_count"`
Live bool `json:"livemode"`
Meta map[string]string `json:"metadata"`
Name string `json:"name"`
Statement string `json:"statement_descriptor"`
Nickname string `json:"nickname"`
Product string `json:"product"`
TrialPeriod uint64 `json:"trial_period_days"`
}

Expand All @@ -39,13 +39,14 @@ type PlanListParams struct {
// For more details see https://stripe.com/docs/api#create_plan and https://stripe.com/docs/api#update_plan.
type PlanParams struct {
Params `form:"*"`
Amount uint64 `form:"amount"`
AmountZero bool `form:"amount,zero"`
Currency Currency `form:"currency"`
ID string `form:"id"`
Interval PlanInterval `form:"interval"`
IntervalCount uint64 `form:"interval_count"`
Name string `form:"name"`
Statement string `form:"statement_descriptor"`
TrialPeriod uint64 `form:"trial_period_days"`
Amount uint64 `form:"amount"`
AmountZero bool `form:"amount,zero"`
Currency Currency `form:"currency"`
ID string `form:"id"`
Interval PlanInterval `form:"interval"`
IntervalCount uint64 `form:"interval_count"`
Nickname string `form:"nickname"`
Product *ProductParams `form:"product"`
ProductID *string `form:"product"`
TrialPeriod uint64 `form:"trial_period_days"`
}
20 changes: 18 additions & 2 deletions plan/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,31 @@ func TestPlanNew(t *testing.T) {
Currency: "usd",
ID: "sapphire-elite",
Interval: "month",
Name: "Sapphire Elite",
Product: &stripe.ProductParams{
Name: "Sapphire Elite",
Type: stripe.ProductTypeService,
},
})
assert.Nil(t, err)
assert.NotNil(t, plan)
}

func TestPlanNewWithProductID(t *testing.T) {
productId := "prod_12345abc"
plan, err := New(&stripe.PlanParams{
Amount: 1,
Currency: "usd",
ID: "sapphire-elite",
Interval: "month",
ProductID: &productId,
})
assert.Nil(t, err)
assert.NotNil(t, plan)
}

func TestPlanUpdate(t *testing.T) {
plan, err := Update("gold", &stripe.PlanParams{
Name: "Updated Name",
Nickname: "Updated nickame",
})
assert.Nil(t, err)
assert.NotNil(t, plan)
Expand Down
11 changes: 9 additions & 2 deletions plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ func TestPlanListParams_AppendTo_Empty(t *testing.T) {
}

func TestPlanParams_AppendTo(t *testing.T) {
productParams := ProductParams{
Name: "Sapphire Elite",
StatementDescriptor: "SAPPHIRE",
Type: ProductTypeService,
}
productId := "prod_123abc"
testCases := []struct {
field string
params *PlanParams
Expand All @@ -49,8 +55,9 @@ func TestPlanParams_AppendTo(t *testing.T) {
{"id", &PlanParams{ID: "sapphire-elite"}, "sapphire-elite"},
{"interval", &PlanParams{Interval: "month"}, "month"},
{"interval_count", &PlanParams{IntervalCount: 3}, strconv.FormatUint(3, 10)},
{"name", &PlanParams{Name: "Sapphire Elite"}, "Sapphire Elite"},
{"statement_descriptor", &PlanParams{Statement: "Sapphire Elite"}, "Sapphire Elite"},
{"product[name]", &PlanParams{Product: &productParams}, "Sapphire Elite"},
{"product[statement_descriptor]", &PlanParams{Product: &productParams}, "SAPPHIRE"},
{"product", &PlanParams{ProductID: &productId}, "prod_123abc"},
{"trial_period_days", &PlanParams{TrialPeriod: 123}, strconv.FormatUint(123, 10)},
}
for _, tc := range testCases {
Expand Down
73 changes: 45 additions & 28 deletions product.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ package stripe

import "encoding/json"

// ProductType is the type of a product.
type ProductType string

const (
// ProductTypeGood is a constant that indicates a product represents a physical good,
// which may be sold through the Stripe Relay API.
ProductTypeGood ProductType = "good"

// ProductTypeService is a constant that indicates a product represents a service
// which is provided on a recurring basis and is priced with a Stripe plan.
ProductTypeService ProductType = "service"
)

// PackageDimensions represents the dimension of a product or a sku from the
// perspective of shipping.
type PackageDimensions struct {
Expand All @@ -16,39 +29,43 @@ type PackageDimensions struct {
// For more details, see https://stripe.com/docs/api#create_product
// and https://stripe.com/docs/api#update_product.
type ProductParams struct {
Params `form:"*"`
Active *bool `form:"active"`
Attrs []string `form:"attributes"`
Caption string `form:"caption"`
DeactivateOn []string `form:"deactivate_on"`
Desc string `form:"description"`
ID string `form:"id"`
Images []string `form:"images"`
Name string `form:"name"`
PackageDimensions *PackageDimensions `form:"package_dimensions"`
Shippable *bool `form:"shippable"`
URL string `form:"url"`
Params `form:"*"`
Active *bool `form:"active"`
Attrs []string `form:"attributes"`
Caption string `form:"caption"`
DeactivateOn []string `form:"deactivate_on"`
Desc string `form:"description"`
ID string `form:"id"`
Images []string `form:"images"`
Name string `form:"name"`
PackageDimensions *PackageDimensions `form:"package_dimensions"`
Shippable *bool `form:"shippable"`
StatementDescriptor string `form:"statement_descriptor"`
Type ProductType `form:"type"`
URL string `form:"url"`
}

// Product is the resource representing a Stripe product.
// For more details see https://stripe.com/docs/api#products.
type Product struct {
Active bool `json:"active"`
Attrs []string `json:"attributes"`
Caption string `json:"caption"`
Created int64 `json:"created"`
DeactivateOn []string `json:"deactivate_on"`
Desc string `json:"description"`
ID string `json:"id"`
Images []string `json:"images"`
Live bool `json:"livemode"`
Meta map[string]string `json:"metadata"`
Name string `json:"name"`
PackageDimensions *PackageDimensions `json:"package_dimensions"`
Shippable bool `json:"shippable"`
Skus *SKUList `json:"skus"`
URL string `json:"url"`
Updated int64 `json:"updated"`
Active bool `json:"active"`
Attrs []string `json:"attributes"`
Caption string `json:"caption"`
Created int64 `json:"created"`
DeactivateOn []string `json:"deactivate_on"`
Desc string `json:"description"`
ID string `json:"id"`
Images []string `json:"images"`
Live bool `json:"livemode"`
Meta map[string]string `json:"metadata"`
Name string `json:"name"`
PackageDimensions *PackageDimensions `json:"package_dimensions"`
Shippable bool `json:"shippable"`
Skus *SKUList `json:"skus"`
StatementDescriptor string `json:"statement_descriptor"`
URL string `json:"url"`
Updated int64 `json:"updated"`
Type ProductType `json:"type"`
}

// ProductList is a list of products as retrieved from a list endpoint.
Expand Down
1 change: 1 addition & 0 deletions product/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func TestProductNew(t *testing.T) {
Width: 6.50,
Weight: 10,
},
Type: stripe.ProductTypeGood,
})
assert.Nil(t, err)
assert.NotNil(t, product)
Expand Down

0 comments on commit 8dbdb3e

Please sign in to comment.