Search code examples
node.jstestingpaypalcheckoutnode-fetch

PayPal-Mock-Response not working (nodejs, paypal negative testing)


I'm working on the code to cover negative scenarios like card declined and similar. According to the documentation the only way to do it for Orders is to use the header PayPal-Mock-Response https://developer.paypal.com/tools/sandbox/negative-testing/request-headers/

However, it doesn't work for me, I'm getting the 403 error with an empty response every time I try to add the "PayPal-Mock-Response" header with any error, can't get it working at all

Example, request:

POST https://api-m.sandbox.paypal.com/v2/checkout/orders
params:
{
    "method": "post",
    "headers": {
        "Content-Type": "application/json",
        "Authorization": "Bearer A21[reducted]",
        "PayPal-Mock-Response": "{\"mock_application_codes\":\"DUPLICATE_INVOICE_ID\"}"
    },
    "body": "{\"intent\":\"CAPTURE\",\"purchase_units\":[{\"custom_id\":89534,\"description\":\"my item name\",\"reference_id\":648,\"amount\":{\"currency_code\":\"USD\",\"value\":\"5.01\"}}]}"
}

Response:

{
  "statusCode": 403,
  "responseText": ""
}

I'm using nodejs, node-fetch package, the auth token is correct as I've got positive scenario working, the 403 error is only being thrown when I add the "PayPal-Mock-Response" header.

what am I doing wrong or is there any other way to make a failed payment on sandbox?


Solution

  • DUPLICATE_INVOICE_ID is not a valid error to mock for a /v2/checkout/orders creation API call...

    POST to https://api-m.sandbox.paypal.com/v2/checkout/orders

    It is, however, a valid error for a v2 orders capture API call:

    POST to https://api.sandbox.paypal.com/v2/checkout/orders/:id/capture

    And this example is what is actually given in the documentation you reference.

    --

    Conceptually, understand that the invoice ID is only checked at capture time. You can create as many orders (for use in checkout attempts) as you want with a same invoice ID, there is no issue with duplication at that the point of doing new creates with that same ID. A duplicate invoice API error is when trying to capture (create a transaction with) an ID that has already resulted in a successful transaction being created for that account. The point is to prevent duplicate payments, not prevent repeat checkout attempts that haven't yet resulted in a payment being created.

    --

    If you want to prevent new order IDs being created for repeat attempts, set a PayPal-Request-Id HTTP header in the create call (e.g. its value could just be the invoice ID if this serves your use case). Repeating the calls with that same PayPal-Request-Id header will return the same information as before, without repeating the operation.