Skip to content

Commit

Permalink
Auth capture idempotency and refund idempotency (#143)
Browse files Browse the repository at this point in the history
* Idempotency for capture auth
  • Loading branch information
kenbolt authored Apr 21, 2020
1 parent 907cf40 commit 7827c14
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
15 changes: 15 additions & 0 deletions authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,28 @@ func (c *Client) GetAuthorization(authID string) (*Authorization, error) {
// To use this method, the original payment must have Intent set to "authorize"
// Endpoint: POST /v2/payments/authorizations/ID/capture
func (c *Client) CaptureAuthorization(authID string, paymentCaptureRequest *PaymentCaptureRequest) (*PaymentCaptureResponse, error) {
return c.CaptureAuthorizationWithPaypalRequestId(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
func (c *Client) CaptureAuthorizationWithPaypalRequestId(
authID string,
paymentCaptureRequest *PaymentCaptureRequest,
requestID string,
) (*PaymentCaptureResponse, error) {
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/authorizations/"+authID+"/capture"), paymentCaptureRequest)
paymentCaptureResponse := &PaymentCaptureResponse{}

if err != nil {
return paymentCaptureResponse, err
}

if requestID != "" {
req.Header.Set("PayPal-Request-Id", requestID)
}

err = c.SendWithAuth(req, paymentCaptureResponse)
return paymentCaptureResponse, err
}
Expand Down
14 changes: 14 additions & 0 deletions order.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,27 @@ func (c *Client) CaptureOrderWithPaypalRequestId(
// RefundCapture - https://developer.paypal.com/docs/api/payments/v2/#captures_refund
// Endpoint: POST /v2/payments/captures/ID/refund
func (c *Client) RefundCapture(captureID string, refundCaptureRequest RefundCaptureRequest) (*RefundResponse, error) {
return c.RefundCaptureWithPaypalRequestId(captureID, refundCaptureRequest, "")
}

// RefundCapture with idempotency - https://developer.paypal.com/docs/api/payments/v2/#captures_refund
// Endpoint: POST /v2/payments/captures/ID/refund
func (c *Client) RefundCaptureWithPaypalRequestId(
captureID string,
refundCaptureRequest RefundCaptureRequest,
requestID string,
) (*RefundResponse, error) {
refund := &RefundResponse{}

req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/captures/"+captureID+"/refund"), refundCaptureRequest)
if err != nil {
return refund, err
}

if requestID != "" {
req.Header.Set("PayPal-Request-Id", requestID)
}

if err = c.SendWithAuth(req, refund); err != nil {
return refund, err
}
Expand Down

0 comments on commit 7827c14

Please sign in to comment.