paypale/payment.go

133 lines
3.9 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-12-29 10:21:11 +01:00
// CreatePaymentResp contains Payment Info and Links slice
type CreatePaymentResp struct {
*Payment
Links []Links `json:"links"`
}
2015-12-29 10:21:11 +01:00
// CreateDirectPaypalPayment sends request to create a payment with payment_method=paypal
// CreatePayment is more common function for any kind of payment
2015-12-29 10:21:11 +01:00
// Endpoint: POST /v1/payments/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
// CreatePayment creates a payment in Paypal
2015-12-29 10:21:11 +01:00
// Depending on the payment_method and the funding_instrument, you can use the payment resource for direct credit card payments, stored credit card payments, or PayPal account payments.
// Endpoint: POST /v1/payments/payment
func (c *Client) CreatePayment(p Payment) (*CreatePaymentResp, error) {
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/payment"), p)
if err != nil {
return &CreatePaymentResp{}, err
}
response := &CreatePaymentResp{}
err = c.SendWithAuth(req, response)
if err != nil {
return response, err
}
return response, nil
}
2015-12-29 10:21:11 +01:00
// ExecuteApprovedPayment - Use this call to execute (complete) a PayPal payment that has been approved by the payer. You can optionally update transaction information when executing the payment by passing in one or more transactions.
// Endpoint: POST /v1/payments/payment/paymentID/execute
2015-11-16 06:11:27 +01:00
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
2015-12-29 10:21:11 +01:00
// Endpoint: GET /v1/payments/payment/ID
2015-11-25 11:30:25 +01:00
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
2015-12-29 10:21:11 +01:00
// Endpoint: GET /v1/payments/payment/
2015-11-25 11:30:25 +01:00
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
}