barion 0.3.4 → 0.4.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 99100de21393d99ec14a7fbb36b45f470b0f400a1ab4267ccac25b30bd692aff
4
- data.tar.gz: c0f7fcdb1d15bdc3556520d25f5a03c47a4a152aeba1620b4df64582d937d648
3
+ metadata.gz: 1ce7445be623c98ab928342e5a7c91e6cefba8289c000deffee990b231ec5579
4
+ data.tar.gz: 279f4a94a13cc650e92f37da59b8fa32819d120192062a931da51cf1dce4f2c0
5
5
  SHA512:
6
- metadata.gz: 8370b7ae8f031cc2e466bceab7c9e5b9546fbd4cc5e793c9d133cce8fa5649bef5f1cd976a7edddc081b5ce1059c33e37ebbb6df2833ffa25709ddcb81590371
7
- data.tar.gz: de5e8b523272341bb6a3f52a48f99b54cc699cf7c832c3a9ff1d1eaef83922f151d4a78765073d8d1d51e148fb3540989b704479858e6ee740291eeb3623d270
6
+ metadata.gz: e149b35819edab85f6455fde84cdcbc9bf90073de71ace121fe77c56175475fdf9d390538320112877018c2c39b6421c5a4018e42ab99df5b25159301b2b9dbc
7
+ data.tar.gz: f17d849b68270f45113ba7a23a1680644f4b2f764fc65bd070308c580165369465ba4913acfbbcae35c5b1adddaa877ad4b49b9707e6ce2dc8ec29240d1315ec
data/MIT-LICENSE CHANGED
File without changes
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # Barion
2
+
2
3
  ![Barion](https://github.com/Meter-reader/Barion/workflows/Ruby/badge.svg)
3
4
  [![codecov](https://codecov.io/gh/Meter-Reader/barion/branch/main/graph/badge.svg?token=DCOKCM7B2J)](https://codecov.io/gh/Meter-Reader/barion)
4
5
  [![Gem Version](https://badge.fury.io/rb/barion.svg)](https://badge.fury.io/rb/barion)
@@ -6,6 +7,7 @@
6
7
  This is a Ruby-on-Rails engine to use the Barion Payment Gateway in any RoR application.
7
8
 
8
9
  ## Installation
10
+
9
11
  Add this line to your application's Gemfile:
10
12
 
11
13
  ```ruby
@@ -13,42 +15,59 @@ gem 'barion'
13
15
  ```
14
16
 
15
17
  And then execute:
18
+
16
19
  ```bash
17
- $ bundle
20
+ bundle
18
21
  ```
19
22
 
20
23
  Or install it yourself as:
24
+
21
25
  ```bash
22
- $ gem install barion
26
+ gem install barion
23
27
  ```
28
+
24
29
  ## Usage
30
+
25
31
  ### Initalization
32
+
26
33
  After installing the gem you have to initalize it.
27
34
  Just create a file in `config/initializers/barion.rb` with the below content:
35
+
28
36
  ```ruby
29
- ::Barion.config do |shop|
30
- shop.poskey = ''
31
- shop.publickey = ''
32
- shop.sandbox = true
33
- shop.acronym = ''
34
- shop.default_payee = ''
35
- end
37
+ Barion.poskey = ''
38
+ Barion.publickey = ''
39
+ Barion.pixel_id = ''
40
+ Barion.sandbox = true
41
+ Barion.acronym = ''
42
+ Barion.default_payee = ''
36
43
  ```
44
+
37
45
  #### POSKey and PublicKey (default: empty)
46
+
38
47
  You can find your `POSKey` and `PublicKey` at the Barion Shop website:
39
48
 
40
- Test : https://secure.test.barion.com/Shop/
49
+ Test : <https://secure.test.barion.com/Shop/>
41
50
 
42
- Production : https://secure.barion.com/Shop/
51
+ Production : <https://secure.barion.com/Shop/>
43
52
 
44
53
  ![POSKey and PublicKey at Barion Shop](https://docs.barion.com/images/2/2b/Poskey.jpg "POSKey and PublicKey at Barion Shop")
45
54
 
46
55
  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.
47
56
 
57
+ #### Pixel ID (default: '')
58
+
59
+ [Implementing the Base Barion Pixel](https://docs.barion.com/Implementing_the_Base_Barion_Pixel)
60
+
61
+ Your Barion Pixel ID can be found in the Details page of your Barion shop. Every webshop (with a unique POSKey) has a different Barion Pixel ID. You can find your Barion Pixel ID in your Barion wallet: click `Manage my shops` menu, `Actions` next to your shop and then `Details`.
62
+
63
+
64
+
48
65
  #### Sandbox (default: true)
66
+
49
67
  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.
50
68
 
51
- #### Acronym (default: empyty)
69
+ #### Acronym (default: empty)
70
+
52
71
  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.
53
72
 
54
73
  ```ruby
@@ -56,39 +75,49 @@ def create_payment_request_id
56
75
  self.payment_request_id = "#{::Barion.acronym}#{::Time.now.to_f.to_s.gsub('.', '')}" if payment_request_id.nil?
57
76
  end
58
77
  ```
78
+
59
79
  #### Default payee (Default: empty)
80
+
60
81
  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.
61
82
 
62
83
  You can see this address in the Barion Shop interface below your shop icon on the top left corner.
63
84
 
64
85
  ### Set up your database
86
+
65
87
  This gem comes with predefined models. To use them, you have to copy the migrations into your application and migrate them using:
88
+
66
89
  ```bash
67
90
  bundle exec rails barion:install:migrations
68
91
  bundle exec rails db:migrate SCOPE=barion
69
92
  ```
70
93
 
71
94
  ### Mounting the engine
95
+
72
96
  You have to mount the Barion engine to be able to receive callback(s) from Barion Gateway like:
97
+
73
98
  ```ruby
74
99
  mount ::Barion::Engine, at: "/barion"
75
100
  ```
76
101
 
77
102
  ### Creating payment
103
+
78
104
  Once initialized you can create payments like below:
105
+
79
106
  ```ruby
80
107
  payment = ::Barion:Payment.new
81
108
  transaction = payment.transactions.build
82
109
  item = transaction.items.build
83
110
  ```
84
- * You can find the properties of Payment here: https://docs.barion.com/Payment-Start-v2,
85
111
 
86
- * for the properties of Transaction see here: https://docs.barion.com/PaymentTransaction
112
+ * You can find the properties of Payment here: <https://docs.barion.com/Payment-Start-v2>,
113
+
114
+ * for the properties of Transaction see here: <https://docs.barion.com/PaymentTransaction>
87
115
  (*Note*: Transaction is used not just for starting the payment but to collect transaction details during the payment lifecycle.)
88
116
 
89
- * and for Item properties: https://docs.barion.com/Item
117
+ * and for Item properties: <https://docs.barion.com/Item>
90
118
 
91
119
  To check if all required properties are filled out you can use Rails built in validation:
120
+
92
121
  ```ruby
93
122
  unless payment.valid?
94
123
  payment.errors.object.each { |error|
@@ -97,9 +126,11 @@ payment.errors.object.each { |error|
97
126
  ```
98
127
 
99
128
  Once the payment is prepared you can start it by issuing
129
+
100
130
  ```ruby
101
131
  payment.execute
102
132
  ```
133
+
103
134
  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.
104
135
 
105
136
  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,6 +139,7 @@ When the payment started you will receive callback(s) from Barion Gateway when t
108
139
  post 'callback', to: 'main#callback', as: :gateway_callback
109
140
  get 'land', to: 'main#land', as: :gateway_back
110
141
  ```
142
+
111
143
  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.
112
144
 
113
145
  You can call this manually too by issuing:
@@ -115,23 +147,28 @@ You can call this manually too by issuing:
115
147
  ```ruby
116
148
  payment.refresh_state
117
149
  ```
118
- You can read more about this callback mechanism at https://docs.barion.com/Callback_mechanism.
150
+
151
+ You can read more about this callback mechanism at <https://docs.barion.com/Callback_mechanism>.
119
152
 
120
153
  #### Payment successful
154
+
121
155
  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.
122
156
  You can change this however by setting the URL in the actual payment when creating it.
123
157
 
124
158
  #### Payment failed
159
+
125
160
  When a payment hasn't been completed if will eventually timeout at the Gateway. But if there was an issue of
126
161
 
127
162
  ### Error handling
163
+
128
164
  The gem has a lightweight wrapper for errors that the Barion API reports.
129
165
 
130
166
  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.
131
167
 
132
168
  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``.
133
169
 
134
- ``::Barion::Error`` has the folowinf properties:
170
+ ``::Barion::Error`` has the following properties:
171
+
135
172
  ```ruby
136
173
  error = ::Barion::Error.new
137
174
  error.title # Holds the value of Title attribute of the API error
@@ -143,14 +180,37 @@ error.endpoint # Holds the value of Endpoint attribute of the API error
143
180
  ```
144
181
 
145
182
  ### Security
183
+
146
184
  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.
147
185
 
148
186
  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.
149
187
 
150
188
  ## Contributing
151
- We encourage you to contribute to Barion gem!
189
+
190
+ We encourage you to contribute to this Barion gem!
191
+ To run tests:
192
+
193
+ 1. Fork the repository
194
+ 2. Check out your fork of this repo
195
+ 3. Set up rbenv and add rbenv-vars as plugin
196
+ 4. Set up your variables in .rbenv-vars like this:
197
+
198
+ ```ruby
199
+ CODECOV_TOKEN=<your token from Codecov.io>
200
+ TEST_PAYEE=<your registered test shop email address at Barion>
201
+ TEST_PIXEL_ID=<your test pixel id from Barion>
202
+ TEST_POSKEY=<your test poskey from Barion>
203
+ TEST_PUBKEY=<your test pubkey from Barion>
204
+ ```
205
+
206
+ 5. Run ``rails t`` to see if tests are working.
207
+ 6. start modifying the code and run tests all the time.
208
+ 7. Submit pull-request when done.
209
+
152
210
  ### Reporting issues or requesting features
211
+
153
212
  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.
154
213
 
155
214
  ## License
215
+
156
216
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile CHANGED
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Barion related functions
4
+ module Barion
5
+ # Adds Barion Pixel javascript to views
6
+ require 'action_view'
7
+ # Provides functions for Barion Pixel implementations in views
8
+ module PixelHelper
9
+ include ActionView::Helpers::TagHelper
10
+ def pixel_basic_js
11
+ javascript_tag %(
12
+ window["bp"] = window["bp"] || function () {(window["bp"].q = window["bp"].q || []).push(arguments);};
13
+ window["bp"].l = 1 * new Date();
14
+ scriptElement = document.createElement("script");
15
+ firstScript = document.getElementsByTagName("script")[0];
16
+ scriptElement.async = true;
17
+ scriptElement.src = 'https://pixel.barion.com/bp.js';
18
+ firstScript.parentNode.insertBefore(scriptElement, firstScript);
19
+ window['barion_pixel_id'] = '#{::Barion.pixel_id}';
20
+ // Send init event
21
+ bp('init', 'addBarionPixelId', window['barion_pixel_id']);'
22
+ ).gsub!(/^#{[/\A\s*/]}/, '')
23
+ end
24
+
25
+ def pixel_basic_noscript
26
+ tag.noscript do
27
+ tag.img height: 1, width: 1,
28
+ style: 'display:none',
29
+ alt: 'Barion Pixel',
30
+ src: "https://pixel.barion.com/a.gif?ba_pixel_id='#{::Barion.pixel_id}'&ev=contentView&noscript=1"
31
+ end
32
+ end
33
+
34
+ def pixel_basic_tags
35
+ pixel_basic_js + pixel_basic_noscript
36
+ end
37
+ end
38
+ end
File without changes
@@ -5,5 +5,6 @@
5
5
  # shop.publickey = 'test2'
6
6
  # shop.sandbox = true
7
7
  # shop.acronym = ''
8
+ # shop.pixel_id = ''
8
9
  # shop.default_payee = 'me@example.org'
9
10
  # end
data/config/routes.rb CHANGED
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # == Route Map
4
- #
5
-
6
3
  Barion::Engine.routes.draw do
7
4
  root to: 'main#land'
8
5
  post 'callback', to: 'main#callback', as: :gateway_callback
data/lib/barion/engine.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # Module enclosing all Barion related functionality
3
4
  module Barion
4
5
  # Barion engine main class
5
6
  class Engine < ::Rails::Engine
@@ -11,4 +12,10 @@ module Barion
11
12
  g.factory_bot dir: 'test/factories'
12
13
  end
13
14
  end
15
+
16
+ ActiveSupport.on_load(:action_view) do
17
+ # self refers to ActiveRecord::Base here,
18
+ # so we can call .include
19
+ include ::Barion::PixelHelper
20
+ end
14
21
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Barion
4
- VERSION = '0.3.4'
4
+ VERSION = '0.4.1'
5
5
  end
data/lib/barion.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'barion/engine'
3
+ require 'barion/engine' if defined?(Rails::Engine)
4
4
 
5
5
  # Main module of Barion engine
6
6
  module Barion
@@ -9,21 +9,42 @@ module Barion
9
9
  prod: 'https://api.barion.com'
10
10
  }.freeze
11
11
 
12
+ PIXELID_PATTERN = ::Regexp.new('BP-.{10}-\d{2}').freeze
13
+
14
+ # rubocop:disable Style/ClassVars
12
15
  mattr_accessor :poskey, default: nil
13
16
  mattr_accessor :publickey, default: nil
14
17
  mattr_accessor :acronym, default: ''
18
+ mattr_accessor :pixel_id, default: ''
15
19
  mattr_accessor :sandbox, default: true
16
20
  mattr_accessor :default_payee
17
21
  mattr_accessor :user_class
18
22
  mattr_accessor :item_class
19
23
  mattr_accessor :rest_client_class, default: '::RestClient::Resource'
20
24
 
25
+ def self.poskey=(key)
26
+ raise ArgumentError, "::Barion.poskey must be set to a String, got #{key.inspect}" unless key.is_a?(String)
27
+
28
+ @@poskey = key
29
+ end
30
+
21
31
  def self.sandbox?
22
- !!sandbox
32
+ sandbox
23
33
  end
24
34
 
25
- def sandbox=(val)
26
- super(!!val)
35
+ def self.sandbox=(val)
36
+ @@sandbox = !!val
37
+ end
38
+
39
+ def self.pixel_id=(id)
40
+ raise ArgumentError, "::Barion.pixel_id must be set to a String, got #{id.inspect}" unless id.is_a?(String)
41
+
42
+ if PIXELID_PATTERN.match(id).nil?
43
+ raise ::ArgumentError,
44
+ "String:'#{id}' is not in Barion Pixel ID format: 'BP-0000000000-00'"
45
+ end
46
+
47
+ @@pixel_id = id
27
48
  end
28
49
 
29
50
  def self.endpoint
@@ -31,7 +52,6 @@ module Barion
31
52
  rest_client_class.new BASE_URL[env]
32
53
  end
33
54
 
34
- # rubocop:disable Style/ClassVars
35
55
  def self.user_class=(class_name)
36
56
  unless class_name.is_a?(String)
37
57
  raise ArgumentError, "Barion.user_class must be set to a String, got #{class_name.inspect}"
File without changes
File without changes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: barion
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Péter Nagy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-01-09 00:00:00.000000000 Z
11
+ date: 2024-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -56,6 +56,7 @@ files:
56
56
  - app/controllers/barion/main_controller.rb
57
57
  - app/helpers/barion/application_helper.rb
58
58
  - app/helpers/barion/main_helper.rb
59
+ - app/helpers/barion/pixel_helper.rb
59
60
  - app/models/barion/address.rb
60
61
  - app/models/barion/application_record.rb
61
62
  - app/models/barion/gift_card_purchase.rb
@@ -102,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
102
103
  - !ruby/object:Gem::Version
103
104
  version: '0'
104
105
  requirements: []
105
- rubygems_version: 3.2.33
106
+ rubygems_version: 3.5.3
106
107
  signing_key:
107
108
  specification_version: 4
108
109
  summary: Barion payment engine for Ruby on Rails