Beispiel #1
0
// 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 *stripe.RequestValues
	var commonParams *stripe.Params

	if params != nil {
		body = &stripe.RequestValues{}

		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)
			}
		}

		if len(params.URL) > 0 {
			body.Add("url", params.URL)
		}

		// Passing empty attributes should unset the attributes.
		if params.Attrs != nil {
			if len(params.Attrs) > 0 {
				for _, v := range params.Attrs {
					body.Add("attributes[]", v)
				}
			} else {
				body.Add("attributes", "")
			}
		}

		for _, app := range params.DeactivateOn {
			body.Add("deactivate_on[]", app)
		}

		params.AppendTo(body)
	}

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

	return p, err
}
Beispiel #2
0
// 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)
			}
		}

		if len(params.URL) > 0 {
			body.Add("url", params.URL)
		}

		params.AppendTo(body)
	}

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

	return p, err
}
Beispiel #3
0
// 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 *stripe.RequestValues
	var commonParams *stripe.Params

	if params != nil {
		body = &stripe.RequestValues{}

		// Required fields
		body.Add("name", params.Name)

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

		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 len(params.URL) > 0 {
			body.Add("url", params.URL)
		}

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

		if params.PackageDimensions != nil {
			body.Add("package_dimensions[height]",
				fmt.Sprintf("%.2f", params.PackageDimensions.Height))
			body.Add("package_dimensions[length]",
				fmt.Sprintf("%.2f", params.PackageDimensions.Length))
			body.Add("package_dimensions[width]",
				fmt.Sprintf("%.2f", params.PackageDimensions.Width))
			body.Add("package_dimensions[weight]",
				fmt.Sprintf("%.2f", params.PackageDimensions.Weight))
		}

		for _, app := range params.DeactivateOn {
			body.Add("deactivate_on[]", app)
		}

		params.AppendTo(body)
	}

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

	return p, err
}