Auth capture idempotency and refund idempotency (#143)

* Idempotency for capture auth
This commit is contained in:
kenbolt 2020-04-21 09:23:44 -07:00 committed by GitHub
parent 907cf40be1
commit 7827c1418f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 0 deletions

View File

@ -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" // To use this method, the original payment must have Intent set to "authorize"
// Endpoint: POST /v2/payments/authorizations/ID/capture // Endpoint: POST /v2/payments/authorizations/ID/capture
func (c *Client) CaptureAuthorization(authID string, paymentCaptureRequest *PaymentCaptureRequest) (*PaymentCaptureResponse, error) { 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) req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/authorizations/"+authID+"/capture"), paymentCaptureRequest)
paymentCaptureResponse := &PaymentCaptureResponse{} paymentCaptureResponse := &PaymentCaptureResponse{}
@ -32,6 +43,10 @@ func (c *Client) CaptureAuthorization(authID string, paymentCaptureRequest *Paym
return paymentCaptureResponse, err return paymentCaptureResponse, err
} }
if requestID != "" {
req.Header.Set("PayPal-Request-Id", requestID)
}
err = c.SendWithAuth(req, paymentCaptureResponse) err = c.SendWithAuth(req, paymentCaptureResponse)
return paymentCaptureResponse, err return paymentCaptureResponse, err
} }

View File

@ -113,6 +113,16 @@ func (c *Client) CaptureOrderWithPaypalRequestId(
// RefundCapture - https://developer.paypal.com/docs/api/payments/v2/#captures_refund // RefundCapture - https://developer.paypal.com/docs/api/payments/v2/#captures_refund
// Endpoint: POST /v2/payments/captures/ID/refund // Endpoint: POST /v2/payments/captures/ID/refund
func (c *Client) RefundCapture(captureID string, refundCaptureRequest RefundCaptureRequest) (*RefundResponse, error) { 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{} refund := &RefundResponse{}
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/captures/"+captureID+"/refund"), refundCaptureRequest) 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 return refund, err
} }
if requestID != "" {
req.Header.Set("PayPal-Request-Id", requestID)
}
if err = c.SendWithAuth(req, refund); err != nil { if err = c.SendWithAuth(req, refund); err != nil {
return refund, err return refund, err
} }