lib/buckaruby/response.rb in buckaruby-1.0.2 vs lib/buckaruby/response.rb in buckaruby-1.1.0
- old
+ new
@@ -1,56 +1,105 @@
+# frozen_string_literal: true
+
+require 'cgi'
require 'date'
module Buckaruby
# Base class for any response.
class Response
attr_reader :params
- def initialize(response, options)
- @params = Support::CaseInsensitiveHash.new(response)
+ def initialize(body, config)
+ @logger = config.logger
- verify_signature!(response, options)
+ @response = parse_response(body)
+ @params = Support::CaseInsensitiveHash.new(@response)
+
+ @logger.debug("[response] params: #{params.inspect}")
+
+ verify_signature!(@response, config)
end
+ def status
+ # See http://support.buckaroo.nl/index.php/Statuscodes
+ case params[:brq_statuscode]
+ when '190'
+ TransactionStatus::SUCCESS
+ when '490', '491', '492'
+ TransactionStatus::FAILED
+ when '690'
+ TransactionStatus::REJECTED
+ when '790', '791', '792', '793'
+ TransactionStatus::PENDING
+ when '890', '891'
+ TransactionStatus::CANCELLED
+ end
+ end
+
+ def timestamp
+ parse_time(params[:brq_timestamp])
+ end
+
private
- def verify_signature!(response, options)
+ def parse_response(body)
+ if body.is_a?(Hash)
+ response = body
+ else
+ response = CGI.parse(body)
+ response.each { |key, value| response[key] = value.first }
+ end
+
+ response
+ end
+
+ def verify_signature!(response, config)
if params[:brq_apiresult] != "Fail"
sent_signature = params[:brq_signature]
- generated_signature = Signature.generate_signature(response, options)
+ generated_signature = Signature.generate_signature(response, config)
if sent_signature != generated_signature
raise SignatureException.new(sent_signature, generated_signature)
end
end
end
+
+ def parse_time(time)
+ time ? Time.strptime(time, '%Y-%m-%d %H:%M:%S') : nil
+ end
end
- # Base class for a transaction response.
- class TransactionResponse < Response
+ # Base for a transaction response.
+ module TransactionResponse
def account_bic
case payment_method
when PaymentMethod::IDEAL
params[:brq_service_ideal_consumerbic]
+ when PaymentMethod::IDEAL_PROCESSING
+ params[:brq_service_idealprocessing_consumerbic]
when PaymentMethod::SEPA_DIRECT_DEBIT
params[:brq_service_sepadirectdebit_customerbic]
end
end
def account_iban
case payment_method
when PaymentMethod::IDEAL
params[:brq_service_ideal_consumeriban]
+ when PaymentMethod::IDEAL_PROCESSING
+ params[:brq_service_idealprocessing_consumeriban]
when PaymentMethod::SEPA_DIRECT_DEBIT
params[:brq_service_sepadirectdebit_customeriban]
end
end
def account_name
case payment_method
when PaymentMethod::IDEAL
params[:brq_service_ideal_consumername] || params[:brq_customer_name]
+ when PaymentMethod::IDEAL_PROCESSING
+ params[:brq_service_idealprocessing_consumername] || params[:brq_customer_name]
when PaymentMethod::SEPA_DIRECT_DEBIT
params[:brq_service_sepadirectdebit_customername] || params[:brq_customer_name]
end
end
@@ -88,14 +137,10 @@
def reversal_transaction_id
params[:brq_relatedtransaction_reversal]
end
- def timestamp
- parse_time(params[:brq_timestamp])
- end
-
def transaction_id
params[:brq_transactions]
end
def transaction_type
@@ -121,23 +166,11 @@
TransactionType::PAYMENT
end
end
def transaction_status
- # See http://support.buckaroo.nl/index.php/Statuscodes
- case params[:brq_statuscode]
- when '190'
- TransactionStatus::SUCCESS
- when '490', '491', '492'
- TransactionStatus::FAILED
- when '690'
- TransactionStatus::REJECTED
- when '790', '791', '792', '793'
- TransactionStatus::PENDING
- when '890', '891'
- TransactionStatus::CANCELLED
- end
+ status
end
def to_h
hash = {
account_bic: account_bic,
@@ -163,41 +196,77 @@
def parse_date(date)
date ? Date.strptime(date, '%Y-%m-%d') : nil
end
- def parse_time(time)
- time ? Time.strptime(time, '%Y-%m-%d %H:%M:%S') : nil
- end
-
def parse_payment_method(method)
method ? method.downcase : nil
end
end
- # Base class for a transaction response via the API.
- class TransactionApiResponse < TransactionResponse
- def initialize(response, options)
- super(response, options)
+ # Base class for a response via the API.
+ class ApiResponse < Response
+ def initialize(response, config)
+ super(response, config)
- if params[:brq_apiresult].nil? || params[:brq_apiresult] == "Fail"
+ if params[:brq_apiresult].nil? || params[:brq_apiresult].casecmp("fail").zero?
raise ApiException, params
end
end
end
# Response when creating a new transaction.
- class SetupTransactionResponse < TransactionApiResponse
+ class SetupTransactionResponse < ApiResponse
+ include TransactionResponse
end
# Response when creating a recurrent transaction.
- class RecurrentTransactionResponse < TransactionApiResponse
+ class RecurrentTransactionResponse < ApiResponse
+ include TransactionResponse
end
+ # Response when creating a refund transaction.
+ class RefundTransactionResponse < ApiResponse
+ include TransactionResponse
+ end
+
+ # Response when retrieving the refund information.
+ class RefundInfoResponse < ApiResponse
+ def payment_method
+ params[:brq_refundinfo_1_servicecode]
+ end
+
+ def refundable?
+ !params[:brq_refundinfo_1_isrefundable].nil? && params[:brq_refundinfo_1_isrefundable].casecmp("true").zero?
+ end
+
+ def maximum_amount
+ params[:brq_refundinfo_1_maximumrefundamount]
+ end
+
+ def invoicenumber
+ params[:brq_refundinfo_1_invoice]
+ end
+
+ def currency
+ params[:brq_refundinfo_1_refundcurrency]
+ end
+ end
+
# Response when getting the status of a transaction.
- class StatusResponse < TransactionApiResponse
+ class StatusResponse < ApiResponse
+ include TransactionResponse
+
+ def cancellable?
+ !params[:brq_transaction_cancelable].nil? && params[:brq_transaction_cancelable].casecmp("true").zero?
+ end
end
+ # Response when cancelling a transaction.
+ class CancelResponse < ApiResponse
+ end
+
# Response when verifying the Buckaroo callback.
- class CallbackResponse < TransactionResponse
+ class CallbackResponse < Response
+ include TransactionResponse
end
end