quickpay-ruby-client 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8dce85d735cd476ba333dcfe8acfbd28b4520a32
4
+ data.tar.gz: 37f806252c5c55f0a486e32b6e7dbfeb2520ac13
5
+ SHA512:
6
+ metadata.gz: e03e6f97162e7652eb00865b142d506de7a8c9fa62355e17a5228097aad2f2f958997eaad89faab88b9ba5e6ab625d8088930f6af8e2a101ad4033aaf94f6604
7
+ data.tar.gz: 109568135f5bf9b144ccb35591285cd84038b67f3221403ec095ff0882a83df704c8f6b0cb10dbffe0e61ee53bb3c6b2f79ab33fd7459eb259640a4fda63b577
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.2
4
+ cache: bundler
5
+ script: bundle exec rspec --format documentation --color
6
+ notifications:
7
+ slack:
8
+ secure: SixeTgiVsOaeWyKwICxLJ0GLN/C9j6qW1ZdaEytIDuZaBAn9oArrRGkJiehFdlzcPUHwzMWC0vl9GQzyBhZ7dbq+B53QY1mH9LTb9A53Y2d1OO1kBjJAkC5Yprvpjm52+x889Dwlz0bfLETvLsC2ej0NZDvSSLKFjpZZIZMOWkg=
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in quickpay-ruby-client.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 QuickPay
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,113 @@
1
+ quickpay-ruby-client
2
+ ======================
3
+ [![Build Status](https://travis-ci.org/QuickPay/quickpay-ruby-client.svg)](https://travis-ci.org/QuickPay/quickpay-ruby-client)
4
+
5
+ `quickpay-ruby-client` is a official client for [QuickPay API](http://tech.quickpay.net/api). The Quickpay API enables you to accept payments in a secure and reliable manner. This gem currently support QuickPay `v10` api.
6
+
7
+ ## Installation
8
+
9
+ Add to your Gemfile
10
+
11
+ $ gem 'quickpay-ruby-client'
12
+
13
+ or install from Rubygems:
14
+
15
+ $ gem install quickpay-ruby-client
16
+
17
+ It is currently tested with Ruby ( >= 2.1.x)
18
+
19
+ * MRI
20
+ * Rubinius (2.0)
21
+
22
+ ## Usage
23
+
24
+ Before doing anything you should register yourself with QuickPay and get access credentials. If you haven't please [click](https://quickpay.net/) here to apply.
25
+
26
+ ### Create a new API client
27
+
28
+ First you should create a client instance that is anonymous or authorized with `api_key` or login credentials provided by QuickPay.
29
+
30
+ To initialise an anonymous client:
31
+
32
+ ```
33
+ require 'quickpay/api/client'
34
+ client = QuickPay::API::Client.new
35
+ ```
36
+
37
+ To initialise a client with QuickPay Api Key:
38
+
39
+ ```
40
+ require 'quickpay/api/client'
41
+ client = QuickPay::API::Client.new(api_key: ENV['QUICKPAY_API_KEY'])
42
+ ```
43
+
44
+ Or you can provide login credentials like:
45
+
46
+ ```
47
+ require 'quickpay/api/client'
48
+ client = QuickPay::API::Client.new(email: ENV['QUICKPAY_LOGIN'], password: ENV['QUICKPAY_PASSWORD'])
49
+ ```
50
+
51
+ To pass request specific headers:
52
+
53
+ ```
54
+ client = Quickpay::API::Client.new({ email: ENV['QUICKPAY_LOGIN'], password: ENV['QUICKPAY_PASSWORD'] },
55
+ :headers => { 'QuickPay-Callback-URL' => 'https://webshop.com' })
56
+ ```
57
+
58
+
59
+ ### API Calls
60
+
61
+ You can afterwards call any method described in QuickPay api with corresponding http method and endpoint. These methods are supported currently: `get`, `post`, `put`, `patch` and `delete`.
62
+
63
+ ```
64
+ client.get("/activities").each do |activity|
65
+ puts activity.id
66
+ end
67
+
68
+ ```
69
+
70
+ If you want raw http response, headers Please add `:raw => true` parameter:
71
+
72
+ ```
73
+ status, body, headers = client.get("/activities", :raw => true)
74
+
75
+ if status == 200
76
+ JSON.parse(body).each do |activity|
77
+ puts activity.id
78
+ end
79
+ else
80
+ puts "Error: #{body}"
81
+ end
82
+
83
+ ```
84
+
85
+ ### Handling API exceptions
86
+
87
+ By default `(get|post|patch|put|delete)` will return JSON parsed body on success (i.e. `2xx` response code) otherwise it will raise appropriate error. Your code should handle the errors appropriately. Following error codes are supported currently:
88
+
89
+
90
+ Response status | Error |
91
+ ----------------| ----------|
92
+ `400` | `QuickPay::API::BadRequest`
93
+ `401` | `QuickPay::API::Unauthorized`
94
+ `402` | `QuickPay::API::PaymentRequired`
95
+ `403` | `QuickPay::API::Forbidden`
96
+ `404` | `QuickPay::API::NotFound`
97
+ `405` | `QuickPay::API::MethodNotAllowed`
98
+ `406` | `QuickPay::API::NotAcceptable`
99
+ `409` | `QuickPay::API::Conflict`
100
+ `500` | `QuickPay::API::ServerError`
101
+
102
+ All exceptions inherits `QuickPay::API::Error`, so you can listen for any api error like:
103
+
104
+ ```
105
+ begin
106
+ client.post("/payments", :currency => :DKK, :order_id => '1212')
107
+ ...
108
+ rescue QuickPay::API::Error => e
109
+ puts e.body
110
+ end
111
+ ```
112
+
113
+ You can read more about api responses at [http://tech.quickpay.net/api/](http://tech.quickpay.net/api).
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,5 @@
1
+ require 'json'
2
+ require 'httparty'
3
+ require 'quickpay/version'
4
+ require 'quickpay/logger'
5
+ require 'quickpay/constants'
@@ -0,0 +1,36 @@
1
+ require 'quickpay'
2
+ require 'quickpay/api/request'
3
+ require 'quickpay/api/errors'
4
+
5
+ module QuickPay
6
+ class << self
7
+ attr_accessor :base_uri
8
+ end
9
+
10
+ module API
11
+ class Client
12
+ attr_accessor :options
13
+
14
+ def initialize(auth_params = {}, opts = {})
15
+ opts[:secret] ||= extract_auth(auth_params)
16
+ opts[:base_uri] ||= (QuickPay.base_uri || QuickPay::BASE_URI)
17
+
18
+ @options = opts.dup
19
+ end
20
+
21
+ def extract_auth params
22
+ if params[:email] and params[:password]
23
+ "#{params[:email]}:#{params[:password]}"
24
+ elsif params[:api_key]
25
+ ":#{params[:api_key]}"
26
+ end
27
+ end
28
+
29
+ [:get, :post, :patch, :put, :delete].each do |method|
30
+ define_method(method) do |*args|
31
+ Request.new(options).request(method, *args)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,10 @@
1
+
2
+ [:quickpay_error, :bad_request, :unauthorized, :payment_required,
3
+ :forbidden, :not_found, :method_not_allowed, :not_acceptable,
4
+ :conflict, :server_error].each do |err_code|
5
+ require "quickpay/api/errors/#{err_code}"
6
+ end
7
+
8
+
9
+
10
+
@@ -0,0 +1,7 @@
1
+
2
+ module QuickPay
3
+ module API
4
+ class BadRequest < Error
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+
2
+ module QuickPay
3
+ module API
4
+ class Conflict < Error
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+
2
+ module QuickPay
3
+ module API
4
+ class Forbidden < Error
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+
2
+ module QuickPay
3
+ module API
4
+ class MethodNotAllowed < Error
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+
2
+ module QuickPay
3
+ module API
4
+ class NotAcceptable < Error
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+
2
+ module QuickPay
3
+ module API
4
+ class NotFound < Error
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+
2
+ module QuickPay
3
+ module API
4
+ class PaymentRequired < Error
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ module QuickPay
2
+ module API
3
+ class Error < StandardError
4
+ attr_reader :code, :status, :body
5
+
6
+ def initialize(code, status, body)
7
+ @code = code
8
+ @status = status
9
+ @body = body
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+
2
+ module QuickPay
3
+ module API
4
+ class ServerError < Error
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+
2
+ module QuickPay
3
+ module API
4
+ class Unauthorized < Error
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,87 @@
1
+
2
+ module QuickPay
3
+ module API
4
+ class Request
5
+ include HTTParty
6
+
7
+ def initialize (options = {})
8
+ @secret = options[:secret]
9
+ self.class.base_uri(options[:base_uri] || BASE_URI)
10
+ end
11
+
12
+ def request method, path, data = {}
13
+ raw = data.delete(:raw)
14
+ req_headers = headers.merge(data.delete(:headers) || {})
15
+
16
+ options = case method
17
+ when :get, :delete
18
+ { query: data }
19
+ when :post, :patch, :put
20
+ { body: data }
21
+ end || {}
22
+
23
+ options = options.merge(:headers => headers.merge(req_headers))
24
+ QuickPay.logger.debug { "#{method.to_s.upcase} #{base_uri}#{path} #{options}" }
25
+ create_response(raw, self.class.send(method, path, options))
26
+ end
27
+
28
+ def create_response raw, res
29
+ if raw
30
+ [res.code, res.body, res.headers]
31
+ else
32
+ response = res.parsed_response
33
+ raise_error(response, res.code) if res.code >= 400
34
+ response
35
+ end
36
+ end
37
+
38
+ def raise_error body, status
39
+ code = API_STATUS_CODES[status].to_s
40
+ args = [code, status, body]
41
+
42
+ klass =
43
+ begin
44
+ require "quickpay/api/errors/#{code}"
45
+ class_name = code.split('_').map(&:capitalize).join('')
46
+ QuickPay::API.const_get(class_name)
47
+ rescue LoadError, NameError
48
+ QuickPay::API::Error
49
+ end
50
+
51
+ fail klass.new(*args), error_description(body)
52
+ end
53
+
54
+ private
55
+
56
+ def base_uri
57
+ self.class.default_options[:base_uri]
58
+ end
59
+
60
+ def error_description msg
61
+ msg
62
+ end
63
+
64
+ def headers
65
+ heads = {
66
+ 'User-Agent' => user_agent,
67
+ 'Accept-Version' => "v#{QuickPay::API_VERSION}"
68
+ }
69
+ heads['Authorization'] = "Basic #{authorization}" if @secret != nil
70
+ heads
71
+ end
72
+
73
+ def user_agent
74
+ user_agent = "quickpay-ruby-client, v#{QuickPay::VERSION}"
75
+ user_agent += ", #{RUBY_VERSION}, #{RUBY_PLATFORM}, #{RUBY_PATCHLEVEL}"
76
+ if defined?(RUBY_ENGINE)
77
+ user_agent += ", #{RUBY_ENGINE}"
78
+ end
79
+ user_agent
80
+ end
81
+
82
+ def authorization
83
+ Base64.strict_encode64(@secret)
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,21 @@
1
+
2
+ module QuickPay
3
+ BASE_URI = 'https://api.quickpay.net'
4
+ API_VERSION = 10
5
+
6
+ API_STATUS_CODES = {
7
+ 200 => :ok,
8
+ 201 => :created,
9
+ 202 => :accepted,
10
+ 400 => :bad_request,
11
+ 401 => :unauthorized,
12
+ 402 => :payment_required,
13
+ 403 => :forbidden,
14
+ 404 => :not_found,
15
+ 405 => :method_not_allowed,
16
+ 406 => :not_acceptable,
17
+ 409 => :conflict,
18
+ 500 => :server_error
19
+ }
20
+ end
21
+
@@ -0,0 +1,16 @@
1
+ require 'logger'
2
+
3
+ module QuickPay
4
+ class << self
5
+ attr_writer :logger
6
+
7
+ def logger
8
+ @logger ||= lambda {
9
+ logger = Logger.new($stdout)
10
+ logger.level = Logger::INFO
11
+ logger
12
+ }.call
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module QuickPay
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'quickpay/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "quickpay-ruby-client"
8
+ spec.version = QuickPay::VERSION
9
+ spec.authors = ["QuickPay Developers"]
10
+ spec.email = ["support@quickpay.net"]
11
+
12
+ spec.summary = "Ruby client for QuickPay API"
13
+ spec.description = "Embed QuickPay's secure payments directly into your Ruby applications. more at https://tech.quickpay.net"
14
+ spec.homepage = "https://github.com/QuickPay/quickpay-ruby-client"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.2"
24
+ spec.add_development_dependency "rspec-mocks"
25
+ spec.add_development_dependency "webmock"
26
+
27
+ spec.add_dependency "httparty", "~> 0.13"
28
+
29
+ end
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: quickpay-ruby-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - QuickPay Developers
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-mocks
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: httparty
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '0.13'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '0.13'
97
+ description: Embed QuickPay's secure payments directly into your Ruby applications.
98
+ more at https://tech.quickpay.net
99
+ email:
100
+ - support@quickpay.net
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - .gitignore
106
+ - .rspec
107
+ - .travis.yml
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - lib/quickpay.rb
113
+ - lib/quickpay/api/client.rb
114
+ - lib/quickpay/api/errors.rb
115
+ - lib/quickpay/api/errors/bad_request.rb
116
+ - lib/quickpay/api/errors/conflict.rb
117
+ - lib/quickpay/api/errors/forbidden.rb
118
+ - lib/quickpay/api/errors/method_not_allowed.rb
119
+ - lib/quickpay/api/errors/not_acceptable.rb
120
+ - lib/quickpay/api/errors/not_found.rb
121
+ - lib/quickpay/api/errors/payment_required.rb
122
+ - lib/quickpay/api/errors/quickpay_error.rb
123
+ - lib/quickpay/api/errors/server_error.rb
124
+ - lib/quickpay/api/errors/unauthorized.rb
125
+ - lib/quickpay/api/request.rb
126
+ - lib/quickpay/constants.rb
127
+ - lib/quickpay/logger.rb
128
+ - lib/quickpay/version.rb
129
+ - quickpay-ruby-client.gemspec
130
+ homepage: https://github.com/QuickPay/quickpay-ruby-client
131
+ licenses:
132
+ - MIT
133
+ metadata: {}
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - '>='
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 2.0.14
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: Ruby client for QuickPay API
154
+ test_files: []