secupay_ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 74cce8c345746dc521f125a039949730aacfab03
4
+ data.tar.gz: d7799f2c75ca39b401e3d52350f6349728823b54
5
+ SHA512:
6
+ metadata.gz: 7e689f7d252c9f5f4515b0f555bc24acd76844e87485841d61e98b59f62dc0531a9ea4da2282b30d1a5950826263974318471e5867d90ac77bafd0bca9b3a3fb
7
+ data.tar.gz: bae6f44fad6ca120da966681c59fa0dd85c2b33b02cda28d8fdc9e9c40480c36789c05c9a86af22d69da351e8d5c74f6c7885a42efbcdb9bd054e2bd31fbaef0
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .env
11
+ .DS_Store
12
+ /vcr/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,237 @@
1
+ AllCops:
2
+ Exclude:
3
+ - "vendor/**/*"
4
+ - "db/schema.rb"
5
+ UseCache: false
6
+ Style/CollectionMethods:
7
+ Description: Preferred collection methods.
8
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#map-find-select-reduce-size
9
+ Enabled: true
10
+ PreferredMethods:
11
+ collect: map
12
+ collect!: map!
13
+ find: detect
14
+ find_all: select
15
+ reduce: inject
16
+ Style/DotPosition:
17
+ Description: Checks the position of the dot in multi-line method calls.
18
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#consistent-multi-line-chains
19
+ Enabled: true
20
+ EnforcedStyle: trailing
21
+ SupportedStyles:
22
+ - leading
23
+ - trailing
24
+ Style/FileName:
25
+ Description: Use snake_case for source file names.
26
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#snake-case-files
27
+ Enabled: false
28
+ Exclude: []
29
+ Style/GuardClause:
30
+ Description: Check for conditionals that can be replaced with guard clauses
31
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-nested-conditionals
32
+ Enabled: false
33
+ MinBodyLength: 1
34
+ Style/IfUnlessModifier:
35
+ Description: Favor modifier if/unless usage when you have a single-line body.
36
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#if-as-a-modifier
37
+ Enabled: false
38
+ MaxLineLength: 80
39
+ Style/OptionHash:
40
+ Description: Don't use option hashes when you can use keyword arguments.
41
+ Enabled: false
42
+ Style/PercentLiteralDelimiters:
43
+ Description: Use `%`-literal delimiters consistently
44
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#percent-literal-braces
45
+ Enabled: false
46
+ PreferredDelimiters:
47
+ "%": "()"
48
+ "%i": "()"
49
+ "%q": "()"
50
+ "%Q": "()"
51
+ "%r": "{}"
52
+ "%s": "()"
53
+ "%w": "()"
54
+ "%W": "()"
55
+ "%x": "()"
56
+ Style/PredicateName:
57
+ Description: Check the names of predicate methods.
58
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#bool-methods-qmark
59
+ Enabled: true
60
+ NamePrefix:
61
+ - is_
62
+ - has_
63
+ - have_
64
+ NamePrefixBlacklist:
65
+ - is_
66
+ Exclude:
67
+ - spec/**/*
68
+ Style/RaiseArgs:
69
+ Description: Checks the arguments passed to raise/fail.
70
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#exception-class-messages
71
+ Enabled: false
72
+ EnforcedStyle: exploded
73
+ SupportedStyles:
74
+ - compact
75
+ - exploded
76
+ Style/SignalException:
77
+ Description: Checks for proper usage of fail and raise.
78
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#fail-method
79
+ Enabled: false
80
+ EnforcedStyle: semantic
81
+ SupportedStyles:
82
+ - only_raise
83
+ - only_fail
84
+ - semantic
85
+ Style/SingleLineBlockParams:
86
+ Description: Enforces the names of some block params.
87
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#reduce-blocks
88
+ Enabled: false
89
+ Methods:
90
+ - reduce:
91
+ - a
92
+ - e
93
+ - inject:
94
+ - a
95
+ - e
96
+ Style/SingleLineMethods:
97
+ Description: Avoid single-line methods.
98
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-single-line-methods
99
+ Enabled: false
100
+ AllowIfMethodIsEmpty: true
101
+ Style/StringLiterals:
102
+ Description: Checks if uses of quotes match the configured preference.
103
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#consistent-string-literals
104
+ Enabled: true
105
+ EnforcedStyle: double_quotes
106
+ SupportedStyles:
107
+ - single_quotes
108
+ - double_quotes
109
+ Style/StringLiteralsInInterpolation:
110
+ Description: Checks if uses of quotes inside expressions in interpolated strings match the configured preference.
111
+ Enabled: true
112
+ EnforcedStyle: single_quotes
113
+ SupportedStyles:
114
+ - single_quotes
115
+ - double_quotes
116
+ Style/TrailingCommaInArguments:
117
+ Description: 'Checks for trailing comma in argument lists.'
118
+ StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-trailing-array-commas'
119
+ Enabled: false
120
+ EnforcedStyleForMultiline: no_comma
121
+ SupportedStyles:
122
+ - comma
123
+ - consistent_comma
124
+ - no_comma
125
+ Style/TrailingCommaInLiteral:
126
+ Description: 'Checks for trailing comma in array and hash literals.'
127
+ StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-trailing-array-commas'
128
+ Enabled: false
129
+ EnforcedStyleForMultiline: no_comma
130
+ SupportedStyles:
131
+ - comma
132
+ - consistent_comma
133
+ - no_comma
134
+ Metrics/AbcSize:
135
+ Description: "A calculated magnitude based on number of assignments, branches, and conditions."
136
+ Enabled: false
137
+ Max: 15
138
+ Metrics/ClassLength:
139
+ Description: Avoid classes longer than 100 lines of code.
140
+ Enabled: false
141
+ CountComments: false
142
+ Max: 100
143
+ Metrics/ModuleLength:
144
+ CountComments: false
145
+ Max: 100
146
+ Description: Avoid modules longer than 100 lines of code.
147
+ Enabled: false
148
+ Metrics/CyclomaticComplexity:
149
+ Description: A complexity metric that is strongly correlated to the number of test cases needed to validate a method.
150
+ Enabled: false
151
+ Max: 6
152
+ Metrics/MethodLength:
153
+ Description: Avoid methods longer than 10 lines of code.
154
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#short-methods
155
+ Enabled: false
156
+ CountComments: false
157
+ Max: 10
158
+ Metrics/ParameterLists:
159
+ Description: Avoid parameter lists longer than three or four parameters.
160
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#too-many-params
161
+ Enabled: false
162
+ Max: 5
163
+ CountKeywordArgs: true
164
+ Metrics/PerceivedComplexity:
165
+ Description: A complexity metric geared towards measuring complexity for a human reader.
166
+ Enabled: false
167
+ Max: 7
168
+ Lint/AssignmentInCondition:
169
+ Description: Don't use assignment in conditions.
170
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#safe-assignment-in-condition
171
+ Enabled: false
172
+ AllowSafeAssignment: true
173
+ Style/InlineComment:
174
+ Description: Avoid inline comments.
175
+ Enabled: false
176
+ Style/AccessorMethodName:
177
+ Description: Check the naming of accessor methods for get_/set_.
178
+ Enabled: false
179
+ Style/Alias:
180
+ Description: Use alias_method instead of alias.
181
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#alias-method
182
+ Enabled: false
183
+ Style/Documentation:
184
+ Description: Document classes and non-namespace modules.
185
+ Enabled: false
186
+ Style/DoubleNegation:
187
+ Description: Checks for uses of double negation (!!).
188
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-bang-bang
189
+ Enabled: false
190
+ Style/EachWithObject:
191
+ Description: Prefer `each_with_object` over `inject` or `reduce`.
192
+ Enabled: false
193
+ Style/EmptyLiteral:
194
+ Description: Prefer literals to Array.new/Hash.new/String.new.
195
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#literal-array-hash
196
+ Enabled: false
197
+ Style/ModuleFunction:
198
+ Description: Checks for usage of `extend self` in modules.
199
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#module-function
200
+ Enabled: false
201
+ Style/OneLineConditional:
202
+ Description: Favor the ternary operator(?:) over if/then/else/end constructs.
203
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#ternary-operator
204
+ Enabled: false
205
+ Style/PerlBackrefs:
206
+ Description: Avoid Perl-style regex back references.
207
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-perl-regexp-last-matchers
208
+ Enabled: false
209
+ Style/Send:
210
+ Description: Prefer `Object#__send__` or `Object#public_send` to `send`, as `send` may overlap with existing methods.
211
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#prefer-public-send
212
+ Enabled: false
213
+ Style/SpecialGlobalVars:
214
+ Description: Avoid Perl-style global variables.
215
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-cryptic-perlisms
216
+ Enabled: false
217
+ Style/VariableInterpolation:
218
+ Description: "Don't interpolate global, instance and class variables directly in strings."
219
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#curlies-interpolate
220
+ Enabled: false
221
+ Style/WhenThen:
222
+ Description: Use when x then ... for one-line cases.
223
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#one-line-cases
224
+ Enabled: false
225
+ Lint/EachWithObjectArgument:
226
+ Description: Check for immutable argument given to each_with_object.
227
+ Enabled: true
228
+ Lint/HandleExceptions:
229
+ Description: Don't suppress exception.
230
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#dont-hide-exceptions
231
+ Enabled: false
232
+ Lint/LiteralInCondition:
233
+ Description: Checks of literals used in conditions.
234
+ Enabled: false
235
+ Lint/LiteralInInterpolation:
236
+ Description: Checks for literals used in interpolation.
237
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3.0
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in secupay_ruby.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Dominic Breuker
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.
data/README.md ADDED
@@ -0,0 +1,123 @@
1
+ # SecupayRuby
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/secupay_ruby`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'secupay_ruby'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install secupay_ruby
22
+
23
+ ## Usage
24
+
25
+ Configure the gem when you load your application like this:
26
+
27
+ ```ruby
28
+ SecupayRuby.configure do |config|
29
+ config.api_key = 'your secupay api key'
30
+ config.host = SecupayRuby::Host::PRODUCTION # default, change to TEST for testing
31
+ end
32
+ ```
33
+
34
+ Create a new payment like this:
35
+
36
+ ```ruby
37
+ payment = SecupayRuby::Payment.new
38
+
39
+ payment.init(amount: 500, payment_type: SecupayRuby::Payment::Types::PREPAY) # calls the API to create a new payment
40
+
41
+ payment.hash # returns a hash serving as payment ID
42
+ payment.purpose # returns reference number to be used as reference in wire transfer
43
+ payment.payment_data # returns bank account information for this payment
44
+ ```
45
+
46
+ Get status updates for a given payment
47
+ ```ruby
48
+ payment = SecupayRuby::Payment.new(hash: 'payment_hash')
49
+ payment.load_status # calls API to get status information
50
+
51
+ payment.status # returns current status
52
+ ...
53
+ ```
54
+
55
+ Working with sub API keys: You can request new API keys through the API with you main key.
56
+ You will at least need some user information and a payout account.
57
+ Create objects containing this information and pass them to the method requesting the code.
58
+ Use the following code to get a new key:
59
+
60
+ ```ruby
61
+ project_name = "Awesome new project"
62
+
63
+ user = SecupayRuby::DataObjects::User.new(title: "Mr.",
64
+ company: "ACME GmbH & Co KG",
65
+ first_name: "Peter",
66
+ last_name: "Müller",
67
+ street: "Sesamstraße",
68
+ house_number: "Sesamstadt",
69
+ zip: "12345",
70
+ city: "Sesamstadt",
71
+ phone_number: "0190123456",
72
+ date_of_birth: "06.10.1980",
73
+ email: "peter@example.com")
74
+
75
+ payout_account = SecupayRuby::DataObjects::PayoutAccount.new(iban: "DE123",
76
+ bic: "BIC456")
77
+
78
+ # request new key
79
+ subkey = SecupayRuby::ApiKey::SubKey.request_api_key(project_name: project_name,
80
+ user: user,
81
+ payout_account: payout_account)
82
+
83
+ # now you can create payment objects with the new key
84
+ payment = SecupayRuby::Payment.new api_key: subkey
85
+ ```
86
+
87
+ For storing keys requested through the API in a database, we recommend you store it in encrypted form.
88
+ You have to store the ciphertext, iv and mac along with the project name.
89
+
90
+ ```ruby
91
+ # for storage, do:
92
+ store_in_database(payment.ciphertext, payment.iv, payment.mac, payment.project_name)
93
+
94
+ ...
95
+
96
+ # load info from database
97
+ ciphertext = load_from_database(ciphertext)
98
+ iv = load_from_database(iv)
99
+ mac = load_from_database(mac)
100
+ project_name = load_from_database(project_name)
101
+
102
+ # create subkey object from this information
103
+ subkey = SecupayRuby::ApiKey::SubKey.new(ciphertext: ciphertext,
104
+ iv: iv,
105
+ mac: mac,
106
+ project_name: project_name)
107
+ ```
108
+
109
+ ## Development
110
+
111
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
112
+
113
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
114
+
115
+ ## Contributing
116
+
117
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/secupay_ruby.
118
+
119
+
120
+ ## License
121
+
122
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
123
+
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/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "secupay_ruby"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,12 @@
1
+ module SecupayRuby
2
+ module ApiKey
3
+ autoload :MasterKey, "secupay_ruby/api_key/master_key"
4
+ autoload :SubKey, "secupay_ruby/api_key/sub_key"
5
+
6
+ class Base
7
+ def key
8
+ raise NotImplementedError.new 'Abstract method'
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ module SecupayRuby
2
+ module ApiKey
3
+ class MasterKey
4
+ def key
5
+ SecupayRuby.config.api_key
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,100 @@
1
+ require 'openssl'
2
+ require 'base64'
3
+
4
+ module SecupayRuby
5
+ module ApiKey
6
+ class SubKey
7
+ attr_reader :ciphertext, :iv, :project_name, :mac
8
+ attr_accessor :contract_id, :payin_account
9
+
10
+ # Changing "iv", "mac" or "project_name" will cause a bad decrypt when getting key!
11
+ def initialize(ciphertext:, iv:, mac:, project_name:, contract_id: , payin_account: {})
12
+ @ciphertext = ciphertext
13
+ @iv = iv
14
+ @project_name = project_name
15
+ @mac = mac
16
+ self.contract_id = contract_id
17
+ self.payin_account = payin_account
18
+ end
19
+
20
+ def key
21
+ decrypt
22
+ end
23
+
24
+ private
25
+
26
+ def decrypt
27
+ d = self.class.cipher
28
+ d.decrypt
29
+ d.key = self.class.generate_secure_key
30
+ d.iv = Base64.decode64(iv)
31
+ d.auth_tag = Base64.decode64(mac)
32
+ d.auth_data = project_name
33
+
34
+ d.update(Base64.decode64(ciphertext)) + d.final
35
+ end
36
+
37
+ class << self
38
+ def request_api_key(project_name:, user:, payout_account:)
39
+ response = make_api_call(project_name, user, payout_account)
40
+
41
+ new_api_key = response.data["apikey"]
42
+ contract_id = response.data["contract_id"]
43
+ payin_account = response.data["payin_account"]
44
+
45
+ encryption = encrypt(new_api_key, project_name)
46
+
47
+ self.new(
48
+ ciphertext: encryption[:ciphertext],
49
+ iv: encryption[:iv],
50
+ project_name: project_name,
51
+ mac: encryption[:mac],
52
+ contract_id: contract_id,
53
+ payin_account: payin_account
54
+ )
55
+ end
56
+
57
+ def generate_secure_key
58
+ @secure_key ||= begin
59
+ pass = SecupayRuby.config.api_key
60
+ salt = "dflkhjfdgljhndgjlbnskjdbkh"
61
+ iter = 20000
62
+ key_len = 16
63
+ OpenSSL::PKCS5.pbkdf2_hmac_sha1(pass, salt, iter, key_len)
64
+ end
65
+ end
66
+
67
+ def cipher
68
+ OpenSSL::Cipher.new("aes-128-gcm")
69
+ end
70
+
71
+ private
72
+
73
+ def make_api_call(project_name, user, payout_account)
74
+ body = {
75
+ project: { name: project_name },
76
+ user: user.to_api_fields,
77
+ payout_account: payout_account.to_api_fields
78
+ }
79
+ response = SecupayRuby::Requests::RequestApiKey.post(api_key: SecupayRuby::ApiKey::MasterKey.new,
80
+ body: body)
81
+ end
82
+
83
+ def encrypt(plaintext, auth_data)
84
+ e = self.cipher
85
+ e.encrypt
86
+ e.key = self.generate_secure_key
87
+ iv = e.random_iv
88
+ e.auth_data = auth_data
89
+ ciphertext = e.update(plaintext) + e.final
90
+
91
+ {
92
+ iv: Base64.encode64(iv),
93
+ ciphertext: Base64.encode64(ciphertext),
94
+ mac: Base64.encode64(e.auth_tag)
95
+ }
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,18 @@
1
+ module SecupayRuby
2
+ module Host
3
+ TEST = "https://api-dist.secupay-ag.de"
4
+ PRODUCTION = "https://api.secupay.ag"
5
+ end
6
+
7
+ class Configuration
8
+ attr_accessor :api_key,
9
+ :host,
10
+ :url_success,
11
+ :url_failure,
12
+ :url_push
13
+
14
+ def initialize
15
+ self.host = SecupayRuby::Host::PRODUCTION
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,45 @@
1
+ module SecupayRuby
2
+ module DataObjects
3
+ autoload :User, "secupay_ruby/data_objects/user"
4
+ autoload :PayoutAccount, "secupay_ruby/data_objects/payout_account"
5
+
6
+ class Base
7
+
8
+ def to_api_fields
9
+ api_fields.keys.inject({}) do |hash, key|
10
+ hash[api_fields[key]] = self.send(key)
11
+ hash
12
+ end
13
+ end
14
+
15
+ def initialize(attributes);
16
+ class << self
17
+ self
18
+ end.class_eval do
19
+ attr_accessor(*(self::API_FIELDS.keys))
20
+ end
21
+
22
+ api_fields.keys.each do |key|
23
+ value = attributes[key]
24
+
25
+ raise_attribute_missing_error(key) if value.nil?
26
+
27
+ self.send("#{key}=", value)
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def raise_attribute_missing_error(attribute)
34
+ raise ArgumentError.new("Required attributes are: " +
35
+ api_fields.keys.to_s +
36
+ " --- missing: " +
37
+ attribute.to_s)
38
+ end
39
+
40
+ def api_fields
41
+ self.class::API_FIELDS
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,10 @@
1
+ module SecupayRuby
2
+ module DataObjects
3
+ class PayoutAccount < Base
4
+ API_FIELDS = {
5
+ iban: :iban,
6
+ bic: :bic
7
+ }
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,19 @@
1
+ module SecupayRuby
2
+ module DataObjects
3
+ class User < Base
4
+ API_FIELDS = {
5
+ title: :title,
6
+ company: :company,
7
+ first_name: :firstname,
8
+ last_name: :lastname,
9
+ street: :street,
10
+ house_number: :housenumber,
11
+ zip: :zip,
12
+ city: :city,
13
+ phone_number: :telephone,
14
+ date_of_birth: :dob_value,
15
+ email: :email
16
+ }
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,4 @@
1
+ module SecupayRuby
2
+ class RequestError < StandardError; end
3
+ class PaymentStatusError < StandardError; end
4
+ end
@@ -0,0 +1,143 @@
1
+ module SecupayRuby
2
+ class Payment
3
+ module Types
4
+ DEBIT = "debit"
5
+ PREPAY = "prepay"
6
+ end
7
+
8
+ attr_reader :api_key, :response, :amount, :payment_type, :demo
9
+
10
+ INIT_FIELDS = {
11
+ hash: "hash",
12
+ iframe_url: "iframe_url",
13
+ purpose: "purpose",
14
+ payment_data: "payment_data"
15
+ }
16
+ attr_reader(*(INIT_FIELDS.keys))
17
+
18
+ STATUS_FIELDS = {
19
+ payment_status: "payment_status",
20
+ status: "status",
21
+ created_at: "created",
22
+ demo: "demo",
23
+ amount: "amount",
24
+ payment_details: "opt",
25
+ transaction_id: "trans_id"
26
+ }
27
+ attr_reader(*(STATUS_FIELDS.keys))
28
+
29
+ def initialize(api_key: SecupayRuby::ApiKey::MasterKey.new, hash: nil)
30
+ @api_key = api_key
31
+ @hash = hash
32
+ end
33
+
34
+ def hash?
35
+ !hash.nil?
36
+ end
37
+
38
+ def init(amount:, payment_type:, demo: 1)
39
+ @response = nil
40
+
41
+ @amount = amount
42
+ @payment_type = payment_type
43
+ @demo = demo
44
+
45
+ raise_if_payment_not_initiated
46
+ raise_if_payment_type_invalid
47
+
48
+ params = {
49
+ amount: amount,
50
+ demo: demo,
51
+ payment_type: payment_type
52
+ }
53
+
54
+ @response = SecupayRuby::Requests::Init.post(api_key: api_key,
55
+ payment: self,
56
+ body: params)
57
+ raise_if_request_error
58
+
59
+ extract_response_params(INIT_FIELDS)
60
+ end
61
+
62
+ def load_status
63
+ raise_if_not_initiated
64
+
65
+ @response = nil
66
+
67
+ params = { hash: hash }
68
+ @response = SecupayRuby::Requests::Status.post(api_key: api_key,
69
+ payment: self,
70
+ body: params)
71
+ raise_if_request_error
72
+
73
+ extract_response_params(STATUS_FIELDS)
74
+ end
75
+
76
+ def capture
77
+ raise_if_not_initiated
78
+
79
+ @response = nil
80
+
81
+ @response = SecupayRuby::Requests::Capture.post(api_key: api_key,
82
+ payment: self)
83
+ raise_if_request_error
84
+ end
85
+
86
+ def cancel
87
+ raise_if_not_initiated
88
+
89
+ @response = nil
90
+
91
+ @response = SecupayRuby::Requests::Cancel.post(api_key: api_key,
92
+ payment: self)
93
+ raise_if_request_error
94
+ end
95
+
96
+ class << self
97
+ def get_types(api_key: SecupayRuby::ApiKey::MasterKey.new)
98
+ response = SecupayRuby::Requests::GetTypes.post(api_key: api_key)
99
+
100
+ raise RequestError, response.errors if response.http_status != "200" || response.status != "ok"
101
+
102
+ response.data
103
+ end
104
+ end
105
+
106
+ private
107
+
108
+ def raise_if_request_error
109
+ raise RequestError.new @response.errors if error?
110
+ end
111
+
112
+ def raise_if_not_initiated
113
+ raise PaymentStatusError.new "Not initiated" unless hash?
114
+ end
115
+
116
+ def raise_if_payment_type_invalid
117
+ unless valid_payment_type?
118
+ raise PaymentStatusError.new "Payment type not supported"
119
+ end
120
+ end
121
+
122
+ def raise_if_payment_not_initiated
123
+ raise PaymentStatusError.new "Payment already initiated" if hash?
124
+ end
125
+
126
+ def error?
127
+ @response.http_status != "200" || @response.status != "ok"
128
+ end
129
+
130
+ def extract_response_params(fields)
131
+ fields.each do |field_name, param_name|
132
+ instance_variable_set("@#{field_name}", @response.data[param_name])
133
+ end
134
+ end
135
+
136
+ def valid_payment_type?
137
+ return false unless @payment_type
138
+
139
+ valid_types = Types.constants.map { |constant| Types.const_get constant }
140
+ valid_types.include?(@payment_type)
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,3 @@
1
+ module SecupayRuby
2
+ PayoutAccount = Struct.new(:iban, :bic)
3
+ end
@@ -0,0 +1,81 @@
1
+ require "net/http"
2
+ require "json"
3
+ require "uri"
4
+
5
+ module SecupayRuby
6
+ module Requests
7
+ autoload :GetTypes, "secupay_ruby/requests/get_types"
8
+ autoload :Init, "secupay_ruby/requests/init"
9
+ autoload :Status, "secupay_ruby/requests/status"
10
+ autoload :Capture, "secupay_ruby/requests/capture"
11
+ autoload :Cancel, "secupay_ruby/requests/cancel"
12
+ autoload :RequestApiKey, "secupay_ruby/requests/request_api_key"
13
+
14
+ class Base
15
+ class << self
16
+ def post(api_key: , payment: nil, body: {})
17
+ new(api_key: api_key,
18
+ payment: payment,
19
+ body: body).post
20
+ end
21
+ end
22
+
23
+ attr_reader :payment, :body
24
+
25
+ def initialize(api_key: , payment: nil, body: {})
26
+ @api_key = api_key
27
+ @payment = payment
28
+ @body = body
29
+ end
30
+
31
+ def post
32
+ http_response
33
+ end
34
+
35
+ def uri
36
+ @uri ||= URI.parse([SecupayRuby.config.host, path].join("/"))
37
+ end
38
+
39
+ def path
40
+ raise NotImplementedError.new "Abstract method!"
41
+ end
42
+
43
+ def defaults
44
+ {
45
+ apikey: @api_key.key
46
+ }
47
+ end
48
+
49
+ def http_request
50
+ @request ||= begin
51
+ request = Net::HTTP::Post.new(uri.request_uri,
52
+ {
53
+ "Content-Type" => "application/json; charset=utf-8;",
54
+ "Accept" => "application/json;"
55
+ })
56
+
57
+ request.body = { data: defaults.merge(body) }.to_json
58
+
59
+ request
60
+ end
61
+ end
62
+
63
+ Response = Struct.new(:http_status, :status, :data, :errors)
64
+
65
+ def http_response
66
+ @response ||= begin
67
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
68
+ http.request(http_request)
69
+ end
70
+
71
+ json_response = JSON.parse(response.body)
72
+
73
+ Response.new(response.header.code,
74
+ json_response["status"],
75
+ json_response["data"],
76
+ json_response["errors"])
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,9 @@
1
+ module SecupayRuby
2
+ module Requests
3
+ class Cancel < Base
4
+ def path
5
+ @path ||= ["payment", payment.hash, "cancel"].join("/")
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module SecupayRuby
2
+ module Requests
3
+ class Capture < Base
4
+ def path
5
+ @path ||= ["payment", payment.hash, "capture"].join("/")
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module SecupayRuby
2
+ module Requests
3
+ class GetTypes < Base
4
+ def path
5
+ @path ||= %w(payment gettypes).join("/")
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,16 @@
1
+ module SecupayRuby
2
+ module Requests
3
+ class Init < Base
4
+ def defaults
5
+ super.merge(
6
+ url_success: SecupayRuby.config.url_success,
7
+ url_failure: SecupayRuby.config.url_failure,
8
+ url_push: SecupayRuby.config.url_push)
9
+ end
10
+
11
+ def path
12
+ @path ||= %w(payment init).join("/")
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,9 @@
1
+ module SecupayRuby
2
+ module Requests
3
+ class RequestApiKey < Base
4
+ def path
5
+ @path ||= ["payment", "requestapikey"].join("/")
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module SecupayRuby
2
+ module Requests
3
+ class Status < Base
4
+ def path
5
+ @path ||= %w(payment status).join("/")
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module SecupayRuby
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,25 @@
1
+ require "secupay_ruby/version"
2
+
3
+ module SecupayRuby
4
+ autoload :Configuration, "secupay_ruby/configuration"
5
+ autoload :Payment, "secupay_ruby/payment"
6
+
7
+ autoload :Requests, "secupay_ruby/requests/base"
8
+ autoload :ApiKey, "secupay_ruby/api_key/base"
9
+ autoload :DataObjects, "secupay_ruby/data_objects/base"
10
+
11
+ autoload :RequestError, "secupay_ruby/exceptions"
12
+ autoload :PaymentStatusError, "secupay_ruby/exceptions"
13
+
14
+ class << self
15
+ def configure
16
+ yield configuration
17
+ end
18
+
19
+ def configuration
20
+ @configuration ||= SecupayRuby::Configuration.new
21
+ end
22
+
23
+ alias :config :configuration
24
+ end
25
+ 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 "secupay_ruby/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "secupay_ruby"
8
+ spec.version = SecupayRuby::VERSION
9
+ spec.authors = ["Dominic Breuker"]
10
+ spec.email = ["dominic.breuker@gmail.com"]
11
+
12
+ spec.summary = "API wrapper for SecuPay"
13
+ spec.description = "Only tested with prepay so far."
14
+ spec.homepage = "https://github.com/HitFox/secupay_ruby"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "dotenv"
25
+ spec.add_development_dependency "pry"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "vcr"
28
+ spec.add_development_dependency "webmock"
29
+ end
metadata ADDED
@@ -0,0 +1,173 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: secupay_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dominic Breuker
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-04 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: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
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: dotenv
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
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: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: vcr
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: webmock
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Only tested with prepay so far.
112
+ email:
113
+ - dominic.breuker@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".rubocop.yml"
121
+ - ".ruby-version"
122
+ - ".travis.yml"
123
+ - Gemfile
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/setup
129
+ - lib/secupay_ruby.rb
130
+ - lib/secupay_ruby/api_key/base.rb
131
+ - lib/secupay_ruby/api_key/master_key.rb
132
+ - lib/secupay_ruby/api_key/sub_key.rb
133
+ - lib/secupay_ruby/configuration.rb
134
+ - lib/secupay_ruby/data_objects/base.rb
135
+ - lib/secupay_ruby/data_objects/payout_account.rb
136
+ - lib/secupay_ruby/data_objects/user.rb
137
+ - lib/secupay_ruby/exceptions.rb
138
+ - lib/secupay_ruby/payment.rb
139
+ - lib/secupay_ruby/payout_account.rb
140
+ - lib/secupay_ruby/requests/base.rb
141
+ - lib/secupay_ruby/requests/cancel.rb
142
+ - lib/secupay_ruby/requests/capture.rb
143
+ - lib/secupay_ruby/requests/get_types.rb
144
+ - lib/secupay_ruby/requests/init.rb
145
+ - lib/secupay_ruby/requests/request_api_key.rb
146
+ - lib/secupay_ruby/requests/status.rb
147
+ - lib/secupay_ruby/version.rb
148
+ - secupay_ruby.gemspec
149
+ homepage: https://github.com/HitFox/secupay_ruby
150
+ licenses:
151
+ - MIT
152
+ metadata: {}
153
+ post_install_message:
154
+ rdoc_options: []
155
+ require_paths:
156
+ - lib
157
+ required_ruby_version: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ required_rubygems_version: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ requirements: []
168
+ rubyforge_project:
169
+ rubygems_version: 2.5.1
170
+ signing_key:
171
+ specification_version: 4
172
+ summary: API wrapper for SecuPay
173
+ test_files: []