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"
|
2015-10-15 10:47:36 +02:00
|
|
|
)
|
|
|
|
|
2015-12-01 05:35:25 +01:00
|
|
|
// GetAuthorization returns an authorization by ID
|
2019-03-27 09:27:53 +01:00
|
|
|
// Endpoint: GET /v2/payments/authorization/ID
|
2015-12-01 05:35:25 +01:00
|
|
|
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{}
|
|
|
|
|
2015-12-01 05:35:25 +01:00
|
|
|
if err != nil {
|
2019-06-16 04:39:08 +02:00
|
|
|
return auth, err
|
2015-12-01 05:35:25 +01:00
|
|
|
}
|
|
|
|
|
2018-08-29 09:52:31 +02:00
|
|
|
err = c.SendWithAuth(req, auth)
|
|
|
|
return auth, err
|
2015-12-01 05:35:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CaptureAuthorization captures and process an existing authorization.
|
|
|
|
// To use this method, the original payment must have Intent set to "authorize"
|
2019-09-06 17:00:21 +02:00
|
|
|
// Endpoint: POST /v2/payments/authorizations/ID/capture
|
|
|
|
func (c *Client) CaptureAuthorization(authID string, paymentCaptureRequest *PaymentCaptureRequest) (*PaymentCaptureResponse, error) {
|
|
|
|
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/authorizations/"+authID+"/capture"), paymentCaptureRequest)
|
|
|
|
paymentCaptureResponse := &PaymentCaptureResponse{}
|
2019-06-16 04:39:08 +02:00
|
|
|
|
2015-12-01 05:35:25 +01:00
|
|
|
if err != nil {
|
2019-09-06 17:00:21 +02:00
|
|
|
return paymentCaptureResponse, err
|
2015-12-01 05:35:25 +01:00
|
|
|
}
|
|
|
|
|
2019-09-06 17:00:21 +02:00
|
|
|
err = c.SendWithAuth(req, paymentCaptureResponse)
|
|
|
|
return paymentCaptureResponse, err
|
2015-12-01 05:35:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// VoidAuthorization voids a previously authorized payment
|
2019-03-27 09:27:53 +01:00
|
|
|
// Endpoint: POST /v2/payments/authorization/ID/void
|
2015-12-01 05:35:25 +01:00
|
|
|
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{}
|
|
|
|
|
2015-12-01 05:35:25 +01:00
|
|
|
if err != nil {
|
2019-06-16 04:39:08 +02:00
|
|
|
return auth, err
|
2015-12-01 05:35:25 +01:00
|
|
|
}
|
|
|
|
|
2018-08-29 09:52:31 +02:00
|
|
|
err = c.SendWithAuth(req, auth)
|
|
|
|
return auth, err
|
2015-12-01 05:35:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2015-12-01 05:35:25 +01:00
|
|
|
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{}
|
|
|
|
|
2015-12-01 05:35:25 +01:00
|
|
|
if err != nil {
|
2019-06-16 04:39:08 +02:00
|
|
|
return auth, err
|
2015-12-01 05:35:25 +01:00
|
|
|
}
|
|
|
|
|
2018-08-29 09:52:31 +02:00
|
|
|
err = c.SendWithAuth(req, auth)
|
|
|
|
return auth, err
|
2015-12-01 05:35:25 +01:00
|
|
|
}
|