Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

halfway on SetExpressCheckout

  • Loading branch information...
commit 4281d92cba0615e19f59c55006a7967bb938a3df 1 parent 3add30f
@nov authored
View
1  Gemfile
@@ -0,0 +1 @@
+gemspec
View
2  LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2009 nov matake
+Copyright (c) 2011 nov matake
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
View
51 Rakefile
@@ -1,53 +1,20 @@
-require 'rubygems'
-require 'rake'
+require 'bundler'
+Bundler::GemHelper.install_tasks
-begin
- require 'jeweler'
- Jeweler::Tasks.new do |gem|
- gem.name = "paypal-express"
- gem.summary = %Q{TODO: one-line summary of your gem}
- gem.description = %Q{TODO: longer description of your gem}
- gem.email = "nov@matake.jp"
- gem.homepage = "http://github.com/nov/paypal-express"
- gem.authors = ["nov matake"]
- gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
- end
- Jeweler::GemcutterTasks.new
-rescue LoadError
- puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
-end
-
-require 'rake/testtask'
-Rake::TestTask.new(:test) do |test|
- test.libs << 'lib' << 'test'
- test.pattern = 'test/**/test_*.rb'
- test.verbose = true
-end
+require 'rspec/core/rake_task'
+RSpec::Core::RakeTask.new(:spec)
-begin
- require 'rcov/rcovtask'
- Rcov::RcovTask.new do |test|
- test.libs << 'test'
- test.pattern = 'test/**/test_*.rb'
- test.verbose = true
- end
-rescue LoadError
- task :rcov do
- abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
- end
+RSpec::Core::RakeTask.new(:rcov) do |spec|
+ spec.rcov = true
+ spec.rcov_opts = ['--exclude spec,gems']
end
-task :test => :check_dependencies
-
-task :default => :test
+task :default => :spec
require 'rake/rdoctask'
Rake::RDocTask.new do |rdoc|
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
-
rdoc.rdoc_dir = 'rdoc'
- rdoc.title = "paypal-express #{version}"
+ rdoc.title = "paypal-express #{File.read('VERSION')}"
rdoc.rdoc_files.include('README*')
rdoc.rdoc_files.include('lib/**/*.rb')
end
View
1  VERSION
@@ -0,0 +1 @@
+0.0.1
View
3,509 lib/cert
3,509 additions, 0 deletions not shown
View
0  lib/paypal-express.rb
No changes.
View
53 lib/paypal.rb
@@ -0,0 +1,53 @@
+require 'restclient_with_ssl_support'
+
+class Paypal
+ attr_required :username, :password, :signature, :endpoint
+ attr_optional :sandbox
+
+ def initialize(attributes = {})
+ @username = attributes[:username]
+ @password = attributes[:password]
+ @signature = attributes[:signature]
+ @sandbox = attributes[:sandbox]
+ @version = '66.0'
+ @endpoint = if @sandbox
+ 'https://api-3t.sandbox.paypal.com/nvp'
+ else
+ 'https://api-3t.paypal.com/nvp'
+ end
+ end
+
+ def common_params
+ {
+ :USER => self.username,
+ :PWD => self.password,
+ :SIGNATURE => self.signature,
+ :VERSION => self.version
+ }
+ end
+
+ def request(method, params = {})
+ attr_missing!
+ response = RestClient.post(
+ self.endpoint,
+ common_params.merge(params).merge(:METHOD => method)
+ )
+ response = CGI.parse(response).inject({}) do |res, (k, v)|
+ res.merge!(k.to_sym => v.first)
+ end
+ case response[:ACK]
+ when 'Success', 'SuccessWithWarning'
+ response
+ else
+ raise APIError.new(response)
+ end
+ rescue AttrRequuired::AttrMissing => e
+ raise AttrMissing.new(e.message)
+ rescue RestClient::Exception => e
+ raise Exception.new(e.http_code, e.message, e.http_body)
+ end
+
+end
+
+require 'paypal/exceptions'
+require 'paypal/express'
View
27 lib/paypal/exceptions.rb
@@ -0,0 +1,27 @@
+class Paypal
+
+ class Exception < StandardError; end
+ class AttrMissing < Exception; end
+
+ class HttpError < Exception
+ attr_accessor :code, :type, :message
+ def initialize(code, message, body = '')
+ @code = code
+ if body.blank?
+ @message = message
+ else
+ response = JSON.parse(body).with_indifferent_access
+ @message = response[:error][:message]
+ @type = response[:error][:type]
+ end
+ end
+ end
+
+ class APIError < Exception
+ attr_accessor :response
+ def initialize(response = {})
+ @response = response
+ end
+ end
+
+end
View
25 lib/paypal/express.rb
@@ -0,0 +1,25 @@
+class Paypal
+ class Express < Paypal
+ attr_required :return_url, :cancel_url
+
+ def initialize(attributes = {})
+ super
+ @return_url = attributes[:return_url]
+ @cancel_url = attributes[:cancel_url]
+ end
+
+ def setup(payment_requests = [])
+ params = {
+ :RETURNURL => self.return_url,
+ :CANCELURL => self.cancel_url
+ }
+ payment_requests.each_with_index do |payment_request, index|
+ params.merge! payment_request.to_params(index)
+ end
+ response = self.request :SetExpressCheckout, params
+ if response['ACK']
+ end
+ end
+
+ end
+end
View
32 lib/paypal/payment_request.rb
@@ -0,0 +1,32 @@
+class Paypal
+ class PaymentRequest
+ attr_required :payment_action, :amount
+ attr_optional :transaction_id, :transaction_type, :payment_type, :order_time, :currency_code, :tax_amount, :payment_status, :pending_reason, :reason_code
+
+ def initialize(attributes = {})
+ (required_attributes + optional_attributes).each do |key|
+ self.send "#{key}=", attributes[key]
+ end
+ @payment_action ||= 'Sale'
+ @amount ||= 0
+ @currency_code ||= 'USD'
+ end
+
+ def formatted_amount(amount)
+ if amount == amount.to_i
+ "#{amount.to_i}.00"
+ else
+ "#{amount.to_i}.#{((amount - amount.to_i) * 100).to_i}"
+ end
+ end
+
+ def to_params(index = 0)
+ {
+ :"PAYMENTREQUEST_#{index}_PAYMENTACTION" => self.payment_action,
+ :"PAYMENTREQUEST_#{index}_AMT" => formatted_amount(self.amount),
+ :"PAYMENTREQUEST_#{index}_CURRENCYCODE" => self.currency_code
+ }
+ end
+
+ end
+end
View
28 lib/restclient_with_ssl_support.rb
@@ -0,0 +1,28 @@
+require 'restclient'
+
+module RestClient
+
+ def self.ssl_settings
+ {
+ :verify_ssl => OpenSSL::SSL::VERIFY_PEER,
+ :ssl_ca_file => File.join(File.dirname(__FILE__), 'cert')
+ }
+ end
+
+ def self.get(url, headers={}, &block)
+ Request.execute(ssl_settings.merge(:method => :get, :url => url, :headers => headers), &block)
+ end
+
+ def self.post(url, payload, headers={}, &block)
+ Request.execute(ssl_settings.merge(:method => :post, :url => url, :payload => payload, :headers => headers), &block)
+ end
+
+ def self.put(url, payload, headers={}, &block)
+ Request.execute(ssl_settings.merge(:method => :put, :url => url, :payload => payload, :headers => headers), &block)
+ end
+
+ def self.delete(url, headers={}, &block)
+ Request.execute(ssl_settings.merge(:method => :delete, :url => url, :headers => headers), &block)
+ end
+
+end
View
22 paypal-express.gemspec
@@ -0,0 +1,22 @@
+Gem::Specification.new do |s|
+ s.name = "paypal-express"
+ s.version = File.read("VERSION")
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.3.6") if s.respond_to? :required_rubygems_version=
+ s.authors = ["nov matake"]
+ s.description = %q{Rugy Gem for PayPal Express Checkout API}
+ s.summary = %q{Rugy Gem for PayPal Express Checkout API}
+ s.email = "nov@matake.jp"
+ s.extra_rdoc_files = ["LICENSE", "README.rdoc"]
+ s.rdoc_options = ["--charset=UTF-8"]
+ s.homepage = "http://github.com/nov/paypal-express"
+ s.require_paths = ["lib"]
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.add_runtime_dependency "rest-client", ">= 1.4"
+ s.add_runtime_dependency "attr_required", ">= 0.0.3"
+ s.add_development_dependency "rake", ">= 0.8"
+ s.add_development_dependency "rcov", ">= 0.9"
+ s.add_development_dependency "rspec", ">= 2"
+ s.add_development_dependency "fakeweb", ">= 1.3.0"
+end
View
10 test/helper.rb
@@ -1,10 +0,0 @@
-require 'rubygems'
-require 'test/unit'
-require 'shoulda'
-
-$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
-$LOAD_PATH.unshift(File.dirname(__FILE__))
-require 'paypal-express'
-
-class Test::Unit::TestCase
-end
View
7 test/test_paypal-express.rb
@@ -1,7 +0,0 @@
-require 'helper'
-
-class TestPaypalExpress < Test::Unit::TestCase
- should "probably rename this file and start testing for real" do
- flunk "hey buddy, you should probably rename this file and start testing for real"
- end
-end
Please sign in to comment.
Something went wrong with that request. Please try again.