barion 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.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +158 -0
  4. data/Rakefile +34 -0
  5. data/app/assets/config/barion_manifest.js +1 -0
  6. data/app/assets/stylesheets/barion/application.css +15 -0
  7. data/app/assets/stylesheets/barion/main.css +4 -0
  8. data/app/controllers/barion/application_controller.rb +7 -0
  9. data/app/controllers/barion/main_controller.rb +28 -0
  10. data/app/helpers/barion/application_helper.rb +7 -0
  11. data/app/helpers/barion/main_helper.rb +7 -0
  12. data/app/models/barion/address.rb +60 -0
  13. data/app/models/barion/application_record.rb +10 -0
  14. data/app/models/barion/gift_card_purchase.rb +66 -0
  15. data/app/models/barion/item.rb +88 -0
  16. data/app/models/barion/payer_account.rb +119 -0
  17. data/app/models/barion/payment.rb +348 -0
  18. data/app/models/barion/payment_transaction.rb +134 -0
  19. data/app/models/barion/purchase.rb +107 -0
  20. data/app/models/concerns/barion/currencies.rb +11 -0
  21. data/app/models/concerns/barion/data_formats.rb +12 -0
  22. data/app/models/concerns/barion/json_serializer.rb +106 -0
  23. data/app/views/barion/main/land.html.erb +7 -0
  24. data/config/initializers/barion.rb +9 -0
  25. data/config/routes.rb +10 -0
  26. data/db/migrate/20201222235354_create_barion_payments.rb +59 -0
  27. data/db/migrate/20201223001557_create_barion_addresses.rb +21 -0
  28. data/db/migrate/20201223002427_create_barion_payment_transactions.rb +28 -0
  29. data/db/migrate/20201223003219_create_barion_purchases.rb +24 -0
  30. data/db/migrate/20210111051233_create_barion_payer_accounts.rb +28 -0
  31. data/db/migrate/20210128220347_create_barion_items.rb +21 -0
  32. data/db/migrate/20210201120609_create_barion_gift_card_purchases.rb +14 -0
  33. data/lib/barion.rb +100 -0
  34. data/lib/barion/engine.rb +14 -0
  35. data/lib/barion/version.rb +5 -0
  36. data/lib/tasks/auto_annotate_models.rake +59 -0
  37. data/lib/tasks/barion_tasks.rake +4 -0
  38. metadata +207 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '087811487218d3c608a2e5623faa76ac7bf8e1b223b1df66df6a441c19577540'
4
+ data.tar.gz: d592774486b56bab36a54746bd7369dd455438057ffa87e466c866e1d4ea8e08
5
+ SHA512:
6
+ metadata.gz: e0b1972b063fc2f178014b3f07725db3667b56d0203082174806d62c03946b9122863f717480d03099d0bc0d7fedbbbc83c96148f8b29d2fb23e2060360b0894
7
+ data.tar.gz: ef3075e553a6a96b430b574c6e9c3e3a8bf9d1dd6a5f85c0c319b430a16d2e6c8542abd5fa2dcef0bb1f8e54fc219f1e24896856b21632fbbb222f89ad66b20d
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2020 Péter Nagy
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,158 @@
1
+ # Barion
2
+ ![Barion](https://github.com/Meter-reader/Barion/workflows/Ruby/badge.svg)
3
+ [![codecov](https://codecov.io/gh/Meter-Reader/barion/branch/main/graph/badge.svg?token=DCOKCM7B2J)](https://codecov.io/gh/Meter-Reader/barion)
4
+
5
+ This is a Ruby-on-Rails engine to use the Barion Payment Gateway in any RoR application.
6
+
7
+ ## Usage
8
+ How to use my plugin.
9
+
10
+ ## Installation
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'barion'
15
+ ```
16
+
17
+ And then execute:
18
+ ```bash
19
+ $ bundle
20
+ ```
21
+
22
+ Or install it yourself as:
23
+ ```bash
24
+ $ gem install barion
25
+ ```
26
+ ## Usage
27
+ ### Intitalization
28
+ After installing the gem you have to initalize it.
29
+ Just create a file in `config/initializers/barion.rb` with the below content:
30
+ ```ruby
31
+ ::Barion.config do |shop|
32
+ shop.poskey = ''
33
+ shop.publickey = ''
34
+ shop.sandbox = true
35
+ shop.acronym = ''
36
+ shop.default_payee = ''
37
+ end
38
+ ```
39
+ #### POSKey and PublicKey (default: empty)
40
+ You can find your `POSKey` and `PublicKey` at the Barion Shop website:
41
+
42
+ Test : https://secure.test.barion.com/Shop/
43
+
44
+ Production : https://secure.barion.com/Shop/
45
+
46
+ ![POSKey and PublicKey at Barion Shop](https://docs.barion.com/images/2/2b/Poskey.jpg "POSKey and PublicKey at Barion Shop")
47
+
48
+ You have to open the shop Details from the `Action` dropdown of your shop. Please be aware that these values are different for Test and Production shops.
49
+
50
+ #### Sandbox (default: true)
51
+ It is **highly recommended** to use a Test shop first to tune the code first. That's why `sandbox` is set to **true** by default.
52
+
53
+ #### Acronym (default: empyty)
54
+ Acronym is used to generate the `payment_request_id` Barion requires to identify every payment request as the below code snippets shows. You can change this by overriding/decorating this method in your code.
55
+
56
+ ```ruby
57
+ def create_payment_request_id
58
+ self.payment_request_id = "#{::Barion.acronym}#{::Time.now.to_f.to_s.gsub('.', '')}" if payment_request_id.nil?
59
+ end
60
+ ```
61
+ #### Default payee (Default: empty)
62
+ Barion requires an e-mail address in every transaction to identify the recipient user of the transaction. The Barion wallet which has this e-mail address registeres will receives the money when the payment is completed by the payer.
63
+
64
+ You can see this address in the Barion Shop interface below your shop icon on the top left corner.
65
+
66
+ ### Set up your database
67
+ This gem comes with predefined models. To use them, you have to copy the migrations into your application and migrate them using:
68
+ ```bash
69
+ bundle exec rails barion:install:migrations
70
+ bundle exec rails db:migrate SCOPE=barion
71
+ ```
72
+
73
+ ### Mounting the engine
74
+ You have to mount the Barion engine to be able to receive callback(s) from Barion Gateway like:
75
+ ```ruby
76
+ mount ::Barion::Engine, at: "/barion"
77
+ ```
78
+
79
+ ### Creating payment
80
+ Once initialized you can create payments like below:
81
+ ```ruby
82
+ payment = ::Barion:Payment.new
83
+ transaction = payment.transactions.build
84
+ item = transaction.items.build
85
+ ```
86
+ * You can find the properties of Payment here: https://docs.barion.com/Payment-Start-v2,
87
+
88
+ * for the properties of Transaction see here: https://docs.barion.com/PaymentTransaction
89
+ (*Note*: Transaction is used not just for starting the payment but to collect transaction details during the payment lifecycle.)
90
+
91
+ * and for Item properties: https://docs.barion.com/Item
92
+
93
+ To check if all required properties are filled out you can use Rails built in validation:
94
+ ```ruby
95
+ unless payment.valid?
96
+ payment.errors.object.each { |error|
97
+ puts error.full_message
98
+ }
99
+ ```
100
+
101
+ Once the payment is prepared you can start it by issuing
102
+ ```ruby
103
+ payment.execute
104
+ ```
105
+ This will send the required API requests to the Barion Gateway, depending on the `::Barion.sandbox` value either to the Test or the Production environment.
106
+
107
+ When the payment started you will receive callback(s) from Barion Gateway when the state of your payment has been changed. There are several routes defined by this gem:
108
+
109
+ ```ruby
110
+ post 'callback', to: 'main#callback', as: :gateway_callback
111
+ get 'land', to: 'main#land', as: :gateway_back
112
+ ```
113
+ The `gateway_callback` route will receive these callbacks from Barion Gateway. Every such message contains a `PaymentId` to identify which Payment has a state change. The gem is automatically tries to call a state refresh and get all the data of the payment from the Gateway.
114
+
115
+ You can call this manually too by issuing:
116
+
117
+ ```ruby
118
+ payment.refresh_state
119
+ ```
120
+ You can read more about this callback mechanism at https://docs.barion.com/Callback_mechanism.
121
+
122
+ #### Payment successful
123
+ When a payment is completed at the Barion Gateway it will automatically redirect the user back to your application. By default every payment request will held this redirect URL, which points to this gem's `:gateway_back` route.
124
+ You can change this however by setting the URL in the actual payment when creating it.
125
+
126
+ #### Payment failed
127
+ When a payment hasn't been completed if will eventually timeout at the Gateway. But if there was an issue of
128
+
129
+ ### Error handling
130
+ The gem has a lightweight wrapper for errors that the Barion API reports.
131
+
132
+ When executing a payment (or in fact any other method which has an API call behind) an error can happen which the Barion API returns.
133
+
134
+ The gem will raise a ``::Barion::Error`` type exception when receives such error(s). If multiple errors are returned, the exception will have an `errors` property which will contains all errors in an array also wrapped by ``::Barion::Error``.
135
+
136
+ ``::Barion::Error`` has the folowinf properties:
137
+ ```ruby
138
+ error = ::Barion::Error.new
139
+ error.title # Holds the value of Title attribute of the API error
140
+ error.message # Holds the value of Description attribute of the API error
141
+ error.error_code # Holds the value of ErrorCode attribute of the API error
142
+ error.happened_at # Holds the value of HappenedAt attribute of the API error
143
+ error.auth_data # Holds the value of AuthData attribute of the API error
144
+ error.endpoint # Holds the value of Endpoint attribute of the API error
145
+ ```
146
+
147
+ ### Security
148
+ When a payment has been sent to the gateway it will become readonly for Rails. Only the Gateway is allowed to change its state trough the callbacks.
149
+
150
+ To achieve this, a has is calculated for every payment when validating it. This hash will be stored in the DB and when a record is loaded in Rails it will be checked against the stored hash. If the hash does not match with the stored data a `::Barion::TamperedData` exception will be raised and the record will not be loaded. This basically mean that the data in the DB has been tampered outside this gem and cannot be considered financially safe.
151
+
152
+ ## Contributing
153
+ We encourage you to contribute to Barion gem!
154
+ ### Reporting issues or requesting features
155
+ This gem uses [GitHub Issue Tracking](https://github.com/meter-reader/barion/issues) to track issues (primarily bugs and contributions of new code). If you've found a bug or have a good idea of a cool feature, this is the place to start. You'll need to create a (free) GitHub account in order to submit an issue, to comment on them, or to create pull requests.
156
+
157
+ ## License
158
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ require 'rdoc/task'
10
+
11
+ RDoc::Task.new(:rdoc) do |rdoc|
12
+ rdoc.rdoc_dir = 'rdoc'
13
+ rdoc.title = 'Barion'
14
+ rdoc.options << '--line-numbers'
15
+ rdoc.rdoc_files.include('README.md')
16
+ rdoc.rdoc_files.include('lib/**/*.rb')
17
+ end
18
+
19
+ APP_RAKEFILE = File.expand_path('test/dummy/Rakefile', __dir__)
20
+ load 'rails/tasks/engine.rake'
21
+
22
+ load 'rails/tasks/statistics.rake'
23
+
24
+ require 'bundler/gem_tasks'
25
+
26
+ require 'rake/testtask'
27
+
28
+ Rake::TestTask.new(:test) do |t|
29
+ t.libs << 'test'
30
+ t.pattern = 'test/**/*_test.rb'
31
+ t.verbose = false
32
+ end
33
+
34
+ task default: :test
@@ -0,0 +1 @@
1
+ //= link_directory ../stylesheets/barion .css
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Barion
4
+ class ApplicationController < ActionController::Base
5
+ protect_from_forgery with: :exception
6
+ end
7
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_dependency 'barion/application_controller'
4
+
5
+ module Barion
6
+ # Barion engine's main controller to receive callback and redirects
7
+ class MainController < ApplicationController
8
+ def callback
9
+ @payment = ::Barion::Payment.find_by_payment_id(payment_params)
10
+ if @payment.present?
11
+ head :ok
12
+ @payment.refresh_state
13
+ else
14
+ head :unprocessable_entity
15
+ end
16
+ end
17
+
18
+ def land
19
+ @payment = ::Barion::Payment.find_by_payment_id(payment_params)
20
+ end
21
+
22
+ private
23
+
24
+ def payment_params
25
+ params.require(:paymentId)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Barion
4
+ # Common helpers for the Barion engine
5
+ module ApplicationHelper
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Barion
4
+ # Helpers for main controller
5
+ module MainHelper
6
+ end
7
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ # == Schema Information
4
+ #
5
+ # Table name: barion_addresses
6
+ #
7
+ # id :integer not null, primary key
8
+ # city :string(50)
9
+ # country :string(2) default("zz"), not null
10
+ # full_name :string(45)
11
+ # region :string(2)
12
+ # street :string(50)
13
+ # street2 :string(50)
14
+ # street3 :string(50)
15
+ # zip :string(16)
16
+ # created_at :datetime not null
17
+ # updated_at :datetime not null
18
+ # payment_id :bigint
19
+ #
20
+ # Indexes
21
+ #
22
+ # index_barion_addresses_on_city (city)
23
+ # index_barion_addresses_on_country (country)
24
+ # index_barion_addresses_on_full_name (full_name)
25
+ # index_barion_addresses_on_payment_id (payment_id)
26
+ # index_barion_addresses_on_zip (zip)
27
+ #
28
+ module Barion
29
+ # Represents a postal address fro Barion engine
30
+ class Address < ApplicationRecord
31
+ include ::Barion::JsonSerializer
32
+
33
+ belongs_to :payment, inverse_of: :shipping_address
34
+ belongs_to :payment, inverse_of: :billing_address
35
+
36
+ validates :country, presence: true, length: { is: 2 }
37
+ validates :zip, length: { maximum: 16 }, allow_nil: true
38
+ validates :city, length: { maximum: 50 }, allow_nil: true
39
+ validates :region, length: { is: 2 }, allow_nil: true
40
+ validates :street, length: { maximum: 50 }, allow_nil: true
41
+ validates :street2, length: { maximum: 50 }, allow_nil: true
42
+ validates :street3, length: { maximum: 50 }, allow_nil: true
43
+ validates :full_name, length: { maximum: 45 }, allow_nil: true
44
+
45
+ def serialize_options
46
+ {
47
+ map: {
48
+ except: %i[updated_at created_at],
49
+ keys: {
50
+ _all: :camelize
51
+ },
52
+ values: {
53
+ _all: proc { |v| v.respond_to?(:camelize) ? v.camelize : v },
54
+ _except: %w[country]
55
+ }
56
+ }
57
+ }
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Barion
4
+ # Abstract data record class
5
+ #
6
+ # @abstract
7
+ class ApplicationRecord < ActiveRecord::Base
8
+ self.abstract_class = true
9
+ end
10
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ # == Schema Information
4
+ #
5
+ # Table name: barion_gift_card_purchases
6
+ #
7
+ # id :integer not null, primary key
8
+ # amount :decimal(, ) not null
9
+ # count :integer not null
10
+ # created_at :datetime not null
11
+ # updated_at :datetime not null
12
+ # purchase_id :integer
13
+ #
14
+ # Indexes
15
+ #
16
+ # index_barion_gift_card_purchases_on_purchase_id (purchase_id)
17
+ #
18
+
19
+ module Barion
20
+ # Represents a Gift card purchase in Barion engine
21
+ class GiftCardPurchase < ApplicationRecord
22
+ include ::Barion::JsonSerializer
23
+
24
+ belongs_to :purchase, inverse_of: :gift_card_purchase
25
+
26
+ attribute :amount, :decimal
27
+ attribute :count, :integer
28
+
29
+ validates_associated :purchase
30
+ validates :amount, numericality: { greater_than: 0 }
31
+ validates :count, numericality: { only_integer: true }, inclusion: { in: 1..99 }
32
+
33
+ def serialize_options
34
+ { except: %i[id created_at updated_at currency],
35
+ map: {
36
+ keys: {
37
+ _all: :camelize
38
+ },
39
+ values: {
40
+ _all: proc { |v| v.respond_to?(:camelize) ? v.camelize : v },
41
+ amount: :as_string
42
+ }
43
+ } }
44
+ end
45
+
46
+ def amount
47
+ value = super
48
+ return nil if value.nil?
49
+
50
+ case currency
51
+ when nil
52
+ value
53
+ when 'HUF'
54
+ value.round
55
+ else
56
+ value.round(2)
57
+ end
58
+ end
59
+
60
+ def currency
61
+ return nil if purchase.nil? || purchase.payment.nil?
62
+
63
+ purchase.payment.currency
64
+ end
65
+ end
66
+ end