tns_payments 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +5 -0
- data/lib/tns_payments.rb +1 -0
- data/lib/tns_payments/connection.rb +4 -3
- data/lib/tns_payments/request.rb +13 -7
- data/lib/tns_payments/transaction.rb +18 -0
- data/lib/tns_payments/version.rb +1 -1
- data/test/unit/conntection_test.rb +6 -3
- data/test/unit/transaction_test.rb +21 -0
- metadata +7 -4
data/CHANGELOG.md
CHANGED
data/lib/tns_payments.rb
CHANGED
@@ -3,7 +3,6 @@ require 'base64'
|
|
3
3
|
module TNSPayments
|
4
4
|
class Connection
|
5
5
|
CREDIT_CARD_TOKEN_FORMAT = /^9\d{15}/
|
6
|
-
MINIMUM_ORDER_ID = 10000000000
|
7
6
|
|
8
7
|
attr_accessor :api_key, :host, :merchant_id
|
9
8
|
attr_writer :session_token
|
@@ -22,7 +21,8 @@ module TNSPayments
|
|
22
21
|
end
|
23
22
|
|
24
23
|
def purchase transaction, token
|
25
|
-
|
24
|
+
transaction = Transaction.new transaction
|
25
|
+
order_id = transaction.order_id
|
26
26
|
transaction_id = transaction.transaction_id
|
27
27
|
params = {
|
28
28
|
'apiOperation' => 'PAY',
|
@@ -35,7 +35,8 @@ module TNSPayments
|
|
35
35
|
end
|
36
36
|
|
37
37
|
def refund transaction
|
38
|
-
|
38
|
+
transaction = Transaction.new transaction
|
39
|
+
order_id = transaction.order_id.to_i
|
39
40
|
transaction_id = transaction.transaction_id
|
40
41
|
params = {
|
41
42
|
'apiOperation' => 'REFUND',
|
data/lib/tns_payments/request.rb
CHANGED
@@ -3,23 +3,29 @@ require 'rest-client'
|
|
3
3
|
|
4
4
|
module TNSPayments
|
5
5
|
class Request
|
6
|
-
attr_accessor :connection, :method, :path, :
|
6
|
+
attr_accessor :connection, :method, :path, :payload
|
7
7
|
|
8
|
-
def initialize connection, method, path,
|
9
|
-
self.connection, self.method, self.path, self.
|
8
|
+
def initialize connection, method, path, payload = {}
|
9
|
+
self.connection, self.method, self.path, self.payload = connection, method, path, payload
|
10
10
|
end
|
11
11
|
|
12
12
|
def perform
|
13
|
-
authorization =
|
13
|
+
authorization = payload.fetch(:authorization) { true }
|
14
14
|
url = "#{connection.host}/api/rest/version/4#{path}"
|
15
15
|
auth_headers = {:Authorization => encode_credentials}
|
16
16
|
headers = {:content_type => 'Application/json;charset=UTF-8', :accept => '*/*'}
|
17
17
|
headers.merge! auth_headers if authorization
|
18
18
|
|
19
|
-
args =
|
20
|
-
|
19
|
+
args = {
|
20
|
+
:method => method,
|
21
|
+
:url => url,
|
22
|
+
:payload => payload.to_json,
|
23
|
+
:headers => headers,
|
24
|
+
:timeout => 120,
|
25
|
+
:open_timeout => 30
|
26
|
+
}
|
21
27
|
|
22
|
-
Response.new RestClient.
|
28
|
+
Response.new RestClient::Request.execute(args)
|
23
29
|
rescue RestClient::Exception => e
|
24
30
|
Response.new({:result => e.message.upcase, :response => JSON.parse(e.response || '{}')}.to_json)
|
25
31
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module TNSPayments
|
2
|
+
class Transaction < SimpleDelegator
|
3
|
+
attr_writer :minimum_order_id
|
4
|
+
|
5
|
+
def initialize transaction
|
6
|
+
@transaction = transaction
|
7
|
+
super
|
8
|
+
end
|
9
|
+
|
10
|
+
def minimum_order_id
|
11
|
+
@minimum_order_id ||= 10000000000
|
12
|
+
end
|
13
|
+
|
14
|
+
def order_id
|
15
|
+
minimum_order_id + @transaction.order_id.to_i
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/tns_payments/version.rb
CHANGED
@@ -199,7 +199,8 @@ private
|
|
199
199
|
end
|
200
200
|
|
201
201
|
def stub_session_token_purchase_request transaction, token
|
202
|
-
|
202
|
+
transaction = Transaction.new transaction
|
203
|
+
stub_request(:put, /https:\/\/:#{@gateway.api_key}@secure\.ap\.tnspayments\.com\/api\/rest\/version\/4\/merchant\/#{@gateway.merchant_id}\/order\/#{transaction.order_id}\/transaction\/#{transaction.transaction_id}/).
|
203
204
|
with :body => JSON.generate({
|
204
205
|
'apiOperation' => 'PAY',
|
205
206
|
'order' => {'reference' => transaction.reference.to_s},
|
@@ -215,7 +216,8 @@ private
|
|
215
216
|
end
|
216
217
|
|
217
218
|
def stub_credit_card_token_purchase_request transaction, token
|
218
|
-
|
219
|
+
transaction = Transaction.new transaction
|
220
|
+
stub_request(:put, /https:\/\/:#{@gateway.api_key}@secure\.ap\.tnspayments\.com\/api\/rest\/version\/4\/merchant\/#{@gateway.merchant_id}\/order\/#{transaction.order_id}\/transaction\/#{transaction.transaction_id}/).
|
219
221
|
with :body => JSON.generate({
|
220
222
|
'apiOperation' => 'PAY',
|
221
223
|
'order' => {'reference' => transaction.reference.to_s},
|
@@ -231,7 +233,8 @@ private
|
|
231
233
|
end
|
232
234
|
|
233
235
|
def stub_refund_request transaction
|
234
|
-
|
236
|
+
transaction = Transaction.new transaction
|
237
|
+
stub_request(:put, /https:\/\/:#{@gateway.api_key}@secure\.ap\.tnspayments\.com\/api\/rest\/version\/4\/merchant\/#{@gateway.merchant_id}\/order\/#{transaction.order_id}\/transaction\/#{transaction.transaction_id}/).
|
235
238
|
with :body => JSON.generate({
|
236
239
|
'apiOperation' => 'REFUND',
|
237
240
|
'transaction' => {'amount' => transaction.amount.to_s, 'currency' => transaction.currency, 'reference' => transaction.transaction_id.to_s}
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TNSPayments::TransactionTest < MiniTest::Unit::TestCase
|
4
|
+
def test_order_id_is_tns_minimum
|
5
|
+
transaction = MiniTest::Mock.new
|
6
|
+
transaction.expect :order_id, 0
|
7
|
+
|
8
|
+
assert_equal 10000000000, Transaction.new(transaction).order_id
|
9
|
+
assert transaction.verify
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_minimum_order_id_is_injectable
|
13
|
+
mock = MiniTest::Mock.new
|
14
|
+
mock.expect :order_id, 10
|
15
|
+
transaction = Transaction.new mock
|
16
|
+
transaction.minimum_order_id = 0
|
17
|
+
|
18
|
+
assert_equal 10, transaction.order_id
|
19
|
+
assert mock.verify
|
20
|
+
end
|
21
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tns_payments
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 15
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 8
|
10
|
+
version: 0.0.8
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Tim Cooper
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-04-
|
18
|
+
date: 2012-04-11 00:00:00 +10:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -106,10 +106,12 @@ files:
|
|
106
106
|
- lib/tns_payments/connection.rb
|
107
107
|
- lib/tns_payments/request.rb
|
108
108
|
- lib/tns_payments/response.rb
|
109
|
+
- lib/tns_payments/transaction.rb
|
109
110
|
- lib/tns_payments/version.rb
|
110
111
|
- test/helper.rb
|
111
112
|
- test/unit/conntection_test.rb
|
112
113
|
- test/unit/response_test.rb
|
114
|
+
- test/unit/transaction_test.rb
|
113
115
|
- tns_payments.gemspec
|
114
116
|
has_rdoc: true
|
115
117
|
homepage: ""
|
@@ -149,3 +151,4 @@ test_files:
|
|
149
151
|
- test/helper.rb
|
150
152
|
- test/unit/conntection_test.rb
|
151
153
|
- test/unit/response_test.rb
|
154
|
+
- test/unit/transaction_test.rb
|