class PaypalFx

Constants

CLIENT_INFO
SANDBOX
SERVER

Attributes

params[RW]

Public Class Methods

new(params= {}) click to toggle source

@param [Hash] with the connection params

{
 user_id: 'user',
 password: 'password',
 signature: 'sign',
 app_id: 'appid',
 test: true
}
# File lib/paypal_fx.rb, line 24
def initialize(params= {})
  self.params = params
end

Public Instance Methods

commit(endpoint, request) click to toggle source

Sends the request to specified end point for paypal SANDBOX or SERVER

@param endpoint [String] endpoint @param request [Hash] request parameters @return [Hash] with the responce parameters

# File lib/paypal_fx.rb, line 49
def commit(endpoint, request)

  headers = {'Content-Type' => 'html/text',
             'X-PAYPAL-SERVICE-VERSION' => '1.0.0',
             'X-PAYPAL-SECURITY-USERID' => self.params[:user_id],
             'X-PAYPAL-SECURITY-PASSWORD' => self.params[:password],
             'X-PAYPAL-SECURITY-SIGNATURE' => self.params[:signature],
             'X-PAYPAL-APPLICATION-ID' => self.params[:app_id],
             'X-PAYPAL-DEVICE-IPADDRESS' => self.params[:ip_address] || '',
             'X-PAYPAL-REQUEST-DATA-FORMAT' => 'NV',
             'X-PAYPAL-RESPONSE-DATA-FORMAT' => 'NV'}


  request_params = request.merge(CLIENT_INFO).map { |k, v| "#{k}=#{CGI.escape(v)}" }.join('&')

  http = Net::HTTP.new(params[:test] ? SANDBOX : SERVER, 443)
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  http.use_ssl = true

  response = http.post2 endpoint, request_params, headers

  clean_namespace CGI.parse(response.body)

end
convert(amount, origin, destination) click to toggle source

Converts an amount from origin currency to destination

@param amount [Float] amount @param origin [String] ISO Currency code @param destination [String] ISO Currency code

# File lib/paypal_fx.rb, line 33
def convert(amount, origin, destination)
  request = {
      'requestEnvelope.errorLanguage' => 'en_US',
      'baseAmountList.currency(0).amount' => amount,
      'baseAmountList.currency(0).code' => origin,
      'convertToCurrencyList.currencyCode(0)' => destination
  }

  commit '/AdaptivePayments/ConvertCurrency', request
end

Protected Instance Methods

clean_namespace(hash) click to toggle source

Cleans the named spaced returns and converts the keys to symlinks

@param hash [Hash] Paypal return hash

# File lib/paypal_fx.rb, line 78
def clean_namespace(hash)
  #i hate my self for this
  h = {}
  hash.each do |k, v|
    val = k.split('.').map { |s| s.tr('^A-Za-z0-9', '') }
    key = val.count == 2 ? val[1] : val[val.count-2..val.count-1].join('_')
    h[key.to_sym] = v[0]
  end
  h
end