gopay-ruby 0.3.0 → 0.4.0.alpha
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 +5 -5
- data/{.coveralls.yaml → .coveralls.yml} +0 -0
- data/.gitignore +2 -1
- data/.travis.yml +4 -16
- data/Gemfile +2 -1
- data/README.md +87 -24
- data/gopay.gemspec +2 -3
- data/lib/gopay/client.rb +67 -0
- data/lib/gopay/gateway.rb +25 -0
- data/lib/gopay/payment.rb +0 -3
- data/lib/gopay/version.rb +2 -2
- data/lib/gopay.rb +5 -51
- metadata +12 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3c5d871fa56f9ff5b430c311521f6baf459163e70002a4bd55e9de4a7890a11e
|
4
|
+
data.tar.gz: 8fe34a47178bc633ae2c00ffb88014239c9409f31016a87653df0b16220707e7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f1e7623ad8f437d3aa18ffe16d16a8d9ffeb49060055ec2e80a964c858f836a61ae8a1fb931a89958cee94476caaf560937bce3155d656d36c053d5c7e544c5
|
7
|
+
data.tar.gz: 21bc88b460bd2bb89e909b8a2ddd7e50179480ac180044618f4c3f50d90e02b643e2c8efa030340d73dcd977b34574ece3791a9dcfa4b941f3e0d0112edec5b2
|
File without changes
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -3,23 +3,11 @@ cache: bundler
|
|
3
3
|
language: ruby
|
4
4
|
|
5
5
|
rvm:
|
6
|
-
- 2.
|
7
|
-
- 2.
|
8
|
-
- 2.
|
9
|
-
- rbx-2
|
6
|
+
- 2.4
|
7
|
+
- 2.5
|
8
|
+
- 2.6
|
10
9
|
- ruby-head
|
11
10
|
|
12
11
|
sudo: false
|
13
12
|
|
14
|
-
bundler_args: --without development --retry=3 --jobs=3
|
15
|
-
|
16
|
-
env:
|
17
|
-
global:
|
18
|
-
- JRUBY_OPTS="$JRUBY_OPTS --debug"
|
19
|
-
|
20
|
-
matrix:
|
21
|
-
allow_failures:
|
22
|
-
- rvm: jruby-head
|
23
|
-
- rvm: rbx-2
|
24
|
-
- rvm: ruby-head
|
25
|
-
fast_finish: true
|
13
|
+
bundler_args: --without development --retry=3 --jobs=3
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -3,11 +3,12 @@
|
|
3
3
|
[](http://badge.fury.io/rb/gopay-ruby)
|
4
4
|
[](https://travis-ci.org/PrimeHammer/gopay-ruby)
|
5
5
|
[](https://codeclimate.com/github/PrimeHammer/gopay-ruby)
|
6
|
+
[](https://coveralls.io/github/PrimeHammer/gopay-ruby?branch=master)
|
6
7
|
|
7
8
|
The GoPay Ruby allows Ruby applications to access to the GoPay REST API.
|
8
9
|
|
9
10
|
## Benefits
|
10
|
-
It does
|
11
|
+
It does authorization under the hood automatically, supports multiple accounts. Easy configuration.
|
11
12
|
|
12
13
|
## Installation
|
13
14
|
|
@@ -26,33 +27,30 @@ Or install it yourself as:
|
|
26
27
|
|
27
28
|
$ gem install gopay-ruby
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
30
|
+
|
31
|
+
|
32
|
+
## Usage
|
33
|
+
|
34
|
+
### Gateway
|
35
|
+
|
36
|
+
Before creating a payment, use Gateway object to configure your access to GoPay:
|
34
37
|
|
35
38
|
```ruby
|
36
|
-
GoPay.
|
37
|
-
config.goid = GOPAY_ID
|
38
|
-
config.client_id = GOPAY_CLIENT_ID
|
39
|
-
config.client_secret = GOPAY_SECRET
|
40
|
-
config.return_host = RETURN_HOST_URL
|
41
|
-
config.notification_host = NOTIFICATION_HOST_URL
|
42
|
-
config.gate = GATE_URL
|
43
|
-
end
|
39
|
+
gateway = GoPay::Gateway.new(gate: 'https://testgw.gopay.cz', goid: 123, client_id: 456, client_secret: 'xxx')
|
44
40
|
```
|
45
41
|
|
46
|
-
|
42
|
+
The values above are just examples. Note that you can use multiple Gateway objects to connect to different accounts. With one Gateway, you can do multiple requests.
|
43
|
+
|
47
44
|
|
48
45
|
### Create a Payment
|
49
|
-
|
46
|
+
|
47
|
+
Before charging a user, we need to create a new payment using our `gateway` object. The method will return a hash including a URL which you can use to popup payment modal or redirect the user to the GoPay payment page.
|
50
48
|
|
51
49
|
```ruby
|
52
|
-
|
50
|
+
gateway.create payment_data
|
53
51
|
```
|
54
52
|
|
55
|
-
|
53
|
+
#### payment_data example
|
56
54
|
|
57
55
|
```ruby
|
58
56
|
{
|
@@ -76,7 +74,7 @@ GoPay::Payment.create payment_data
|
|
76
74
|
}
|
77
75
|
```
|
78
76
|
|
79
|
-
|
77
|
+
#### Response example
|
80
78
|
This is a basic example of response hash, for more complex examples visit [GoPay API docs](https://doc.gopay.com).
|
81
79
|
```ruby
|
82
80
|
{
|
@@ -105,7 +103,7 @@ This is a basic example of response hash, for more complex examples visit [GoPay
|
|
105
103
|
If you want to return a payment object from GoPay REST API.
|
106
104
|
|
107
105
|
```ruby
|
108
|
-
|
106
|
+
gateway.retrieve gopay_id
|
109
107
|
```
|
110
108
|
|
111
109
|
### Refund of the payment
|
@@ -113,14 +111,14 @@ This functionality allows you to refund payment paid by a customer.
|
|
113
111
|
You can use the refund in two ways. First option is a full refund payment and the other one is a partial refund. Both based on `amount` parameter.
|
114
112
|
|
115
113
|
```ruby
|
116
|
-
|
114
|
+
gateway.refund gopay_id, amount
|
117
115
|
```
|
118
116
|
|
119
117
|
### Cancellation of the recurring payment
|
120
118
|
The functionality allows you to cancel recurrence of a previously created recurring payment.
|
121
119
|
|
122
120
|
```ruby
|
123
|
-
|
121
|
+
gateway.void_recurrence gopay_id
|
124
122
|
```
|
125
123
|
|
126
124
|
## Dealing with errors
|
@@ -129,13 +127,78 @@ You can easily catch errors in your models as shown below.
|
|
129
127
|
|
130
128
|
```ruby
|
131
129
|
begin
|
132
|
-
response =
|
130
|
+
response = gateway.refund(gopay_id, gopay_amount)
|
133
131
|
rescue GoPay::Error => exception
|
134
132
|
log_gopay_error exception
|
135
133
|
end
|
136
134
|
```
|
137
135
|
|
138
|
-
|
136
|
+
|
137
|
+
## Testing
|
138
|
+
|
139
|
+
Use these env variables in `.env` file:
|
140
|
+
|
141
|
+
```
|
142
|
+
GOPAY_GATE='https://testgw.gopay.cz'
|
143
|
+
|
144
|
+
GOPAY_1_GOID=1111111111
|
145
|
+
GOPAY_1_CLIENT_ID=1
|
146
|
+
GOPAY_1_CLIENT_SECRET=x
|
147
|
+
|
148
|
+
GOPAY_2_GOID=2222222222
|
149
|
+
GOPAY_2_CLIENT_ID=2
|
150
|
+
GOPAY_2_CLIENT_SECRET=x
|
151
|
+
```
|
152
|
+
|
153
|
+
Then run:
|
154
|
+
|
155
|
+
```ruby
|
156
|
+
bundle exec rspec
|
157
|
+
```
|
158
|
+
|
159
|
+
|
160
|
+
### Upgrading
|
161
|
+
|
162
|
+
#### 0.4.0
|
163
|
+
|
164
|
+
In short, using `GoPay` class and `GoPay::Payment` was deprecated.
|
165
|
+
|
166
|
+
Using `GoPay.configure` and `GoPay.request` is discouraged in favor of `Gateway` object. Instead of this:
|
167
|
+
|
168
|
+
```ruby
|
169
|
+
GoPay.configure do |config|
|
170
|
+
config.goid = GOPAY_ID
|
171
|
+
config.client_id = GOPAY_CLIENT_ID
|
172
|
+
config.client_secret = GOPAY_SECRET
|
173
|
+
config.return_host = RETURN_HOST_URL
|
174
|
+
config.notification_host = NOTIFICATION_HOST_URL
|
175
|
+
config.gate = GATE_URL
|
176
|
+
end
|
177
|
+
```
|
178
|
+
|
179
|
+
please use `Gateway` object:
|
180
|
+
|
181
|
+
```ruby
|
182
|
+
gateway = GoPay::Gateway.new(gate: GATE_URL, goid: GOPAY_ID, client_id: GOPAY_CLIENT_ID, client_secret: GOPAY_SECRET)
|
183
|
+
```
|
184
|
+
|
185
|
+
`return_host` and `notification_host` will be deprecated as well.
|
186
|
+
|
187
|
+
Also `GoPay::Payment` will be deprecated. Instead of this:
|
188
|
+
|
189
|
+
```ruby
|
190
|
+
GoPay::Payment.retrieve gopay_id
|
191
|
+
```
|
192
|
+
|
193
|
+
use `Gateway` for creating payments, retrieval, refunds:
|
194
|
+
|
195
|
+
```ruby
|
196
|
+
gateway.retrieve gopay_id
|
197
|
+
```
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
## Full Documentation
|
139
202
|
Parameters for all GoPay methods follow the official documentation. For further explanation please visit [GoPay API docs](https://doc.gopay.com).
|
140
203
|
|
141
204
|
## Contributing
|
data/gopay.gemspec
CHANGED
@@ -8,16 +8,15 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = GoPay::Version
|
9
9
|
spec.authors = ["David Hrachovy & Ondrej Zadnik"]
|
10
10
|
spec.email = ["david.hrachovy@gmail.com"]
|
11
|
-
|
12
11
|
spec.summary = %q{Unofficial wrapper for GoPay REST API}
|
13
12
|
spec.description = %q{Unofficial wrapper for GoPay REST API}
|
14
13
|
spec.homepage = "https://github.com/PrimeHammer/gopay-ruby"
|
15
|
-
spec.required_ruby_version = '>= 2.
|
14
|
+
spec.required_ruby_version = '>= 2.4'
|
16
15
|
spec.license = "MIT"
|
17
16
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
17
|
spec.bindir = "exe"
|
19
18
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
19
|
spec.require_paths = ["lib"]
|
21
20
|
spec.add_development_dependency "bundler"
|
22
|
-
spec.add_dependency '
|
21
|
+
spec.add_dependency 'rest-client', '~> 2.1.0'
|
23
22
|
end
|
data/lib/gopay/client.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'rest-client'
|
2
|
+
|
3
|
+
module GoPay
|
4
|
+
class Client
|
5
|
+
def initialize(config)
|
6
|
+
@config = config
|
7
|
+
end
|
8
|
+
|
9
|
+
def request(method, path, body_parameters: {})
|
10
|
+
token = token get_token_scope(method, path)
|
11
|
+
content_type = get_content_type(path)
|
12
|
+
|
13
|
+
body_parameters = content_type == 'application/json' ? body_parameters.to_json : from_hash_to_query(body_parameters)
|
14
|
+
|
15
|
+
begin
|
16
|
+
response = RestClient::Request.execute(method: method, url: @config[:gate]+path, payload: body_parameters, headers: { "Accept" => "application/json", "Content-Type" => content_type, "Authorization" => "Bearer #{token}" })
|
17
|
+
rescue RestClient::ExceptionWithResponse => e
|
18
|
+
raise Error.handle_gopay_error(e.response)
|
19
|
+
end
|
20
|
+
|
21
|
+
unless response.code == 200
|
22
|
+
raise Error.handle_gopay_error(response)
|
23
|
+
end
|
24
|
+
|
25
|
+
JSON.parse response.body
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def get_token_scope(method, path)
|
31
|
+
if method == :post && path == '/api/payments/payment'
|
32
|
+
'payment-create'
|
33
|
+
else
|
34
|
+
'payment-all'
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def get_content_type(path)
|
39
|
+
if (path == '/api/payments/payment') || (path =~ /create-recurrence/)
|
40
|
+
'application/json'
|
41
|
+
else
|
42
|
+
'application/x-www-form-urlencoded'
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# payment-create - for new payment
|
47
|
+
# payment-all - for testing state etc
|
48
|
+
def token(scope = 'payment-create')
|
49
|
+
response = RestClient.post(
|
50
|
+
"#{@config[:gate]}/api/oauth2/token",
|
51
|
+
{ grant_type: 'client_credentials',
|
52
|
+
scope: scope,
|
53
|
+
},
|
54
|
+
{
|
55
|
+
"Accept" => "application/json",
|
56
|
+
"Content-Type" => "application/x-www-form-urlencoded",
|
57
|
+
"Authorization" => "Basic #{Base64.encode64(@config[:client_id] + ':' + @config[:client_secret])}"
|
58
|
+
})
|
59
|
+
JSON.parse(response.body)["access_token"]
|
60
|
+
end
|
61
|
+
|
62
|
+
def from_hash_to_query(hash)
|
63
|
+
hash = hash == "{}" ? "{}" : URI.escape(hash.collect { |key,val| "#{CGI.escape(key.to_s)}=#{CGI.escape(val.to_s)}" }.join('&'))
|
64
|
+
return hash
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module GoPay
|
2
|
+
class Gateway
|
3
|
+
def initialize(config)
|
4
|
+
@client = Client.new(config)
|
5
|
+
@goid = config[:goid]
|
6
|
+
end
|
7
|
+
|
8
|
+
def create(payment_data)
|
9
|
+
target = { target: { type: "ACCOUNT", goid: @goid } }
|
10
|
+
@client.request :post, "/api/payments/payment", body_parameters: payment_data.merge(target)
|
11
|
+
end
|
12
|
+
|
13
|
+
def retrieve(id)
|
14
|
+
@client.request :get, "/api/payments/payment/#{id}"
|
15
|
+
end
|
16
|
+
|
17
|
+
def refund(id, amount)
|
18
|
+
@client.request :post, "/api/payments/payment/#{id}/refund", body_parameters: { amount: amount }
|
19
|
+
end
|
20
|
+
|
21
|
+
def void_recurrence(id)
|
22
|
+
@client.request :post, "/api/payments/payment/#{id}/void-recurrence"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/gopay/payment.rb
CHANGED
data/lib/gopay/version.rb
CHANGED
data/lib/gopay.rb
CHANGED
@@ -1,4 +1,7 @@
|
|
1
|
+
require 'gopay/client'
|
1
2
|
require 'gopay/payment'
|
3
|
+
require 'gopay/gateway'
|
4
|
+
require 'gopay/error'
|
2
5
|
|
3
6
|
module GoPay
|
4
7
|
class << self
|
@@ -10,56 +13,7 @@ module GoPay
|
|
10
13
|
end
|
11
14
|
|
12
15
|
def self.request(method, path, body_parameters: {})
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
body_parameters = content_type == 'application/json' ? body_parameters.to_json : from_hash_to_query(body_parameters)
|
17
|
-
|
18
|
-
response = Unirest.send(method, GoPay.gate+path, headers: { "Accept" => "application/json", "Content-Type" => content_type, "Authorization" => "Bearer #{token}" }, parameters: body_parameters)
|
19
|
-
|
20
|
-
unless response.code == 200
|
21
|
-
raise GoPay::Error.handle_gopay_error(response)
|
22
|
-
end
|
23
|
-
|
24
|
-
response.body
|
25
|
-
end
|
26
|
-
|
27
|
-
class << self
|
28
|
-
private
|
29
|
-
|
30
|
-
def get_token_scope(method, path)
|
31
|
-
if method == :post && path == '/api/payments/payment'
|
32
|
-
'payment-create'
|
33
|
-
else
|
34
|
-
'payment-all'
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def get_content_type(path)
|
39
|
-
if (path == '/api/payments/payment') || (path =~ /create-recurrence/)
|
40
|
-
'application/json'
|
41
|
-
else
|
42
|
-
'application/x-www-form-urlencoded'
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
# payment-create - for new payment
|
47
|
-
# payment-all - for testing state etc
|
48
|
-
def token(scope = 'payment-create')
|
49
|
-
response = Unirest.post(
|
50
|
-
"#{gate}/api/oauth2/token",
|
51
|
-
headers: {
|
52
|
-
"Accept" => "application/json",
|
53
|
-
"Content-Type" => "application/x-www-form-urlencoded",
|
54
|
-
"Authorization" => "Basic #{Base64.encode64(client_id + ':' + client_secret)}"
|
55
|
-
},
|
56
|
-
parameters: "grant_type=client_credentials&scope=#{scope}")
|
57
|
-
response.body["access_token"]
|
58
|
-
end
|
59
|
-
|
60
|
-
def from_hash_to_query(hash)
|
61
|
-
hash = hash == "{}" ? "{}" : URI.escape(hash.collect { |key,val| "#{CGI.escape(key.to_s)}=#{CGI.escape(val.to_s)}" }.join('&'))
|
62
|
-
return hash
|
63
|
-
end
|
16
|
+
client = GoPay::Client.new({gate: gate, client_id: client_id, goid: goid, client_secret: client_secret})
|
17
|
+
client.request(method, path, body_parameters: body_parameters)
|
64
18
|
end
|
65
19
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gopay-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0.alpha
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Hrachovy & Ondrej Zadnik
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-10-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -25,19 +25,19 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: rest-client
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 2.1.0
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 2.1.0
|
41
41
|
description: Unofficial wrapper for GoPay REST API
|
42
42
|
email:
|
43
43
|
- david.hrachovy@gmail.com
|
@@ -45,7 +45,7 @@ executables: []
|
|
45
45
|
extensions: []
|
46
46
|
extra_rdoc_files: []
|
47
47
|
files:
|
48
|
-
- ".coveralls.
|
48
|
+
- ".coveralls.yml"
|
49
49
|
- ".gitignore"
|
50
50
|
- ".rspec"
|
51
51
|
- ".travis.yml"
|
@@ -55,7 +55,9 @@ files:
|
|
55
55
|
- Rakefile
|
56
56
|
- gopay.gemspec
|
57
57
|
- lib/gopay.rb
|
58
|
+
- lib/gopay/client.rb
|
58
59
|
- lib/gopay/error.rb
|
60
|
+
- lib/gopay/gateway.rb
|
59
61
|
- lib/gopay/payment.rb
|
60
62
|
- lib/gopay/version.rb
|
61
63
|
homepage: https://github.com/PrimeHammer/gopay-ruby
|
@@ -70,15 +72,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
70
72
|
requirements:
|
71
73
|
- - ">="
|
72
74
|
- !ruby/object:Gem::Version
|
73
|
-
version: '2.
|
75
|
+
version: '2.4'
|
74
76
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
77
|
requirements:
|
76
|
-
- - "
|
78
|
+
- - ">"
|
77
79
|
- !ruby/object:Gem::Version
|
78
|
-
version:
|
80
|
+
version: 1.3.1
|
79
81
|
requirements: []
|
80
82
|
rubyforge_project:
|
81
|
-
rubygems_version: 2.
|
83
|
+
rubygems_version: 2.7.8
|
82
84
|
signing_key:
|
83
85
|
specification_version: 4
|
84
86
|
summary: Unofficial wrapper for GoPay REST API
|