Permalink
Browse files

basic specs and nvp/response

  • Loading branch information...
1 parent e81e661 commit 301e2d224cae016dddab39147feae5463ab33d3b @nov committed Feb 2, 2011
View
@@ -17,5 +17,6 @@ tmtags
coverage
rdoc
pkg
+Gemfile.lock
## PROJECT::SPECIFIC
View
2 .rspec
@@ -0,0 +1,2 @@
+--color
+--format=documentation
View
@@ -4,5 +4,10 @@
require 'paypal/exceptions'
require 'paypal/nvp'
+require 'paypal/nvp/response'
require 'paypal/express'
-require 'paypal/payment_request'
+require 'paypal/payment_request'
+
+module Paypal
+ API_VERSION = '66.0'
+end
View
@@ -5,9 +5,9 @@ class Express < NVP
attr_required :return_url, :cancel_url
def initialize(attributes = {})
- super
@return_url = attributes[:return_url]
@cancel_url = attributes[:cancel_url]
+ super
end
def setup(payment_requests)
View
@@ -4,17 +4,25 @@ class NVP
attr_required :username, :password, :signature, :version, :endpoint
attr_optional :sandbox
+ ENDPOINT = {
+ :production => 'https://api-3t.paypal.com/nvp',
+ :sandbox => 'https://api-3t.sandbox.paypal.com/nvp'
+ }
+
def initialize(attributes = {})
@username = attributes[:username]
@password = attributes[:password]
@signature = attributes[:signature]
@sandbox = attributes[:sandbox]
- @version = '66.0'
+ @version = API_VERSION
@endpoint = if @sandbox
- 'https://api-3t.sandbox.paypal.com/nvp'
+ ENDPOINT[:sandbox]
else
- 'https://api-3t.paypal.com/nvp'
+ ENDPOINT[:production]
end
+ attr_missing!
+ rescue AttrRequired::AttrMissing => e
+ raise AttrMissing.new(e.message)
end
def common_params
@@ -27,22 +35,27 @@ def common_params
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)|
+ handle_response do
+ post(method, params)
+ end
+ end
+
+ private
+
+ def post(method, params)
+ RestClient.post(self.endpoint, common_params.merge(params).merge(:METHOD => method))
+ end
+
+ def handle_response
+ CGI.parse(yield).inject({}) do |res, (k, v)|
res.merge!(k.to_sym => v.first)
end
case response[:ACK]
when 'Success', 'SuccessWithWarning'
- response
+ Response.new response
else
raise APIError.new(response)
end
- rescue AttrRequired::AttrMissing => e
- raise AttrMissing.new(e.message)
rescue RestClient::Exception => e
raise Exception.new(e.http_code, e.message, e.http_body)
end
View
@@ -0,0 +1,15 @@
+module Paypal
+ class NVP::Response
+ attr_accessor :ack, :correlation_id, :timestamp, :build, :version, :token
+ alias_method :status, :ack
+
+ def initialize(response_params = {})
+ @ack = response_params['ACK']
+ @correlation_id = response_params['CORRELATIONID']
+ @timestamp = response_params['TIMESTAMP']
+ @build = response_params['BUILD']
+ @version = response_params['VERSION']
+ @token = response_params['token']
+ end
+ end
+end
View
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = "paypal-express"
- s.version = File.read("VERSION")
+ s.version = File.read(File.join(File.dirname(__FILE__), "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}
@@ -0,0 +1 @@
+TIMESTAMP=2011%2d02%2d02T02%3a16%3a50Z&CORRELATIONID=379d1b7f97afb&ACK=Failure&L_ERRORCODE0=10001&L_SHORTMESSAGE0=Internal%20Error&L_LONGMESSAGE0=Timeout%20processing%20request
@@ -0,0 +1 @@
+ACK=Success&BUILD=1721431&CORRELATIONID=5549ea3a78af1&TIMESTAMP=2011-02-02T02%3A02%3A18Z&TOKEN=EC-5YJ90598G69065317&VERSION=66.0
@@ -0,0 +1,18 @@
+require 'fakeweb'
+
+module FakeResponseHelper
+
+ def fake_response(method, params, file_path, options = {})
+ FakeWeb.register_uri(
+ :post,
+ Paypal::NVP::ENDPOINT[:production],
+ options.merge(
+ :body => File.read(File.join(File.dirname(__FILE__), '../fake_reponse', "#{file_path}.txt"))
+ )
+ )
+ end
+
+end
+
+FakeWeb.allow_net_connect = false
+include FakeResponseHelper
@@ -0,0 +1,36 @@
+require 'spec_helper.rb'
+
+describe Paypal::Express, '.new' do
+
+ let(:required_params) do
+ {
+ :username => 'nov',
+ :password => 'password',
+ :signature => 'sig',
+ :return_url => 'http://example.com/success',
+ :cancel_url => 'http://example.com/cancel'
+ }
+ end
+
+ context 'when any required parameters are missing' do
+ it 'should raise AttrMissing exception' do
+ required_params.keys.each do |missing_key|
+ insufficient_attributes = required_params.reject do |key, value|
+ key == missing_key
+ end
+ lambda do
+ Paypal::Express.new insufficient_attributes
+ end.should raise_error Paypal::AttrMissing
+ end
+ end
+ end
+
+ context 'when all required parameters are given' do
+ it 'should succeed' do
+ lambda do
+ Paypal::Express.new required_params
+ end.should_not raise_error Paypal::AttrMissing
+ end
+ end
+
+end
View
@@ -0,0 +1,45 @@
+require 'spec_helper.rb'
+
+describe Paypal::NVP, '.new' do
+
+ let(:required_params) do
+ {
+ :username => 'nov',
+ :password => 'password',
+ :signature => 'sig'
+ }
+ end
+
+ context 'when any required parameters are missing' do
+ it 'should raise AttrMissing exception' do
+ required_params.keys.each do |missing_key|
+ insufficient_attributes = required_params.reject do |key, value|
+ key == missing_key
+ end
+ lambda do
+ Paypal::NVP.new insufficient_attributes
+ end.should raise_error Paypal::AttrMissing
+ end
+ end
+ end
+
+ context 'when all required parameters are given' do
+ it 'should succeed' do
+ lambda do
+ Paypal::NVP.new required_params
+ end.should_not raise_error Paypal::AttrMissing
+ end
+
+ it 'should setup endpoint and version' do
+ client = Paypal::NVP.new required_params
+ client.version.should == Paypal::API_VERSION
+ client.endpoint.should == Paypal::NVP::ENDPOINT[:production]
+ end
+
+ it 'should support sandbox mode' do
+ client = Paypal::NVP.new required_params.merge(:sandbox => true)
+ client.endpoint.should == Paypal::NVP::ENDPOINT[:sandbox]
+ end
+ end
+
+end
View
@@ -0,0 +1,6 @@
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+
+require 'paypal'
+require 'rspec'
+require 'helpers/fake_response_helper'

0 comments on commit 301e2d2

Please sign in to comment.