lib/buckaruby/request.rb in buckaruby-1.0.2 vs lib/buckaruby/request.rb in buckaruby-1.1.0
- old
+ new
@@ -1,30 +1,29 @@
+# frozen_string_literal: true
+
require 'bigdecimal'
require 'cgi'
require 'date'
-require 'logger'
require 'net/http'
require 'openssl'
require 'uri'
module Buckaruby
# Base class for any request.
class Request
- def initialize(options)
- @options = options
-
- @logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
+ def initialize(config)
+ @config = config
+ @logger = config.logger
end
def execute(options)
- uri = URI.parse(api_url)
+ uri = URI.parse(@config.api_url)
uri.query = "op=#{options[:operation]}" if options[:operation]
- raw_response = post_buckaroo(uri, build_request_data(options))
- response = parse_response(raw_response)
+ response = post_buckaroo(uri, build_request_data(options))
- @logger.debug("[execute] response: #{response.inspect}")
+ # @logger.debug("[execute] response: #{response.inspect}")
response
end
def build_request_params(_options)
@@ -44,48 +43,35 @@
unless raw_response.is_a?(Net::HTTPSuccess)
raise InvalidResponseException, raw_response
end
- return raw_response.body
+ raw_response.body
+
# Try to catch some common exceptions Net::HTTP might raise
rescue Errno::ETIMEDOUT, Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED, Errno::EHOSTUNREACH,
IOError, SocketError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::OpenTimeout,
Net::ProtocolError, Net::ReadTimeout, OpenSSL::SSL::SSLError => ex
raise ConnectionException, ex
end
def build_request_data(options)
- params = { brq_websitekey: @options[:website] }
+ params = { brq_websitekey: @config.website }
params.merge!(build_request_params(options))
params[:add_buckaruby] = "Buckaruby #{Buckaruby::VERSION}"
# Sign the data with our secret key.
- params[:brq_signature] = Signature.generate_signature(params, @options)
+ params[:brq_signature] = Signature.generate_signature(params, @config)
params
end
def post_data(params)
params.map { |key, value| "#{key}=#{CGI.escape(value.to_s)}" }.join("&")
end
-
- def parse_response(body)
- query = CGI.parse(body)
- query.each { |key, value| query[key] = value.first }
- query
- end
-
- def test?
- @options[:mode] == :test
- end
-
- def api_url
- test? ? Urls::TEST_URL : Urls::PRODUCTION_URL
- end
end
# Base class for a transaction request.
class TransactionRequest < Request
def execute(options)
@@ -95,11 +81,11 @@
def build_request_params(options)
params = {
brq_payment_method: options[:payment_method],
brq_culture: options[:culture] || Language::DUTCH,
brq_currency: options[:currency] || Currency::EURO,
- brq_amount: BigDecimal.new(options[:amount].to_s).to_s("F"),
+ brq_amount: BigDecimal(options[:amount].to_s).to_s("F"),
brq_invoicenumber: options[:invoicenumber]
}
params.merge!(build_transaction_request_params(options))
@@ -125,10 +111,16 @@
params.merge!(
brq_service_ideal_action: Action::PAY,
brq_service_ideal_issuer: options[:payment_issuer],
brq_service_ideal_version: "2"
)
+ when PaymentMethod::IDEAL_PROCESSING
+ params.merge!(
+ brq_service_idealprocessing_action: Action::PAY,
+ brq_service_idealprocessing_issuer: options[:payment_issuer],
+ brq_service_idealprocessing_version: "2"
+ )
when PaymentMethod::SEPA_DIRECT_DEBIT
params.merge!(
brq_service_sepadirectdebit_action: Action::PAY,
brq_service_sepadirectdebit_customeriban: options[:account_iban],
brq_service_sepadirectdebit_customeraccountname: options[:account_name]
@@ -173,10 +165,48 @@
params
end
end
+ # Request for a creating a refund.
+ class RefundTransactionRequest < Request
+ def execute(options)
+ super(options.merge(operation: Operation::TRANSACTION_REQUEST))
+ end
+
+ def build_request_params(options)
+ params = {
+ brq_payment_method: options[:payment_method],
+ brq_amount_credit: BigDecimal(options[:amount].to_s).to_s("F"),
+ brq_currency: options[:currency] || Currency::EURO,
+ brq_invoicenumber: options[:invoicenumber]
+ }
+
+ key = :"brq_service_#{options[:payment_method]}_action"
+ params[key] = Action::REFUND
+
+ params[:brq_originaltransaction] = options[:transaction_id]
+
+ params
+ end
+ end
+
+ # Request for retrieving refund information.
+ class RefundInfoRequest < Request
+ def execute(options)
+ super(options.merge(operation: Operation::REFUND_INFO))
+ end
+
+ def build_request_params(options)
+ params = {}
+
+ params[:brq_transaction] = options[:transaction_id]
+
+ params
+ end
+ end
+
# Request for getting the status of a transaction.
class StatusRequest < Request
def execute(options)
super(options.merge(operation: Operation::TRANSACTION_STATUS))
end
@@ -184,9 +214,22 @@
def build_request_params(options)
params = {}
params[:brq_transaction] = options[:transaction_id] if options[:transaction_id]
params[:brq_payment] = options[:payment_id] if options[:payment_id]
+
+ params
+ end
+ end
+
+ # Request for cancelling a transaction.
+ class CancelRequest < Request
+ def execute(options)
+ super(options.merge(operation: Operation::CANCEL_TRANSACTION))
+ end
+
+ def build_request_params(options)
+ params = { brq_transaction: options[:transaction_id] }
params
end
end
end