2021-04-01 18:20:26 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-04-20 17:22:53 +02:00
|
|
|
# PayZen payments gateway
|
2021-04-01 18:20:26 +02:00
|
|
|
module PayZen; end
|
|
|
|
|
|
|
|
API_PATH = '/api-payment/V4'
|
|
|
|
|
|
|
|
# Client for connecting to the PayZen REST API
|
|
|
|
class PayZen::Client
|
|
|
|
def initialize(base_url: nil, username: nil, password: nil)
|
|
|
|
@base_url = base_url
|
|
|
|
@username = username
|
|
|
|
@password = password
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2021-04-02 17:16:27 +02:00
|
|
|
def post(rel_url, payload)
|
2021-04-01 18:20:26 +02:00
|
|
|
require 'uri'
|
|
|
|
require 'net/http'
|
|
|
|
require 'json'
|
|
|
|
|
2021-04-02 17:16:27 +02:00
|
|
|
uri = URI(File.join(base_url, API_PATH, rel_url))
|
2021-04-01 18:20:26 +02:00
|
|
|
headers = {
|
2021-04-02 17:16:27 +02:00
|
|
|
'Authorization' => authorization_header,
|
2021-04-01 18:20:26 +02:00
|
|
|
'Content-Type' => 'application/json'
|
|
|
|
}
|
|
|
|
|
|
|
|
res = Net::HTTP.post(uri, payload.to_json, headers)
|
2021-05-27 17:13:24 +02:00
|
|
|
raise PayzenError unless res.is_a?(Net::HTTPSuccess)
|
|
|
|
|
|
|
|
json = JSON.parse(res.body)
|
|
|
|
raise PayzenError(json['answer']['errorMessage']) if json['status'] == 'ERROR'
|
|
|
|
|
|
|
|
json
|
2021-04-01 18:20:26 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def base_url
|
2021-04-02 17:16:27 +02:00
|
|
|
@base_url || Setting.get('payzen_endpoint')
|
2021-04-01 18:20:26 +02:00
|
|
|
end
|
|
|
|
|
2021-04-02 17:16:27 +02:00
|
|
|
def authorization_header
|
|
|
|
username = @username || Setting.get('payzen_username')
|
|
|
|
password = @password || Setting.get('payzen_password')
|
2021-04-01 18:20:26 +02:00
|
|
|
|
|
|
|
credentials = Base64.strict_encode64("#{username}:#{password}")
|
|
|
|
"Basic #{credentials}"
|
|
|
|
end
|
|
|
|
end
|