Coder Social home page Coder Social logo

khrome / card-swipe Goto Github PK

View Code? Open in Web Editor NEW
19.0 6.0 11.0 64 KB

An NPM for detecting CC track inputs from streaming character data and for extracting data from them.

License: MIT License

JavaScript 100.00%
card-swipe magstripe swipe credit-card scanner

card-swipe's Introduction

card-swipe.js

NPM version npm Travis

A utility for detecting CC track inputs from streaming character data and for extracting data from them.

A credit card magstripe contains a string of data representing a composite of account details. A 'card present' transaction (which gives you a better retail percentage on your transaction fees) generally consists of setting a flag and passing along this data, which has strict regulations on them preventing them from ever being saved to disk. Normally a magstripe is just a keyboard as far as the OS knows, which often leads to tedious PC POS interfaces where you click into a field and then swipe the card, your other option is to build an input sniffer which allows you to scan time restricted character buffers for track data patterns, so you can react/generate events/whatever. So that's what this is, you plug keystroke input into it, it reacts whenever it sees a cardswipe. In addition it can use bin ranges on the account number to determine account type and issuer.

Usage

require the library

var Swipe = require('card-swipe');

the simplest way to get it running is to use the built-in stdio hook to get it running from the terminal:

Swipe.stdIn()
new Swipe(function(swipeData){
    console.log('swipe', swipeData);
});

this is the shorthand for:

var scanner = new Swipe.Scanner();
new Swipe({
    scanner : scanner,
    onSwipe : function(swipeData){
        console.log('swipe', swipeData);
    }
});
process.stdin.setRawMode();
process.stdin.resume();
process.stdin.on('data', function (chunk, key) {
    chunk = chunk.toString();
    for(var lcv=0; lcv < chunk.length; lcv++) scanner.input(chunk[lcv]);
    if (key && key.ctrl && key.name == 'c') process.exit();
});

likely if you are integrating this into an app, stdin is not going to be good enough for you... but luckily the scanner will wire up to just about anything.

Additionally, so I could test these things out I built a generator function

Swipe.generate(field, [values])

which can generate luhn and bin valid account numbers, track_one and track_two data (since you can't really be saving these things, and test cards are continually expiring).

and for my testing harness

Swipe.fake(scanner)

which generates a random fake swipe across the passed in scanner.

Browser Example - Vue Component

<template>
    <div class="swipe">
        <slot></slot>
    </div>
</template>

<script>
    import * as Swipe from 'card-swipe';

    export default {
        name: 'CardSwipe',
        methods : {
          handleSwipe : function(swipe){
              console.log('CHAR', swipe);
          }
        },
        created: function(){
            window.addEventListener('keydown', (e)=>{
                if(e.key.length === 1) scanner.input(e.key);
                //if(e.key === 'Enter') scanner.input("\n");
            });
            let scanner = new Swipe.Scanner();
            new Swipe({
                scanner : scanner,
                onScan : (swipeData)=>{
                    this.handleSwipe(swipeData);
                    console.log('swipe', swipeData);
                }
            });
        }
    }
</script>
<style scoped></style>

Testing

Tests use mocha/should to execute the tests from root

mocha

If you find any rough edges, please submit a bug!

Right now this only supports credit cards, but this could easily expand to gift cards, EBT, checks, etc. If you have a specific interest, contact me.

Enjoy,

-Abbey Hawk Sparrow

card-swipe's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

card-swipe's Issues

Does this work in React Native or Cordova? Any Recommended card readers?

Hey, i am looking to build a mobile app with react native and want to implement a way to process our own privately owned cards. This seems like a great fit so far.

Do you know if I can work with it inside react native or cordova? probably cordova since its just a web page wrapped in native shell. If it works on cordova I cant see why it wont work on electron for desktop.

Also, do you recommend any particular card reader? brand or otherwise? anything you have worked with personally.

Thanks again

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.