Class: PaypalFx

Inherits:
Object
  • Object
show all
Defined in:
lib/paypal_fx.rb

Constant Summary collapse

SERVER =
'svcs.paypal.com'
SANDBOX =
'svcs.sandbox.paypal.com'
CLIENT_INFO =
{ VERSION: '60.0', SOURCE: 'PayPalRubySDKV1.2.0' }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ PaypalFx

Returns a new instance of PaypalFx.

Parameters:

  • with (Hash)

    the connection params

    user_id: 'user',
    password: 'password',
    signature: 'sign',
    app_id: 'appid',
    test: true
    



24
25
26
# File 'lib/paypal_fx.rb', line 24

def initialize(params= {})
  self.params = params
end

Instance Attribute Details

#paramsObject

Returns the value of attribute params.



14
15
16
# File 'lib/paypal_fx.rb', line 14

def params
  @params
end

Instance Method Details

#commit(endpoint, request) ⇒ Hash

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

Parameters:

  • endpoint (String)

    endpoint

  • request (Hash)

    request parameters

Returns:

  • (Hash)

    with the responce parameters



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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) ⇒ Object

Converts an amount from origin currency to destination

Parameters:

  • amount (Float)

    amount

  • origin (String)

    ISO Currency code

  • destination (String)

    ISO Currency code



33
34
35
36
37
38
39
40
41
42
# 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