cardflex-ruby 0.0.1 → 0.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3ef2bceb03de84a741378a2761eb5bb7c6736316
4
- data.tar.gz: afb94e6ff243cee5a76ec19051fdedb46b1dbfda
3
+ metadata.gz: 1da29dfbacec65d1aeb178a2aca6f0814f17ebbb
4
+ data.tar.gz: 17ac130da88ce00fd6b42e3c9225561b87c8a3ec
5
5
  SHA512:
6
- metadata.gz: 2169e2f0d1cbb395ad6099b5b66a74160270dc3de4a577d4189434fe85c61952b378119b4b6a59170f3f2792f715ae21877a52d596155ed5ce1b4ac81722341e
7
- data.tar.gz: 7e894a100ca40369a959849ab5efb8fefef68d9f2e80bd1c2f11f038da2ce259fb61fd943e4ed1bac7e06434129d7a9fb7cbddf3a438385d9a66e421ced08a9f
6
+ metadata.gz: 0703d9e0b1500fed41ae0f4f6ea6ce2af0fc08d955bc8541c2ad14b393e98baa1932b25a30d63c559b5411391c6b911c584c659e6dbd7c8e28b9002c1be93cd5
7
+ data.tar.gz: 293bd2099846cf4d887c219fc42cc22c39e138d58b75de68341e41d0dacb89316eb298c93115d008c58258830a471d8d5f718eb2d40422ccae7366d92a8c3413
data/README.md CHANGED
@@ -55,8 +55,10 @@ result = Cardflex::ThreeStep.complete(token_id)
55
55
 
56
56
  if result.success?
57
57
  # payment has been processed, send user the all clear
58
+ # the result of the transaction is in result.transaction
59
+ result.transaction.transaction_id
58
60
  else
59
- @error_message = step_one.result_text
61
+ @error_message = result.result_text
60
62
  end
61
63
  ```
62
64
 
data/cardflex.gemspec CHANGED
@@ -4,6 +4,7 @@ require 'cardflex/version'
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "cardflex-ruby"
6
6
  s.summary = "Cardflex gateway ruby client library"
7
+ s.description = "A ruby library for interacting with the Cardflex payment gateway API"
7
8
  s.version = Cardflex::Version::Full
8
9
  s.license = "MIT"
9
10
  s.author = "Tom King"
@@ -29,6 +29,15 @@ module Cardflex
29
29
  base.extend(self)
30
30
  end
31
31
 
32
+ # add support for merchant_defined_field_#
33
+ def method_missing(method, *args)
34
+ if method =~ /^merchant_defined_field_\d+$/
35
+ instance_variable_get("@#{method}")
36
+ else
37
+ super
38
+ end
39
+ end
40
+
32
41
  include ClassMethods
33
42
  end
34
43
  end
@@ -1,14 +1,15 @@
1
1
  module Cardflex
2
2
  class Configuration
3
- API_VERSION = 3
3
+ API_VERSION = 2
4
4
  SERVER = "secure.cardflexonline.com"
5
5
  PORT = 443
6
6
 
7
7
  class << self
8
8
  attr_writer :logger, :api_key
9
+ attr_accessor :username, :password
9
10
  end
10
11
 
11
- attr_reader :api_key
12
+ attr_reader :api_key, :username, :password
12
13
 
13
14
  def self.expectant_reader(*attributes)
14
15
  attributes.each do |attribute|
@@ -22,7 +23,7 @@ module Cardflex
22
23
  expectant_reader :api_key, :environment
23
24
 
24
25
  def initialize(options={})
25
- [:environment, :api_key, :logger].each do |attr|
26
+ [:environment, :api_key, :username, :password, :logger].each do |attr|
26
27
  instance_variable_set("@#{attr}", options[attr])
27
28
  end
28
29
  end
@@ -43,6 +44,8 @@ module Cardflex
43
44
  config = new(
44
45
  :environment => @environment,
45
46
  :logger => logger,
47
+ :username => username,
48
+ :password => password,
46
49
  :api_key => api_key
47
50
  )
48
51
  end
@@ -59,6 +62,10 @@ module Cardflex
59
62
  "/api/v2/three-step"
60
63
  end
61
64
 
65
+ def query_path
66
+ "/api/query.php"
67
+ end
68
+
62
69
  def server
63
70
  SERVER
64
71
  end
data/lib/cardflex/http.rb CHANGED
@@ -6,13 +6,19 @@ module Cardflex
6
6
  @config = config
7
7
  end
8
8
 
9
+ def get(params={})
10
+ uri = "#{@config.query_path}#{_hash_to_query_string(params)}"
11
+ response = _do_http(Net::HTTP::Get, uri)
12
+ Xml.parse(response.body)
13
+ end
14
+
9
15
  def post(params={})
10
16
  params = _add_api_key(params)
11
- response = _do_http(Net::HTTP::Post, Xml.hash_to_xml(params))
17
+ response = _do_http(Net::HTTP::Post, @config.three_step_path, Xml.hash_to_xml(params))
12
18
  Xml.parse(response.body)
13
19
  end
14
20
 
15
- def _do_http(http_verb, body=nil)
21
+ def _do_http(http_verb, path, body=nil)
16
22
  connection = Net::HTTP.new(@config.server, @config.port)
17
23
  connection.open_timeout = 60
18
24
  connection.read_timeout = 60
@@ -24,7 +30,7 @@ module Cardflex
24
30
  end
25
31
 
26
32
  connection.start do |http|
27
- request = http_verb.new("#{@config.three_step_path}")
33
+ request = http_verb.new(path)
28
34
  request['Accept'] = 'text/xml'
29
35
  @config.logger.debug("[Cardflex] [#{_current_time}] #{request.method} as text/xml")
30
36
  if body
@@ -42,6 +48,20 @@ module Cardflex
42
48
  raise Cardflex::SSLCertificateError
43
49
  end
44
50
 
51
+ def _hash_to_query_string(hash)
52
+ str = "?"
53
+ hash.each do |key, value|
54
+ case value
55
+ when ::Array
56
+ str << "#{key}=#{value.join(',')}&"
57
+ when ::String
58
+ str << "#{key}=#{value}&"
59
+ end
60
+ end
61
+
62
+ str.gsub(/&$/, '') # remove trailing &, if any
63
+ end
64
+
45
65
  def _current_time
46
66
  Time.now.utc.strftime("%d/%b/%Y %H:%M:%S %Z")
47
67
  end
@@ -7,6 +7,7 @@ module Cardflex
7
7
  @config = gateway.config
8
8
  end
9
9
 
10
+ # Three Step API
10
11
  def get_form_url(attributes)
11
12
  root = attributes.keys[0]
12
13
  raise ArgumentError, 'missing :redirect_url' unless attributes[root][:redirect_url]
@@ -41,6 +41,7 @@ module Cardflex
41
41
  set_instance_variables_from_hash(attributes)
42
42
  end
43
43
 
44
+ # Three Step API
44
45
  def self.request(attributes)
45
46
  Configuration.gateway.transaction.request(attributes)
46
47
  end
@@ -48,5 +49,10 @@ module Cardflex
48
49
  create_helper_methods(Type) do |attributes|
49
50
  request(attributes)
50
51
  end
52
+
53
+ # Query API
54
+ def self.query(attributes)
55
+ Configuration.gateway.transaction.query(attributes)
56
+ end
51
57
  end
52
58
  end
@@ -7,11 +7,22 @@ module Cardflex
7
7
  @config = gateway.config
8
8
  end
9
9
 
10
+ # Three Step API
10
11
  def request(attributes)
11
12
  res = @config.http.post(attributes)
12
13
  _handle_response(res)
13
14
  end
14
15
 
16
+ # Query API
17
+ def query(attributes)
18
+ unless username = @config.username && password = @config.password
19
+ raise ArgumentError, 'missing username and/or password'
20
+ end
21
+
22
+ res = @config.http.get(attributes.merge(:username => username, :password => password))
23
+ _handle_query_response(res)
24
+ end
25
+
15
26
  def _handle_response(res)
16
27
  if res[:response][:result] == '1'
17
28
  SuccessResponse.new(:transaction => Transaction.new(@gateway, res[:response]))
@@ -19,5 +30,13 @@ module Cardflex
19
30
  ErrorResponse.new(@gateway, res[:response])
20
31
  end
21
32
  end
33
+
34
+ def _handle_query_response(res)
35
+ if res[:nm_response].keys.include?(:error_response)
36
+ ErrorResponse.new(@gateway, :result => 3, :result_text => res[:nm_response][:error_response])
37
+ else
38
+ SuccessResponse.new(:transaction => Transaction.new(@gateway, res[:nm_response][:transaction]))
39
+ end
40
+ end
22
41
  end
23
42
  end
@@ -1,9 +1,9 @@
1
1
  module Cardflex
2
2
  module Version
3
3
  Major = '0'
4
- Minor = '0'
5
- Tiny = '1'
4
+ Minor = '1'
5
+ Patch = '1'
6
6
 
7
- Full = "#{Major}.#{Minor}.#{Tiny}"
7
+ Full = "#{Major}.#{Minor}.#{Patch}"
8
8
  end
9
9
  end
@@ -25,24 +25,6 @@ module Cardflex
25
25
  def self._snakecase(key)
26
26
  key.tr('-', '_').to_sym
27
27
  end
28
-
29
- def self.parse(string)
30
- return {} if string.nil?
31
-
32
- doc = REXML::Document.new(string)
33
- { _snakecase(doc.root.name).to_sym => _to_hash({}, doc.root) }
34
- end
35
-
36
- def self._to_hash(hash, element)
37
- if element.has_elements?
38
- element.each_element do |child|
39
- hash[_snakecase(child.name).to_sym] = child.text
40
- end
41
- end
42
-
43
- hash
44
- end
45
-
46
28
  end
47
29
  end
48
30
  end
@@ -13,7 +13,7 @@ describe Cardflex::Http do
13
13
  new_output = StringIO.new
14
14
  Cardflex::Configuration.logger = Logger.new(new_output)
15
15
  Cardflex::Configuration.logger.level = Logger::DEBUG
16
- Cardflex::Configuration.instantiate.http._do_http(Net::HTTP::Get, '/')
16
+ Cardflex::Configuration.instantiate.http._do_http(Net::HTTP::Get, '/api/v2/three-step')
17
17
  expect(new_output.string).to include("[Cardflex] [01/Jan/2014 00:00:00 UTC] GET")
18
18
  expect(new_output.string).to include("[Cardflex] [01/Jan/2014 00:00:00 UTC] GET 200")
19
19
  end
@@ -0,0 +1,35 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ # Unfortunately, there is no testing account for the query API. Stub it instead
4
+
5
+ describe Cardflex::Transaction do
6
+ it 'should raise an argument error if no username/password supplied' do
7
+ expect do
8
+ Cardflex::Transaction.query(:transaction_id => 1)
9
+ end.to raise_error(ArgumentError)
10
+ end
11
+
12
+ it 'should fail to query the cardflex api' do
13
+ allow_any_instance_of(Cardflex::Http).to receive(:_do_http).and_return(
14
+ SpecHelper::FakeResponse.new("<nm_response><error_response>failure</error_response></nm_response>")
15
+ )
16
+ Cardflex::Configuration.username = "demo"
17
+ Cardflex::Configuration.password = "password"
18
+ result = Cardflex::Transaction.query(:transaction_id => 1)
19
+
20
+ expect(result.success?).to be false
21
+ expect(result.result_text).to eq "failure"
22
+ end
23
+
24
+ it 'should query the cardflex api' do
25
+ allow_any_instance_of(Cardflex::Http).to receive(:_do_http).and_return(
26
+ SpecHelper::FakeResponse.new("<nm_response><transaction><transaction_id>1</transaction_id></transaction></nm_response>")
27
+ )
28
+ Cardflex::Configuration.username = "demo"
29
+ Cardflex::Configuration.password = "password"
30
+ result = Cardflex::Transaction.query(:transaction_id => 1)
31
+
32
+ expect(result.success?).to be true
33
+ expect(result.transaction.transaction_id.to_i).to eq 1
34
+ end
35
+ end
@@ -8,6 +8,10 @@ describe Cardflex::BaseModule do
8
8
  B = 'b'
9
9
  C = 'c'
10
10
  end
11
+
12
+ def initialize(attrs)
13
+ set_instance_variables_from_hash(attrs)
14
+ end
11
15
  end
12
16
 
13
17
  it 'should assign instance variables from a hash' do
@@ -31,4 +35,13 @@ describe Cardflex::BaseModule do
31
35
  TestClass.__send__(:create_helper_methods, TestClass::Type)
32
36
  expect(TestClass.methods).to include(:a, :b, :c)
33
37
  end
38
+
39
+ it 'should have merchant defined fields available' do
40
+ sut = TestClass.new(:merchant_defined_field_1 => 'test')
41
+ expect(sut.merchant_defined_field_1).to eq 'test'
42
+ end
43
+
44
+ it 'should raise an undefined method error for non existent merchant field' do
45
+ expect { TestClass.new.merchant_defined_field_1 }.to raise_error
46
+ end
34
47
  end
@@ -23,7 +23,7 @@ describe Cardflex::Configuration do
23
23
  expect { Cardflex::Configuration.environment = :invalid }.to raise_error ArgumentError
24
24
  end
25
25
 
26
- it 'should use http in development, https otherwise' do
26
+ it 'should use https' do
27
27
  sut = Cardflex::Configuration.new({ :environment => :test })
28
28
  expect(sut.protocol).to eq "https"
29
29
  expect(sut.ssl?).to be true
@@ -34,14 +34,19 @@ describe Cardflex::Configuration do
34
34
  expect(sut.ca_file).to match "ca-certificates.ca.crt"
35
35
  end
36
36
 
37
- it 'should get the 3 step base url' do
37
+ it 'should get the 3 step url' do
38
38
  sut = Cardflex::Configuration.new({ :environment => :test })
39
39
  expect(sut.three_step_path).to match "/api/v2/three-step"
40
40
  end
41
41
 
42
+ it 'should get the query url' do
43
+ sut = Cardflex::Configuration.new({ :environment => :test })
44
+ expect(sut.query_path).to match "/api/query.php"
45
+ end
46
+
42
47
  it 'should get the api version' do
43
48
  sut = Cardflex::Configuration.new
44
- expect(sut.api_version).to eq 3
49
+ expect(sut.api_version).to eq 2
45
50
  end
46
51
 
47
52
  it 'should get the correct port' do
@@ -14,5 +14,23 @@ describe Cardflex::Http do
14
14
  sut = Cardflex::Configuration.instantiate.http._verify_ssl_certificate(false, context)
15
15
  expect(sut).to eq false
16
16
  end
17
+
18
+ it 'should convert a hash to a query string' do
19
+ http = Cardflex::Configuration.instantiate.http
20
+ result = http._hash_to_query_string(:field1 => 'test', :field2 => 'test')
21
+ expect(result).to eq "?field1=test&field2=test"
22
+ end
23
+
24
+ it 'should convert an array to comma separated string values' do
25
+ http = Cardflex::Configuration.instantiate.http
26
+ result = http._hash_to_query_string(:field1 => ["a", "b", "c"])
27
+ expect(result).to eq "?field1=a,b,c"
28
+ end
29
+
30
+ it 'should not convert a nested hash' do
31
+ http = Cardflex::Configuration.instantiate.http
32
+ result = http._hash_to_query_string(:field1 => 'test', :field2 => { :field3 => "a" })
33
+ expect(result).to eq "?field1=test"
34
+ end
17
35
  end
18
36
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cardflex-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom King
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-23 00:00:00.000000000 Z
11
+ date: 2014-12-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: builder
@@ -24,7 +24,7 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 2.0.0
27
- description:
27
+ description: A ruby library for interacting with the Cardflex payment gateway API
28
28
  email:
29
29
  executables: []
30
30
  extensions: []
@@ -59,6 +59,7 @@ files:
59
59
  - lib/ssl/ca-certificates.ca.crt
60
60
  - spec/integration/cardflex/http_spec.rb
61
61
  - spec/integration/cardflex/plan_spec.rb
62
+ - spec/integration/cardflex/query_spec.rb
62
63
  - spec/integration/cardflex/three_step_gateway_spec.rb
63
64
  - spec/integration/cardflex/three_step_spec.rb
64
65
  - spec/integration/spec_helper.rb