Check a card with Luhn validation and BIN lookup

What the first six digits of a card tell you before you charge it, what they never tell you, and how to get that answer without letting full card numbers anywhere near your servers.

A card number is not one fact. It is three stacked together: a prefix that identifies the issuing bank and product, an account number the bank assigned, and a final check digit that exists so typing mistakes get caught before they cost a network round trip. Each part answers a different question, and each part carries a different amount of risk when it passes through your code.

This lesson is about the first and last of those parts. The check digit tells you whether a number is well-formed. The prefix, called the BIN (bank identification number, or IIN in the ISO standard), tells you who issued the card, whether it is debit or credit, and which country it belongs to. Neither one tells you whether the card has money on it, whether it is stolen, or whether the person typing it owns it. Only an authorization through your payment processor answers those. Keep that boundary in mind and the rest of this lesson is straightforward.

Start with PCI scope, not code

Before you write a line, decide which digits your servers are allowed to see. This is the decision that matters most, and it is easy to get wrong by accident.

PCI DSS is the card industry's security standard. It applies to every system that stores, processes or transmits a full card number (the PAN). A server that receives a PAN in a request body is in scope, even if it never writes the number down. So is the log pipeline that captured that request body, and the error tracker that recorded it when something threw. Scope spreads along every path the number travels.

Modern checkouts avoid this by never letting the PAN reach the merchant at all. The card form is hosted by the processor (an iframe or a hosted page), the browser sends the number straight to the processor, and your server receives a token. If that is your setup, keep it. Nothing in this lesson is worth breaking it.

What you can still have, without a PAN:

  • The BIN. The first six or eight digits. Processors expose it on the token or payment method object, and card-form libraries can hand it to you as the user types. PCI DSS treats the BIN on its own as data you can display and keep, which is why receipts show it.
  • The last four. For display and for support conversations.
  • The brand and length, which come from the BIN.

That is enough to run a BIN lookup, and a BIN lookup is where most of the useful signal lives.

Check yourself

Your checkout uses the processor's hosted card fields, so your server only ever receives a token. A teammate wants to add a server route that accepts the full card number so you can call a validation API on it. What is the main cost of that change?

Answer the check above to continue.

Run every example on your own key. Free plan, no card.

Scaling up?

Volume pricing, custom SLAs, and dedicated support for high-traffic teams.

Contact sales