Testing PayPal's Orders API in the sandbox
PayPal's REST APIs have a full sandbox with its own accounts, money and hosts, so you can test the entire checkout without a real payment. The flow has one step an API test can't do by itself (the buyer approving the payment), and a few details worth pinning down with assertions.
Step 1: Sandbox app and environment
In the PayPal Developer Dashboard, switch to Sandbox, create an app, and copy its client id and secret. The dashboard also gives you a sandbox personal (buyer) account.
| Variable | Value |
|---|---|
ppBase | https://api-m.sandbox.paypal.com |
ppClientId | the sandbox client id |
ppSecret | the sandbox secret, marked as secret |
On the collection's Authorization tab, choose OAuth 2.0 with the Client credentials grant, token URL {{ppBase}}/v1/oauth2/token, and the client id and secret. Get new access token fetches it; requests in the collection then send it as a Bearer token. Sandbox tokens last several hours.
Step 2: Create an order
POST {{ppBase}}/v2/checkout/orders
Content-Type: application/json
PayPal-Request-Id: order-{{$guid}}
{
"intent": "CAPTURE",
"purchase_units": [{
"reference_id": "test-order-1",
"amount": {"currency_code": "EUR", "value": "49.90"}
}]
}const order = pm.response.json();
pm.test('order created', () => {
pm.expect(pm.response.code).to.equal(201);
pm.expect(order.status).to.equal('CREATED');
});
const approve = order.links.find((l) => l.rel === 'approve' || l.rel === 'payer-action');
console.log('Approve as the sandbox buyer:', approve.href);
pm.environment.set('orderId', order.id);Amounts are decimal strings in the currency's precision: "49.90" for EUR, "5000" for JPY. A number, or "49.9", answers 422. See testing money and decimal precision.
Step 3: Approve, then capture
Open the approval link, sign in as the sandbox buyer and approve. Then:
POST {{ppBase}}/v2/checkout/orders/{{orderId}}/capture
Content-Type: application/json
PayPal-Request-Id: capture-{{orderId}}const c = pm.response.json();
pm.test('captured', () => {
pm.expect(c.status).to.equal('COMPLETED');
const capture = c.purchase_units[0].payments.captures[0];
pm.expect(capture.amount.value).to.equal('49.90');
});Capturing before approval answers 422 with "issue": "ORDER_NOT_APPROVED" in details. That's a good negative test: duplicate the request and run it on a fresh order.
Step 4: Idempotency
Send the capture again with the same PayPal-Request-Id. PayPal returns the stored result instead of capturing twice. Without the header, a retry after a timeout can charge twice, or fail with ORDER_ALREADY_CAPTURED. Your backend should always send it; this test shows what it does. More in testing idempotency keys.
Step 5: Errors
PayPal errors have a name, a message, a debug_id and details with an issue:
pm.test('validation error explained', () => {
const e = pm.response.json();
pm.expect(pm.response.code).to.equal(422);
pm.expect(e.details[0].issue).to.be.a('string');
console.log('debug_id for PayPal support:', e.debug_id);
});Log debug_id: PayPal support asks for it.