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

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

	coupon := &stripe.Coupon{}

	err := c.B.Call("GET", "/coupons/"+id, c.Key, body, commonParams, coupon)

	return coupon, err
}
Exemplo n.º 2
0
func (c Client) New(params *stripe.CouponParams) (*stripe.Coupon, error) {
	// TODO: this doesn't check that the params are not nil.

	body := &url.Values{
		"duration": {string(params.Duration)},
	}

	if len(params.ID) > 0 {
		body.Add("id", params.ID)
	}

	if params.Percent > 0 {
		body.Add("percent_off", strconv.FormatUint(params.Percent, 10))
	} else if params.Amount > 0 {
		body.Add("amount_off", strconv.FormatUint(params.Amount, 10))
		body.Add("currency", string(params.Currency))
	} else {
		err := errors.New("Invalid coupon params: either amount and currency or percent need to be set")
		return nil, err
	}

	if params.Duration == Repeating {
		body.Add("duration_in_months", strconv.FormatUint(params.DurationPeriod, 10))
	}

	if params.Redemptions > 0 {
		body.Add("max_redemptions", strconv.FormatUint(params.Redemptions, 10))
	}

	if params.RedeemBy > 0 {
		body.Add("redeem_by", strconv.FormatInt(params.RedeemBy, 10))
	}

	params.AppendTo(body)

	coupon := &stripe.Coupon{}

	err := c.B.Call("POST", "/coupons", c.Key, body, &params.Params, coupon)

	return coupon, err
}