コード例 #1
0
ファイル: client.go プロジェクト: mousadialo/stripe-go
// Update updates a SKU's properties.
// For more details see https://stripe.com/docs/api#update_sku.
func (c Client) Update(id string, params *stripe.SKUParams) (*stripe.SKU, error) {
	var body *url.Values
	var commonParams *stripe.Params

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

		// Required fields
		if params.Price > 0 {
			body.Add("price", strconv.FormatInt(params.Price, 10))
		}

		if len(params.Currency) > 0 {
			body.Add("currency", params.Currency)
		}

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

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

		if len(params.Image) > 0 {
			body.Add("image", params.Image)
		}

		inventory := params.Inventory

		if len(inventory.Type) > 0 {
			body.Add("inventory[type]", inventory.Type)
			switch inventory.Type {
			case "finite":
				body.Add("inventory[quantity]", strconv.FormatInt(inventory.Quantity, 10))
			case "bucket":
				body.Add("inventory[value]", inventory.Value)
			}
		}

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

		params.AppendTo(body)
	}

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

	return p, err
}
コード例 #2
0
ファイル: client.go プロジェクト: captain401/stripe-go
func (c Client) Get(id string, params *stripe.SKUParams) (*stripe.SKU, error) {
	sku := &stripe.SKU{}
	var body *stripe.RequestValues
	var commonParams *stripe.Params

	if params != nil {
		commonParams = &params.Params
		body = &stripe.RequestValues{}
		params.AppendTo(body)
	}
	err := c.B.Call("GET", "/skus/"+id, c.Key, body, commonParams, sku)

	return sku, err
}