Test data only. Local fixtures do not issue cards, verify accounts or provide payment authorization. Use them only in authorized tests. Why?
5 test cards
- Generated fields are not uploaded
- Randomly generated in your browser
- Every number passes the Luhn check
Keep the checksum result separate from the form decision
A checksum result is a fact about a digit string. What the form does with that result is
a configured policy. If those two are stored in one valid flag, changing a validation
option can make a failed checksum appear to have passed. This exercise preserves both
results so a test can see exactly what changed.
The generator produces 16- or 19-digit strings starting with 62 or 81, and every
generated number passes Luhn. It also supplies a three-digit synthetic security code.
The fixed download includes deliberately failing checksum variants that the generator
itself will not produce.
Select a policy explicitly
Braintree’s card-validator documentation
documents luhnValidateUnionPay, which enables Luhn validation for UnionPay rather than
using the library’s default exception. That is a documented library behavior, not evidence
that every checkout should ignore checksum failures. Match the requirements of the provider
and the library version used in your integration.
The downloadable example has two named local test profiles:
require-luhn asks for a correction on failure, while observe-only records the failure
without using it as a local gate. Missing or misspelled policies return policy-required.
The observe-only case is a comparison fixture, not a recommendation to bypass production
validation or to submit generated numbers to a live gateway.
| Number and policy | Checksum result | Local decision |
|---|---|---|
6212345678901232, require-luhn | Pass | local-checks-complete |
6212345678901233, require-luhn | Fail | correct-number |
| Same failing number, observe-only | Still fail | local-checks-complete |
| Either number, no policy | Pass or fail remains visible | policy-required |
A 65 prefix with observe-only | Not checked | outside-profile |
The failing pair differs only in its last digit. The example never repairs that digit automatically. If you want to understand why the result changes, the Luhn walkthrough explains the arithmetic independently.
Run the checksum and policy comparison
Keep the policy fixtures and the executable UnionPay checks in one directory:
node --test unionpay-policy.test.mjs
The fixture includes both prefixes at 16 and 19 digits, the altered check digit, missing configuration, an unknown policy name, and values outside the target shape. The runner also changes each digit of a passing number and checks that every single-digit substitution fails Luhn.
function passesLuhn(number) {
let sum = 0;
for (let i = number.length - 1, double = false; i >= 0; i -= 1, double = !double) {
let digit = Number(number[i]);
if (double) digit = digit * 2 > 9 ? digit * 2 - 9 : digit * 2;
sum += digit;
}
return sum % 10 === 0;
}
function assessUnionPay(number, policy) {
if (typeof number !== 'string' || !/^(62|81)(?:\d{14}|\d{17})$/.test(number)) {
return { checksum: 'not-checked', decision: 'outside-profile' };
}
const checksum = passesLuhn(number) ? 'pass' : 'fail';
if (policy !== 'require-luhn' && policy !== 'observe-only') {
return { checksum, decision: 'policy-required' };
}
return {
checksum,
decision: checksum === 'fail' && policy === 'require-luhn'
? 'correct-number' : 'local-checks-complete'
};
}The function accepts unformatted strings only. Its shape gate matches this generator’s
62/81 and 16/19-digit profile. It does not cover all UnionPay products or perform a
BIN lookup. In particular, the 17-digit control is outside this test profile; the result
must not be presented as a universal network restriction.
Apply the comparison without hiding an error
Replace assessUnionPay with an adapter around your own configured validation layer and
keep separate assertions for the checksum and the form’s action. Test the same malformed
input under both local profiles so an option change cannot rewrite the checksum field.
Keep unknown configuration distinct from an intentional observe-only decision.
In the UI, check that feedback points to the card-number field and explains what the user
can correct. A local decision of local-checks-complete must not bypass expiry, security
code, merchant acceptance or other required checks. None of these fixture decisions is an
authorization result.
If a 62 value is labeled Maestro, diagnose that separately with the
overlapping-rule exercise. If a 19-digit value changes during
serialization, use the Visa string-preservation tests. These
failures should not be hidden by changing the checksum policy.
Use the gateway sandbox reference for provider-approved test inputs and response behavior. The local failing pair here only exercises the validation contract.