Testing Square payments in the sandbox with test card nonces
Square's sandbox lets you test payments end to end with no cards and no money: instead of collecting a card in a web form, you pass a test nonce that stands for "a card that works" or "a card that's declined". That makes the whole payment flow testable from an API collection.
Step 1: Sandbox app and environment
In the Square Developer Console, open your application, switch to Sandbox, and copy the sandbox access token.
| Variable | Value |
|---|---|
sqBase | https://connect.squareupsandbox.com/v2 |
sqToken | the sandbox access token, marked as secret |
sqVersion | the API version your code uses, such as 2025-01-23 |
Authorization: Bearer token with {{sqToken}}. Add Square-Version: {{sqVersion}} on every request with a collection pre-request script:
pm.request.headers.set('Square-Version', pm.environment.get('sqVersion'));Without the header, Square uses the application's default version, which changes when someone clicks "upgrade" in the console. Pinning it makes the tests match your code.
Step 2: A location
GET {{sqBase}}/locationsconst loc = pm.response.json().locations.find((l) => l.status === 'ACTIVE');
pm.test('an active location', () => pm.expect(loc).to.exist);
pm.environment.set('locationId', loc.id);Step 3: Take a payment
POST {{sqBase}}/payments
Content-Type: application/json
{
"idempotency_key": "{{$guid}}",
"source_id": "cnon:card-nonce-ok",
"amount_money": {"amount": 1050, "currency": "USD"},
"location_id": "{{locationId}}",
"reference_id": "api-test-order-1"
}const p = pm.response.json().payment;
pm.test('payment completed', () => {
pm.expect(p.status).to.equal('COMPLETED');
pm.expect(p.amount_money.amount).to.equal(1050);
});
pm.environment.set('paymentId', p.id);Step 4: Declines
Duplicate the request with a declining nonce:
{"idempotency_key": "{{$guid}}", "source_id": "cnon:card-nonce-declined", "amount_money": {"amount": 1050, "currency": "USD"}, "location_id": "{{locationId}}"}pm.test('declined with a reason', () => {
pm.expect(pm.response.code).to.equal(400);
const err = pm.response.json().errors[0];
pm.expect(err.category).to.equal('PAYMENT_METHOD_ERROR');
pm.expect(err.code).to.be.a('string');
});Square has more test nonces for specific failures (bad CVV, bad postal code, insufficient funds) in its sandbox docs. One request per decline reason your checkout shows a message for is a good set.
Step 5: Idempotency keys
Every write needs an idempotency_key. Send the same payment twice with the same key and Square returns the original payment instead of charging again. Reuse the key with a different body and Square refuses it with an IDEMPOTENCY_KEY_REUSED error. Both are worth a test, because they're how your checkout survives a network retry. See testing idempotency keys.
Step 6: Refund as cleanup
POST {{sqBase}}/refunds
Content-Type: application/json
{"idempotency_key": "{{$guid}}", "payment_id": "{{paymentId}}", "amount_money": {"amount": 1050, "currency": "USD"}}Refunds start PENDING and complete shortly after in the sandbox.