paypale/payment.go

31 lines
877 B
Go
Raw Normal View History

2015-10-23 04:29:36 +02:00
package paypalsdk
2015-11-02 04:34:16 +01:00
import (
"bytes"
2015-11-02 10:39:07 +01:00
"errors"
2015-11-02 04:34:16 +01:00
"fmt"
"net/http"
2015-11-02 10:39:07 +01:00
"strconv"
2015-11-02 04:34:16 +01:00
)
2015-10-23 04:29:36 +02:00
2015-11-02 04:34:16 +01:00
// CreateDirectPaypalPayment sends request with payment
2015-11-02 10:39:07 +01:00
func (c *Client) CreateDirectPaypalPayment(amount Amount) (*PaymentResponse, error) {
buf := bytes.NewBuffer([]byte("{\"intent\":\"sale\",\"payer\":{\"payment_method\":\"paypal\"},\"transactions\":[{\"amount\":{\"total\":\"" + strconv.FormatFloat(amount.Total, 'f', 2, 64) + "\",\"currency\":\"" + amount.Currency + "\"}}]}"))
2015-11-02 04:34:16 +01:00
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/payment"), buf)
if err != nil {
return &PaymentResponse{}, err
}
req.SetBasicAuth(c.ClientID, c.Secret)
req.Header.Set("Authorization", "Bearer "+c.Token.Token)
p := PaymentResponse{}
err = c.Send(req, &p)
2015-11-02 10:39:07 +01:00
if p.ID == "" {
return &p, errors.New("Unable to create payment with this access token")
}
return &p, err
2015-10-23 04:29:36 +02:00
}