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

- old
+ new

@@ -1,47 +1,46 @@ # frozen_string_literal: true -require 'bigdecimal' -require 'cgi' require 'date' require 'net/http' require 'openssl' require 'uri' module Buckaruby # Base class for any request. class Request def initialize(config) @config = config - @logger = config.logger end def execute(options) uri = URI.parse(@config.api_url) - uri.query = "op=#{options[:operation]}" if options[:operation] + uri.query = URI.encode_www_form(op: operation) if operation - response = post_buckaroo(uri, build_request_data(options)) + post_buckaroo(uri, build_request_data(options)) + end - # @logger.debug("[execute] response: #{response.inspect}") - - response + # Returns the service operation for this request. + def operation + nil end + # Returns the request parameters (to be implemented by subclasses). def build_request_params(_options) raise NotImplementedError end private def post_buckaroo(uri, params) http = Net::HTTP.new(uri.host, uri.port) - if uri.scheme == "https" + if uri.scheme == 'https' http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER end - raw_response = http.post(uri.request_uri, post_data(params)) + raw_response = http.post(uri.request_uri, URI.encode_www_form(params)) unless raw_response.is_a?(Net::HTTPSuccess) raise InvalidResponseException, raw_response end @@ -62,40 +61,36 @@ params.merge!(build_additional_params(options[:additional])) if options[:additional] params[:add_buckaruby] = "Buckaruby #{Buckaruby::VERSION}" # Sign the data with our secret key. - params[:brq_signature] = Signature.generate_signature(params, @config) + params[:brq_signature] = Signature.generate(@config, params) params end def build_custom_params(options) - options.map { |key, value| [:"cust_#{key}", value] }.to_h + options.transform_keys { |key| :"cust_#{key}" } end def build_additional_params(options) - options.map { |key, value| [:"add_#{key}", value] }.to_h + options.transform_keys { |key| :"add_#{key}" } end - - def post_data(params) - params.map { |key, value| "#{key}=#{CGI.escape(value.to_s)}" }.join("&") - end end # Base class for a transaction request. class TransactionRequest < Request - def execute(options) - super(options.merge(operation: Operation::TRANSACTION_REQUEST)) + def operation + Operation::TRANSACTION_REQUEST end 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(options[:amount].to_s).to_s("F"), + brq_amount: Amount.new(options[:amount]).to_s, brq_invoicenumber: options[:invoicenumber] } params.merge!(build_transaction_request_params(options)) @@ -118,101 +113,102 @@ case options[:payment_method] when PaymentMethod::IDEAL params.merge!( brq_service_ideal_action: Action::PAY, - brq_service_ideal_issuer: options[:payment_issuer], - brq_service_ideal_version: "2" + brq_service_ideal_issuer: options[: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" + brq_service_idealprocessing_issuer: options[: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] + brq_service_sepadirectdebit_customeriban: options[:consumer_iban], + brq_service_sepadirectdebit_customeraccountname: options[:consumer_name] ) - if options[:account_bic] - params[:brq_service_sepadirectdebit_customerbic] = options[:account_bic] + if options[:consumer_bic] + params[:brq_service_sepadirectdebit_customerbic] = options[:consumer_bic] end if options[:collect_date] - params[:brq_service_sepadirectdebit_collectdate] = options[:collect_date].strftime("%Y-%m-%d") + params[:brq_service_sepadirectdebit_collectdate] = options[:collect_date].strftime('%Y-%m-%d') end if options[:mandate_reference] params.merge!( - brq_service_sepadirectdebit_action: [Action::PAY, Action::EXTRA_INFO].join(","), + brq_service_sepadirectdebit_action: [Action::PAY, Action::EXTRA_INFO].join(','), brq_service_sepadirectdebit_mandatereference: options[:mandate_reference], - brq_service_sepadirectdebit_mandatedate: Date.today.strftime("%Y-%m-%d") + brq_service_sepadirectdebit_mandatedate: Date.today.strftime('%Y-%m-%d') ) end end params[:brq_startrecurrent] = options[:recurring] if options[:recurring] params end end - # Request for a creating a transaction specification. - class TransactionSpecificationRequest < Request - def execute(options) - super(options.merge(operation: Operation::TRANSACTION_REQUEST_SPECIFICATION)) - end - - def build_request_params(options) - params = {} - - if options[:payment_method] - if options[:payment_method].respond_to?(:join) - params[:brq_services] = options[:payment_method].join(",") - else - params[:brq_services] = options[:payment_method] - end - end - - params[:brq_latestversiononly] = "true" - params[:brq_culture] = options[:culture] || Language::DUTCH - - params - end - end - # Request for a creating a recurrent transaction. class RecurrentTransactionRequest < TransactionRequest def build_transaction_request_params(options) params = {} key = :"brq_service_#{options[:payment_method]}_action" params[key] = Action::PAY_RECURRENT # Indicate that this is a request without user redirection to a webpage. # This is needed to make recurrent payments working. - params[:brq_channel] = "backoffice" + params[:brq_channel] = 'backoffice' params[:brq_originaltransaction] = options[:transaction_id] params end end + # Request for a creating a transaction specification. + class TransactionSpecificationRequest < Request + def operation + Operation::TRANSACTION_REQUEST_SPECIFICATION + end + + def build_request_params(options) + params = {} + + if options[:payment_method] + params[:brq_services] = + if options[:payment_method].respond_to?(:join) + options[:payment_method].join(',') + else + options[:payment_method] + end + end + + params[:brq_latestversiononly] = 'true' + params[:brq_culture] = options[:culture] || Language::DUTCH + + params + end + end + # Request for a creating a refund. class RefundTransactionRequest < Request - def execute(options) - super(options.merge(operation: Operation::TRANSACTION_REQUEST)) + def 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_amount_credit: Amount.new(options[:amount]).to_s, brq_currency: options[:currency] || Currency::EURO, brq_invoicenumber: options[:invoicenumber] } key = :"brq_service_#{options[:payment_method]}_action" @@ -224,12 +220,12 @@ end end # Request for retrieving refund information. class RefundInfoRequest < Request - def execute(options) - super(options.merge(operation: Operation::REFUND_INFO)) + def operation + Operation::REFUND_INFO end def build_request_params(options) params = {} @@ -239,12 +235,12 @@ end end # Request for getting the status of a transaction. class StatusRequest < Request - def execute(options) - super(options.merge(operation: Operation::TRANSACTION_STATUS)) + def operation + Operation::TRANSACTION_STATUS end def build_request_params(options) params = {} @@ -255,16 +251,14 @@ end end # Request for cancelling a transaction. class CancelRequest < Request - def execute(options) - super(options.merge(operation: Operation::CANCEL_TRANSACTION)) + def operation + Operation::CANCEL_TRANSACTION end def build_request_params(options) - params = { brq_transaction: options[:transaction_id] } - - params + { brq_transaction: options[:transaction_id] } end end end