项目作者: ramoona

项目描述 :
Community driven database to get bank info (name, brand color etc.) by bankcard prefix (BIN)
高级语言: JavaScript
项目地址: git://github.com/ramoona/banks-db.git
创建时间: 2016-01-03T14:31:45Z
项目社区:https://github.com/ramoona/banks-db

开源协议:

下载


Build Status
Latest Stable Version
NPM Downloads

Returns bank’s name and brand color by bankcard prefix (BIN).

It is useful on billing pages to use bank’s brand color when user starts to type card number.

It’s a community driven database, so it can potentially contains mistakes. It’s not a problem for UX enhancement,
but you must not use Banks DB in your billing logic.

Demo

Try your card prefix in our demo. Note that only first 6 digits of card number are required.

Usage

PostCSS

With postcss-banks-db and
postcss-contrast you can
generate CSS for each bank:

  1. .billing-form {
  2. transition: background .6s, color .6s;
  3. background: #eee;
  4. }
  5. @banks-db-template {
  6. .billing-form.is-%code% {
  7. background-color: %color%;
  8. color: contrast(%color%);
  9. }
  10. }

And then switch bank’s style in JS:

  1. import banksDB from 'banks-db';
  2. const bank = banksDB(cardNumberField.value);
  3. if ( bank.code ) {
  4. billingForm.className = 'billing-form is-' + (bank.code || 'other');
  5. bankName.innerText = bank.country === 'ru' ? bank.localTitle : bank.engTitle;
  6. } else {
  7. billingForm.className = 'billing-form';
  8. bankName.innerText = '';
  9. }

CSS-in-JS

  1. import contrast from 'contrast';
  2. import banksDB from 'banks-db';
  3. BillingForm = ({ cardNumber }) => {
  4. const title, color;
  5. const bank = banksDB(this.props.cardNumber);
  6. if ( bank.code ) {
  7. title = bank.country === 'ru' ? bank.localTitle : bank.engTitle;
  8. color = bank.color;
  9. } else {
  10. title = '';
  11. color = '#eee';
  12. }
  13. return (
  14. <div style={{
  15. transition: 'background .6s, color .6s',
  16. background: color,
  17. color: contrast(color) === 'light' ? 'black' : 'white'
  18. }}>
  19. <h2>{ title }</h2>
  20. </div>
  21. );
  22. }

Other Best Practices

See also best practices for billing forms:

API

There are two options to use BanksDB depends of whether you need specific countries or not.

If you need banks for all countries

Library exports banksDB function. It accepts bankcard number and returns
bank object.

  1. var banksDB = require('banks-db');
  2. var bank = banksDB('5275 9400 0000 0000');
  3. console.log(bank.code) //=> 'ru-citibank'
  4. console.log(bank.type) //=> 'mastercard'

If database doesn’t contain some bank prefix, bank object will have only
type field.

  1. var unknown = banksDB('4111 1111 1111 1111');
  2. console.log(bank.code) //=> undefined
  3. console.log(bank.type) //=> 'visa'

You can also get banks database by banksDB.data:

  1. for ( let bank of banksDB.data ) {
  2. console.log(bank);
  3. }

If you need only banks for specific countries

Instead of banks-db use banks-db/core:

  1. var banksDBCore = require('banks-db/core');

Then require desired countries from banks-db/banks by two letters code:

  1. var banksOfRussia = require('banks-db/banks/ru');
  2. var banksOfChina = require('banks-db/banks/cn');

All that left is to call banksDBCore with your countries data to initialize. banksDBCore is a function that accepts one argument with banks data for countries that you’ve specified, and returns an instance of BanksDB object with findBank method and data property.

  1. var BanksDB = banksDBCore([banksOfRussia, banksOfChina]);
  2. // var BanksDB = banksDBCore(banksOfRussia); no need for an array if there's only one country

That’s it! Ready to use:

  1. var bank = BanksDB.findBank('5275 9400 0000 0000');
  2. var data = BanksDB.data;

Bank Object

  • type: bankcard type. For example, 'visa' or 'mastercard'.
    Banks DB will return it even if bank is unknown.
  • code: unique code, contains country and name. For example, you can use it to generate CSS selectors for each bank.
  • color: bank’s brand color in HEX-format.
  • localTitle: bank’s title in local language.
  • engTitle: international bank’s title.
  • name: short bank’s name (not unique). For example, 'citibank'.
  • country: bank’s operation country. For example, you can use it
    to display localTitle for local banks and engTitle for others.
  • url: bank’s website URL.

Contributing

In case your bankcard doesn’t work, please check if your bank already in Banks DB:

  • If your bank is already included, you can open an issue with your prefix (NOT full number of your card, just first 5 or 6 symbols) or send a pull request.
  • Otherwise you can add a new bank (see contributing guide).

Changelog

See CHANGELOG.md or release notes (with commits history).