Skip to content

Commit 4281d92

Browse files
committed
halfway on SetExpressCheckout
1 parent 3add30f commit 4281d92

14 files changed

+3708
-60
lines changed

Diff for: Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gemspec

Diff for: LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2009 nov matake
1+
Copyright (c) 2011 nov matake
22

33
Permission is hereby granted, free of charge, to any person obtaining
44
a copy of this software and associated documentation files (the

Diff for: Rakefile

+9-42
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,20 @@
1-
require 'rubygems'
2-
require 'rake'
1+
require 'bundler'
2+
Bundler::GemHelper.install_tasks
33

4-
begin
5-
require 'jeweler'
6-
Jeweler::Tasks.new do |gem|
7-
gem.name = "paypal-express"
8-
gem.summary = %Q{TODO: one-line summary of your gem}
9-
gem.description = %Q{TODO: longer description of your gem}
10-
gem.email = "[email protected]"
11-
gem.homepage = "http://github.com/nov/paypal-express"
12-
gem.authors = ["nov matake"]
13-
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14-
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15-
end
16-
Jeweler::GemcutterTasks.new
17-
rescue LoadError
18-
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19-
end
20-
21-
require 'rake/testtask'
22-
Rake::TestTask.new(:test) do |test|
23-
test.libs << 'lib' << 'test'
24-
test.pattern = 'test/**/test_*.rb'
25-
test.verbose = true
26-
end
4+
require 'rspec/core/rake_task'
5+
RSpec::Core::RakeTask.new(:spec)
276

28-
begin
29-
require 'rcov/rcovtask'
30-
Rcov::RcovTask.new do |test|
31-
test.libs << 'test'
32-
test.pattern = 'test/**/test_*.rb'
33-
test.verbose = true
34-
end
35-
rescue LoadError
36-
task :rcov do
37-
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38-
end
7+
RSpec::Core::RakeTask.new(:rcov) do |spec|
8+
spec.rcov = true
9+
spec.rcov_opts = ['--exclude spec,gems']
3910
end
4011

41-
task :test => :check_dependencies
42-
43-
task :default => :test
12+
task :default => :spec
4413

4514
require 'rake/rdoctask'
4615
Rake::RDocTask.new do |rdoc|
47-
version = File.exist?('VERSION') ? File.read('VERSION') : ""
48-
4916
rdoc.rdoc_dir = 'rdoc'
50-
rdoc.title = "paypal-express #{version}"
17+
rdoc.title = "paypal-express #{File.read('VERSION')}"
5118
rdoc.rdoc_files.include('README*')
5219
rdoc.rdoc_files.include('lib/**/*.rb')
5320
end

Diff for: VERSION

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.0.1

Diff for: lib/cert

+3,509
Large diffs are not rendered by default.

Diff for: lib/paypal-express.rb

Whitespace-only changes.

Diff for: lib/paypal.rb

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
require 'restclient_with_ssl_support'
2+
3+
class Paypal
4+
attr_required :username, :password, :signature, :endpoint
5+
attr_optional :sandbox
6+
7+
def initialize(attributes = {})
8+
@username = attributes[:username]
9+
@password = attributes[:password]
10+
@signature = attributes[:signature]
11+
@sandbox = attributes[:sandbox]
12+
@version = '66.0'
13+
@endpoint = if @sandbox
14+
'https://api-3t.sandbox.paypal.com/nvp'
15+
else
16+
'https://api-3t.paypal.com/nvp'
17+
end
18+
end
19+
20+
def common_params
21+
{
22+
:USER => self.username,
23+
:PWD => self.password,
24+
:SIGNATURE => self.signature,
25+
:VERSION => self.version
26+
}
27+
end
28+
29+
def request(method, params = {})
30+
attr_missing!
31+
response = RestClient.post(
32+
self.endpoint,
33+
common_params.merge(params).merge(:METHOD => method)
34+
)
35+
response = CGI.parse(response).inject({}) do |res, (k, v)|
36+
res.merge!(k.to_sym => v.first)
37+
end
38+
case response[:ACK]
39+
when 'Success', 'SuccessWithWarning'
40+
response
41+
else
42+
raise APIError.new(response)
43+
end
44+
rescue AttrRequuired::AttrMissing => e
45+
raise AttrMissing.new(e.message)
46+
rescue RestClient::Exception => e
47+
raise Exception.new(e.http_code, e.message, e.http_body)
48+
end
49+
50+
end
51+
52+
require 'paypal/exceptions'
53+
require 'paypal/express'

Diff for: lib/paypal/exceptions.rb

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Paypal
2+
3+
class Exception < StandardError; end
4+
class AttrMissing < Exception; end
5+
6+
class HttpError < Exception
7+
attr_accessor :code, :type, :message
8+
def initialize(code, message, body = '')
9+
@code = code
10+
if body.blank?
11+
@message = message
12+
else
13+
response = JSON.parse(body).with_indifferent_access
14+
@message = response[:error][:message]
15+
@type = response[:error][:type]
16+
end
17+
end
18+
end
19+
20+
class APIError < Exception
21+
attr_accessor :response
22+
def initialize(response = {})
23+
@response = response
24+
end
25+
end
26+
27+
end

Diff for: lib/paypal/express.rb

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Paypal
2+
class Express < Paypal
3+
attr_required :return_url, :cancel_url
4+
5+
def initialize(attributes = {})
6+
super
7+
@return_url = attributes[:return_url]
8+
@cancel_url = attributes[:cancel_url]
9+
end
10+
11+
def setup(payment_requests = [])
12+
params = {
13+
:RETURNURL => self.return_url,
14+
:CANCELURL => self.cancel_url
15+
}
16+
payment_requests.each_with_index do |payment_request, index|
17+
params.merge! payment_request.to_params(index)
18+
end
19+
response = self.request :SetExpressCheckout, params
20+
if response['ACK']
21+
end
22+
end
23+
24+
end
25+
end

Diff for: lib/paypal/payment_request.rb

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Paypal
2+
class PaymentRequest
3+
attr_required :payment_action, :amount
4+
attr_optional :transaction_id, :transaction_type, :payment_type, :order_time, :currency_code, :tax_amount, :payment_status, :pending_reason, :reason_code
5+
6+
def initialize(attributes = {})
7+
(required_attributes + optional_attributes).each do |key|
8+
self.send "#{key}=", attributes[key]
9+
end
10+
@payment_action ||= 'Sale'
11+
@amount ||= 0
12+
@currency_code ||= 'USD'
13+
end
14+
15+
def formatted_amount(amount)
16+
if amount == amount.to_i
17+
"#{amount.to_i}.00"
18+
else
19+
"#{amount.to_i}.#{((amount - amount.to_i) * 100).to_i}"
20+
end
21+
end
22+
23+
def to_params(index = 0)
24+
{
25+
:"PAYMENTREQUEST_#{index}_PAYMENTACTION" => self.payment_action,
26+
:"PAYMENTREQUEST_#{index}_AMT" => formatted_amount(self.amount),
27+
:"PAYMENTREQUEST_#{index}_CURRENCYCODE" => self.currency_code
28+
}
29+
end
30+
31+
end
32+
end

Diff for: lib/restclient_with_ssl_support.rb

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'restclient'
2+
3+
module RestClient
4+
5+
def self.ssl_settings
6+
{
7+
:verify_ssl => OpenSSL::SSL::VERIFY_PEER,
8+
:ssl_ca_file => File.join(File.dirname(__FILE__), 'cert')
9+
}
10+
end
11+
12+
def self.get(url, headers={}, &block)
13+
Request.execute(ssl_settings.merge(:method => :get, :url => url, :headers => headers), &block)
14+
end
15+
16+
def self.post(url, payload, headers={}, &block)
17+
Request.execute(ssl_settings.merge(:method => :post, :url => url, :payload => payload, :headers => headers), &block)
18+
end
19+
20+
def self.put(url, payload, headers={}, &block)
21+
Request.execute(ssl_settings.merge(:method => :put, :url => url, :payload => payload, :headers => headers), &block)
22+
end
23+
24+
def self.delete(url, headers={}, &block)
25+
Request.execute(ssl_settings.merge(:method => :delete, :url => url, :headers => headers), &block)
26+
end
27+
28+
end

Diff for: paypal-express.gemspec

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Gem::Specification.new do |s|
2+
s.name = "paypal-express"
3+
s.version = File.read("VERSION")
4+
s.required_rubygems_version = Gem::Requirement.new(">= 1.3.6") if s.respond_to? :required_rubygems_version=
5+
s.authors = ["nov matake"]
6+
s.description = %q{Rugy Gem for PayPal Express Checkout API}
7+
s.summary = %q{Rugy Gem for PayPal Express Checkout API}
8+
s.email = "[email protected]"
9+
s.extra_rdoc_files = ["LICENSE", "README.rdoc"]
10+
s.rdoc_options = ["--charset=UTF-8"]
11+
s.homepage = "http://github.com/nov/paypal-express"
12+
s.require_paths = ["lib"]
13+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
14+
s.files = `git ls-files`.split("\n")
15+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16+
s.add_runtime_dependency "rest-client", ">= 1.4"
17+
s.add_runtime_dependency "attr_required", ">= 0.0.3"
18+
s.add_development_dependency "rake", ">= 0.8"
19+
s.add_development_dependency "rcov", ">= 0.9"
20+
s.add_development_dependency "rspec", ">= 2"
21+
s.add_development_dependency "fakeweb", ">= 1.3.0"
22+
end

Diff for: test/helper.rb

-10
This file was deleted.

Diff for: test/test_paypal-express.rb

-7
This file was deleted.

0 commit comments

Comments
 (0)