billsafe 0.2.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.
data/.gitignore ADDED
@@ -0,0 +1,20 @@
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
+ .idea/
19
+ .idea
20
+
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in billsafe.gemspec
4
+ gemspec
5
+ gem 'hash-deep-merge'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Mike Zaschka
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.
data/README.md ADDED
@@ -0,0 +1,114 @@
1
+ # Billsafe
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ gem 'billsafe', "0.2.0"
8
+
9
+ And then execute:
10
+
11
+ $ bundle install
12
+
13
+ Or install it yourself as:
14
+
15
+ $ gem install billsafe
16
+
17
+ ## Usage
18
+
19
+ To disable the sandbox for using the API put this in config/initializers/billsafe.rb
20
+
21
+ Billsafe.sandbox = false
22
+
23
+ The usage of the API is straightforward. First you need a client with your login information:
24
+
25
+ client = Billsafe::Client.new(
26
+ merchant_id: "your-merchant_id",
27
+ merchant_license: "your-merchant_license",
28
+ application_signature: "your-application_signature",
29
+ application_version: "your-application_version"
30
+ )
31
+
32
+ Then you can use the client to push your calls to the API.
33
+
34
+ response = client.call("PrepareOrder", attributes)
35
+
36
+ The attributes hash must be filled with the required parameters for the API call. Attributes are transformed into a query string before the request is made.
37
+ The available attributes can be read from the full documentation (see link below).
38
+
39
+ One note on filling the attributes hash:
40
+
41
+ The BillSAFE API uses keys like _order_number_ and _articleList_0_grossPrice_.
42
+ You don't have to flatten the attributes link this. Instead you can think of the underscores as a new hash:
43
+
44
+ attributes = {
45
+ order: {
46
+ number: @order.order_id,
47
+ amount: @order.credit_totals.to_f.precision(2),
48
+ taxAmount: @order.taxes.to_f.precision(2),
49
+ currencyCode: "EUR"
50
+ },
51
+ articleList: {
52
+ 0 => {
53
+ number: item.product.sku,
54
+ name: item.product.name,
55
+ type: "goods",
56
+ quantity: item.amount,
57
+ grossPrice: item.price.to_f.precision(2),
58
+ tax: "19.00"
59
+ }
60
+ }
61
+ }
62
+
63
+ This will be transformed to:
64
+
65
+ attributes = {
66
+ order_number: number: @order.order_id,
67
+ order_amount: @order.credit_totals.to_f.precision(2),
68
+ order_taxAmount: @order.taxes.to_f.precision(2),
69
+ order_currencyCode: "EUR",
70
+ articleList_0_number: item.product.sku,
71
+ articleList_0_name: item.product.name,
72
+ articleList_0_type: "goods",
73
+ articleList_0_quantity: item.amount,
74
+ articleList_0_grossPrice: item.price.to_f.precision(2),
75
+ articleList_0_tax: "19.00"
76
+ }
77
+
78
+ The response is a hash with the returned values. You can fetch the values with
79
+
80
+ token = response['token']
81
+
82
+ The response hash is treated like the attributes hash in a reversed way. So every key containing an underscore is beeing transformed into its own hash.
83
+
84
+ The full API documentation can be found here: http://www.billsafe.de/integration/manuals/BillSAFE_API.pdf
85
+
86
+ ## Example code
87
+
88
+ def checkout
89
+ response = @client.call("prepareOrder", order_attributes)
90
+ if response['token']
91
+ redirect_to Billsafe.payment_url + "?token="+response['token']
92
+ end
93
+ end
94
+
95
+ def success
96
+ response = @client.call("getTransactionResult", token: params[:token])
97
+ if response['status'] == "ACCEPTED"
98
+ transaction_id = response['transactionId']
99
+ ...
100
+ else
101
+ error_code = response['declineReason']['code']
102
+ error_message = response['declineReason']['message']
103
+ user_error_message = response['declineReason']['buyerMessage']
104
+ ...
105
+ end
106
+ end
107
+
108
+ ## Contributing
109
+
110
+ 1. Fork it
111
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
112
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
113
+ 4. Push to the branch (`git push origin my-new-feature`)
114
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+
6
+ task :default => :spec
data/billsafe.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'billsafe/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "billsafe"
8
+ gem.version = Billsafe::VERSION
9
+ gem.authors = ["Mike Zaschka"]
10
+ gem.email = ["mike.zaschka@gmail.com"]
11
+ gem.description = %q{Ruby wrapper for the BillSAFE API}
12
+ gem.summary = %q{Ruby wrapper for the BillSAFE API}
13
+ gem.homepage = "https://github.com/mikezaschka/ruby-billsafe"
14
+
15
+ gem.add_development_dependency "rake"
16
+ gem.add_development_dependency "activesupport"
17
+ gem.add_development_dependency "rspec"
18
+ gem.add_development_dependency "webmock"
19
+
20
+ gem.files = `git ls-files`.split($/)
21
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
22
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
23
+ gem.require_paths = ["lib"]
24
+ end
data/billsafe.iml ADDED
@@ -0,0 +1,25 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="RUBY_MODULE" version="4">
3
+ <component name="NewModuleRootManager" inherit-compiler-output="true">
4
+ <exclude-output />
5
+ <content url="file://$MODULE_DIR$" />
6
+ <orderEntry type="jdk" jdkName="RVM: ruby-1.9.2-p320" jdkType="RUBY_SDK" />
7
+ <orderEntry type="sourceFolder" forTests="false" />
8
+ <orderEntry type="library" scope="PROVIDED" name="activemodel (v3.2.11, RVM: ruby-1.9.2-p320) [gem]" level="application" />
9
+ <orderEntry type="library" scope="PROVIDED" name="activesupport (v3.2.11, RVM: ruby-1.9.2-p320) [gem]" level="application" />
10
+ <orderEntry type="library" scope="PROVIDED" name="addressable (v2.3.3, RVM: ruby-1.9.2-p320) [gem]" level="application" />
11
+ <orderEntry type="library" scope="PROVIDED" name="builder (v3.0.4, RVM: ruby-1.9.2-p320) [gem]" level="application" />
12
+ <orderEntry type="library" scope="PROVIDED" name="bundler (v1.2.0, RVM: ruby-1.9.2-p320) [gem]" level="application" />
13
+ <orderEntry type="library" scope="PROVIDED" name="crack (v0.3.2, RVM: ruby-1.9.2-p320) [gem]" level="application" />
14
+ <orderEntry type="library" scope="PROVIDED" name="diff-lcs (v1.1.3, RVM: ruby-1.9.2-p320) [gem]" level="application" />
15
+ <orderEntry type="library" scope="PROVIDED" name="i18n (v0.6.1, RVM: ruby-1.9.2-p320) [gem]" level="application" />
16
+ <orderEntry type="library" scope="PROVIDED" name="multi_json (v1.5.0, RVM: ruby-1.9.2-p320) [gem]" level="application" />
17
+ <orderEntry type="library" scope="PROVIDED" name="rake (v10.0.3, RVM: ruby-1.9.2-p320) [gem]" level="application" />
18
+ <orderEntry type="library" scope="PROVIDED" name="rspec (v2.12.0, RVM: ruby-1.9.2-p320) [gem]" level="application" />
19
+ <orderEntry type="library" scope="PROVIDED" name="rspec-core (v2.12.2, RVM: ruby-1.9.2-p320) [gem]" level="application" />
20
+ <orderEntry type="library" scope="PROVIDED" name="rspec-expectations (v2.12.1, RVM: ruby-1.9.2-p320) [gem]" level="application" />
21
+ <orderEntry type="library" scope="PROVIDED" name="rspec-mocks (v2.12.2, RVM: ruby-1.9.2-p320) [gem]" level="application" />
22
+ <orderEntry type="library" scope="PROVIDED" name="webmock (v1.9.3, RVM: ruby-1.9.2-p320) [gem]" level="application" />
23
+ </component>
24
+ </module>
25
+
@@ -0,0 +1,17 @@
1
+ module Billsafe
2
+
3
+ class Client
4
+
5
+ attr_accessor :base_attributes
6
+
7
+ def initialize(base_attributes = {})
8
+ @base_attributes = base_attributes
9
+ end
10
+
11
+ def call(method, attributes)
12
+ response = Billsafe.request(method, base_attributes.merge(attributes))
13
+ response
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module Billsafe
2
+ VERSION = "0.2.0"
3
+ end
data/lib/billsafe.rb ADDED
@@ -0,0 +1,93 @@
1
+ require "billsafe/version"
2
+ require 'active_support/all'
3
+
4
+ module Billsafe
5
+
6
+ API_BASE = "nvp.billsafe.de/"
7
+ API_BASE_SANDBOX = "sandbox-nvp.billsafe.de"
8
+ API_VERSION = "V208"
9
+
10
+ PAYMENT_BASE = "payment.billsafe.de"
11
+ PAYMENT_BASE_SANDBOX = "sandbox-payment.billsafe.de"
12
+ PAYMENT_VERSION = "V200"
13
+
14
+ @@sandbox = true
15
+
16
+ autoload :Client, "billsafe/client"
17
+
18
+ class BillsafeError < StandardError
19
+ end
20
+
21
+ class AuthenticationError < BillsafeError; end
22
+ class APIError < BillsafeError; end
23
+ class ArgumentError < BillsafeError; end
24
+
25
+ class << self
26
+ def sandbox?
27
+ @@sandbox
28
+ end
29
+
30
+ def sandbox=(sandbox)
31
+ @@sandbox = sandbox
32
+ end
33
+
34
+ def payment_url
35
+ api_base = Billsafe.sandbox? ? PAYMENT_BASE_SANDBOX : PAYMENT_BASE
36
+ "https://#{api_base}/#{PAYMENT_VERSION}"
37
+ end
38
+
39
+ def request(api_method, data)
40
+ api_base = Billsafe.sandbox? ? API_BASE_SANDBOX : API_BASE
41
+ https = Net::HTTP.new(api_base , Net::HTTP.https_default_port)
42
+ https.use_ssl = true
43
+ https.verify_mode = OpenSSL::SSL::VERIFY_NONE
44
+ https.start do |connection|
45
+ path = "/#{API_VERSION}"
46
+ https_request = Net::HTTP::Post.new(path)
47
+ https_request.body = nvp_params(data.merge(method: api_method, format: 'JSON'))
48
+ @response = https.request(https_request)
49
+ end
50
+ raise AuthenticationError if @response.code.to_i == 401
51
+ raise APIError if @response.code.to_i >= 500
52
+
53
+ data = JSON.parse(@response.body)
54
+ raise APIError.new(data['errorList'].
55
+ map{ |err| [err['code'], err['message']].join(" ")}.join("\r\n")) if data["ack"] == "ERROR"
56
+ deflatten_params(data)
57
+ end
58
+
59
+ def nvp_params(params)
60
+ flattened_params = flatten_params(params)
61
+ URI.encode_www_form(flattened_params)
62
+ end
63
+
64
+ def flatten_params(hash, prefix = "")
65
+ new_hash = {}
66
+ hash.each_pair do |key, val|
67
+ if val.is_a?(Hash)
68
+ new_hash.merge!(flatten_params(val, prefix + key.to_s + "_"))
69
+ else
70
+ new_hash[prefix + key.to_s] = val
71
+ end
72
+ end
73
+ new_hash
74
+ end
75
+
76
+ def deflatten_params(hash)
77
+ new_hash = {}
78
+ hash.each_pair do |key, val|
79
+ if key.include? "_"
80
+ parts = key.split("_")
81
+ parts.reverse.each do |part|
82
+ val = { part => val }
83
+ end
84
+ else
85
+ val = { key => val }
86
+ end
87
+ new_hash.deep_merge!(val)
88
+ end
89
+ new_hash
90
+ end
91
+
92
+ end
93
+ end
@@ -0,0 +1,15 @@
1
+ require "spec_helper"
2
+ require "request_spec_helper"
3
+
4
+ describe Billsafe::Client do
5
+ include RequestSpecHelper
6
+
7
+ let(:valid_attributes) do
8
+ { merchant_id: "meid", merchant_license: "meli", application_signature: "appsig", application_version: "appve" }
9
+ end
10
+
11
+ let (:client) do
12
+ Billsafe::Client.new(valid_attributes)
13
+ end
14
+
15
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe Billsafe do
4
+
5
+ let(:deflattened_hash) do
6
+ {
7
+ "items" => {
8
+ "0" => "ruby",
9
+ "1" => "bi",
10
+ "nested" => {
11
+ "0" => "ruby"
12
+ }
13
+ },
14
+ "status" => "OK"
15
+ }
16
+ end
17
+
18
+ let(:flattened_hash) do
19
+ {
20
+ "items_0" => "ruby",
21
+ "items_1" => "bi",
22
+ "items_nested_0"=>"ruby",
23
+ "status" => "OK"
24
+ }
25
+ end
26
+
27
+ it "should flatten params" do
28
+ Billsafe.send(:flatten_params, deflattened_hash).should eql(flattened_hash)
29
+ end
30
+
31
+ it "should deflatten params" do
32
+ Billsafe.send(:deflatten_params, flattened_hash).should eql(deflattened_hash)
33
+ end
34
+
35
+ end
@@ -0,0 +1,25 @@
1
+ module RequestSpecHelper
2
+
3
+ def stub_ok_request(token, status = 200)
4
+ stub_request(:get, "https://sandbox-nvp.billsafe.de/V209?Order_currencyCode=EUR&format=json&method=PrepareOrder").
5
+ with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
6
+ to_return(:status => 200, :body => '{
7
+ "ack":"OK",
8
+ "status":"ACCEPTED",
9
+ "transactionId":"987654321",
10
+ "token":"' + token + '"}', :headers => {})
11
+ end
12
+
13
+ def stub_error_request()
14
+ stub_request(:get, "https://sandbox-nvp.billsafe.de/V209?Order_currencyCode=EUR&format=json&method=PrepareOrder").
15
+ with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
16
+ to_return(:status => 200, :body => '{
17
+ "ack":"ERROR",
18
+ "errorList":[
19
+ { "code":"123", "message":"Fehler 1" },
20
+ { "code":"456", "message":"Fehler 2"}
21
+ ]
22
+ }', :headers => {})
23
+ end
24
+
25
+ end
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
3
+
4
+ require 'billsafe'
5
+ require 'rspec/autorun'
6
+ require 'webmock/rspec'
7
+
8
+ RSpec.configure do |config|
9
+
10
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: billsafe
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mike Zaschka
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activesupport
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: webmock
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Ruby wrapper for the BillSAFE API
79
+ email:
80
+ - mike.zaschka@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .rspec
87
+ - Gemfile
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - billsafe.gemspec
92
+ - billsafe.iml
93
+ - lib/billsafe.rb
94
+ - lib/billsafe/client.rb
95
+ - lib/billsafe/version.rb
96
+ - spec/billsafe/client_spec.rb
97
+ - spec/billsafe_spec.rb
98
+ - spec/request_spec_helper.rb
99
+ - spec/spec_helper.rb
100
+ homepage: https://github.com/mikezaschka/ruby-billsafe
101
+ licenses: []
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 1.8.24
121
+ signing_key:
122
+ specification_version: 3
123
+ summary: Ruby wrapper for the BillSAFE API
124
+ test_files:
125
+ - spec/billsafe/client_spec.rb
126
+ - spec/billsafe_spec.rb
127
+ - spec/request_spec_helper.rb
128
+ - spec/spec_helper.rb