swipehq 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in swipehq.gemspec
4
+ gemspec
5
+
6
+ gem 'rb-fsevent'
@@ -0,0 +1,23 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec' do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ # Rails example
10
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
11
+ watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
12
+ watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
13
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
14
+ watch('config/routes.rb') { "spec/routing" }
15
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
16
+
17
+ # Capybara features specs
18
+ watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
19
+
20
+ # Turnip features and steps
21
+ watch(%r{^spec/acceptance/(.+)\.feature$})
22
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
23
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Marcelo Wiermann
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Swipehq::Ruby
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'swipehq'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install swipehq
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,40 @@
1
+ require File.expand_path('../../lib/swipehq', __FILE__)
2
+
3
+ puts "=== Starting SwipeHQ API test"
4
+ SwipeHQ.configure do |config|
5
+ config.api_key = ENV['SWIPEHQ_API_KEY']
6
+ config.merchant_id = ENV['SWIPEHQ_MERCHANT_ID']
7
+ config.callback_url = ENV['SWIPEHQ_CALLBACK_URL']
8
+ end
9
+
10
+ puts "=== Setting up Callback URL"
11
+ SwipeHQ.set_callback_url
12
+
13
+ puts "=== Creating Transaction"
14
+ params = {
15
+ item: 'Item',
16
+ description: 'Description for Item',
17
+ amount: 100.00,
18
+ default_quantity: 1,
19
+ user_data: 'Data for user',
20
+ currency: 'NZD'
21
+ }
22
+ @transaction = SwipeHQ::Transaction.new(params)
23
+ @transaction.create
24
+ msg = <<-EOF
25
+
26
+ Transaction Identifier: #{@transaction.identifier}
27
+ Transaction ID: #{@transaction.id}
28
+ Payment URL: #{@transaction.payment_url}
29
+
30
+ EOF
31
+
32
+ puts msg
33
+
34
+ puts "=== Complete Transaction on browser and press enter to proceed"
35
+ proceed = gets
36
+ if !proceed.empty?
37
+ puts "=== Verifying Transaction Status"
38
+ @transaction.verify
39
+ puts msg
40
+ end
@@ -0,0 +1,58 @@
1
+ require 'bundler/setup'
2
+ require 'rest_client'
3
+ require 'json'
4
+
5
+ require "swipehq/version"
6
+ require "swipehq/transaction"
7
+
8
+ module SwipeHQ
9
+ extend self
10
+
11
+ LIB_PATH = File.dirname(__FILE__) + '/swipehq/'
12
+
13
+ def configure
14
+ yield self
15
+ end
16
+
17
+ %w{ api_key merchant_id callback_url lpn_url }.each do |setting|
18
+ define_method "#{setting}" do
19
+ instance_variable_get("@#{setting}") || instance_variable_set("@#{setting}", nil)
20
+ end
21
+
22
+ define_method "#{setting}=" do |value|
23
+ instance_variable_set("@#{setting}", value)
24
+ end
25
+ end
26
+
27
+ def request(method = :get, url, params)
28
+ raw_response = RestClient.send(method, url, params: params)
29
+ if raw_response
30
+ JSON.parse(raw_response)
31
+ else
32
+ nil
33
+ end
34
+ end
35
+
36
+ def set_callback_url
37
+ return nil unless callback_url
38
+ params = {
39
+ api_key: SwipeHQ.api_key,
40
+ merchant_id: SwipeHQ.merchant_id,
41
+ callback_url: callback_url
42
+ }
43
+ url = 'https://api.swipehq.com/setCallback.php'
44
+ request(:post, url, params: params)
45
+ end
46
+
47
+ def set_lpn_url
48
+ return nil unless lpn_url
49
+ params = {
50
+ api_key: SwipeHQ.api_key,
51
+ merchant_id: SwipeHQ.merchant_id,
52
+ lpn_url: lpn_url
53
+ }
54
+ url = 'https://api.swipehq.com/setCallback.php'
55
+ request(:post, url, params: params)
56
+ end
57
+
58
+ end
@@ -0,0 +1,72 @@
1
+ module SwipeHQ
2
+ class Transaction
3
+
4
+ attr_accessor :identifier, :item, :description, :amount, :default_quantity, :user_data, :currency,
5
+ :status
6
+
7
+ attr_reader :id, :status, :approved
8
+
9
+ def initialize(params = nil)
10
+ if params
11
+ params.each do |k,v|
12
+ self.send("#{k}=",v)
13
+ end
14
+ end
15
+ end
16
+
17
+ def attributes
18
+ a = {}
19
+ %w{ identifier id item description amount default_quantity user_data currency }.each do |att|
20
+ a[att.to_sym] = self.send(att)
21
+ end
22
+ return a
23
+ end
24
+
25
+ def create
26
+ params = {
27
+ api_key: SwipeHQ.api_key,
28
+ merchant_id: SwipeHQ.merchant_id
29
+ }
30
+ attributes.each do |k,v|
31
+ params["td_#{k}".to_sym] = v
32
+ end
33
+ res = SwipeHQ.request(
34
+ :get,
35
+ 'https://api.swipehq.com/createTransactionIdentifier.php',
36
+ params
37
+ )
38
+ if res['response_code'] and res['response_code'] == 200
39
+ @identifier = res['data']['identifier']
40
+ else
41
+ @identifier = nil
42
+ end
43
+ end
44
+
45
+ def verify
46
+ return nil unless @identifier
47
+ params = {
48
+ api_key: SwipeHQ.api_key,
49
+ merchant_id: SwipeHQ.merchant_id,
50
+ transaction_id: @identifier
51
+ }
52
+ res = SwipeHQ.request(
53
+ :get,
54
+ 'https://api.swipehq.com/verifyTransaction.php',
55
+ params
56
+ )
57
+ if res['response_code'] and res['response_code'] == 200
58
+ @id = res['data']['transaction_id']
59
+ @status = res['data']['status'] || 'pending'
60
+ @approved = res['data']['transaction_approved'] == 'yes'
61
+ else
62
+ @status = nil
63
+ end
64
+ end
65
+
66
+ def payment_url
67
+ return nil unless @identifier
68
+ "https://payment.swipehq.com/?identifier_id=#{@identifier}"
69
+ end
70
+
71
+ end
72
+ end
@@ -0,0 +1,3 @@
1
+ module SwipeHQ
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,12 @@
1
+ require 'factory_girl'
2
+
3
+ FactoryGirl.define do
4
+ factory :transaction, class: SwipeHQ::Transaction do
5
+ sequence(:item){ |n| "Item #{n}" }
6
+ sequence(:description){ |n| "Description for Item #{n}" }
7
+ amount{ 100 }
8
+ default_quantity{ 1 }
9
+ user_data { { a: 1, b: 2, c: 3 }.to_json }
10
+ currency{ 'NZD' }
11
+ end
12
+ end
@@ -0,0 +1,18 @@
1
+ require File.expand_path('../../lib/swipehq', __FILE__)
2
+ require 'rspec'
3
+ require 'mocha/setup'
4
+ require 'factory_girl'
5
+
6
+ FactoryGirl.find_definitions
7
+
8
+ RSpec.configure do |config|
9
+ config.treat_symbols_as_metadata_keys_with_true_values = true
10
+ config.run_all_when_everything_filtered = true
11
+ config.filter_run :focus
12
+
13
+ # Run specs in random order to surface order dependencies. If you find an
14
+ # order dependency and want to debug it, you can fix the order by providing
15
+ # the seed, which is printed after each run.
16
+ # --seed 1234
17
+ config.order = 'random'
18
+ end
@@ -0,0 +1,105 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ describe SwipeHQ::Transaction do
4
+
5
+ let(:successful_create_request){ SwipeHQ.stub(:request) }
6
+
7
+ describe '#initialize' do
8
+ context 'sets parameters correctly' do
9
+ let(:params){
10
+ {
11
+ item: 'Item',
12
+ description: 'Description for Item',
13
+ amount: 100.00,
14
+ default_quantity: 1,
15
+ user_data: 'Data for user',
16
+ currency: 'NZD'
17
+ }
18
+ }
19
+ let(:transaction){ SwipeHQ::Transaction.new(params) }
20
+
21
+ it { transaction.attributes =~ params }
22
+ end
23
+ end
24
+
25
+ describe '#create' do
26
+
27
+ let(:transaction){ FactoryGirl.build(:transaction) }
28
+
29
+ context 'valid params' do
30
+ let(:response){ { 'response_code' => 200, 'message' => 'OK', 'data' => {'identifier' => 'XXXX'} } }
31
+
32
+ before(:each) do
33
+ SwipeHQ.stubs(:request).returns(response)
34
+ transaction.create
35
+ end
36
+
37
+ it{ transaction.identifier.should eq response['data']['identifier'] }
38
+ end
39
+
40
+ end
41
+
42
+ describe '#verify' do
43
+
44
+ let(:transaction){ FactoryGirl.build(:transaction, identifier: 'XXXX') }
45
+
46
+ context 'pending' do
47
+ let(:response){ { 'response_code' => 200, 'message' => 'OK', 'data' => { 'transaction_id' => 'YYYY', 'status' => nil, 'transaction_approved' => 'no' } } }
48
+
49
+ before(:each) do
50
+ SwipeHQ.stubs(:request).returns(response)
51
+ transaction.verify
52
+ end
53
+
54
+ it { transaction.id.should eq(response['data']['transaction_id']) }
55
+ it { transaction.status.should eq('pending') }
56
+ it { transaction.approved.should eq(false) }
57
+ end
58
+
59
+ context 'approved' do
60
+ let(:response){ { 'response_code' => 200, 'message' => 'OK', 'data' => { 'transaction_id' => nil, 'status' => 'approved', 'transaction_approved' => 'yes' } } }
61
+
62
+ before(:each) do
63
+ SwipeHQ.stubs(:request).returns(response)
64
+ transaction.verify
65
+ end
66
+
67
+ it { transaction.id.should eq(nil) }
68
+ it { transaction.status.should eq('approved') }
69
+ it { transaction.approved.should eq(true) }
70
+ end
71
+
72
+ context 'declined' do
73
+ let(:response){ { 'response_code' => 200, 'message' => 'OK', 'data' => { 'transaction_id' => 'YYYY', 'status' => 'declined', 'transaction_approved' => 'no' } } }
74
+
75
+ before(:each) do
76
+ SwipeHQ.stubs(:request).returns(response)
77
+ transaction.verify
78
+ end
79
+
80
+ it { transaction.id.should eq(response['data']['transaction_id']) }
81
+ it { transaction.status.should eq('declined') }
82
+ it { transaction.approved.should eq(false) }
83
+ end
84
+
85
+ end
86
+
87
+ describe '#payment_url' do
88
+
89
+ context 'with transaction identifier' do
90
+ let(:identifier) { 'XXXX' }
91
+ let(:transaction) { FactoryGirl.build(:transaction, identifier: identifier) }
92
+
93
+ it { transaction.payment_url.should eq("https://payment.swipehq.com/?identifier_id=#{identifier}") }
94
+ end
95
+
96
+ context 'without transaction identifier' do
97
+ let(:identifier) { nil }
98
+ let(:transaction) { FactoryGirl.build(:transaction, identifier: identifier) }
99
+
100
+ it { transaction.payment_url.should eq(nil) }
101
+ end
102
+
103
+ end
104
+
105
+ end
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'swipehq/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "swipehq"
8
+ gem.version = SwipeHQ::VERSION
9
+ gem.authors = ["Marcelo Wiermann"]
10
+ gem.email = ["marcelo.wiermann@gmail.com"]
11
+ gem.description = %q{SwipeHQ for Ruby}
12
+ gem.summary = %q{SwipeHQ for Ruby}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'rest-client'
21
+ gem.add_dependency 'json'
22
+
23
+ gem.add_development_dependency 'rspec'
24
+ gem.add_development_dependency 'factory_girl'
25
+ gem.add_development_dependency 'mocha'
26
+ gem.add_development_dependency 'rb-fsevent'
27
+ gem.add_development_dependency 'shoulda'
28
+ gem.add_development_dependency 'guard'
29
+ gem.add_development_dependency 'guard-rspec'
30
+
31
+ end
metadata ADDED
@@ -0,0 +1,207 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: swipehq
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Marcelo Wiermann
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rest-client
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: json
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: factory_girl
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: mocha
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rb-fsevent
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: shoulda
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: guard
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ - !ruby/object:Gem::Dependency
143
+ name: guard-rspec
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ description: SwipeHQ for Ruby
159
+ email:
160
+ - marcelo.wiermann@gmail.com
161
+ executables: []
162
+ extensions: []
163
+ extra_rdoc_files: []
164
+ files:
165
+ - .gitignore
166
+ - .rspec
167
+ - Gemfile
168
+ - Guardfile
169
+ - LICENSE.txt
170
+ - README.md
171
+ - Rakefile
172
+ - examples/simple.rb
173
+ - lib/swipehq.rb
174
+ - lib/swipehq/transaction.rb
175
+ - lib/swipehq/version.rb
176
+ - spec/factories/transactions.rb
177
+ - spec/spec_helper.rb
178
+ - spec/transaction_spec.rb
179
+ - swipehq.gemspec
180
+ homepage: ''
181
+ licenses: []
182
+ post_install_message:
183
+ rdoc_options: []
184
+ require_paths:
185
+ - lib
186
+ required_ruby_version: !ruby/object:Gem::Requirement
187
+ none: false
188
+ requirements:
189
+ - - ! '>='
190
+ - !ruby/object:Gem::Version
191
+ version: '0'
192
+ required_rubygems_version: !ruby/object:Gem::Requirement
193
+ none: false
194
+ requirements:
195
+ - - ! '>='
196
+ - !ruby/object:Gem::Version
197
+ version: '0'
198
+ requirements: []
199
+ rubyforge_project:
200
+ rubygems_version: 1.8.24
201
+ signing_key:
202
+ specification_version: 3
203
+ summary: SwipeHQ for Ruby
204
+ test_files:
205
+ - spec/factories/transactions.rb
206
+ - spec/spec_helper.rb
207
+ - spec/transaction_spec.rb