paypro 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,14 @@
1
+ require 'openssl'
2
+ require 'json'
3
+
4
+ require 'faraday'
5
+
6
+ require 'paypro/client'
7
+ require 'paypro/errors'
8
+ require 'paypro/version'
9
+
10
+ module PayPro
11
+ CA_BUNDLE_FILE = File.dirname(__FILE__) + '/data/ca-bundle.crt'
12
+ API_URL = 'https://paypro.nl/post_api'.freeze
13
+ API_VERSION = 'v1'.freeze
14
+ end
@@ -0,0 +1,66 @@
1
+ module PayPro
2
+ # Client class to connect to the PayPro V1 API.
3
+ # Requires an API key to authenticate API calls, you
4
+ # can also supply your own Faraday connection instead of the default one.
5
+ # This can be useful if you want to add more middleware or want finer
6
+ # control of the connection being used.
7
+ class Client
8
+ attr_accessor :command, :params
9
+
10
+ def initialize(api_key, conn = default_conn)
11
+ @api_key = api_key
12
+ @params = {}
13
+ @conn = conn
14
+ end
15
+
16
+ # Executes the API call and handles the response. Will raise errors if
17
+ # there were problems while connecting or handeling the response.
18
+ def execute
19
+ response = @conn.post do |req|
20
+ req.body = body
21
+ end
22
+ handle_response(response)
23
+ rescue Faraday::ClientError => e
24
+ raise ConnectionError, "Could not connect to the PayPro API: #{e.inspect}"
25
+ end
26
+
27
+ # Returns the body that is used in the POST request.
28
+ def body
29
+ {
30
+ apikey: @api_key,
31
+ command: @command,
32
+ params: JSON.generate(@params)
33
+ }
34
+ end
35
+
36
+ private
37
+
38
+ def ca_bundle_file
39
+ PayPro::CA_BUNDLE_FILE
40
+ end
41
+
42
+ def cert_store
43
+ cert_store = OpenSSL::X509::Store.new
44
+ cert_store.add_file ca_bundle_file
45
+ cert_store
46
+ end
47
+
48
+ def default_conn
49
+ Faraday.new(
50
+ PayPro::API_URL,
51
+ ssl: {
52
+ cert_store: cert_store,
53
+ verify: true
54
+ }
55
+ )
56
+ end
57
+
58
+ def handle_response(response)
59
+ parsed_response = JSON.parse(response.body)
60
+ @params = {}
61
+ parsed_response
62
+ rescue JSON::ParserError
63
+ raise InvalidResponseError, "The API request returned an error or is invalid: #{response.body}"
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,4 @@
1
+ module PayPro
2
+ class ConnectionError < StandardError; end
3
+ class InvalidResponseError < StandardError; end
4
+ end
@@ -0,0 +1,3 @@
1
+ module PayPro
2
+ VERSION = '0.0.1'.freeze
3
+ end
@@ -0,0 +1,21 @@
1
+ require File.expand_path('../lib/paypro/version', __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'paypro'
5
+ s.version = PayPro::VERSION
6
+ s.license = 'MIT'
7
+ s.homepage = 'https://github.com/paypronl/paypro-ruby-v1'
8
+ s.author = 'PayPro'
9
+ s.email = 'support@paypro.nl'
10
+ s.summary = 'Ruby client for PayPro API v1'
11
+
12
+ s.required_ruby_version = '>= 2.0.0'
13
+ s.add_dependency 'faraday', '~> 0.13'
14
+
15
+ s.add_development_dependency 'rspec', '~> 3.6'
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = Dir.glob('spec/**/*_spec.rb')
19
+
20
+ s.require_path = 'lib'
21
+ end
@@ -0,0 +1,112 @@
1
+ require 'spec_helper'
2
+
3
+ describe PayPro::Client do
4
+ let(:api_key) { 'a16e84b3ef5a80ef9af289d37788e87e' }
5
+ let(:client) { described_class.new(api_key) }
6
+
7
+ describe '#initialize' do
8
+ let(:client) { described_class.new(api_key) }
9
+
10
+ it 'sets params to an empty hash' do
11
+ expect(client.params).to eql({})
12
+ end
13
+ end
14
+
15
+ describe '#body' do
16
+ let(:command) { 'create_payment' }
17
+
18
+ subject { client.body }
19
+
20
+ before { client.command = command }
21
+
22
+ it { is_expected.to include(apikey: api_key) }
23
+ it { is_expected.to include(command: command) }
24
+
25
+ context 'when params is empty' do
26
+ it { is_expected.to include(params: '{}') }
27
+ end
28
+
29
+ context 'when params is not empty' do
30
+ before { client.params = { amount: 500 } }
31
+ it { is_expected.to include(params: '{"amount":500}') }
32
+ end
33
+ end
34
+
35
+ describe '#execute' do
36
+ let(:client) { described_class.new(api_key, conn) }
37
+ let(:conn) do
38
+ Faraday.new do |builder|
39
+ builder.adapter :test, stubs
40
+ end
41
+ end
42
+
43
+ subject { client.execute }
44
+
45
+ context 'when connection fails' do
46
+ let(:conn) { double }
47
+
48
+ before { allow(conn).to receive(:post).and_raise(Faraday::ClientError, 'Message') }
49
+
50
+ it 'raises a PayPro::Connection error' do
51
+ expect { subject }.to raise_error(PayPro::ConnectionError, kind_of(String))
52
+ end
53
+ end
54
+
55
+ context 'when body is invalid json' do
56
+ let(:stubs) do
57
+ Faraday::Adapter::Test::Stubs.new do |stub|
58
+ stub.post('/') { |_| [200, {}, 'invalid json }'] }
59
+ end
60
+ end
61
+
62
+ it 'raises a PayPro::InvalidResponse error' do
63
+ expect { subject }.to raise_error(
64
+ PayPro::InvalidResponseError,
65
+ 'The API request returned an error or is invalid: invalid json }'
66
+ )
67
+ end
68
+ end
69
+
70
+ context 'when api returns an error' do
71
+ let(:stubs) do
72
+ Faraday::Adapter::Test::Stubs.new do |stub|
73
+ stub.post('/') { |_| [200, {}, 'Invalid amount'] }
74
+ end
75
+ end
76
+
77
+ it 'raises a PayPro::InvalidResponse error' do
78
+ expect { subject }.to raise_error(
79
+ PayPro::InvalidResponseError,
80
+ 'The API request returned an error or is invalid: Invalid amount'
81
+ )
82
+ end
83
+ end
84
+
85
+ context 'when api call is valid' do
86
+ let(:stubs) do
87
+ Faraday::Adapter::Test::Stubs.new do |stub|
88
+ stub.post('/') do |_|
89
+ [
90
+ 200,
91
+ {},
92
+ '{"payment_url":"https://paypro.nl/betalen/payment_hash","payment_hash":"payment_hash"}'
93
+ ]
94
+ end
95
+ end
96
+ end
97
+
98
+ before { client.params = { amount: 500 } }
99
+
100
+ it 'returns a hash with the response' do
101
+ expect(subject).to include(
102
+ 'payment_url' => 'https://paypro.nl/betalen/payment_hash',
103
+ 'payment_hash' => 'payment_hash'
104
+ )
105
+ end
106
+
107
+ it 'clears the old params' do
108
+ expect { subject }.to change { client.params }.from(amount: 500).to({})
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe PayPro do
4
+ it 'returns the correct api url' do
5
+ expect(described_class::API_URL).to eql 'https://paypro.nl/post_api'
6
+ end
7
+
8
+ it 'returns the correct path for ca-bundle.crt' do
9
+ expect(File).to exist(described_class::CA_BUNDLE_FILE)
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ require 'rspec'
2
+ require 'paypro'
3
+
4
+ RSpec.configure do |config|
5
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paypro
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - PayPro
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-08-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.13'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.6'
41
+ description:
42
+ email: support@paypro.nl
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - ".circleci/config.yml"
48
+ - ".gitignore"
49
+ - ".rubocop.yml"
50
+ - Gemfile
51
+ - LICENSE
52
+ - README.md
53
+ - Rakefile
54
+ - VERSION
55
+ - examples/create_payment.rb
56
+ - lib/data/ca-bundle.crt
57
+ - lib/paypro.rb
58
+ - lib/paypro/client.rb
59
+ - lib/paypro/errors.rb
60
+ - lib/paypro/version.rb
61
+ - paypro.gemspec
62
+ - spec/paypro/client_spec.rb
63
+ - spec/paypro_spec.rb
64
+ - spec/spec_helper.rb
65
+ homepage: https://github.com/paypronl/paypro-ruby-v1
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: 2.0.0
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.5.2
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Ruby client for PayPro API v1
89
+ test_files:
90
+ - spec/paypro/client_spec.rb
91
+ - spec/paypro_spec.rb