From 7827c1418f2b498876e32dd64ea1b0d1d1f49a78 Mon Sep 17 00:00:00 2001 From: kenbolt <54861412+kenbolt@users.noreply.github.com> Date: Tue, 21 Apr 2020 09:23:44 -0700 Subject: [PATCH] Auth capture idempotency and refund idempotency (#143) * Idempotency for capture auth --- authorization.go | 15 +++++++++++++++ order.go | 14 ++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/authorization.go b/authorization.go index 2f9b353..5fc4237 100644 --- a/authorization.go +++ b/authorization.go @@ -25,6 +25,17 @@ 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{} @@ -32,6 +43,10 @@ func (c *Client) CaptureAuthorization(authID string, paymentCaptureRequest *Paym return paymentCaptureResponse, err } + if requestID != "" { + req.Header.Set("PayPal-Request-Id", requestID) + } + err = c.SendWithAuth(req, paymentCaptureResponse) return paymentCaptureResponse, err } diff --git a/order.go b/order.go index b901269..9b732bf 100644 --- a/order.go +++ b/order.go @@ -113,6 +113,16 @@ 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) @@ -120,6 +130,10 @@ func (c *Client) RefundCapture(captureID string, refundCaptureRequest RefundCapt return refund, err } + if requestID != "" { + req.Header.Set("PayPal-Request-Id", requestID) + } + if err = c.SendWithAuth(req, refund); err != nil { return refund, err }