Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Paypal NVP certificate authentication, updated specs #96

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/paypal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
require 'rest_client'

module Paypal
mattr_accessor :api_version
mattr_accessor :api_version, :certificate_auth
self.api_version = '88.0'

ENDPOINT = {
Expand All @@ -20,7 +20,13 @@ module Paypal

def self.endpoint
if sandbox?
Paypal::ENDPOINT[:sandbox]
if certificate_auth?
Paypal::ENDPOINT[:sandbox_cert]
else
Paypal::ENDPOINT[:sandbox]
end
elsif certificate_auth?
Paypal::ENDPOINT[:production_cert]
else
Paypal::ENDPOINT[:production]
end
Expand Down Expand Up @@ -56,6 +62,10 @@ def self.sandbox=(boolean)
end
self.sandbox = false

def self.certificate_auth?
@@certificate_auth
end

end

require 'paypal/util'
Expand Down
36 changes: 29 additions & 7 deletions lib/paypal/nvp/request.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
module Paypal
module NVP
class Request < Base
attr_required :username, :password, :signature
attr_optional :subject
attr_required :username, :password
attr_optional :subject, :certificate, :signature
attr_accessor :version

ENDPOINT = {
:production => 'https://api-3t.paypal.com/nvp',
:sandbox => 'https://api-3t.sandbox.paypal.com/nvp'
:production_cert => 'https://api.paypal.com/nvp',
:sandbox => 'https://api-3t.sandbox.paypal.com/nvp',
:sandbox_cert => 'https://api.sandbox.paypal.com/nvp'
}

def self.endpoint
if Paypal.sandbox?
ENDPOINT[:sandbox]
if Paypal.certificate_auth?
ENDPOINT[:sandbox_cert]
else
ENDPOINT[:sandbox]
end
elsif Paypal.certificate_auth?
ENDPOINT[:production_cert]
else
ENDPOINT[:production]
end
Expand All @@ -24,13 +32,16 @@ def initialize(attributes = {})
end

def common_params
{
params = {
:USER => self.username,
:PWD => self.password,
:SIGNATURE => self.signature,
:SUBJECT => self.subject,
:VERSION => self.version
}

params.merge!(:SIGNATURE => self.signature) unless Paypal.certificate_auth
params
end

def request(method, params = {})
Expand All @@ -42,7 +53,18 @@ def request(method, params = {})
private

def post(method, params)
RestClient.post(self.class.endpoint, common_params.merge(params).merge(:METHOD => method))
request_params = {
:method => :post,
:url => self.class.endpoint,
:payload => common_params.merge(params).merge(:METHOD => method)
}

request_params.merge!({
:ssl_client_cert => OpenSSL::X509::Certificate.new(self.certificate),
:ssl_client_key => OpenSSL::PKey::RSA.new(self.certificate)
}) if Paypal.certificate_auth?

RestClient::Request.execute(request_params)
end

def handle_response
Expand All @@ -61,4 +83,4 @@ def handle_response
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/helpers/fake_response_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ def request_to(endpoint, method = :get)
end

FakeWeb.allow_net_connect = false
include FakeResponseHelper
include FakeResponseHelper
3 changes: 1 addition & 2 deletions spec/paypal/express/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ def post_with_logging(method, params)
let :attributes do
{
:username => 'nov',
:password => 'password',
:signature => 'sig'
:password => 'password'
}
end

Expand Down
21 changes: 18 additions & 3 deletions spec/paypal/nvp/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
let :attributes do
{
:username => 'nov',
:password => 'password',
:signature => 'sig'
:password => 'password'
}
end

Expand Down Expand Up @@ -39,12 +38,28 @@
client.class.endpoint.should == Paypal::NVP::Request::ENDPOINT[:production]
end

it 'should setup certificate endpoint and version' do
Paypal.certificate_auth = true
client = Paypal::NVP::Request.new attributes
client.class.endpoint.should == Paypal::NVP::Request::ENDPOINT[:production_cert]
Paypal.certificate_auth = false
end

it 'should support sandbox mode' do
sandbox_mode do
client = Paypal::NVP::Request.new attributes
client.class.endpoint.should == Paypal::NVP::Request::ENDPOINT[:sandbox]
end
end

it 'should support cert sandbox mode' do
sandbox_mode do
Paypal.certificate_auth = true
client = Paypal::NVP::Request.new attributes.merge(certificate: '/path/to/cert.pem')
client.class.endpoint.should == Paypal::NVP::Request::ENDPOINT[:sandbox_cert]
Paypal.certificate_auth = false
end
end
end

context 'when optional parameters are given' do
Expand Down Expand Up @@ -113,4 +128,4 @@
end
end
end
end
end