Sha256: b6f227827bca8dad15cdec26c8f467fc919facb671b6688a47ced552bcf61312

Contents?: true

Size: 1.72 KB

Versions: 2

Compression:

Stored size: 1.72 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['%{transaction_mode}'] = transaction_mode
      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

    def transaction_mode
      WirecardGiropay.sandboxed? ? 'demo' : 'live'
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
wirecard_giropay-0.0.2 lib/wirecard_giropay/request.rb
wirecard_giropay-0.0.1 lib/wirecard_giropay/request.rb