Ejemplo n.º 1
0
func (c Client) New(params *stripe.ChargeParams) (*stripe.Charge, error) {
	body := &url.Values{
		"amount":   {strconv.FormatUint(params.Amount, 10)},
		"currency": {string(params.Currency)},
	}

	if params.Source == nil && len(params.Customer) == 0 {
		err := errors.New("Invalid charge params: either customer or a source must be set")
		return nil, err
	}

	if params.Source != nil {
		params.Source.AppendDetails(body, true)
	}

	if len(params.Customer) > 0 {
		body.Add("customer", params.Customer)
	}

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

	if len(params.Statement) > 0 {
		body.Add("statement_descriptor", params.Statement)
	}

	if len(params.Email) > 0 {
		body.Add("receipt_email", params.Email)
	}

	if len(params.Dest) > 0 {
		body.Add("destination", params.Dest)
	}

	body.Add("capture", strconv.FormatBool(!params.NoCapture))

	token := c.Key
	if params.Fee > 0 {
		body.Add("application_fee", strconv.FormatUint(params.Fee, 10))
	}

	if params.Shipping != nil {
		params.Shipping.AppendDetails(body)
	}

	params.AppendTo(body)

	charge := &stripe.Charge{}
	err := c.B.Call("POST", "/charges", token, body, &params.Params, charge)

	return charge, err
}
Ejemplo n.º 2
0
func (c Client) Get(id string, params *stripe.ChargeParams) (*stripe.Charge, error) {
	var body *url.Values
	var commonParams *stripe.Params

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

	charge := &stripe.Charge{}
	err := c.B.Call("GET", "/charges/"+id, c.Key, body, commonParams, charge)

	return charge, err
}
Ejemplo n.º 3
0
func (c Client) Update(id string, params *stripe.ChargeParams) (*stripe.Charge, error) {
	var body *url.Values
	var commonParams *stripe.Params

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

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

		if len(params.Fraud) > 0 {
			body.Add("fraud_details[user_report]", string(params.Fraud))
		}

		params.AppendTo(body)
	}

	charge := &stripe.Charge{}
	err := c.B.Call("POST", "/charges/"+id, c.Key, body, commonParams, charge)

	return charge, err
}