github.com/plutov/paypal/v4@v4.7.1/authorization.go (about) 1 package paypal 2 3 import ( 4 "bytes" 5 "context" 6 "fmt" 7 "net/http" 8 ) 9 10 // GetAuthorization returns an authorization by ID 11 // Endpoint: GET /v2/payments/authorizations/ID 12 func (c *Client) GetAuthorization(ctx context.Context, authID string) (*Authorization, error) { 13 buf := bytes.NewBuffer([]byte("")) 14 req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s%s%s", c.APIBase, "/v2/payments/authorizations/", authID), buf) 15 auth := &Authorization{} 16 17 if err != nil { 18 return auth, err 19 } 20 21 err = c.SendWithAuth(req, auth) 22 return auth, err 23 } 24 25 // CaptureAuthorization captures and process an existing authorization. 26 // To use this method, the original payment must have Intent set to "authorize" 27 // Endpoint: POST /v2/payments/authorizations/ID/capture 28 func (c *Client) CaptureAuthorization(ctx context.Context, authID string, paymentCaptureRequest *PaymentCaptureRequest) (*PaymentCaptureResponse, error) { 29 return c.CaptureAuthorizationWithPaypalRequestId(ctx, authID, paymentCaptureRequest, "") 30 } 31 32 // CaptureAuthorization captures and process an existing authorization with idempotency. 33 // To use this method, the original payment must have Intent set to "authorize" 34 // Endpoint: POST /v2/payments/authorizations/ID/capture 35 func (c *Client) CaptureAuthorizationWithPaypalRequestId(ctx context.Context, 36 authID string, 37 paymentCaptureRequest *PaymentCaptureRequest, 38 requestID string, 39 ) (*PaymentCaptureResponse, error) { 40 req, err := c.NewRequest(ctx, "POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/authorizations/"+authID+"/capture"), paymentCaptureRequest) 41 paymentCaptureResponse := &PaymentCaptureResponse{} 42 43 if err != nil { 44 return paymentCaptureResponse, err 45 } 46 47 if requestID != "" { 48 req.Header.Set("PayPal-Request-Id", requestID) 49 } 50 51 err = c.SendWithAuth(req, paymentCaptureResponse) 52 return paymentCaptureResponse, err 53 } 54 55 // VoidAuthorization voids a previously authorized payment 56 // Endpoint: POST /v2/payments/authorizations/ID/void 57 func (c *Client) VoidAuthorization(ctx context.Context, authID string) (*Authorization, error) { 58 buf := bytes.NewBuffer([]byte("")) 59 req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/authorizations/"+authID+"/void"), buf) 60 auth := &Authorization{} 61 62 if err != nil { 63 return auth, err 64 } 65 66 err = c.SendWithAuth(req, auth) 67 return auth, err 68 } 69 70 // ReauthorizeAuthorization reauthorize a Paypal account payment. 71 // PayPal recommends reauthorizing payment after ~3 days 72 // Endpoint: POST /v2/payments/authorizations/ID/reauthorize 73 func (c *Client) ReauthorizeAuthorization(ctx context.Context, authID string, a *Amount) (*Authorization, error) { 74 buf := bytes.NewBuffer([]byte(`{"amount":{"currency_code":"` + a.Currency + `","value":"` + a.Total + `"}}`)) 75 req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/authorizations/"+authID+"/reauthorize"), buf) 76 auth := &Authorization{} 77 78 if err != nil { 79 return auth, err 80 } 81 82 err = c.SendWithAuth(req, auth) 83 return auth, err 84 }