Module: ActiveMerchant::Billing::XPPAuthorization

Included in:
PaypalPermissionsGateway
Defined in:
lib/paypal_permissions/x_pp_authorization.rb

Instance Method Summary collapse

Instance Method Details

#paypal_encode(str) ⇒ Object

The PayPalURLEncoder java class percent encodes everything other than ‘a-zA-Z0-9 _’. Then it converts ‘ ’ to ‘+’. Ruby’s CGI.encode takes care of the ‘ ’ and ‘*’ to satisfy PayPal (but beware, URI.encode percent encodes spaces, and does nothing with ‘*’). Finally, CGI.encode does not encode ‘.-’, which we need to do here.



60
61
62
63
# File 'lib/paypal_permissions/x_pp_authorization.rb', line 60

def paypal_encode str
  s = str.dup
  CGI.escape(s).gsub('.', '%2E').gsub('-', '%2D')
end

#x_pp_authorization_header(url, api_user_id, api_password, access_token, access_token_verifier) ⇒ Object



18
19
20
21
22
# File 'lib/paypal_permissions/x_pp_authorization.rb', line 18

def x_pp_authorization_header url, api_user_id, api_password, access_token, access_token_verifier
  timestamp = Time.now.to_i.to_s
  signature = x_pp_authorization_signature url, api_user_id, api_password, timestamp, access_token, access_token_verifier
  { 'X-PAYPAL-AUTHORIZATION' => "token=#{access_token},signature=#{signature},timestamp=#{timestamp}" }
end

#x_pp_authorization_signature(url, api_user_id, api_password, timestamp, access_token, access_token_verifier) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/paypal_permissions/x_pp_authorization.rb', line 25

def x_pp_authorization_signature url, api_user_id, api_password, timestamp, access_token, access_token_verifier
  # no query params, but if there were, this is where they'd go
  query_params = {}
  key = [
    paypal_encode(api_password),
    paypal_encode(access_token_verifier),
  ].join("&")

  params = query_params.dup.merge({
    "oauth_consumer_key" => api_user_id,
    "oauth_version" => "1.0",
    "oauth_signature_method" => "HMAC-SHA1",
    "oauth_token" => access_token,
    "oauth_timestamp" => timestamp,
  })
  sorted_query_string = params.to_paypal_permissions_query

  base = [
    "POST",
    paypal_encode(url),
    paypal_encode(sorted_query_string)
  ].join("&")
  base = base.gsub /%([0-9A-F])([0-9A-F])/ do
    "%#{$1.downcase}#{$2.downcase}"  # hack to match PayPal Java SDK bit for bit
  end

  digest = OpenSSL::HMAC.digest('sha1', key, base)
  Base64.encode64(digest).chomp
end