paypal_adaptive 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,7 +3,7 @@ This gem is a lightweight wrapper for the paypal adaptive payments API.
3
3
 
4
4
  This is very much a work in progress! Use at your own risk or submit bug fixes :)
5
5
 
6
- Before you need start, download pp_adaptivepayments.pdf and IPNGuide.pdf from the Paypal's dev site http://x.com
6
+ Before you need start, download pp_adaptivepayments.pdf from the Paypal's dev site http://x.com
7
7
  It'll be invaluable for parameters and error messages.
8
8
 
9
9
  ## HOWTO
@@ -31,7 +31,7 @@ Create paypal_adaptive.yml to your config folder:
31
31
 
32
32
  Make the payment request:
33
33
 
34
- pay_request = PaypalAdaptive::PayRequest.new
34
+ pay_request = PaypalAdaptive::Request.new
35
35
 
36
36
  data = {
37
37
  "returnUrl" => "http://testserver.com/payments/completed_payment_request",
@@ -62,8 +62,15 @@ They can also click cancel to go to http://testserver.com/payments/canceled_paym
62
62
  The actual payment details will be sent to your server via "ipnNotificationUrl"
63
63
  You have to create a listener to receive POST messages from paypal. I added a Rails metal template in the templates folder which handles the callbcak.
64
64
 
65
+ Additionally, you can make calls to Paypal Adaptive's other APIs:
66
+ payment_details, preapproval, preapproval_details, cancel_preapproval, convert_currency, refund
67
+
68
+ Input is just a Hash just like the pay method. Refer to the Paypal manual for more details.
65
69
 
66
70
  ## Changelog
71
+ 0.0.3
72
+ Renamed PayRequest, PayResponse into Request, Response since other api calls use the class as well.
73
+
67
74
  0.0.2
68
75
  Fixed initialized constant warning.
69
76
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -1,4 +1,4 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), "config"))
2
- require File.expand_path(File.join(File.dirname(__FILE__), "pay_request"))
3
- require File.expand_path(File.join(File.dirname(__FILE__), "pay_response"))
2
+ require File.expand_path(File.join(File.dirname(__FILE__), "request"))
3
+ require File.expand_path(File.join(File.dirname(__FILE__), "response"))
4
4
  require File.expand_path(File.join(File.dirname(__FILE__), "ipn_notification"))
@@ -1,85 +1,84 @@
1
- require 'net/http'
2
- require 'net/https'
3
1
  require 'json'
4
2
  require 'config'
5
- require 'pay_response'
3
+ require 'net/http'
4
+ require 'net/https'
5
+ require 'response'
6
6
 
7
7
  module PaypalAdaptive
8
8
  class NoDataError < Exception
9
9
  end
10
-
11
- class PayRequest
12
- attr_accessor :pay_key
13
- attr_accessor :errors
14
- attr_accessor :env
15
-
10
+
11
+ class Request
16
12
  def initialize(env = nil)
17
13
  @env = env
18
14
  @@config ||= PaypalAdaptive::Config.new(@env)
19
- @@api_base_url ||= @@config.api_base_url
15
+ @@api_base_url ||= @@config.api_base_url
20
16
  @@headers ||= @@config.headers
21
17
  end
22
-
18
+
23
19
  def validate
24
20
  #TODO the receiverList field not validating properly
25
-
21
+
26
22
  # @@schema_filepath = "../lib/pay_request_schema.json"
27
23
  # @@schema = File.open(@@schema_filepath, "rb"){|f| JSON.parse(f.read)}
28
24
  # see page 42 of PP Adaptive Payments PDF for explanation of all fields.
29
25
  #JSON::Schema.validate(@data, @@schema)
30
26
  end
31
-
27
+
32
28
  def pay(data)
33
29
  raise NoDataError unless data
34
-
35
- call_api(data, "/AdaptivePayments/Pay")
30
+
31
+ response_data = call_api(data, "/AdaptivePayments/Pay")
32
+ PaypalAdaptive::Response.new(response_data, @env)
36
33
  end
37
-
34
+
38
35
  def payment_details(data)
39
36
  raise NoDataError unless data
40
-
37
+
41
38
  call_api(data, "/AdaptivePayments/PaymentDetails")
42
39
  end
43
40
 
44
41
  def preapproval(data)
45
42
  raise NoDataError unless data
46
-
43
+
47
44
  call_api(data, "/AdaptivePayments/Preapproval")
48
45
  end
49
46
 
50
47
  def preapproval_details(data)
51
48
  raise NoDataError unless data
52
-
49
+
53
50
  call_api(data, "/AdaptivePayments/PreapprovalDetails")
54
51
  end
55
52
 
56
53
  def cancel_preapproval(data)
57
54
  raise NoDataError unless data
58
-
55
+
59
56
  call_api(data, "/AdaptivePayments/CancelPreapproval")
60
57
  end
61
58
 
62
59
  def convert_currency(data)
63
60
  raise NoDataError unless data
64
-
61
+
65
62
  call_api(data, "/AdaptivePayments/ConvertCurrency")
66
63
  end
67
64
 
68
65
  def refund(data)
69
66
  raise NoDataError unless data
70
-
67
+
71
68
  call_api(data, "/AdaptivePayments/Refund")
72
69
  end
73
-
70
+
74
71
  def call_api(data, path)
75
72
  #hack fix: JSON.unparse doesn't work in Rails 2.3.5; only {}.to_json does..
76
73
  api_request_data = JSON.unparse(data) rescue data.to_json
77
74
  url = URI.parse @@api_base_url
78
75
  http = Net::HTTP.new(url.host, 443)
79
76
  http.use_ssl = (url.scheme == 'https')
80
-
77
+
81
78
  resp, response_data = http.post(path, api_request_data, @@headers)
82
- pp_response = PaypalAdaptive::PayResponse.new(response_data, @env)
79
+
80
+ JSON.parse(response_data)
83
81
  end
84
82
  end
83
+
85
84
  end
@@ -1,32 +1,27 @@
1
- require 'json'
2
-
3
1
  module PaypalAdaptive
4
- class PayResponse
2
+ class Response < Hash
5
3
  def initialize(response, env=nil)
6
4
  @@config ||= PaypalAdaptive::Config.new(env)
7
5
  @@paypal_base_url ||= @@config.paypal_base_url
8
6
 
9
- @json_response = JSON.parse(response)
7
+ self.merge!(response)
10
8
  end
11
9
 
12
10
  def success?
13
- @json_response['responseEnvelope']['ack'] == 'Success'
14
- end
15
-
16
- def pay_key
17
- @json_response['payKey']
11
+ self['responseEnvelope']['ack'] == 'Success'
18
12
  end
19
13
 
20
14
  def errors
21
15
  if success?
22
16
  return []
23
17
  else
24
- @json_response['error']
18
+ self['error']
25
19
  end
26
20
  end
27
21
 
28
22
  def approve_paypal_payment_url
29
- "#{@@paypal_base_url}/webscr?cmd=_ap-payment&paykey=#{pay_key}"
23
+ "#{@@paypal_base_url}/webscr?cmd=_ap-payment&paykey=#{self['payKey']}"
30
24
  end
25
+
31
26
  end
32
27
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{paypal_adaptive}
8
- s.version = "0.0.2"
8
+ s.version = "0.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tommy Chheng"]
@@ -25,10 +25,10 @@ Gem::Specification.new do |s|
25
25
  "config/paypal_adaptive.yml",
26
26
  "lib/config.rb",
27
27
  "lib/ipn_notification.rb",
28
- "lib/pay_request.rb",
29
28
  "lib/pay_request_schema.json",
30
- "lib/pay_response.rb",
31
29
  "lib/paypal_adaptive.rb",
30
+ "lib/request.rb",
31
+ "lib/response.rb",
32
32
  "paypal_adaptive.gemspec",
33
33
  "templates/paypal_ipn.rb",
34
34
  "test/data/invalid_chain_pay_request.json",
@@ -39,7 +39,8 @@ Gem::Specification.new do |s|
39
39
  "test/data/valid_simple_pay_request_1.json",
40
40
  "test/helper.rb",
41
41
  "test/pay_request_schema_test.rb",
42
- "test/pay_request_test.rb"
42
+ "test/pay_request_test.rb",
43
+ "test/payment_details_test.rb"
43
44
  ]
44
45
  s.homepage = %q{http://github.com/tc/paypal_adaptive}
45
46
  s.rdoc_options = ["--charset=UTF-8"]
@@ -49,7 +50,8 @@ Gem::Specification.new do |s|
49
50
  s.test_files = [
50
51
  "test/helper.rb",
51
52
  "test/pay_request_schema_test.rb",
52
- "test/pay_request_test.rb"
53
+ "test/pay_request_test.rb",
54
+ "test/payment_details_test.rb"
53
55
  ]
54
56
 
55
57
  if s.respond_to? :specification_version then
@@ -4,8 +4,7 @@
4
4
  "currencyCode":"USD",
5
5
  "receiverList":{"receiver":[
6
6
  {"email":"testpp_1261697850_per@nextsprocket.com", "amount":"100.00", "primary": "true"},
7
- {"email":"sender_1261713739_per@nextsprocket.com", "amount":"50.00", "primary": "false"},
8
- {"email":"ppsell_1261697921_biz@nextsprocket.com", "amount":"50.00", "primary": "false"}
7
+ {"email":"ppsell_1261697921_biz@nextsprocket.com", "amount":"90.00", "primary": "false"}
9
8
  ]},
10
9
  "cancelUrl":"http://127.0.0.1:3000/payments/canceled_payment_request",
11
10
  "actionType":"PAY"
@@ -1,4 +1,5 @@
1
1
  require 'rubygems'
2
+ require 'JSON'
2
3
  require 'test/unit'
3
4
 
4
5
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
@@ -1,9 +1,9 @@
1
1
  require 'helper'
2
- require '../lib/pay_request'
2
+ require '../lib/request'
3
3
 
4
4
  class PayRequestTest < Test::Unit::TestCase
5
5
  def setup
6
- @pay_request = PaypalAdaptive::PayRequest.new("test")
6
+ @pay_request = PaypalAdaptive::Request.new("test")
7
7
  end
8
8
 
9
9
  def test_valid_simple_pay
@@ -36,6 +36,11 @@ class PayRequestTest < Test::Unit::TestCase
36
36
  data = read_json_file(data_filepath)
37
37
  pp_response = @pay_request.pay(data)
38
38
  puts "redirect url to\n #{pp_response.approve_paypal_payment_url}"
39
+
40
+ unless pp_response.success?
41
+ puts pp_response.errors
42
+ end
43
+
39
44
  assert pp_response.success?
40
45
  end
41
46
 
@@ -69,9 +74,6 @@ class PayRequestTest < Test::Unit::TestCase
69
74
  assert pp_response.success? == false
70
75
  end
71
76
 
72
- def test_payment_details
73
- #TODO
74
- end
75
77
 
76
78
  def test_preapproval
77
79
  #TODO
@@ -0,0 +1,35 @@
1
+ require 'helper'
2
+ require '../lib/request'
3
+
4
+ class PaymentDetailsTest < Test::Unit::TestCase
5
+ def setup
6
+ @pay_request = PaypalAdaptive::Request.new("test")
7
+ @payment_details_request = PaypalAdaptive::Request.new("test")
8
+ end
9
+
10
+ def test_payment_details
11
+ puts "-------"
12
+ puts "payment details"
13
+ data_filepath = "../test/data/valid_chain_pay_request.json"
14
+
15
+ data = read_json_file(data_filepath)
16
+ pp_response = @pay_request.pay(data)
17
+ puts "redirect url to\n #{pp_response.approve_paypal_payment_url}"
18
+
19
+ unless pp_response.success?
20
+ puts pp_response.errors
21
+ end
22
+
23
+ assert pp_response.success?
24
+
25
+ data = {"requestEnvelope"=>{"errorLanguage"=>"en_US"}, "payKey" => pp_response['payKey']}
26
+
27
+ response = @payment_details_request.payment_details(data)
28
+ assert_equal 'CREATED', response['status']
29
+ end
30
+
31
+ def read_json_file(filepath)
32
+ File.open(filepath, "rb"){|f| JSON.parse(f.read)}
33
+ end
34
+
35
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paypal_adaptive
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tommy Chheng
@@ -50,10 +50,10 @@ files:
50
50
  - config/paypal_adaptive.yml
51
51
  - lib/config.rb
52
52
  - lib/ipn_notification.rb
53
- - lib/pay_request.rb
54
53
  - lib/pay_request_schema.json
55
- - lib/pay_response.rb
56
54
  - lib/paypal_adaptive.rb
55
+ - lib/request.rb
56
+ - lib/response.rb
57
57
  - paypal_adaptive.gemspec
58
58
  - templates/paypal_ipn.rb
59
59
  - test/data/invalid_chain_pay_request.json
@@ -65,6 +65,7 @@ files:
65
65
  - test/helper.rb
66
66
  - test/pay_request_schema_test.rb
67
67
  - test/pay_request_test.rb
68
+ - test/payment_details_test.rb
68
69
  has_rdoc: true
69
70
  homepage: http://github.com/tc/paypal_adaptive
70
71
  licenses: []
@@ -97,3 +98,4 @@ test_files:
97
98
  - test/helper.rb
98
99
  - test/pay_request_schema_test.rb
99
100
  - test/pay_request_test.rb
101
+ - test/payment_details_test.rb