Skip to content

Commit

Permalink
Merge 1f11b3b into 23b2484
Browse files Browse the repository at this point in the history
  • Loading branch information
dayweek committed Oct 19, 2019
2 parents 23b2484 + 1f11b3b commit 73c0719
Show file tree
Hide file tree
Showing 31 changed files with 1,726 additions and 467 deletions.
1 change: 1 addition & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
service_name: travis-ci
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
*.DS_Store
.DS_Store
*.gem
*.rbc
*.rbc
.env
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ cache: bundler
language: ruby

rvm:
- 2.0.0
- 2.1
- 2.2
- 2.3
- 2.4
- 2.5
- 2.6
- ruby-head

sudo: false

Expand Down
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ gem 'rspec'
gem 'vcr'
gem 'webmock'
gem 'addressable'
gem 'dotenv'
gem "coveralls", require: false
gem "simplecov"

# Specify your gem's dependencies in gopay-ruby.gemspec
gemspec
112 changes: 87 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

[![Gem Version](https://badge.fury.io/rb/gopay-ruby.png)](http://badge.fury.io/rb/gopay-ruby)
[![Build Status](https://travis-ci.org/PrimeHammer/gopay-ruby.png?branch=master)](https://travis-ci.org/PrimeHammer/gopay-ruby)
[![Dependency Status](https://gemnasium.com/PrimeHammer/gopay-ruby.png)](https://gemnasium.com/PrimeHammer/gopay-ruby)
[![Code Climate](https://codeclimate.com/github/PrimeHammer/gopay-ruby.png)](https://codeclimate.com/github/PrimeHammer/gopay-ruby)
[![Coverage Status](https://coveralls.io/repos/github/PrimeHammer/gopay-ruby/badge.svg?branch=master)](https://coveralls.io/github/PrimeHammer/gopay-ruby?branch=master)

The GoPay Ruby allows Ruby applications to access to the GoPay REST API.

## Benefits
It does OAuth authorization under the hood automatically. Easy configuration through initializer.
It does authorization under the hood automatically, supports multiple accounts. Easy configuration.

## Installation

Expand All @@ -27,33 +27,30 @@ Or install it yourself as:

$ gem install gopay-ruby

## Configure
The Gem is framework agnostic. However, if you use Rails, put the initializer in Rails config dir:
```ruby
config/initializers/gopay.rb
```


## Usage

### Gateway

Before creating a payment, use Gateway object to configure your access to GoPay:

```ruby
GoPay.configure do |config|
config.goid = GOPAY_ID
config.client_id = GOPAY_CLIENT_ID
config.client_secret = GOPAY_SECRET
config.return_host = RETURN_HOST_URL
config.notification_host = NOTIFICATION_HOST_URL
config.gate = GATE_URL
end
gateway = GoPay::Gateway.new(gate: 'https://testgw.gopay.cz', goid: 123, client_id: 456, client_secret: 'xxx')
```

## Usage
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.


### Create a Payment
Before charging a user, we need to create a new payment. This will return a hash including a URL which you can use to popup payment modal or redirect the user to the GoPay payment page.

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.

```ruby
GoPay::Payment.create payment_data
gateway.create payment_data
```

### payment_data example
#### payment_data example

```ruby
{
Expand All @@ -77,7 +74,7 @@ GoPay::Payment.create payment_data
}
```

### Create a Payment response example
#### Response example
This is a basic example of response hash, for more complex examples visit [GoPay API docs](https://doc.gopay.com).
```ruby
{
Expand Down Expand Up @@ -106,22 +103,22 @@ This is a basic example of response hash, for more complex examples visit [GoPay
If you want to return a payment object from GoPay REST API.

```ruby
GoPay::Payment.retrieve gopay_id
gateway.retrieve gopay_id
```

### Refund of the payment
This functionality allows you to refund payment paid by a customer.
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.

```ruby
GoPay::Payment.refund gopay_id, amount
gateway.refund gopay_id, amount
```

### Cancellation of the recurring payment
The functionality allows you to cancel recurrence of a previously created recurring payment.

```ruby
GoPay::Payment.void_recurrence gopay_id
gateway.void_recurrence gopay_id
```

## Dealing with errors
Expand All @@ -130,13 +127,78 @@ You can easily catch errors in your models as shown below.

```ruby
begin
response = GoPay::Payment.refund(gopay_id, gopay_amount)
response = gateway.refund(gopay_id, gopay_amount)
rescue GoPay::Error => exception
log_gopay_error exception
end
```

## Documentation

## Testing

Use these env variables in `.env` file:

```
GOPAY_GATE='https://testgw.gopay.cz'
GOPAY_1_GOID=1111111111
GOPAY_1_CLIENT_ID=1
GOPAY_1_CLIENT_SECRET=x
GOPAY_2_GOID=2222222222
GOPAY_2_CLIENT_ID=2
GOPAY_2_CLIENT_SECRET=x
```

Then run:

```ruby
bundle exec rspec
```


### Upgrading

#### 0.4.0

In short, using `GoPay` class and `GoPay::Payment` was deprecated.

Using `GoPay.configure` and `GoPay.request` is discouraged in favor of `Gateway` object. Instead of this:

```ruby
GoPay.configure do |config|
config.goid = GOPAY_ID
config.client_id = GOPAY_CLIENT_ID
config.client_secret = GOPAY_SECRET
config.return_host = RETURN_HOST_URL
config.notification_host = NOTIFICATION_HOST_URL
config.gate = GATE_URL
end
```

please use `Gateway` object:

```ruby
gateway = GoPay::Gateway.new(gate: GATE_URL, goid: GOPAY_ID, client_id: GOPAY_CLIENT_ID, client_secret: GOPAY_SECRET)
```

`return_host` and `notification_host` will be deprecated as well.

Also `GoPay::Payment` will be deprecated. Instead of this:

```ruby
GoPay::Payment.retrieve gopay_id
```

use `Gateway` for creating payments, retrieval, refunds:

```ruby
gateway.retrieve gopay_id
```



## Full Documentation
Parameters for all GoPay methods follow the official documentation. For further explanation please visit [GoPay API docs](https://doc.gopay.com).

## Contributing
Expand Down
5 changes: 2 additions & 3 deletions gopay.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ Gem::Specification.new do |spec|
spec.version = GoPay::Version
spec.authors = ["David Hrachovy & Ondrej Zadnik"]
spec.email = ["david.hrachovy@gmail.com"]

spec.summary = %q{Unofficial wrapper for GoPay REST API}
spec.description = %q{Unofficial wrapper for GoPay REST API}
spec.homepage = "https://github.com/PrimeHammer/gopay-ruby"
spec.required_ruby_version = '>= 2.0'
spec.required_ruby_version = '>= 2.4'
spec.license = "MIT"
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.add_development_dependency "bundler"
spec.add_dependency 'unirest', '~> 1.1'
spec.add_dependency 'rest-client', '~> 2.1.0'
end
56 changes: 5 additions & 51 deletions lib/gopay.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
require 'gopay/client'
require 'gopay/payment'
require 'gopay/gateway'
require 'gopay/error'

module GoPay
class << self
Expand All @@ -10,56 +13,7 @@ def self.configure
end

def self.request(method, path, body_parameters: {})
token = token get_token_scope(method, path)
content_type = get_content_type(path)

body_parameters = content_type == 'application/json' ? body_parameters.to_json : from_hash_to_query(body_parameters)

response = Unirest.send(method, GoPay.gate+path, headers: { "Accept" => "application/json", "Content-Type" => content_type, "Authorization" => "Bearer #{token}" }, parameters: body_parameters)

unless response.code == 200
raise GoPay::Error.handle_gopay_error(response)
end

response.body
end

class << self
private

def get_token_scope(method, path)
if method == :post && path == '/api/payments/payment'
'payment-create'
else
'payment-all'
end
end

def get_content_type(path)
if (path == '/api/payments/payment') || (path =~ /create-recurrence/)
'application/json'
else
'application/x-www-form-urlencoded'
end
end

# payment-create - for new payment
# payment-all - for testing state etc
def token(scope = 'payment-create')
response = Unirest.post(
"#{gate}/api/oauth2/token",
headers: {
"Accept" => "application/json",
"Content-Type" => "application/x-www-form-urlencoded",
"Authorization" => "Basic #{Base64.encode64(client_id + ':' + client_secret)}"
},
parameters: "grant_type=client_credentials&scope=#{scope}")
response.body["access_token"]
end

def from_hash_to_query(hash)
hash = hash == "{}" ? "{}" : URI.escape(hash.collect { |key,val| "#{CGI.escape(key.to_s)}=#{CGI.escape(val.to_s)}" }.join('&'))
return hash
end
client = GoPay::Client.new({gate: gate, client_id: client_id, goid: goid, client_secret: client_secret})
client.request(method, path, body_parameters: body_parameters)
end
end
67 changes: 67 additions & 0 deletions lib/gopay/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require 'rest-client'

module GoPay
class Client
def initialize(config)
@config = config
end

def request(method, path, body_parameters: {})
token = token get_token_scope(method, path)
content_type = get_content_type(path)

body_parameters = content_type == 'application/json' ? body_parameters.to_json : from_hash_to_query(body_parameters)

begin
response = RestClient::Request.execute(method: method, url: @config[:gate]+path, payload: body_parameters, headers: { "Accept" => "application/json", "Content-Type" => content_type, "Authorization" => "Bearer #{token}" })
rescue RestClient::ExceptionWithResponse => e
raise Error.handle_gopay_error(e.response)
end

unless response.code == 200
raise Error.handle_gopay_error(response)
end

JSON.parse response.body
end

private

def get_token_scope(method, path)
if method == :post && path == '/api/payments/payment'
'payment-create'
else
'payment-all'
end
end

def get_content_type(path)
if (path == '/api/payments/payment') || (path =~ /create-recurrence/)
'application/json'
else
'application/x-www-form-urlencoded'
end
end

# payment-create - for new payment
# payment-all - for testing state etc
def token(scope = 'payment-create')
response = RestClient.post(
"#{@config[:gate]}/api/oauth2/token",
{ grant_type: 'client_credentials',
scope: scope,
},
{
"Accept" => "application/json",
"Content-Type" => "application/x-www-form-urlencoded",
"Authorization" => "Basic #{Base64.encode64(@config[:client_id] + ':' + @config[:client_secret])}"
})
JSON.parse(response.body)["access_token"]
end

def from_hash_to_query(hash)
hash = hash == "{}" ? "{}" : URI.escape(hash.collect { |key,val| "#{CGI.escape(key.to_s)}=#{CGI.escape(val.to_s)}" }.join('&'))
return hash
end
end
end
25 changes: 25 additions & 0 deletions lib/gopay/gateway.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module GoPay
class Gateway
def initialize(config)
@client = Client.new(config)
@goid = config[:goid]
end

def create(payment_data)
target = { target: { type: "ACCOUNT", goid: @goid } }
@client.request :post, "/api/payments/payment", body_parameters: payment_data.merge(target)
end

def retrieve(id)
@client.request :get, "/api/payments/payment/#{id}"
end

def refund(id, amount)
@client.request :post, "/api/payments/payment/#{id}/refund", body_parameters: { amount: amount }
end

def void_recurrence(id)
@client.request :post, "/api/payments/payment/#{id}/void-recurrence"
end
end
end

0 comments on commit 73c0719

Please sign in to comment.