lib/buckaruby/response.rb in buckaruby-1.7.0 vs lib/buckaruby/response.rb in buckaruby-2.0.0

- old
+ new

@@ -6,21 +6,21 @@ require 'date' module Buckaruby # Base class for any response. class Response - attr_reader :params + attr_reader :response def initialize(body, config) - @logger = config.logger - @response = parse_response(body) - @params = Support::CaseInsensitiveHash.new(@response) - @logger.debug("[response] params: #{params.inspect}") + logger = config.logger + logger.debug("[response] params: #{params.inspect}") + end - verify_signature!(@response, config) + def params + @params ||= Support::CaseInsensitiveHash.new(response) end def status TransactionStatus.parse(params[:brq_statuscode]) end @@ -32,11 +32,11 @@ def custom @custom ||= begin custom = Support::CaseInsensitiveHash.new params.each do |key, value| - next unless key.upcase.start_with?("CUST_") + next unless key.upcase.start_with?('CUST_') new_key = key.to_s[5..-1] custom[new_key] = value end @@ -47,11 +47,11 @@ def additional @additional ||= begin additional = Support::CaseInsensitiveHash.new params.each do |key, value| - next unless key.upcase.start_with?("ADD_") + next unless key.upcase.start_with?('ADD_') new_key = key.to_s[4..-1] additional[new_key] = value end @@ -70,51 +70,40 @@ end response end - def verify_signature!(response, config) - if params[:brq_apiresult].nil? || params[:brq_apiresult].casecmp("fail") != 0 - sent_signature = params[:brq_signature] - 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 + Time.strptime(time, '%Y-%m-%d %H:%M:%S') if time end end # Base for a transaction response. module TransactionResponse - def account_bic + def consumer_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 + def consumer_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 + def consumer_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] @@ -145,100 +134,91 @@ def payment_method parse_payment_method(params[:brq_payment_method] || params[:brq_transaction_method]) end - def redirect_url - params[:brq_redirecturl] - end - - def refund_transaction_id - params[:brq_relatedtransaction_refund] - end - - def reversal_transaction_id - params[:brq_relatedtransaction_reversal] - end - def transaction_id params[:brq_transactions] end - def transaction_type - TransactionType.parse(params[:brq_transaction_type], params[:brq_recurring]) - end - def transaction_status status end - def to_h - hash = { - account_bic: account_bic, - account_iban: account_iban, - account_name: account_name, - collect_date: collect_date, - invoicenumber: invoicenumber, - mandate_reference: mandate_reference, - payment_id: payment_id, - payment_method: payment_method, - refund_transaction_id: refund_transaction_id, - reversal_transaction_id: reversal_transaction_id, - timestamp: timestamp, - transaction_id: transaction_id, - transaction_type: transaction_type, - transaction_status: transaction_status - }.reject { |_key, value| value.nil? } - - hash + def transaction_type + TransactionType.parse(params[:brq_transaction_type], params[:brq_recurring]) end private def parse_date(date) - date ? Date.strptime(date, '%Y-%m-%d') : nil + Date.strptime(date, '%Y-%m-%d') if date end def parse_payment_method(method) - method ? method.downcase : nil + method&.downcase end end # Base class for a response via the API. class ApiResponse < Response - def initialize(response, config) - super(response, config) + def initialize(body, config) + super(body, config) - if params[:brq_apiresult].nil? || params[:brq_apiresult].casecmp("fail").zero? + if api_result.nil? || api_result.casecmp('fail').zero? raise ApiException, params end + + Signature.verify!(config, response) end + + private + + def api_result + params[:brq_apiresult] + end end # Response when creating a new transaction. class SetupTransactionResponse < ApiResponse include TransactionResponse + + def redirect_url + params[:brq_redirecturl] + end end # Response when creating a recurrent transaction. class RecurrentTransactionResponse < ApiResponse include TransactionResponse + + def transaction_type + TransactionType::PAYMENT_RECURRENT + end end # Response when creating a refund transaction. class RefundTransactionResponse < ApiResponse include TransactionResponse + + def refunded_transaction_id + params[:brq_relatedtransaction_refund] + end + + def transaction_type + TransactionType::REFUND + end 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? + !params[:brq_refundinfo_1_isrefundable].nil? && params[:brq_refundinfo_1_isrefundable].casecmp('true').zero? end def maximum_amount params[:brq_refundinfo_1_maximumrefundamount] end @@ -255,20 +235,42 @@ # Response when getting the status of a transaction. class StatusResponse < ApiResponse include TransactionResponse def cancellable? - !params[:brq_transaction_cancelable].nil? && params[:brq_transaction_cancelable].casecmp("true").zero? + !params[:brq_transaction_cancelable].nil? && params[:brq_transaction_cancelable].casecmp('true').zero? end + + def refunded_transaction_id + params[:brq_relatedtransaction_refund] + end + + def reversed_transaction_id + params[:brq_relatedtransaction_reversal] + end end # Response when cancelling a transaction. class CancelResponse < ApiResponse end - # Response when verifying the Buckaroo callback. - class CallbackResponse < Response + # Response when verifying the push response. + class PushResponse < Response include TransactionResponse + + def initialize(body, config) + super(body, config) + + Signature.verify!(config, response) + end + + def refunded_transaction_id + params[:brq_relatedtransaction_refund] + end + + def reversed_transaction_id + params[:brq_relatedtransaction_reversal] + end end # Response when retrieving the specification for a transaction. class TransactionSpecificationResponse < ApiResponse def services