cardgate 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,60 @@
1
+ module Cardgate
2
+
3
+ class Refund
4
+
5
+ attr_accessor :site_id, :referenced_transaction_id, :amount, :reason
6
+
7
+ def initialize(attributes = {})
8
+ attributes.each do |k,v|
9
+ send("#{k}=", v)
10
+ end
11
+ end
12
+
13
+ def default_params
14
+ {
15
+ refund: {
16
+ site_id: @site_id,
17
+ referenced_transaction_id: @referenced_transaction_id,
18
+ amount: @amount,
19
+ reason: @reason
20
+ }
21
+ }
22
+ end
23
+
24
+ def params
25
+ default_params.deep_merge!(refund_params)
26
+ end
27
+
28
+ def refund_params
29
+ {}
30
+ end
31
+
32
+ def initiate
33
+ @response ||= response
34
+
35
+ self
36
+ end
37
+
38
+ def transaction_id
39
+ @response.body['refund']['transaction_id']
40
+ end
41
+
42
+ def api_refund_endpoint
43
+ "/rest/v1/#{provider}/refund/"
44
+ end
45
+
46
+ private
47
+
48
+ def response
49
+ result = Cardgate::Gateway.connection.post do |req|
50
+ req.url api_refund_endpoint
51
+ req.headers['Accept'] = 'application/json'
52
+ req.body = params
53
+ end
54
+
55
+ Cardgate::Response.new(result)
56
+ end
57
+
58
+ end
59
+
60
+ end
@@ -0,0 +1,19 @@
1
+ module Cardgate
2
+
3
+ class Response
4
+
5
+ attr_reader :body
6
+
7
+ def initialize(response)
8
+ if response.body.nil?
9
+ raise Cardgate::Exception, 'Got an empty response from API'
10
+ elsif !response.status.between?(200, 299)
11
+ raise Cardgate::Exception, "#{response.body["error"]["code"]}: #{response.body["error"]["message"]}"
12
+ else
13
+ @body = response.body
14
+ end
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -0,0 +1,32 @@
1
+ module Cardgate
2
+
3
+ class Transaction
4
+
5
+ attr_reader :transaction_id, :site_id, :site_name, :payment_method, :status, :timestamp, :currency, :amount, :ref,
6
+ :first_name, :last_name, :company_name, :address, :city, :state, :postal_code, :country_code, :phone_number, :email
7
+
8
+ def initialize(attributes = {})
9
+ attributes.map do |attribute, value|
10
+ if attribute == 'customer' && !value.nil?
11
+ attributes['customer'].map do |attribute, value|
12
+ set_variable_from_attribute(attribute, value)
13
+ end
14
+ else
15
+ set_variable_from_attribute(attribute, value)
16
+ end
17
+ end
18
+ end
19
+
20
+ def successful?
21
+ [200, 210].include?(self.status.to_i)
22
+ end
23
+
24
+ private
25
+
26
+ def set_variable_from_attribute(attribute, value)
27
+ instance_variable_set("@#{attribute}".to_sym, value)
28
+ end
29
+
30
+ end
31
+
32
+ end
@@ -0,0 +1,24 @@
1
+ module Cardgate
2
+
3
+ class Transactions
4
+
5
+ def self.find(transaction_id)
6
+ result = Cardgate::Gateway.connection.get do |req|
7
+ req.url "/rest/v1/transactions/#{transaction_id}/"
8
+ req.headers['Accept'] = 'application/json'
9
+ end
10
+
11
+ response = Cardgate::Response.new(result)
12
+
13
+ transaction = response.body['transaction']
14
+
15
+ if !transaction.empty?
16
+ Cardgate::Transaction.new(transaction)
17
+ else
18
+ raise Cardgate::Exception, 'Transaction was empty'
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,3 @@
1
+ module Cardgate
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,27 @@
1
+ class Hash
2
+ # Returns a new hash with +self+ and +other_hash+ merged recursively.
3
+ #
4
+ # h1 = { x: { y: [4, 5, 6] }, z: [7, 8, 9] }
5
+ # h2 = { x: { y: [7, 8, 9] }, z: 'xyz' }
6
+ #
7
+ # h1.deep_merge(h2) # => {x: {y: [7, 8, 9]}, z: "xyz"}
8
+ # h2.deep_merge(h1) # => {x: {y: [4, 5, 6]}, z: [7, 8, 9]}
9
+ # h1.deep_merge(h2) { |key, old, new| Array.wrap(old) + Array.wrap(new) }
10
+ # # => {:x=>{:y=>[4, 5, 6, 7, 8, 9]}, :z=>[7, 8, 9, "xyz"]}
11
+ def deep_merge(other_hash, &block)
12
+ dup.deep_merge!(other_hash, &block)
13
+ end
14
+
15
+ # Same as +deep_merge+, but modifies +self+.
16
+ def deep_merge!(other_hash, &block)
17
+ other_hash.each_pair do |k,v|
18
+ tv = self[k]
19
+ if tv.is_a?(Hash) && v.is_a?(Hash)
20
+ self[k] = tv.deep_merge(v, &block)
21
+ else
22
+ self[k] = block && tv ? block.call(k, tv, v) : v
23
+ end
24
+ end
25
+ self
26
+ end
27
+ end
@@ -0,0 +1,93 @@
1
+ require 'test_helper'
2
+ require 'digest/md5'
3
+
4
+ module CardgateTestCases
5
+
6
+ class CallbackTestCases < Test::Unit::TestCase
7
+
8
+ def new_callback_hash(status, test=1)
9
+ elements = {
10
+ is_test: test,
11
+ transaction_id: 1,
12
+ currency: 'EUR',
13
+ amount: 1000,
14
+ ref: '420-1234567890',
15
+ status: status,
16
+ hash_key: 'abcdefg'
17
+ }
18
+
19
+ string = "1EUR1000420-1234567890#{status}abcdefg"
20
+ string = 'TEST' + string if test == 1
21
+
22
+ elements[:hash] = Digest::MD5.hexdigest(string)
23
+
24
+ elements
25
+ end
26
+
27
+ def test_valid_callback
28
+ callback = Cardgate::Callback.new(new_callback_hash(200))
29
+
30
+ assert callback.valid?
31
+
32
+ callback = Cardgate::Callback.new(new_callback_hash(200, 0))
33
+
34
+ assert callback.valid?
35
+ end
36
+
37
+ def test_invalid_callback
38
+ callback = Cardgate::Callback.new({
39
+ is_test: 0,
40
+ transaction_id: 1,
41
+ currency: 'EUR',
42
+ amount: 1000,
43
+ ref: '420-1234567890',
44
+ status: 200,
45
+ hash_key: 'abcdefg-----',
46
+ hash: "1EUR1000420-1234567890200abcdefg"
47
+ })
48
+
49
+ refute callback.valid?
50
+ end
51
+
52
+ def test_response
53
+ hash = new_callback_hash(200)
54
+ callback = Cardgate::Callback.new(hash)
55
+
56
+ assert_equal "#{hash[:transaction_id]}.#{hash[:status]}", callback.response
57
+ end
58
+
59
+ def test_successful_transaction
60
+ callback = Cardgate::Callback.new(new_callback_hash(200))
61
+
62
+ assert callback.successful?
63
+
64
+ callback = Cardgate::Callback.new(new_callback_hash(210))
65
+
66
+ assert callback.successful?
67
+ end
68
+
69
+ def test_failed_transaction
70
+ callback = Cardgate::Callback.new(new_callback_hash(300))
71
+
72
+ assert callback.failed?
73
+
74
+ callback = Cardgate::Callback.new(new_callback_hash(301))
75
+
76
+ assert callback.failed?
77
+ end
78
+
79
+ def test_recurring_transaction_failed
80
+ callback = Cardgate::Callback.new(new_callback_hash(310))
81
+
82
+ assert callback.recurring_failed?
83
+ end
84
+
85
+ def test_transaction_refunded
86
+ callback = Cardgate::Callback.new(new_callback_hash(400))
87
+
88
+ assert callback.refund?
89
+ end
90
+
91
+ end
92
+
93
+ end
@@ -0,0 +1,16 @@
1
+ require 'test_helper'
2
+
3
+ module CardgateTestCases
4
+
5
+ class CreditcardRefundTestCases < Test::Unit::TestCase
6
+
7
+ def test_params
8
+ refund = Cardgate::Creditcard::Refund.new({descriptor: 'Descriptor'})
9
+ params = refund.params
10
+
11
+ assert_equal 'Descriptor', params[:refund][:descriptor]
12
+ end
13
+
14
+ end
15
+
16
+ end
@@ -0,0 +1,15 @@
1
+ class CardgateFixtures
2
+
3
+ ISSUERS_LIST = '{"issuers":[{"list":"Short", "name":"Test Issuer", "id":121}, {"list":"Short", "name":"Test Issuer 2", "id":151}, {"list":"Long", "name":"Test Issuer 3", "id":171}]}'
4
+ ISSUERS_LIST_EMPTY = '{"issuers":[]}'
5
+
6
+ PAYMENT_SUCCESSFUL = '{"payment":{"transaction_id":2307459, "issuer_auth_url":"https://gateway.cardgateplus.com/simulator/?return_url=https://www.stofzuigermarkt.nl"}}'
7
+ PAYMENT_UNSUCCESSFUL = '{"error":{"message":"We encountered an internal error. Please try again later.", "code":"InternalServerError", "resource":"/rest/v1/ideal/payment/"}}'
8
+
9
+ TRANSACTION_WITHOUT_CUSTOMER = '{"transaction":{"status":200, "payment_method":"ideal", "timestamp":"2014-04-22T15:19:01", "site_id":5112, "currency":"EUR", "amount":100, "ref":"test4", "transaction_id":2307824, "customer":null}}'
10
+ TRANSACTION_WITH_CUSTOMER = '{"transaction":{"status":200, "payment_method":"ideal", "timestamp":"2014-04-22T15:19:01", "site_id":5112, "currency":"EUR", "amount":100, "ref":"test4", "transaction_id":2307824, "customer":{"first_name":"Youri", "last_name":"van der Lans", "company_name":"ITflows", "address":"Coenecoop 750", "city":"Waddinxveen", "state":"Zuid-Holland", "postal_code":"2741 PW", "country_code":"NL", "phone_number":"1234567890", "email":"youri@itflows.nl"}}}'
11
+
12
+ REFUND_SUCCESSFUL = '{"refund":{"transaction_id":2307831}}'
13
+ REFUND_UNSUCCESSFUL = '{"error":{"message":"Refund amount exceeded transaction amount.", "code":"TransactionRefundAmount- Exceeded", "resource":"/rest/v1/ideal/refund/"}}'
14
+
15
+ end
@@ -0,0 +1,48 @@
1
+ require 'test_helper'
2
+
3
+ module CardgateTestCases
4
+
5
+ class ClassMethodsTest < Test::Unit::TestCase
6
+
7
+ def test_is_test_environment?
8
+ Cardgate::Gateway.environment = :test
9
+
10
+ assert Cardgate::Gateway.is_test_environment?
11
+ end
12
+
13
+ def test_is_not_test_environment?
14
+ Cardgate::Gateway.environment = :live
15
+
16
+ refute Cardgate::Gateway.is_test_environment?
17
+ end
18
+
19
+ end
20
+
21
+ class GatewayTest < Test::Unit::TestCase
22
+
23
+ def test_request_url_for_test
24
+ Cardgate::Gateway.environment = :test
25
+
26
+ assert_equal 'https://api-test.cardgate.com', Cardgate::Gateway.request_url
27
+ end
28
+
29
+ def test_request_url_for_live
30
+ Cardgate::Gateway.environment = :live
31
+
32
+ assert_equal 'https://api.cardgate.com', Cardgate::Gateway.request_url
33
+ end
34
+
35
+ def test_connection_raises
36
+ assert_raises Cardgate::Exception do
37
+ Cardgate::Gateway.connection
38
+ end
39
+ end
40
+
41
+ def test_request_logger
42
+ Cardgate::Gateway.request_logger = true
43
+
44
+ assert_equal true, Cardgate::Gateway.request_logger
45
+ end
46
+ end
47
+
48
+ end
@@ -0,0 +1,21 @@
1
+ require 'test_helper'
2
+
3
+ module CardgateTestCases
4
+
5
+ class IdealIssuerTestCases < Test::Unit::TestCase
6
+
7
+ def test_eql
8
+
9
+ issuer_1a = Cardgate::Ideal::Issuer.new(1, 'Name', 'Short')
10
+ issuer_1b = Cardgate::Ideal::Issuer.new(1, 'Name', 'Short')
11
+ issuer_2 = Cardgate::Ideal::Issuer.new(2, 'Name', 'Short')
12
+
13
+ assert_equal issuer_1a, issuer_1a
14
+ assert_equal issuer_1a, issuer_1b
15
+ assert_not_equal issuer_1a, issuer_2
16
+
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -0,0 +1,48 @@
1
+ require 'test_helper'
2
+
3
+ module CardgateTestCases
4
+
5
+ class IdealIssuersTestCases < Test::Unit::TestCase
6
+
7
+ def stub_cardgate_connection(response)
8
+ cardgate_connection do |stubs|
9
+ stubs.get('/rest/v1/ideal/issuers/') { [200, {}, response] }
10
+ end
11
+ end
12
+
13
+ def test_list_returns_issuers
14
+ cardgate_connection = stub_cardgate_connection(CardgateFixtures::ISSUERS_LIST)
15
+
16
+ Cardgate::Gateway.stubs(:connection).returns(cardgate_connection)
17
+
18
+ issuers = Cardgate::Ideal::Issuers.list
19
+
20
+ issuer = issuers.first
21
+
22
+ assert issuer.is_a? Cardgate::Ideal::Issuer
23
+ assert_equal 121, issuer.id
24
+ assert_equal 'Test Issuer', issuer.name
25
+ assert_equal 'Short', issuer.list
26
+ end
27
+
28
+ def test_list_no_response
29
+ cardgate_connection = stub_cardgate_connection(nil)
30
+
31
+ Cardgate::Gateway.stubs(:connection).returns(cardgate_connection)
32
+
33
+ assert_raises Cardgate::Exception do
34
+ Cardgate::Ideal::Issuers.list
35
+ end
36
+ end
37
+
38
+ def test_list_no_issuers
39
+ cardgate_connection = stub_cardgate_connection(CardgateFixtures::ISSUERS_LIST_EMPTY)
40
+
41
+ Cardgate::Gateway.stubs(:connection).returns(cardgate_connection)
42
+
43
+ assert Cardgate::Ideal::Issuers.list.empty?
44
+ end
45
+
46
+ end
47
+
48
+ end
@@ -0,0 +1,108 @@
1
+ require 'test_helper'
2
+
3
+ module CardgateTestCases
4
+
5
+ class IdealPaymentTestCases < Test::Unit::TestCase
6
+
7
+ def stub_cardgate_connection(status, response)
8
+ cardgate_connection do |stubs|
9
+ stubs.post('/rest/v1/ideal/payment/') { [status, {}, response] }
10
+ end
11
+ end
12
+
13
+ def new_payment_attributes_hash
14
+ {
15
+ site_id: 1,
16
+ issuer_id: 1,
17
+ return_url: 'https://stofzuigermarkt.nl',
18
+ ref: 'transaction-reference',
19
+ amount: 100,
20
+ currency: 'EUR',
21
+ ip_address: '127.0.0.1',
22
+ description: 'test'
23
+ }
24
+ end
25
+
26
+ def test_successful_payment
27
+ cardgate_connection = stub_cardgate_connection(201, CardgateFixtures::PAYMENT_SUCCESSFUL)
28
+
29
+ Cardgate::Gateway.stubs(:connection).returns(cardgate_connection)
30
+
31
+ payment_attributes = new_payment_attributes_hash
32
+
33
+ payment = Cardgate::Ideal::Payment.new(payment_attributes)
34
+ payment.initiate
35
+
36
+ assert_equal 'https://gateway.cardgateplus.com/simulator/?return_url=https://www.stofzuigermarkt.nl', payment.payment_url
37
+ assert_equal 2307459, payment.transaction_id
38
+ end
39
+
40
+ def test_unsuccessful_payment
41
+ cardgate_connection = stub_cardgate_connection(500, CardgateFixtures::PAYMENT_UNSUCCESSFUL)
42
+
43
+ Cardgate::Gateway.stubs(:connection).returns(cardgate_connection)
44
+
45
+ payment_attributes = new_payment_attributes_hash
46
+
47
+ payment = Cardgate::Ideal::Payment.new(payment_attributes)
48
+
49
+ assert_raises Cardgate::Exception do
50
+ payment.initiate
51
+ end
52
+ end
53
+
54
+ def test_params
55
+ payment_attributes = new_payment_attributes_hash
56
+ customer_attributes = {
57
+ first_name: 'Youri',
58
+ last_name: 'van der Lans',
59
+ company_name: 'ITflows',
60
+ address: 'Coenecoop 750',
61
+ city: 'Waddinxveen',
62
+ state: 'Zuid-Holland',
63
+ postal_code: '2741 PW',
64
+ country_code: 'NL',
65
+ phone_number: '1234567890',
66
+ email: 'youri@itflows.nl'
67
+ }
68
+
69
+ payment_attributes.merge!(customer_attributes)
70
+
71
+ payment = Cardgate::Ideal::Payment.new(payment_attributes)
72
+ params = payment.params
73
+
74
+ assert_equal 1, params[:payment][:site_id]
75
+ assert_equal 1, params[:payment][:issuer_id]
76
+ assert_equal 'https://stofzuigermarkt.nl', params[:payment][:return_url]
77
+ assert_equal 'transaction-reference', params[:payment][:ref]
78
+ assert_equal 100, params[:payment][:amount]
79
+ assert_equal 'EUR', params[:payment][:currency]
80
+ assert_equal '127.0.0.1', params[:payment][:ip_address]
81
+ assert_equal 'test', params[:payment][:description]
82
+
83
+ assert_not_nil params[:payment][:customer]
84
+
85
+ assert_equal 'Youri', params[:payment][:customer][:first_name]
86
+ assert_equal 'van der Lans', params[:payment][:customer][:last_name]
87
+ assert_equal 'ITflows', params[:payment][:customer][:company_name]
88
+ assert_equal 'Coenecoop 750', params[:payment][:customer][:address]
89
+ assert_equal 'Waddinxveen', params[:payment][:customer][:city]
90
+ assert_equal 'Zuid-Holland', params[:payment][:customer][:state]
91
+ assert_equal '2741 PW', params[:payment][:customer][:postal_code]
92
+ assert_equal 'NL', params[:payment][:customer][:country_code]
93
+ assert_equal '1234567890', params[:payment][:customer][:phone_number]
94
+ assert_equal 'youri@itflows.nl', params[:payment][:customer][:email]
95
+ end
96
+
97
+ def test_empty_customer_params
98
+ payment_attributes = new_payment_attributes_hash
99
+
100
+ payment = Cardgate::Ideal::Payment.new(payment_attributes)
101
+ params = payment.params
102
+
103
+ assert_nil params[:payment][:customer]
104
+ end
105
+
106
+ end
107
+
108
+ end