Exemplo n.º 1
0
func (c Client) Get(id string, params *stripe.OrderParams) (*stripe.Order, error) {
	var body *url.Values
	var commonParams *stripe.Params

	if params != nil {
		body = &url.Values{}
		commonParams = &params.Params
		params.AppendTo(body)
	}

	order := &stripe.Order{}
	err := c.B.Call("GET", "/orders/"+id, c.Key, body, commonParams, order)
	return order, err
}
Exemplo n.º 2
0
// New POSTs a new order.
// For more details see https://stripe.com/docs/api#create_order.
func (c Client) New(params *stripe.OrderParams) (*stripe.Order, error) {
	var body *url.Values
	var commonParams *stripe.Params

	if params != nil {
		body = &url.Values{}
		commonParams = &params.Params
		// Required fields
		body.Add("currency", string(params.Currency))

		if params.Customer != "" {
			body.Add("customer", params.Customer)
		}

		if params.Email != "" {
			body.Add("email", params.Email)
		}

		if params.Email != "" {
			body.Add("email", params.Email)
		}

		if len(params.Items) > 0 {
			for _, item := range params.Items {
				body.Add("items[][description]", item.Description)
				body.Add("items[][type]", string(item.Type))
				body.Add("items[][amount]", strconv.FormatInt(item.Amount, 10))
				if item.Currency != "" {
					body.Add("items[][currency]", string(item.Currency))
				}
				if item.Parent != "" {
					body.Add("items[][parent]", item.Parent)
				}
				if item.Quantity != nil {
					body.Add("items[][quantity]", strconv.FormatInt(*item.Quantity, 10))
				}
			}
		}

		body.Add("shipping[address][line1]", params.Shipping.Address.Line1)
		if params.Shipping.Address.Line2 != "" {
			body.Add("shipping[address][line2]", params.Shipping.Address.Line2)
		}
		if params.Shipping.Address.City != "" {
			body.Add("shipping[address][city]", params.Shipping.Address.City)
		}
		if params.Shipping.Address.Country != "" {
			body.Add("shipping[address][country]", params.Shipping.Address.Country)
		}
		if params.Shipping.Address.PostalCode != "" {
			body.Add("shipping[address][postal_code]", params.Shipping.Address.PostalCode)
		}
		if params.Shipping.Address.State != "" {
			body.Add("shipping[address][state]", params.Shipping.Address.State)
		}

		if params.Shipping.Name != "" {
			body.Add("shipping[name]", params.Shipping.Name)
		}
		if params.Shipping.Phone != "" {
			body.Add("shipping[phone]", params.Shipping.Phone)
		}

		params.AppendTo(body)
	}

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

	return p, err
}