slidepay 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ module SlidePay
2
+ ENDPOINT_SUFFIX = '/rest.svc/API/'
3
+ SUPERVISOR_URL = 'https://supervisor.getcube.com:65532/rest.svc/API/'
4
+ PROD_API_URL = 'https://api.getcube.com:65532/rest.svc/API/'
5
+ DEV_API_URL = 'https://dev.getcube.com:65532/rest.svc/API/'
6
+ DEBUG = false
7
+ end
@@ -0,0 +1,11 @@
1
+ module SlidePay
2
+ class ApiKey < ApiResource
3
+ def initialize(values_hash={})
4
+ @url_root = "api_key"
5
+ @id_attribute = "api_key_id"
6
+
7
+ super(values_hash)
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,117 @@
1
+ module SlidePay
2
+ class ApiResource < Hash
3
+ attr_accessor :url_root, :id_attribute, :token, :api_key, :endpoint
4
+
5
+ def initialize(values_hash={})
6
+ if values_hash[:url_root]
7
+ @url_root = values_hash[:url_root]
8
+ values_hash.delete(:url_root)
9
+ end
10
+
11
+ if values_hash[:id_attribute]
12
+ @id_attribute = values_hash[:id_attribute]
13
+ values_hash.delete(:id_attribute)
14
+ end
15
+
16
+ if values_hash[:token]
17
+ @token = values_hash[:token]
18
+ values_hash.delete(:token)
19
+ end
20
+
21
+ if values_hash[:api_key]
22
+ @api_key = values_hash[:api_key]
23
+ values_hash.delete(:api_key)
24
+ end
25
+
26
+ if values_hash[:endpoint]
27
+ @endpoint = values_hash[:endpoint]
28
+ values_hash.delete(:endpoint)
29
+ end
30
+
31
+ merge! values_hash
32
+ end
33
+
34
+ def url
35
+ if is_new?
36
+ @url_root
37
+ else
38
+ "#{@url_root}/#{self.id()}"
39
+ end
40
+ end
41
+
42
+ def id
43
+ self[@id_attribute]
44
+ end
45
+
46
+ def is_new?
47
+ if id() == nil
48
+ true
49
+ else
50
+ false
51
+ end
52
+ end
53
+
54
+ def populate_from_response(response)
55
+ if response.data.instance_of? Array
56
+ self.merge! response.data.first
57
+ elsif response.data.instance_of? Hash
58
+ self.merge! response.data
59
+ else
60
+ raise Exception.new("Resource with the specified id not found on the server.")
61
+ end
62
+ end
63
+
64
+ def retrieve(options_hash={})
65
+ token = @token || options_hash[:token]
66
+ api_key = @api_key || options_hash[:api_key]
67
+ endpoint = @endpoint || options_hash[:endpoint]
68
+
69
+ if is_new?
70
+ raise Exception.new("Cannot retrieve an unsaved object from the server.")
71
+ end
72
+
73
+ response = SlidePay.get(path: self.url(), token: token, api_key: api_key, endpoint: endpoint)
74
+ if response.was_successful?
75
+ self.populate_from_response(response)
76
+ end
77
+ end
78
+
79
+ def save(options_hash={})
80
+ token = @token || options_hash[:token]
81
+ api_key = @api_key || options_hash[:api_key]
82
+ endpoint = @endpoint || options_hash[:endpoint]
83
+
84
+ if is_new?
85
+ response = SlidePay.post(path: self.url(), token: token, api_key: api_key, endpoint: endpoint, data: self.to_json)
86
+ else
87
+ response = SlidePay.put(path: self.url(), token: token, api_key: api_key, endpoint: endpoint, data: self.to_json)
88
+ end
89
+
90
+ if response.was_successful?
91
+ self.populate_from_response(response)
92
+ true
93
+ else
94
+ false
95
+ end
96
+ end
97
+
98
+ def destroy(options_hash={})
99
+ token = @token || options_hash[:token]
100
+ api_key = @api_key || options_hash[:api_key]
101
+ endpoint = @endpoint || options_hash[:endpoint]
102
+
103
+ if is_new?
104
+ raise Exception.new("Cannot destroy a resource that has not been saved.")
105
+ end
106
+
107
+ response = SlidePay.delete(path: self.url(), token: token, api_key: api_key, endpoint: endpoint)
108
+
109
+ if response.was_successful?
110
+ self[@id_attribute] = nil
111
+ true
112
+ else
113
+ false
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,11 @@
1
+ module SlidePay
2
+ class BankAccount < ApiResource
3
+ def initialize(values_hash={})
4
+ @url_root = "bank_account"
5
+ @id_attribute = "bank_account_id"
6
+
7
+ super(values_hash)
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,48 @@
1
+ module SlidePay
2
+ class Payment < ApiResource
3
+ def initialize(values_hash={})
4
+ @id_attribute = "payment_id"
5
+ @url_root = "payment"
6
+
7
+ super(values_hash)
8
+ end
9
+
10
+ def save(options_hash={})
11
+ process(options_hash)
12
+ end
13
+
14
+ def destroy(options_hash={})
15
+ refund(options_hash)
16
+ end
17
+
18
+ def process(options_hash={})
19
+ token = @token || options_hash[:token]
20
+ api_key = @api_key || options_hash[:api_key]
21
+ endpoint = @endpoint || options_hash[:endpoint]
22
+
23
+ response = SlidePay.post(path: "payment/simple", api_key: api_key, token: token, endpoint: endpoint, data: self.to_json)
24
+
25
+ if response.was_successful?
26
+ self["payment_id"] = response.data["payment_id"]
27
+ self["order_master_id"] = response.data["order_master_id"]
28
+ true
29
+ elsif response.error_text
30
+ raise Exception.new(response.error_text)
31
+ elsif response.data["status_message"]
32
+ raise Exception.new(response.data["status_message"])
33
+ else
34
+ raise Exception.new("Payment could not be processed.")
35
+ end
36
+ end
37
+
38
+ def refund(options_hash={})
39
+ token = @token || options_hash[:token]
40
+ api_key = @api_key || options_hash[:api_key]
41
+ endpoint = @endpoint || options_hash[:endpoint]
42
+
43
+ response = SlidePay.post(path: "payment/refund/#{self.id()}", api_key: api_key, token: token, endpoint: endpoint, data: self.to_json)
44
+
45
+ response.was_successful?
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,60 @@
1
+ module SlidePay
2
+ class Response < String
3
+
4
+ attr_accessor :success, :custom, :operation, :endpoint, :timezone,
5
+ :method, :obj, :id, :milliseconds, :data, :data_md5
6
+
7
+ def initialize(response_json=nil)
8
+ if response_json
9
+ replace response_json
10
+ parse_object_from_json
11
+ end
12
+ end
13
+
14
+ def parse_object_from_json
15
+ object = MultiJson.decode(self)
16
+
17
+ @success = object['success']
18
+ @custom = object['custom']
19
+ @operation = object['operation']
20
+ @endpoint = object['endpoint']
21
+ @timezone = object['timezone']
22
+ @method = object['method']
23
+ @obj = object['obj']
24
+ @id = object['id']
25
+ @milliseconds = object['milliseconds']
26
+ @data = object['data']
27
+ @data_md5 = object['data_md5']
28
+ end
29
+
30
+ def was_successful?
31
+ @success == true
32
+ end
33
+
34
+ # response metadata fields
35
+ def endpoint
36
+ "#{@endpoint}#{ENDPOINT_SUFFIX}"
37
+ end
38
+
39
+ # error fields
40
+ def error
41
+ if was_successful? == false
42
+ @data
43
+ else
44
+ nil
45
+ end
46
+ end
47
+
48
+ def error_code
49
+ if error
50
+ @data['error_code']
51
+ end
52
+ end
53
+
54
+ def error_text
55
+ if error
56
+ @data['error_text']
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,5 @@
1
+ module SlidePay
2
+ module Util
3
+
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module SlidePay
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,32 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), 'lib'))
2
+ $:.unshift(File.join(File.dirname(__FILE__), 'spec'))
3
+
4
+ require 'multi_json'
5
+ require 'slidepay'
6
+ require 'spec_helper'
7
+
8
+ include SlidePay
9
+
10
+ SlidePay.configure(development: true)
11
+ SlidePay.api_key = ENV["api_key"]
12
+
13
+ # RestClient.proxy = "http://127.0.0.1:8888"
14
+
15
+ # a = ApiKey.new
16
+ # a["api_key_id"] = 17
17
+ # a.is_new?
18
+ # a.id_attribute
19
+ # a.retrieve()
20
+
21
+ p = Payment.new
22
+ p["amount"] = 1.01
23
+ p["method"] = "CreditCard"
24
+ p["cc_number"] = "4111111111111111"
25
+ p["cc_cvv2"] = "111"
26
+ p["cc_billing_zip"] = "11111"
27
+ p["cc_expiry_month"] = "10"
28
+ p["cc_expiry_year"] = "14"
29
+ r = p.process()
30
+ p.refund()
31
+
32
+
data/slidepay.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require 'slidepay/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "slidepay"
9
+ spec.version = SlidePay::VERSION
10
+ spec.authors = ["Matt Rothstein"]
11
+ spec.email = ["matt@slidepay.com"]
12
+ spec.description = "SlidePay makes it ridiculously easy to take swiped payments anywhere, on any device. Check out http://www.slidepay.com for more info."
13
+ spec.summary = "SlidePay Ruby SDK"
14
+ spec.homepage = "http://www.slidepay.com"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "rest-client"
23
+ spec.add_dependency "multi_json"
24
+ spec.add_dependency "json"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.3"
27
+ spec.add_development_dependency "rspec", "~> 2.6"
28
+ spec.add_development_dependency "rake"
29
+ spec.add_development_dependency "guard"
30
+ spec.add_development_dependency "guard-rspec"
31
+ spec.add_development_dependency "growl"
32
+ end
@@ -0,0 +1,38 @@
1
+ require "slidepay"
2
+ require "spec_helper"
3
+
4
+ describe SlidePay::ApiKey do
5
+ it "should have an id_attribute" do
6
+ b = SlidePay::ApiKey.new()
7
+ expect(b.id_attribute).to eq("api_key_id")
8
+ end
9
+
10
+ it "should have a root url" do
11
+ b = SlidePay::ApiKey.new()
12
+ expect(b.url_root).to eq("api_key")
13
+ end
14
+
15
+ describe "url" do
16
+ it "should not append the object id if no id is set" do
17
+ b = SlidePay::ApiKey.new()
18
+ expect(b.url()).to eq("api_key")
19
+ end
20
+
21
+ it "should append the object id if set" do
22
+ b = SlidePay::ApiKey.new("api_key_id" => 2)
23
+ expect(b.url()).to eq("api_key/2")
24
+ end
25
+ end
26
+
27
+ describe "id" do
28
+ it "should return nil if the id is not set" do
29
+ b = SlidePay::ApiKey.new()
30
+ expect(b.id()).to eq(nil)
31
+ end
32
+
33
+ it "should return the id if it is present" do
34
+ b = SlidePay::ApiKey.new("api_key_id" => 2)
35
+ expect(b.id()).to eq(2)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,227 @@
1
+ require "slidepay"
2
+ require "spec_helper"
3
+
4
+ describe SlidePay::ApiResource do
5
+ before(:all) do
6
+ SlidePay.configure(development: true)
7
+ end
8
+
9
+ it "should be a Hash" do
10
+ r = SlidePay::ApiResource.new
11
+ expect(r).to be_a(Hash)
12
+ end
13
+
14
+ describe "initialize" do
15
+ it "should allow instantiation with no parameters" do
16
+ r = SlidePay::ApiResource.new
17
+ expect(r).to be_a(SlidePay::ApiResource)
18
+ end
19
+
20
+ it "should set api_key, token, and endpoint when they are present" do
21
+ resource = SlidePay::ApiResource.new(api_key: "TEST_API_KEY", token: "TEST_TOKEN", endpoint: "TEST_ENDPOINT")
22
+
23
+ expect(resource[:api_key]).to eq(nil)
24
+ expect(resource[:token]).to eq(nil)
25
+ expect(resource[:endpoint]).to eq(nil)
26
+
27
+ expect(resource.api_key).to eq("TEST_API_KEY")
28
+ expect(resource.token).to eq("TEST_TOKEN")
29
+ expect(resource.endpoint).to eq("TEST_ENDPOINT")
30
+ end
31
+
32
+ it "should values for keys other than :api_key, :token, or :endpoint into the instance" do
33
+ resource = SlidePay::ApiResource.new(api_key: "TEST_API_KEY", dog: "Fido", cat: "Dog")
34
+
35
+ expect(resource[:api_key]).to eq(nil)
36
+ expect(resource[:dog]).to eq("Fido")
37
+ expect(resource[:cat]).to eq("Dog")
38
+ end
39
+ end
40
+
41
+ describe "to_json" do
42
+ it "should return an empty object for a clean resource" do
43
+ r = SlidePay::ApiResource.new()
44
+ expect(r.to_json).to eq("{}")
45
+ end
46
+
47
+ it "should return a json representation of the hash contents" do
48
+ r = SlidePay::ApiResource.new(id: 1, name: "Dog the Bounty Hunter")
49
+ expect(r.to_json).to eq("{\"id\":1,\"name\":\"Dog the Bounty Hunter\"}" )
50
+ end
51
+ end
52
+
53
+ describe "is_new?" do
54
+ it "should return true if id_attribute is not set" do
55
+ resource = SlidePay::ApiResource.new(id: 1, name: "Dog the Bounty Hunter")
56
+ expect(resource.is_new?).to be_true
57
+ end
58
+
59
+ it "should return true if id_attribute is set but not present in the hash" do
60
+ resource = SlidePay::ApiResource.new(name: "Dog the Bounty Hunter")
61
+ resource.id_attribute = :id
62
+
63
+ expect(resource.is_new?).to be_true
64
+ end
65
+
66
+ it "should return false if id_attribute is set and present in the hash" do
67
+ resource = SlidePay::ApiResource.new(id: 1, name: "Dog the Bounty Hunter")
68
+ resource.id_attribute = :id
69
+
70
+ expect(resource.is_new?).to be_false
71
+ end
72
+ end
73
+
74
+ describe "id" do
75
+ it "should return nil if the id_attribute is not set" do
76
+ resource = SlidePay::ApiResource.new(id: 1, name: "Dog the Bounty Hunter")
77
+ expect(resource.id()).to eq(nil)
78
+ end
79
+
80
+ it "should return nil if the id_attribute is set but not present in the hash" do
81
+ resource = SlidePay::ApiResource.new(name: "Dog the Bounty Hunter")
82
+ resource.id_attribute = :id
83
+ expect(resource.id()).to eq(nil)
84
+ end
85
+
86
+ it "should return the value of the the id_attribute key inside the hash" do
87
+ resource = SlidePay::ApiResource.new(id: 1, name: "Dog the Bounty Hunter")
88
+ resource.id_attribute = :id
89
+ expect(resource.id()).to eq(1)
90
+ end
91
+ end
92
+
93
+ describe "url_root" do
94
+ context "when id_attribute is not set but is in the hash" do
95
+ resource = SlidePay::ApiResource.new(id: 1, name: "Dog the Bounty Hunter")
96
+ resource.url_root = "mullet_hero"
97
+
98
+ it "should return only the url_root" do
99
+ expect(resource.url()).to eq(resource.url_root)
100
+ end
101
+ end
102
+
103
+ context "when id_attribute is set but not in the hash" do
104
+ resource = SlidePay::ApiResource.new(name: "Dog the Bounty Hunter")
105
+ resource.id_attribute = :id
106
+ resource.url_root = "mullet_hero"
107
+
108
+ it "should return only the url_root" do
109
+ expect(resource.url()).to eq(resource.url_root)
110
+ end
111
+ end
112
+
113
+ context "when id_attribute is set and in the hash" do
114
+ resource = SlidePay::ApiResource.new(id: 1, name: "Dog the Bounty Hunter")
115
+ resource.id_attribute = :id
116
+ resource.url_root = "mullet_hero"
117
+
118
+ it "should return a specific path like :url_root/:id_attribute" do
119
+ expect(resource.url()).to eq("#{resource.url_root}/#{resource[resource.id_attribute]}")
120
+ end
121
+ end
122
+ end
123
+
124
+ describe "retrieve" do
125
+ it "should raise an exception if the resource has no id" do
126
+ resource = SlidePay::ApiResource.new(name: "Dog the Bounty Hunter")
127
+ expect { resource.retrieve() }.to raise_error
128
+ end
129
+
130
+ it "should make a get request if the resource has an id" do
131
+ resource = SlidePay::ApiResource.new(id: 1)
132
+ resource.url_root = "person"
133
+ resource.id_attribute = :id
134
+
135
+ SlidePay.should_receive(:get).with(path: "person/1", token: nil, api_key: nil, endpoint: nil).and_return(a_response_object)
136
+ resource.retrieve()
137
+ end
138
+
139
+ it "should have the contents of the response when call is successful" do
140
+ resource = SlidePay::ApiResource.new({"id" => 1})
141
+ resource.url_root = "person"
142
+ resource.id_attribute = "id"
143
+
144
+ SlidePay.should_receive(:get).with(path: "person/1", token: nil, api_key: nil, endpoint: nil).and_return(a_response_object)
145
+ resource.retrieve()
146
+
147
+ expect(resource["id"]).to eq("1")
148
+ expect(resource["name"]).to eq("Dog the Bounty Hunter")
149
+ end
150
+ end
151
+
152
+ describe "save" do
153
+ before(:all) do
154
+ SlidePay.stub(:get) { a_response_object }
155
+ SlidePay.stub(:post) { a_response_object }
156
+ SlidePay.stub(:put) { a_response_object }
157
+ end
158
+
159
+ context "when the resource is new (has no id)" do
160
+ it "should make a post request if the resource has no id" do
161
+ resource = SlidePay::ApiResource.new(name: "Dog the Bounty Hunter")
162
+ resource.url_root = "person"
163
+ resource.id_attribute = "id"
164
+ SlidePay.should_receive(:post).with(path: "person", token: nil, api_key: nil, endpoint: nil, data: resource.to_json).and_return(a_response_object)
165
+ resource.save()
166
+ end
167
+
168
+ it "should populate the object with the returned values on a successful response" do
169
+ resource = SlidePay::ApiResource.new(name: "Dog the Bounty Hunter")
170
+ SlidePay.should_receive(:post).and_return(a_response_object)
171
+ resource.save()
172
+
173
+ expect(resource["id"]).to eq("1")
174
+ expect(resource["name"]).to eq("Dog the Bounty Hunter")
175
+ end
176
+ end
177
+
178
+ context "when the resource is not new (has an id)" do
179
+ it "should make a put request if the resource has an id" do
180
+ resource = SlidePay::ApiResource.new(id: 1, name: "---this should be replaced--")
181
+ resource.url_root = "person"
182
+ resource.id_attribute = :id
183
+
184
+ SlidePay.should_receive(:put).with(path: "person/1", token: nil, api_key: nil, endpoint: nil, data: resource.to_json).and_return(a_response_object)
185
+ resource.save()
186
+ end
187
+
188
+ it "should populate the object with the returned values on a successful response" do
189
+ resource = SlidePay::ApiResource.new(id: 1, name: "---this should be replaced--")
190
+ resource.url_root = "person"
191
+ resource.id_attribute = :id
192
+
193
+ SlidePay.should_receive(:put).and_return(a_response_object)
194
+ resource.save()
195
+
196
+ expect(resource["id"]).to eq("1")
197
+ expect(resource["name"]).to eq("Dog the Bounty Hunter")
198
+ end
199
+ end
200
+ end
201
+
202
+ describe "destroy" do
203
+ it "should raise an error if the resource is new (has no id)" do
204
+ resource = SlidePay::ApiResource.new
205
+ expect { resource.destroy() }.to raise_error
206
+ end
207
+
208
+ it "should make a delete request and return true" do
209
+ resource = SlidePay::ApiResource.new(id: 1, name: "Dog the Bounty Hunter")
210
+ resource.id_attribute = :id
211
+ resource.url_root = "person"
212
+
213
+ SlidePay.should_receive(:delete).and_return(a_response_object(successful_deletion_response))
214
+ expect(resource.destroy()).to be_true
215
+ end
216
+
217
+ it "should delete the id_attribute key/value pair from the hash on deletion" do
218
+ resource = SlidePay::ApiResource.new(id: 1, name: "Dog the Bounty Hunter")
219
+ resource.id_attribute = :id
220
+ resource.url_root = "person"
221
+
222
+ SlidePay.should_receive(:delete).and_return(a_response_object(successful_deletion_response))
223
+ expect(resource.destroy()).to be_true
224
+ expect(resource[:id]).to be_nil
225
+ end
226
+ end
227
+ end