magtek_card_reader 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ require 'libusb'
2
+
3
+ require 'magtek_card_reader/list.rb'
4
+ require 'magtek_card_reader/reader.rb'
@@ -0,0 +1,8 @@
1
+ module Magtek
2
+
3
+ def self.available_devices
4
+ usb = LIBUSB::Context.new
5
+ usb.devices.select { |d| d.idVendor == 0x0801 }.map { |d| [ d.idProduct, d.product ] }
6
+ end
7
+
8
+ end
@@ -0,0 +1,63 @@
1
+ module Magtek
2
+
3
+ class CardReader
4
+
5
+ def initialize(product_id = nil)
6
+ product_id = Magtek.available_devices.first unless product_id
7
+ @usb = LIBUSB::Context.new
8
+ @device = @usb.devices(:idVendor => 0x0801, :idProduct => product_id).first
9
+ fail "Device not found" unless @device
10
+ @interface = @device.interfaces.first
11
+ fail "Interface not found" unless @interface
12
+ @endpoint = @interface.endpoints.first
13
+ fail "Endpoint not found" unless @endpoint
14
+ @open = false
15
+ end
16
+
17
+ def open
18
+ close
19
+ begin
20
+ @handle = @device.open
21
+ @handle.detach_kernel_driver(0) if @handle.kernel_driver_active?(0)
22
+ @handle.set_configuration(1)
23
+ @handle.claim_interface(0)
24
+ @open = true
25
+ return true
26
+ rescue
27
+ return false
28
+ end
29
+ end
30
+
31
+ def close
32
+ return true unless @open
33
+ @handle.release_interface(0)
34
+ @handle.close
35
+ true
36
+ end
37
+
38
+ def read(options)
39
+ timeout = options[:timeout] || 5000
40
+
41
+ @interrupt_transfer = LIBUSB::InterruptTransfer.new
42
+ @interrupt_transfer.dev_handle = @handle
43
+ @interrupt_transfer.endpoint = @endpoint
44
+ @interrupt_transfer.alloc_buffer(337)
45
+ @interrupt_transfer.timeout = timeout
46
+ begin
47
+ @interrupt_transfer.submit_and_wait!
48
+ rescue => e
49
+ @handle.reset_device unless e.message == "error TRANSFER_TIMED_OUT"
50
+ return false
51
+ end
52
+ return false unless @interrupt_transfer.actual_length==337
53
+
54
+ match = /B([0-9]{16})\^(.*)\^([0-9]{2})([0-9]{2})/.match(@interrupt_transfer.actual_buffer)
55
+ return false unless match
56
+
57
+ number, name, exp_year, exp_month = match.captures
58
+ return [true, number, name, exp_year, exp_month]
59
+ end
60
+
61
+ end
62
+
63
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: magtek_card_reader
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Derrick Parkhurst
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-20 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: libusb
16
+ requirement: &70107785676380 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.2.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70107785676380
25
+ description: Provides a convenient wrapper around libusb to read credit cards with
26
+ a Magtek Credit Card Reader.
27
+ email: derrick.parkhurst@gmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - lib/magtek_card_reader/list.rb
33
+ - lib/magtek_card_reader/reader.rb
34
+ - lib/magtek_card_reader.rb
35
+ homepage: http://github.com/thirtysixthspan/magtek_card_reader
36
+ licenses: []
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.6
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Magtek Credit Card Reader Library
59
+ test_files: []