Sha256: cbc14fa04fe93cb27f0b6c1bbf2c0bac4e8d432897d093b6ebb22fe3a34d4839

Contents?: true

Size: 1.58 KB

Versions: 2

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_mode 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

2 entries across 2 versions & 1 rubygems

Version Path
wirecard_giropay-1.0.2 lib/wirecard_giropay/request.rb
wirecard_giropay-1.0.1 lib/wirecard_giropay/request.rb