dibs 0.1.0

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,18 @@
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
18
+ spec/config.yml
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dibs.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Maciej Litwiniuk
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,61 @@
1
+ # Dibs
2
+
3
+ DIBS payment API wrapper for Ruby. For now it supports only [`authorize`](http://tech.dibs.dk/dibs_api/payment_functions/authcgi/) and
4
+ [`capture`](http://tech.dibs.dk/dibs_api/payment_functions/capturecgi/) calls.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'dibs'
11
+
12
+ And then execute:
13
+
14
+ $ bundle install
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install dibs
19
+
20
+ ## Usage
21
+
22
+ 1. Obtain merchant id, key1 and key2 from http://www.dibs.dk/
23
+ 2. Initialize DIBS class:
24
+
25
+ `@dibs = ::Dibs::Dibs.new(merchant,key1,key2)`
26
+
27
+ 3. Construct a hash of required parameters:
28
+ ```
29
+ parameters = {
30
+ :amount=>10000, # 100
31
+ :currency=>208, # DDK
32
+ :cardno=>'5019100000000000',
33
+ :expmon=>'06',
34
+ :expyear=>'24',
35
+ :cvc=>'684',
36
+ :orderId=>"abc_test_#{Time.now.to_s.gsub(/\s/, '_')}", # HAS TO BE UNIQUE
37
+ :test=>true # DO AS YOU WANT
38
+ }
39
+ ```
40
+
41
+ 4. call `authorize` method and pass in parameters
42
+
43
+ `@dibs.authorize(parameters)`
44
+
45
+ 5. And you're done :)
46
+
47
+ ## Contributing
48
+
49
+ 1. Fork it
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create new Pull Request
54
+
55
+ ## TODO
56
+
57
+ 1. Cleanup
58
+ 2. Rest of API methods
59
+ 3. Better error handling?
60
+
61
+
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/dibs/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Maciej Litwiniuk"]
6
+ gem.email = ["maciej@litwiniuk.net"]
7
+ gem.description = %q{DIBS payment gateway library}
8
+ gem.summary = %q{DIBS payment API wrapper for Ruby}
9
+ gem.homepage = "https://github.com/prograils/dibs"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "dibs"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Dibs::VERSION
17
+ gem.add_development_dependency "rspec", ">= 2.0.0"
18
+ end
@@ -0,0 +1,89 @@
1
+ # encoding: UTF-8
2
+ require "dibs/version"
3
+ require 'net/http'
4
+ require 'net/https'
5
+ require 'digest/md5'
6
+ require 'dibs/core_ext' unless Object.instance_methods.member?('to_query')
7
+ require 'results.rb'
8
+ require 'errors.rb'
9
+ module Dibs
10
+ class Dibs
11
+ @@server = "https://payment.architrade.com"
12
+ def initialize(merchant, key1, key2)
13
+ @merchant, @key1, @key2 = merchant, key1, key2
14
+ end
15
+
16
+ def authorize(opts={})
17
+ opts = {
18
+ :merchant=>@merchant,
19
+ :amount=>0,
20
+ :currency=>'',
21
+ :cardno=>'',
22
+ :expmon=>'',
23
+ :expyear=>'',
24
+ :cvc=>'',
25
+ :orderId=>'',
26
+ :test=>false
27
+ }.merge(opts)
28
+ opts.symbolize_keys!
29
+ check_for_missing_parameter opts, %w{ merchant amount currency cardno expmon expyear cvc orderId }
30
+ md5 = "#{@key1}merchant=#{@merchant}&orderid=#{opts[:orderId]}&currency=#{opts[:currency]}&amount=#{opts[:amount]}"
31
+ opts[:md5key]=calculate_md5(md5)
32
+ endpoint = '/cgi-ssl/auth.cgi'
33
+ res = do_http_post(opts, endpoint)
34
+ ::Dibs::Results::Authorize.new(res.body)
35
+ end
36
+
37
+ def call_authorize_with_test_data
38
+ self.authorize
39
+ end
40
+
41
+ def capture(opts={})
42
+ opts = {
43
+ :merchant=>@merchant,
44
+ :amount=>0,
45
+ :transact=>'',
46
+ :orderid=>'',
47
+ :test=>false,
48
+ :force=>false
49
+ }.merge(opts)
50
+ if opts[:amount].blank? or opts[:transact].blank? or order[:orderid].blank?
51
+ raise ::Dibs::Errors::ParameterMissingError
52
+ end
53
+ md5 = "#{@key1}merchant=#{@merchant}&orderid=#{opts[:orderId]}&transact=#{opts[:transact]}&amount=#{opts[:amount]}"
54
+ opts[:md5key]=calculate_md5(md5)
55
+ endpoint = '/cgi-ssl/capture.cgi'
56
+ res = do_http_post(opts, endpoint)
57
+ ::Dibs::Results::Capture.new(res.body)
58
+ end
59
+
60
+ private
61
+ def do_http_post(opts, endpoint)
62
+ opts[:test] = 'yes' if opts[:test]
63
+ opts[:textreply] = 'yes'
64
+ opts[:fullreply] = 'yes'
65
+ opts[:postype] = 'ssl'
66
+ uri = URI("#{@@server}#{endpoint}")
67
+ http = Net::HTTP.new(uri.host, uri.port)
68
+ http.use_ssl = true
69
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
70
+ http.post(uri.path, opts.to_query)
71
+ end
72
+
73
+ def calculate_md5(md5)
74
+ md5 = Digest::MD5.hexdigest(md5)
75
+ md5 = Digest::MD5.hexdigest("#{@key2}#{md5}")
76
+ end
77
+
78
+ def check_for_missing_parameter(opts, keys_array)
79
+ keys_array.each do |k|
80
+ ks = k.to_sym
81
+ if not opts[ks] or opts[ks] == ''
82
+ raise ::Dibs::Errors::ParameterMissingError
83
+ end
84
+ end
85
+ end
86
+
87
+ end
88
+
89
+ end
@@ -0,0 +1,20 @@
1
+
2
+ # @TODO looks like shit, to be fixed. Keeping it just for now, to make shit
3
+ # work
4
+ class Hash
5
+ def to_query
6
+ keys.map{|k| self.key_to_query(k) }.join('&')
7
+ end
8
+
9
+ def key_to_query(key)
10
+ require 'cgi' unless defined?(CGI) && defined?(CGI::escape)
11
+ "#{CGI.escape(key.to_s)}=#{CGI.escape(self[key].to_s)}"
12
+ end
13
+
14
+ def symbolize_keys!
15
+ keys.each do |key|
16
+ self[(key.to_sym rescue key) || key] = delete(key)
17
+ end
18
+ self
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module Dibs
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,7 @@
1
+ module Dibs
2
+ module Errors
3
+ class ParameterMissingError < Exception
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,4 @@
1
+ # encoding: UTF-8
2
+ require 'results/dibs_result.rb'
3
+ require 'results/authorize.rb'
4
+ require 'results/capture.rb'
@@ -0,0 +1,26 @@
1
+ # encoding: UTF-8
2
+ module Dibs
3
+ module Results
4
+ class Authorize < DibsResult
5
+
6
+ def result_text
7
+ case self.reason
8
+ when "0" then "Rejected by acquirer."
9
+ when "1" then "Communication problems."
10
+ when "2" then "Error in the parameters sent to the DIBS server. An additional parameter called \"message\" is returned, with a value that may help identifying the error."
11
+ when "3" then "Error at the acquirer."
12
+ when "4" then "Credit card expired."
13
+ when "5" then "Your shop does not support this credit card type, the credit card type could not be identified, or the credit card number was not modulus correct."
14
+ when "6" then "Instant capture failed."
15
+ when "7" then "The order number (orderid) is not unique."
16
+ when "8" then "There number of amount parameters does not correspond to the number given in the split parameter."
17
+ when "9" then "Control numbers (cvc) are missing."
18
+ when "10" then "The credit card does not comply with the credit card type."
19
+ when "11" then "Declined by DIBS Defender."
20
+ when "20" then "Cancelled by user at 3D Secure authentication step"
21
+ end
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,28 @@
1
+ # encoding: UTF-8
2
+ module Dibs
3
+ module Results
4
+ class Capture < DibsResult
5
+
6
+ def result_text
7
+ case self.reason
8
+ when "0" then "Accepted"
9
+ when "1" then "No response from acquirer."
10
+ when "2" then "Timeout"
11
+ when "3" then "Credit card expired."
12
+ when "4" then "Rejected by acquirer."
13
+ when "5" then "Authorisation older than 7 days. "
14
+ when "6" then "Transaction status on the DIBS server does not allow capture."
15
+ when "7" then "Amount too high."
16
+ when "8" then "Error in the parameters sent to the DIBS server. An additional parameter called \"message\" is returned, with a value that may help identifying the error."
17
+ when "9" then "Order number (orderid) does not correspond to the authorisation order number."
18
+ when "10" then "Re-authorisation of the transaction was rejected."
19
+ when "11" then "Not able to communicate with the acquier."
20
+ when "12" then "Confirm request error"
21
+ when "14" then "Capture is called for a transaction which is pending for batch - i.e. capture was already called"
22
+ when "15" then "Capture was blocked by DIBS."
23
+ end
24
+ end
25
+
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,35 @@
1
+ # encoding: UTF-8
2
+ module Dibs
3
+ module Results
4
+ class DibsResult
5
+ attr_reader :result_parsed
6
+ attr_reader :result
7
+
8
+ def initialize(result_string)
9
+ @result = result_string
10
+ parts = @result.split(/&|=/)
11
+ @result_parsed = Hash[*parts]
12
+
13
+ end
14
+
15
+ def accepted?
16
+ @result_parsed['status'] == 'ACCEPTED'
17
+ end
18
+
19
+ def declined?
20
+ not self.accepted?
21
+ end
22
+
23
+ def method_missing(sym, *args, &block)
24
+ return @result_parsed[sym.to_s] if @result_parsed.has_key?(sym.to_s)
25
+ super(sym, *args, &block)
26
+ end
27
+
28
+ def result_text
29
+ "not implemented"
30
+ end
31
+
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,4 @@
1
+ default:
2
+ merchant: abc
3
+ key1: foo
4
+ key2: bar
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+ describe Dibs do
3
+ context "Authorization" do
4
+ before(:each) do
5
+ @dibs = ::Dibs::Dibs.new(API_CONFIG['merchant'],API_CONFIG['key1'],API_CONFIG['key2'])
6
+ @good_looking_data = {
7
+ :amount=>10000, # 100
8
+ :currency=>208, # DDK
9
+ :cardno=>'5019100000000000',
10
+ :expmon=>'06',
11
+ :expyear=>'24',
12
+ :cvc=>'684',
13
+ :orderId=>"abc_test_#{Time.now.to_s.gsub(/\s/, '_')}",
14
+ :test=>true
15
+ }
16
+ end
17
+
18
+ it "should authorize transaction for properly provided data" do
19
+ res = @dibs.authorize(@good_looking_data)
20
+ res.accepted?.should == true
21
+ end
22
+
23
+ it "should throw an exception" do
24
+ params = @good_looking_data.delete(:cardno)
25
+ expect { @dibs.authorize(@good_looking_data) }.should raise_error
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper.rb"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ require 'dibs'
8
+ require 'yaml'
9
+
10
+ RSpec.configure do |config|
11
+ config.treat_symbols_as_metadata_keys_with_true_values = true
12
+ config.run_all_when_everything_filtered = true
13
+ config.filter_run :focus
14
+ end
15
+
16
+ raw_config = File.read("spec/config.yml")
17
+ API_CONFIG = YAML.load(raw_config)['default']
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dibs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Maciej Litwiniuk
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-01 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70337281307080 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.0.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70337281307080
25
+ description: DIBS payment gateway library
26
+ email:
27
+ - maciej@litwiniuk.net
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - .rspec
34
+ - Gemfile
35
+ - LICENSE
36
+ - README.md
37
+ - Rakefile
38
+ - dibs.gemspec
39
+ - lib/dibs.rb
40
+ - lib/dibs/core_ext.rb
41
+ - lib/dibs/version.rb
42
+ - lib/errors.rb
43
+ - lib/results.rb
44
+ - lib/results/authorize.rb
45
+ - lib/results/capture.rb
46
+ - lib/results/dibs_result.rb
47
+ - spec/config.sample.yml
48
+ - spec/dibs_spec.rb
49
+ - spec/spec_helper.rb
50
+ homepage: https://github.com/prograils/dibs
51
+ licenses: []
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 1.8.10
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: DIBS payment API wrapper for Ruby
74
+ test_files:
75
+ - spec/config.sample.yml
76
+ - spec/dibs_spec.rb
77
+ - spec/spec_helper.rb
78
+ has_rdoc: