paypal/payment.go

103 lines
2.7 KiB
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-10-23 04:29:36 +02:00
2015-11-25 11:30:25 +01:00
// ListPaymentsResp slice of payments
type ListPaymentsResp struct {
Payments []Payment `json:"payments"`
}
2015-11-02 04:34:16 +01:00
// CreateDirectPaypalPayment sends request with payment
2015-11-24 10:03:13 +01:00
func (c *Client) CreateDirectPaypalPayment(amount Amount, redirectURI string, cancelURI string, description string) (*PaymentResponse, error) {
2015-11-16 06:11:27 +01:00
buf := bytes.NewBuffer([]byte("{\"intent\":\"sale\",\"payer\":{\"payment_method\":\"paypal\"}," +
2015-11-25 11:30:25 +01:00
"\"transactions\":[{\"amount\":{\"total\":\"" + amount.Total +
2015-11-24 10:03:13 +01:00
"\",\"currency\":\"" + amount.Currency + "\"},\"description\":\"" + description + "\"}],\"redirect_urls\":{\"return_url\":\"" +
2015-11-20 09:01:11 +01:00
redirectURI + "\",\"cancel_url\":\"" + cancelURI + "\"}}"))
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{}
2015-11-20 03:57:45 +01:00
err = c.SendWithAuth(req, &p)
2015-11-25 11:30:25 +01:00
if err != nil {
return &p, err
}
2015-11-02 04:34:16 +01:00
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
}
2015-11-16 06:11:27 +01:00
// ExecuteApprovedPayment executes approved payment
func (c *Client) ExecuteApprovedPayment(paymentID string, payerID string) (*ExecuteResponse, error) {
buf := bytes.NewBuffer([]byte("{\"payer_id\":\"" + payerID + "\"}"))
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/payment/"+paymentID+"/execute"), buf)
if err != nil {
return &ExecuteResponse{}, err
}
req.SetBasicAuth(c.ClientID, c.Secret)
req.Header.Set("Authorization", "Bearer "+c.Token.Token)
e := ExecuteResponse{}
2015-11-20 03:57:45 +01:00
err = c.SendWithAuth(req, &e)
2015-11-25 11:30:25 +01:00
if err != nil {
return &e, err
}
2015-11-16 06:11:27 +01:00
if e.ID == "" {
return &e, errors.New("Unable to execute payment with paymentID=" + paymentID)
}
return &e, err
}
2015-11-25 11:30:25 +01:00
// GetPayment gets a payment from PayPal
func (c *Client) GetPayment(paymentID string) (*Payment, error) {
p := Payment{}
req, err := http.NewRequest("GET", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/payment/"+paymentID), nil)
if err != nil {
return &p, err
}
err = c.SendWithAuth(req, &p)
if err != nil {
return &p, err
}
if p.ID == "" {
return &p, errors.New("Unable to get payment with paymentID=" + paymentID)
}
return &p, nil
}
// GetPayments retrieve payments resources from Paypal
func (c *Client) GetPayments() ([]Payment, error) {
var p ListPaymentsResp
req, err := http.NewRequest("GET", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/payment/"), nil)
if err != nil {
return p.Payments, err
}
err = c.SendWithAuth(req, &p)
if err != nil {
return p.Payments, err
}
return p.Payments, nil
}