paypale/payment.go

55 lines
1.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-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-20 09:01:11 +01:00
func (c *Client) CreateDirectPaypalPayment(amount Amount, redirectURI string, cancelURI string) (*PaymentResponse, error) {
2015-11-16 06:11:27 +01:00
buf := bytes.NewBuffer([]byte("{\"intent\":\"sale\",\"payer\":{\"payment_method\":\"paypal\"}," +
"\"transactions\":[{\"amount\":{\"total\":\"" + strconv.FormatFloat(amount.Total, 'f', 2, 64) +
2015-11-20 09:01:11 +01:00
"\",\"currency\":\"" + amount.Currency + "\"},\"description\":\"logpacker.com\"}],\"redirect_urls\":{\"return_url\":\"" +
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-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-16 06:11:27 +01:00
if e.ID == "" {
return &e, errors.New("Unable to execute payment with paymentID=" + paymentID)
}
return &e, err
}