ruby-billsafe 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
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,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in billsafe.gemspec
4
+ gemspec
5
+
6
+ gem 'rake'
7
+ gem 'rspec'
8
+ gem 'webmock'
9
+ gem 'activesupport'
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,49 @@
1
+ # Billsafe
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ gem 'billsafe'
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", params)
35
+
36
+ The response is a hash with the returned values. You can fetch the values with
37
+
38
+ token = response['token']
39
+
40
+ The full API documentation can be found here: http://www.billsafe.de/integration/manuals/BillSAFE_API.pdf
41
+
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 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,19 @@
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 = "ruby-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 = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ 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
+
data/lib/billsafe.rb ADDED
@@ -0,0 +1,81 @@
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
+ 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
+
78
+ end
79
+
80
+ end
81
+ end
@@ -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.1.0"
3
+ end
@@ -0,0 +1,38 @@
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
+ describe "#initialize" do
16
+ it "initializes all attributes correctly" do
17
+ client.merchant_id.should eql("meid")
18
+ client.merchant_license.should eql("meli")
19
+ client.application_signature.should eql("appsig")
20
+ client.application_version.should eql("appve")
21
+ end
22
+ end
23
+
24
+ describe "sending requests" do
25
+ it "should handle valid requests" do
26
+ stub_ok_request "yxz"
27
+ result = client.call("PrepareOrder", :Order_currencyCode => "EUR")
28
+ result['token'].should eql "yxz"
29
+ end
30
+
31
+ it "should throw errors with invalid requests" do
32
+ stub_error_request
33
+ expect { client.call("PrepareOrder", :Order_currencyCode => "EUR") }.to raise_error Billsafe::APIError
34
+ end
35
+
36
+ end
37
+
38
+ end
@@ -0,0 +1,6 @@
1
+ require 'spec_helper'
2
+
3
+ describe Billsafe do
4
+
5
+
6
+ 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,11 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
3
+
4
+ require 'billsafe'
5
+ require 'active_model'
6
+ require 'rspec/autorun'
7
+ require 'webmock/rspec'
8
+
9
+ RSpec.configure do |config|
10
+
11
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-billsafe
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mike Zaschka
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-11 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Ruby wrapper for the BillSAFE API
15
+ email:
16
+ - mike.zaschka@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - .rspec
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - billsafe.gemspec
28
+ - billsafe.iml
29
+ - lib/billsafe.rb
30
+ - lib/billsafe/client.rb
31
+ - lib/billsafe/version.rb
32
+ - spec/billsafe/client_spec.rb
33
+ - spec/billsafe_spec.rb
34
+ - spec/request_spec_helper.rb
35
+ - spec/spec_helper.rb
36
+ homepage: ''
37
+ licenses: []
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 1.8.24
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: Ruby wrapper for the BillSAFE API
60
+ test_files:
61
+ - spec/billsafe/client_spec.rb
62
+ - spec/billsafe_spec.rb
63
+ - spec/request_spec_helper.rb
64
+ - spec/spec_helper.rb