Sha256: 81164874ab90d0a2e50d7d9349d007c29e07dca1b252b7eed7151e1c9bcd76ba

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

module WirecardGiropay
  class Request
    TEMPLATE_PATH = File.expand_path('../../../templates/request.xml', __FILE__)

    class InvalidParamsError < StandardError; end

    PARAMS = %i(business_case_signature transaction_id first_name last_name account_number bank_code
                country amount_in_cents currency usage success_redirect_url failure_redirect_url redirect_window_name
                notification_url alternate_notification_url order_data
               )

    def initialize(given_params = {})
      @given_params = given_params
      missing_params = PARAMS.select { |k,v| !given_params.has_key? k }
      raise InvalidParamsError.new("Missing parameters: #{missing_params}") if missing_params.count > 0

      order_data = given_params[:order_data]
      raise InvalidParamsError.new("Parameter 'order_data' must be a Hash.") if order_data and !order_data.is_a?(Hash)
    end

    def to_xml
      xml_template = File.read TEMPLATE_PATH
      xml_template.gsub /%\{\w+\}/, replace_params
    end

    def run
      Response.from_xml Typhoeus.post(WirecardGiropay.gateway_url, body: to_xml).body
    end

    private

    def replace_params
      rep_params = {}
      @given_params.each { |key, value| rep_params["%{#{key}}"] = value }
      rep_params['%{order_data}'] = order_data_xml
      rep_params
    end

    def order_data_xml
      return '' unless @given_params[:order_data]
      orders = '<ORDER_DATA>'
      @given_params[:order_data].each { |key, parameter| orders << "<Parameter name=\"#{key}\">#{parameter}</Parameter>" }
      orders << '</ORDER_DATA>'
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wirecard_giropay-1.0.0 lib/wirecard_giropay/request.rb