2021-04-01 18:20:26 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# PayZen payement gateway
|
|
|
|
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)
|
|
|
|
JSON.parse(res.body) if res.is_a?(Net::HTTPSuccess)
|
|
|
|
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
|