paypale/authorization.go

73 lines
2.3 KiB
Go
Raw Normal View History

2019-08-21 15:50:20 +02:00
package paypal
2015-10-15 10:47:36 +02:00
import (
2015-10-30 08:02:32 +01:00
"bytes"
"fmt"
"net/http"
2017-11-23 03:15:11 +01:00
"strconv"
2015-10-15 10:47:36 +02:00
)
// GetAuthorization returns an authorization by ID
2019-03-27 09:27:53 +01:00
// Endpoint: GET /v2/payments/authorization/ID
func (c *Client) GetAuthorization(authID string) (*Authorization, error) {
buf := bytes.NewBuffer([]byte(""))
2019-03-27 09:27:53 +01:00
req, err := http.NewRequest("GET", fmt.Sprintf("%s%s%s", c.APIBase, "/v2/payments/authorization/", authID), buf)
2019-06-16 04:39:08 +02:00
auth := &Authorization{}
if err != nil {
2019-06-16 04:39:08 +02:00
return auth, err
}
2018-08-29 09:52:31 +02:00
err = c.SendWithAuth(req, auth)
return auth, err
}
// CaptureAuthorization captures and process an existing authorization.
// To use this method, the original payment must have Intent set to "authorize"
2019-03-27 09:27:53 +01:00
// Endpoint: POST /v2/payments/authorization/ID/capture
func (c *Client) CaptureAuthorization(authID string, a *Amount, isFinalCapture bool) (*Capture, error) {
2017-11-23 03:15:11 +01:00
isFinalStr := strconv.FormatBool(isFinalCapture)
2018-10-13 23:45:06 +02:00
buf := bytes.NewBuffer([]byte(`{"amount":{"currency":"` + a.Currency + `,"total":"` + a.Total + `"},"is_final_capture":` + isFinalStr + `}`))
2019-03-27 09:27:53 +01:00
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/authorization/"+authID+"/capture"), buf)
2019-06-16 04:39:08 +02:00
capture := &Capture{}
if err != nil {
2019-06-16 04:39:08 +02:00
return capture, err
}
2018-08-29 09:52:31 +02:00
err = c.SendWithAuth(req, capture)
return capture, err
}
// VoidAuthorization voids a previously authorized payment
2019-03-27 09:27:53 +01:00
// Endpoint: POST /v2/payments/authorization/ID/void
func (c *Client) VoidAuthorization(authID string) (*Authorization, error) {
buf := bytes.NewBuffer([]byte(""))
2019-03-27 09:27:53 +01:00
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/authorization/"+authID+"/void"), buf)
2019-06-16 04:39:08 +02:00
auth := &Authorization{}
if err != nil {
2019-06-16 04:39:08 +02:00
return auth, err
}
2018-08-29 09:52:31 +02:00
err = c.SendWithAuth(req, auth)
return auth, err
}
// ReauthorizeAuthorization reauthorize a Paypal account payment.
// PayPal recommends to reauthorize payment after ~3 days
2019-03-27 09:27:53 +01:00
// Endpoint: POST /v2/payments/authorization/ID/reauthorize
func (c *Client) ReauthorizeAuthorization(authID string, a *Amount) (*Authorization, error) {
2018-10-13 23:45:06 +02:00
buf := bytes.NewBuffer([]byte(`{"amount":{"currency":"` + a.Currency + `","total":"` + a.Total + `"}}`))
2019-03-27 09:27:53 +01:00
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/authorization/"+authID+"/reauthorize"), buf)
2019-06-16 04:39:08 +02:00
auth := &Authorization{}
if err != nil {
2019-06-16 04:39:08 +02:00
return auth, err
}
2018-08-29 09:52:31 +02:00
err = c.SendWithAuth(req, auth)
return auth, err
}