-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Version 0.4.0. Support multiple Gopay accounts
Add Client & Gateway to handle multiple Gopay accounts Replace old unirest with rest-client Drop ruby support for versions <2.4 Update cassettes Add more Ruby versions to Travis
- Loading branch information
Showing
30 changed files
with
1,695 additions
and
464 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,5 @@ | |
*.DS_Store | ||
.DS_Store | ||
*.gem | ||
*.rbc | ||
*.rbc | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,3 @@ | ||
require 'unirest' | ||
require 'gopay/error' | ||
|
||
module GoPay | ||
class Payment | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
module GoPay | ||
class Version | ||
MAJOR = 0 | ||
MINOR = 3 | ||
MINOR = 4 | ||
PATCH = 0 | ||
|
||
class << self | ||
|
Oops, something went wrong.