Added PayPal-Mock-Response for capture (#232)

This commit is contained in:
Florian Kinder 2022-03-05 08:16:25 +01:00 committed by GitHub
parent ca6845e257
commit d381642422
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package paypal
import (
"context"
"encoding/json"
"fmt"
)
@ -104,7 +105,7 @@ func (c *Client) AuthorizeOrder(ctx context.Context, orderID string, authorizeOr
// CaptureOrder - https://developer.paypal.com/docs/api/orders/v2/#orders_capture
// Endpoint: POST /v2/checkout/orders/ID/capture
func (c *Client) CaptureOrder(ctx context.Context, orderID string, captureOrderRequest CaptureOrderRequest) (*CaptureOrderResponse, error) {
return c.CaptureOrderWithPaypalRequestId(ctx, orderID, captureOrderRequest, "")
return c.CaptureOrderWithPaypalRequestId(ctx, orderID, captureOrderRequest, "", nil)
}
// CaptureOrder with idempotency - https://developer.paypal.com/docs/api/orders/v2/#orders_capture
@ -114,6 +115,7 @@ func (c *Client) CaptureOrderWithPaypalRequestId(ctx context.Context,
orderID string,
captureOrderRequest CaptureOrderRequest,
requestID string,
mockResponse *CaptureOrderMockResponse,
) (*CaptureOrderResponse, error) {
capture := &CaptureOrderResponse{}
@ -127,6 +129,15 @@ func (c *Client) CaptureOrderWithPaypalRequestId(ctx context.Context,
req.Header.Set("PayPal-Request-Id", requestID)
}
if mockResponse != nil {
mock, err := json.Marshal(mockResponse)
if err != nil {
return nil, err
}
req.Header.Set("PayPal-Mock-Response", string(mock))
}
if err = c.SendWithAuth(req, capture); err != nil {
return capture, err
}

View File

@ -299,6 +299,11 @@ type (
PaymentSource *PaymentSource `json:"payment_source"`
}
// CaptureOrderMockResponse - https://developer.paypal.com/docs/api-basics/sandbox/request-headers/#test-api-error-handling-routines
CaptureOrderMockResponse struct {
MockApplicationCodes string `json:"mock_application_codes"`
}
// RefundOrderRequest - https://developer.paypal.com/docs/api/payments/v2/#captures_refund
RefundCaptureRequest struct {
Amount *Money `json:"amount,omitempty"`