Module: Epay::Api

Defined in:
lib/epay/api.rb,
lib/epay/api/response.rb

Defined Under Namespace

Classes: Response

Class Method Summary collapse

Class Method Details

.authorize(post) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/epay/api.rb', line 4

def authorize(post)
  # Authorize transaction:
  RestClient.post AUTHORIZE_URL, post do |response, request, result|
    # The authorization request redirects to either accept or decline url:
    if location = response.headers[:location]
      query = CGI::parse(URI.parse(location.gsub(' ', '%20')).query)
    
      Hash[query.map do |k, v|
        [k, v.is_a?(Array) && v.size == 1 ? v[0] : v] # make values like ['v'] into 'v'
      end]
    else
      # No location header found
      raise ApiError, response
    end
  end
end

.default_post_for_params(params) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/epay/api.rb', line 21

def default_post_for_params(params)        
  {
    :merchantnumber => Epay.merchant_number,
    
    :cardno     => params[:card_no],
    :cvc        => params[:cvc],
    :expmonth   => params[:exp_month],
    :expyear    => params[:exp_year],
    
    :amount     => (params[:amount] * 100).to_i,
    :currency   => Epay::CURRENCY_CODES[(params[:currency] || Epay.default_currency).to_sym],
    :orderid    => params[:order_no],
    
    :accepturl  => AUTHORIZE_URL + "?accept=1",
    :declineurl => AUTHORIZE_URL + "?decline=1",
  }
end

.handle_failed_response(response) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/epay/api.rb', line 39

def handle_failed_response(response)
  case response.data['epayresponse']
  when "-1002" then raise InvalidMerchantNumber
  when "-1008" then raise TransactionNotFound
  else              raise ApiError, response.data['epayresponse']
  end
end

.request(url, action, params = {}, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/epay/api.rb', line 47

def request(url, action, params = {}, &block)
  service_url = "#{url}.asmx"
  soap_action = url + '/' + action
  
  params[:merchantnumber] ||= Epay.merchant_number
  params[:pwd] = Epay.password if Epay.password.present?

  headers = {
    'Content-Type'  => 'text/xml; charset=utf-8',
    'SOAPAction'    => soap_action,
    'User-Agent'    => "Ruby / epay (#{VERSION})"
  }

  # Setup the SOAP body:
  xml = Builder::XmlMarkup.new(:indent => 2)
  xml.instruct!
  xml.tag! 'soap:Envelope', { 'xmlns:xsi' => 'http://schemas.xmlsoap.org/soap/envelope/',
                              'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
                              'xmlns:soap' => 'http://schemas.xmlsoap.org/soap/envelope/' } do

    xml.tag! 'soap:Body' do
      xml.tag! action, { 'xmlns' => url } do
        params.each do |attribute, value|
          xml.tag! attribute, value if value.present?
        end
      end
    end
  end
  
  RestClient.post service_url, xml.target!, headers do |raw_response, request, result|
    response = Response.new(raw_response, action)
    
    if block_given?
      yield response
    else
      if response.success?
        return response
      else
        handle_failed_response(response)
      end
    end
  end
end