1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-11 22:24:21 +01:00
fab-manager/lib/pay_zen/client.rb

50 lines
1.1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
# PayZen payments 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)
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))
headers = {
2021-04-02 17:16:27 +02:00
'Authorization' => authorization_header,
'Content-Type' => 'application/json'
}
res = Net::HTTP.post(uri, payload.to_json, headers)
2021-05-28 09:10:34 +02:00
raise ::PayzenError unless res.is_a?(Net::HTTPSuccess)
json = JSON.parse(res.body)
2021-05-28 09:10:34 +02:00
raise ::PayzenError, json['answer']['errorMessage'] if json['status'] == 'ERROR'
json
end
def base_url
2021-04-02 17:16:27 +02:00
@base_url || Setting.get('payzen_endpoint')
end
2021-04-02 17:16:27 +02:00
def authorization_header
username = @username || Setting.get('payzen_username')
password = @password || Setting.get('payzen_password')
credentials = Base64.strict_encode64("#{username}:#{password}")
"Basic #{credentials}"
end
end