will_paypal 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg
2
+ doc
3
+ Manifest
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use --create ree@nvp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,26 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ will_paypal (0.1.0)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.1.2)
10
+ fakeweb (1.3.0)
11
+ rspec (2.6.0)
12
+ rspec-core (~> 2.6.0)
13
+ rspec-expectations (~> 2.6.0)
14
+ rspec-mocks (~> 2.6.0)
15
+ rspec-core (2.6.0)
16
+ rspec-expectations (2.6.0)
17
+ diff-lcs (~> 1.1.2)
18
+ rspec-mocks (2.6.0)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ fakeweb
25
+ rspec (~> 2.6.0)
26
+ will_paypal!
data/README.rdoc ADDED
@@ -0,0 +1,56 @@
1
+ = Paypal NVP
2
+
3
+ Will Paypal allow to connect your Ruby on Rails application to the Paypal NVP API. It's based on the Paypal NVP Gem https://github.com/solisoft/paypal_nvp but completely rewritten.
4
+
5
+ == Installation
6
+
7
+ The recommended way is that you get the gem:
8
+
9
+ $ sudo gem install will_paypal
10
+
11
+ Specify the API-Credentials within the constructor, add a logger. You can also use a 'settings.yml' to put everything there
12
+
13
+ # All those fields are mandatory
14
+
15
+ sandbox:
16
+ url: "https://api-3t.sandbox.paypal.com/nvp" (default)
17
+ user: "o.bonn_1237393081_biz_api1.solisoft.net"
18
+ password: "1237393093"
19
+ signature: "AU2Yv5COwWPCfeYLv34Z766F-gfNAzX6LaQE6VZkHMRq35Gmite-bMXu"
20
+ version: "72.0" (default)
21
+
22
+ live:
23
+ url: "https://api-3t.paypal.com/nvp" (default)
24
+ user: "o.bonn_1237393081_biz_api1.solisoft.net"
25
+ password: "1237393093"
26
+ signature: "AU2Yv5COwWPCfeYLv34Z766F-gfNAzX6LaQE6VZkHMRq35Gmite-bMXu"
27
+ version: "72.0" (default)
28
+
29
+ == Example usage
30
+
31
+ p = WillPaypal.new(:user => Settings.payment.paypal.merchant, :password => Settings.payment.paypal.pass, :sandbox => Settings.payment.paypal.sandbox, :signature => Settings.payment.paypal.cert, :version => "72.0", :logger => logger)
32
+
33
+ data = {
34
+ :method => "MyPaypalMethod",
35
+ :amt => "55"
36
+ # other params needed
37
+ }
38
+ result = p.call_paypal(data) # will return a hash
39
+ puts result[:parsed_body]["ACK"] # Success
40
+
41
+ The Result-Hash looks like that:
42
+
43
+ :parsed_body=>{
44
+ "TIMESTAMP"=>"2011-05-19T00:25:19Z",
45
+ "BUILD"=>"1882144",
46
+ "VERSION"=>"72.0",
47
+ "CORRELATIONID"=>"correlationid",
48
+ "TOKEN"=>"mytoken",
49
+ "ACK"=>"Success"
50
+ },
51
+ :status=>"200",
52
+ :body=>"TOKEN=mytoken&TIMESTAMP=2011%2d05%2d19T00%3a25%3a19Z&CORRELATIONID=correlationid&ACK=Success&VERSION=72%2e0&BUILD=1882144"
53
+
54
+ == PAYPAL API Documentation
55
+
56
+ https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/howto_api_reference
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ desc "Run specs"
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+
8
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
@@ -0,0 +1,69 @@
1
+ require "logger"
2
+ require "net/https"
3
+ require "cgi"
4
+
5
+ class WillPaypal
6
+
7
+ attr_accessor :config, :logger
8
+
9
+ DEFAULT_OPTIONS = {
10
+ :version => "72.0",
11
+ :sandbox => false,
12
+ :params => {}
13
+ }
14
+
15
+ def initialize(config={})
16
+ self.config = DEFAULT_OPTIONS.merge(config)
17
+ self.logger = config.delete(:logger) || Logger.new(STDOUT)
18
+ self.config[:url] ||= self.config[:sandbox] ? "https://api-3t.sandbox.paypal.com/nvp" : "https://api-3t.paypal.com/nvp"
19
+ end
20
+
21
+ def query_string_for(data)
22
+ data.merge!({
23
+ "USER" => self.config[:user],
24
+ "PWD" => self.config[:password],
25
+ "SIGNATURE" => self.config[:signature],
26
+ "VERSION" => self.config[:version]
27
+ })
28
+ data.merge!(self.config[:params])
29
+ query = []
30
+ data.each do |key, value|
31
+ query << "#{key.to_s.upcase}=#{URI.escape(value.to_s)}"
32
+ end
33
+ query.join("&")
34
+ end
35
+
36
+ def hash_from_query_string(query_string)
37
+ hash = {}
38
+ query_string.split("&").each do |element|
39
+ a = element.split("=")
40
+ hash[a[0]] = CGI.unescape(a[1]) if a.size == 2
41
+ end
42
+ hash
43
+ end
44
+
45
+ def call_paypal(data)
46
+ uri = URI.parse(self.config[:url])
47
+
48
+ http = Net::HTTP.new(uri.host, uri.port)
49
+ http.use_ssl = true
50
+ rootCA = '/etc/ssl/certs'
51
+ if File.directory? rootCA
52
+ http.ca_path = rootCA
53
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
54
+ http.verify_depth = 5
55
+ else
56
+ self.logger.warn "WARNING: no ssl certs found. Paypal communication will be insecure. DO NOT DEPLOY"
57
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
58
+ end
59
+
60
+ response = http.request_post(uri.path, self.query_string_for(data))
61
+ response_hash = { :status => response.code, :body => response.body, :parsed_body => {} }
62
+
63
+ if response.kind_of? Net::HTTPSuccess
64
+ response_hash[:parsed_body].merge! self.hash_from_query_string(response.body)
65
+ end
66
+ response_hash
67
+ end
68
+
69
+ end
@@ -0,0 +1,9 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require "will_paypal"
5
+ require "rspec"
6
+ require 'fakeweb'
7
+
8
+ RSpec.configure do |config|
9
+ end
@@ -0,0 +1,122 @@
1
+ require "spec_helper"
2
+
3
+ describe "WillPaypal" do
4
+
5
+ describe "initialize" do
6
+
7
+ context "overwriting defaults" do
8
+ subject { WillPaypal.new(:version => "1").config[:version] }
9
+ it { should == "1" }
10
+ end
11
+
12
+ context "with defaults" do
13
+ subject { WillPaypal.new }
14
+
15
+ its(:config) do
16
+ should eql({
17
+ :version => "72.0",
18
+ :sandbox => false,
19
+ :url => "https://api-3t.paypal.com/nvp",
20
+ :params => {}
21
+ })
22
+ end
23
+
24
+ end
25
+ end
26
+
27
+ describe "Request to Paypal" do
28
+
29
+ describe "Query String" do
30
+
31
+ let(:paypal) { WillPaypal.new }
32
+
33
+ it "should create a valid querystring from request params" do
34
+ %w{FOO=bar BAR=foo USER= PWD= SIGNATURE=}.each do |a|
35
+ paypal.query_string_for({ :foo => "bar", :bar => "foo" }).should include (a)
36
+ end
37
+ end
38
+
39
+ %w{user pwd signature}.each do |param|
40
+ it "should include #{param} by default" do
41
+ paypal.query_string_for({}).should include("#{param.to_s.upcase}=#{URI.escape(paypal.config[param.to_sym].to_s)}")
42
+ end
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+
49
+ describe "Paypal response" do
50
+
51
+ let(:paypal) { WillPaypal.new }
52
+
53
+ it "should convert a query string like" do
54
+ paypal.hash_from_query_string("FOO=bar&BAR=foo&SIGNATURE=123456").should eql({
55
+ "FOO" => "bar", "BAR" => "foo", "SIGNATURE" => "123456"
56
+ })
57
+ end
58
+
59
+ it "should remove blank attributes" do
60
+ paypal.hash_from_query_string("FOO=bar&BAR=foo&USER=&PWD=&SIGNATURE=123456&VERSION=72.0").should eql({
61
+ "FOO" => "bar", "BAR" => "foo", "SIGNATURE" => "123456", "VERSION" => "72.0"
62
+ })
63
+ end
64
+
65
+ end
66
+
67
+ describe "Paypal Call" do
68
+
69
+ context "success" do
70
+ let(:paypal) { WillPaypal.new(:user => "team_1220115929_biz_api1.example.com", :pass => "1240413944", :cert => "acert4qi4-AdHZBSIbxJNpN30UcsogreatcertP", :version => "72.0") }
71
+
72
+ it "should return the successful response" do
73
+ response_data = "TIMESTAMP=2011%2d05%2d18T17%3a51%3a55Z&CORRELATIONID=f45a2a3867a2b&ACK=Success&VERSION=72%2e0&BUILD=1882144&L_ERRORCODE0=10002&L_SHORTMESSAGE0=Authentication%2fAuthorization%20Failed&L_LONGMESSAGE0=You%20do%20not%20have%20permissions%20to%20make%20this%20API%20call&L_SEVERITYCODE0=Error"
74
+ FakeWeb.register_uri(:post, %r|https://api-3t.paypal.com/nvp|, :body => response_data, :times => 1)
75
+ paypal.call_paypal({}).should eql({:status => "200", :body => response_data, :parsed_body => {"L_SEVERITYCODE0"=>"Error", "TIMESTAMP"=>"2011-05-18T17:51:55Z", "L_ERRORCODE0"=>"10002", "BUILD"=>"1882144", "L_LONGMESSAGE0"=>"You do not have permissions to make this API call", "VERSION"=>"72.0", "L_SHORTMESSAGE0"=>"Authentication/Authorization Failed", "CORRELATIONID"=>"f45a2a3867a2b", "ACK"=>"Success"}})
76
+ end
77
+ end
78
+
79
+ context "ssl" do
80
+ let(:paypal) { WillPaypal.new(:user => "team_1220115929_biz_api1.example.com", :pass => "1240413944", :cert => "acert4qi4-AdHZBSIbxJNpN30UcsogreatcertP", :version => "72.0") }
81
+
82
+ it "should do an ssl call" do
83
+ response_data = "TIMESTAMP=2011%2d05%2d18T17%3a51%3a55Z&CORRELATIONID=f45a2a3867a2b&ACK=Failure&VERSION=72%2e0&BUILD=1882144&L_ERRORCODE0=10002&L_SHORTMESSAGE0=Authentication%2fAuthorization%20Failed&L_LONGMESSAGE0=You%20do%20not%20have%20permissions%20to%20make%20this%20API%20call&L_SEVERITYCODE0=Error"
84
+ FakeWeb.register_uri(:post, %r|https://api-3t.paypal.com/nvp|, :body => response_data, :times => 1)
85
+ File.should_receive(:directory?).with('/etc/ssl/certs').and_return(true)
86
+ http = double("Net::HTTP")
87
+ Net::HTTP.stub(:new).and_return(http)
88
+ http_response = double("response")
89
+ http_response.should_receive(:code)
90
+ http_response.should_receive(:body)
91
+ http.should_receive(:request_post).with("/nvp", "USER=team_1220115929_biz_api1.example.com&SIGNATURE=&VERSION=72.0&PWD=").and_return(http_response)
92
+ http.should_receive(:use_ssl=).with(true)
93
+ http.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_PEER)
94
+ http.should_receive(:ca_path=).with('/etc/ssl/certs')
95
+ http.should_receive(:verify_depth=).with(5)
96
+ paypal.call_paypal({})
97
+ end
98
+
99
+ end
100
+
101
+ context "without ssl" do
102
+ let(:paypal) { WillPaypal.new(:user => "team_1220115929_biz_api1.example.com", :pass => "1240413944", :cert => "acert4qi4-AdHZBSIbxJNpN30UcsogreatcertP", :version => "72.0") }
103
+
104
+ it "should do an ssl call" do
105
+ response_data = "TIMESTAMP=2011%2d05%2d18T17%3a51%3a55Z&CORRELATIONID=f45a2a3867a2b&ACK=Failure&VERSION=72%2e0&BUILD=1882144&L_ERRORCODE0=10002&L_SHORTMESSAGE0=Authentication%2fAuthorization%20Failed&L_LONGMESSAGE0=You%20do%20not%20have%20permissions%20to%20make%20this%20API%20call&L_SEVERITYCODE0=Error"
106
+ FakeWeb.register_uri(:post, %r|https://api-3t.paypal.com/nvp|, :body => response_data, :times => 1)
107
+ File.should_receive(:directory?).with('/etc/ssl/certs').and_return(false)
108
+ http = double("Net::HTTP")
109
+ Net::HTTP.stub(:new).and_return(http)
110
+ http_response = double("response")
111
+ http_response.should_receive(:code)
112
+ http_response.should_receive(:body)
113
+ http.should_receive(:use_ssl=).with(true)
114
+ http.should_receive(:request_post).with("/nvp", "USER=team_1220115929_biz_api1.example.com&SIGNATURE=&VERSION=72.0&PWD=").and_return(http_response)
115
+ http.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
116
+ paypal.call_paypal({})
117
+ end
118
+ end
119
+
120
+ end
121
+
122
+ end
Binary file
@@ -0,0 +1,36 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{will_paypal}
5
+ s.version = "0.1.1"
6
+ s.platform = Gem::Platform::RUBY
7
+
8
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
9
+ s.authors = ["Michael Bumann - Railslove", "Jan Kus - Railslove"]
10
+ s.description = %q{Paypal NVP API Class.}
11
+ s.email = ["michael@railslove.com", "jan@railslove.com"]
12
+ s.extra_rdoc_files = ["lib/will_paypal.rb", "README.rdoc"]
13
+ s.files = ["init.rb", "lib/will_paypal.rb", "Manifest", "Rakefile", "README.rdoc", "will_paypal.gemspec"]
14
+ s.homepage = %q{https://github.com/railslove/Will-Paypal}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Will_Paypal", "--main", "README.rdoc"]
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ s.rubyforge_project = %q{will_paypal}
21
+ s.rubygems_version = %q{1.7.2}
22
+ s.summary = %q{Paypal NVP API Class.}
23
+
24
+ s.add_development_dependency "rspec", "~> 2.6.0"
25
+ s.add_development_dependency "fakeweb"
26
+
27
+ if s.respond_to? :specification_version then
28
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
29
+ s.specification_version = 3
30
+
31
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
32
+ else
33
+ end
34
+ else
35
+ end
36
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: will_paypal
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
11
+ platform: ruby
12
+ authors:
13
+ - Michael Bumann - Railslove
14
+ - Jan Kus - Railslove
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-05-19 00:00:00 +02:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: rspec
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ hash: 23
31
+ segments:
32
+ - 2
33
+ - 6
34
+ - 0
35
+ version: 2.6.0
36
+ type: :development
37
+ version_requirements: *id001
38
+ - !ruby/object:Gem::Dependency
39
+ name: fakeweb
40
+ prerelease: false
41
+ requirement: &id002 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ hash: 3
47
+ segments:
48
+ - 0
49
+ version: "0"
50
+ type: :development
51
+ version_requirements: *id002
52
+ description: Paypal NVP API Class.
53
+ email:
54
+ - michael@railslove.com
55
+ - jan@railslove.com
56
+ executables: []
57
+
58
+ extensions: []
59
+
60
+ extra_rdoc_files:
61
+ - lib/will_paypal.rb
62
+ - README.rdoc
63
+ files:
64
+ - .gitignore
65
+ - .rvmrc
66
+ - Gemfile
67
+ - Gemfile.lock
68
+ - README.rdoc
69
+ - Rakefile
70
+ - lib/will_paypal.rb
71
+ - spec/spec_helper.rb
72
+ - spec/will_paypal_spec.rb
73
+ - will_paypal-0.1.0.gem
74
+ - will_paypal.gemspec
75
+ has_rdoc: true
76
+ homepage: https://github.com/railslove/Will-Paypal
77
+ licenses: []
78
+
79
+ post_install_message:
80
+ rdoc_options:
81
+ - --line-numbers
82
+ - --inline-source
83
+ - --title
84
+ - Will_Paypal
85
+ - --main
86
+ - README.rdoc
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ hash: 3
95
+ segments:
96
+ - 0
97
+ version: "0"
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ hash: 11
104
+ segments:
105
+ - 1
106
+ - 2
107
+ version: "1.2"
108
+ requirements: []
109
+
110
+ rubyforge_project: will_paypal
111
+ rubygems_version: 1.5.0
112
+ signing_key:
113
+ specification_version: 3
114
+ summary: Paypal NVP API Class.
115
+ test_files:
116
+ - spec/spec_helper.rb
117
+ - spec/will_paypal_spec.rb