Skip to content

Commit

Permalink
product update.
Browse files Browse the repository at this point in the history
  • Loading branch information
siddarth committed May 27, 2015
1 parent 4493e3d commit 5256c0d
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
14 changes: 14 additions & 0 deletions product.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package stripe

import "encoding/json"

// ProductParams is the set of parameters that can be used
// when creating or updating a product.
// For more details, see https://stripe.com/docs/api#create_product
// and https://stripe.com/docs/api#update_product.
type ProductParams struct {
Params
ID string
Expand All @@ -10,9 +14,12 @@ type ProductParams struct {
Caption string
Desc string
Attrs []string
Images []string `json:"images"`
Shippable *bool
}

// Product is the resource representing a Stripe product.
// For more details see https://stripe.com/docs/api#products.
type Product struct {
ID string `json:"id"`
Created int64 `json:"created"`
Expand All @@ -24,15 +31,22 @@ type Product struct {
Desc string `json:"description"`
Attrs []string `json:"attributes"`
Shippable bool `json:"shippable"`
Images []string `json:"images"`
Meta map[string]string `json:"metdata"`
Skus *SKUList `json:"skus"`
}

// ProductListParams is the set of parameters that can be used when
// listing products. For more details, see:
// https://stripe.com/docs/api#list_products.
type ProductListParams struct {
ListParams
Created int64
}

// UnmarshalJSON handles deserialization of a Product.
// This custom unmarshaling is needed because the resulting
// property may be an id or the full struct if it was expanded.
func (p *Product) UnmarshalJSON(data []byte) error {
type product Product
var pr product
Expand Down
56 changes: 56 additions & 0 deletions product/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ type Client struct {
Key string
}

// New POSTs a new product.
// For more details see https://stripe.com/docs/api#create_product.
func New(params *stripe.ProductParams) (*stripe.Product, error) {
return getC().New(params)
}

// New POSTs a new product.
// For more details see https://stripe.com/docs/api#create_product.
func (c Client) New(params *stripe.ProductParams) (*stripe.Product, error) {
var body *url.Values
var commonParams *stripe.Params
Expand All @@ -32,17 +36,27 @@ func (c Client) New(params *stripe.ProductParams) (*stripe.Product, error) {
if params.ID != "" {
body.Add("id", params.ID)
}

if params.Active != nil {
body.Add("active", strconv.FormatBool(*(params.Active)))
}

if params.Caption != "" {
body.Add("caption", params.Caption)
}

if len(params.Attrs) > 0 {
for _, v := range params.Attrs {
body.Add("attributes[]", v)
}
}

if len(params.Images) > 0 {
for _, v := range params.Images {
body.Add("images[]", v)
}
}

if params.Shippable != nil {
body.Add("shippable", strconv.FormatBool(*(params.Shippable)))
}
Expand All @@ -56,6 +70,48 @@ func (c Client) New(params *stripe.ProductParams) (*stripe.Product, error) {
return p, err
}

// Update updates a product's properties.
// For more details see https://stripe.com/docs/api#update_product.
func Update(id string, params *stripe.ProductParams) (*stripe.Product, error) {
return getC().Update(id, params)
}

// Update updates a product's properties.
// For more details see https://stripe.com/docs/api#update_product.
func (c Client) Update(id string, params *stripe.ProductParams) (*stripe.Product, error) {
var body *url.Values
var commonParams *stripe.Params

if params != nil {
body = &url.Values{}

if len(params.Name) > 0 {
body.Add("name", params.Name)
}

if len(params.Desc) > 0 {
body.Add("description", params.Desc)
}

if params.Active != nil {
body.Add("active", strconv.FormatBool(*(params.Active)))
}

if len(params.Images) > 0 {
for _, v := range params.Images {
body.Add("images[]", v)
}
}

params.AppendTo(body)
}

p := &stripe.Product{}
err := c.B.Call("POST", "/products/"+id, c.Key, body, commonParams, p)

return p, err
}

// Get returns the details of an product
// For more details see https://stripe.com/docs/api#retrieve_product.
func Get(id string) (*stripe.Product, error) {
Expand Down
23 changes: 23 additions & 0 deletions product/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"fmt"
"math/rand"
"testing"
"time"

stripe "github.com/stripe-internal/stripe-go"
. "github.com/stripe-internal/stripe-go/utils"
)

func init() {
stripe.Key = GetTestKey()
rand.Seed(time.Now().UTC().UnixNano())
}

func TestProduct(t *testing.T) {
Expand Down Expand Up @@ -76,6 +78,27 @@ func TestProductWithCustomID(t *testing.T) {
}
}

func TestProductUpdate(t *testing.T) {
randID := fmt.Sprintf("TEST-PRODUCT-%v", randSeq(16))
p, err := New(&stripe.ProductParams{
ID: randID,
Name: "Test product name",
Desc: "Test description",
})
if err != nil {
t.Fatalf("%+v", err)
}
p, err = Update(p.ID, &stripe.ProductParams{
Desc: "new description",
})
if err != nil {
t.Fatalf("%+v", err)
}
if p.Desc != "new description" {
t.Errorf("Invalid description: %v", p.Desc)
}
}

// h/t: http://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-golang
func randSeq(n int) string {
letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
Expand Down

0 comments on commit 5256c0d

Please sign in to comment.