paypale/authorization.go

85 lines
3.0 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"
2021-01-03 10:28:52 +01:00
"context"
2015-10-30 08:02:32 +01:00
"fmt"
"net/http"
2015-10-15 10:47:36 +02:00
)
// GetAuthorization returns an authorization by ID
// Endpoint: GET /v2/payments/authorizations/ID
2021-01-03 10:28:52 +01:00
func (c *Client) GetAuthorization(ctx context.Context, authID string) (*Authorization, error) {
buf := bytes.NewBuffer([]byte(""))
2021-01-03 10:28:52 +01:00
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s%s%s", c.APIBase, "/v2/payments/authorizations/", 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-09-06 17:00:21 +02:00
// Endpoint: POST /v2/payments/authorizations/ID/capture
2021-01-03 10:28:52 +01:00
func (c *Client) CaptureAuthorization(ctx context.Context, authID string, paymentCaptureRequest *PaymentCaptureRequest) (*PaymentCaptureResponse, error) {
return c.CaptureAuthorizationWithPaypalRequestId(ctx, authID, paymentCaptureRequest, "")
}
// CaptureAuthorization captures and process an existing authorization with idempotency.
// To use this method, the original payment must have Intent set to "authorize"
// Endpoint: POST /v2/payments/authorizations/ID/capture
2021-01-03 10:28:52 +01:00
func (c *Client) CaptureAuthorizationWithPaypalRequestId(ctx context.Context,
authID string,
paymentCaptureRequest *PaymentCaptureRequest,
requestID string,
) (*PaymentCaptureResponse, error) {
2021-01-03 10:28:52 +01:00
req, err := c.NewRequest(ctx, "POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/authorizations/"+authID+"/capture"), paymentCaptureRequest)
2019-09-06 17:00:21 +02:00
paymentCaptureResponse := &PaymentCaptureResponse{}
2019-06-16 04:39:08 +02:00
if err != nil {
2019-09-06 17:00:21 +02:00
return paymentCaptureResponse, err
}
if requestID != "" {
req.Header.Set("PayPal-Request-Id", requestID)
}
2019-09-06 17:00:21 +02:00
err = c.SendWithAuth(req, paymentCaptureResponse)
return paymentCaptureResponse, err
}
// VoidAuthorization voids a previously authorized payment
// Endpoint: POST /v2/payments/authorizations/ID/void
2021-01-03 10:28:52 +01:00
func (c *Client) VoidAuthorization(ctx context.Context, authID string) (*Authorization, error) {
buf := bytes.NewBuffer([]byte(""))
2021-01-03 10:28:52 +01:00
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/authorizations/"+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
// Endpoint: POST /v2/payments/authorizations/ID/reauthorize
2021-01-03 10:28:52 +01:00
func (c *Client) ReauthorizeAuthorization(ctx context.Context, 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 + `"}}`))
2021-01-03 10:28:52 +01:00
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/authorizations/"+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
}