paypal_adaptive 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -21,3 +21,4 @@ pkg
21
21
  ## PROJECT::SPECIFIC
22
22
  config
23
23
  Gemfile.lock
24
+ .rvmrc
data/README.markdown CHANGED
@@ -96,6 +96,9 @@ for each environment, e.g.:
96
96
  The api_cert_file should point to your cert_key_pem.txt that is downloaded through the paypal developer interface. It will contain a section that specifies the RSA private key and another section that specifies a certificate. If this is left empty, paypal_adaptive will attempt to use the signature method of validation with PayPal, so your signature config must not be nil.
97
97
 
98
98
  ## Changelog
99
+ 0.3.4
100
+ Locale specific paypal urls and improved testing. changed approve_paypal_payment_url method signature with deprecation warning. thanks mreinsch.
101
+
99
102
  0.3.3
100
103
  Handle JSON parsing error. Added validation for config.
101
104
 
@@ -1,6 +1,6 @@
1
1
  module PaypalAdaptive
2
2
  class Response < Hash
3
- def initialize(response, env=nil)
3
+ def initialize(response={}, env=nil)
4
4
  config = PaypalAdaptive.config(env)
5
5
  @paypal_base_url = config.paypal_base_url
6
6
 
@@ -26,14 +26,25 @@ module PaypalAdaptive
26
26
  message = errors.first['message'] rescue nil
27
27
  end
28
28
 
29
- def approve_paypal_payment_url(type=nil)
30
- if self['payKey'].nil?
31
- return nil
32
- elsif ['mini', 'light'].include?(type.to_s)
33
- return "#{@paypal_base_url}/webapps/adaptivepayment/flow/pay?expType=#{type.to_s}&paykey=#{self['payKey']}"
29
+ # URL to redirect to in order for the user to approve the payment
30
+ #
31
+ # options:
32
+ # * country: default country code for the user
33
+ # * type: mini / light
34
+ def approve_paypal_payment_url(opts = {})
35
+ if opts.is_a?(Symbol) || opts.is_a?(String)
36
+ warn "[DEPRECATION] use approve_paypal_payment_url(:type => #{opts})"
37
+ opts = {:type => opts}
38
+ end
39
+ return nil if self['payKey'].nil?
40
+
41
+ if ['mini', 'light'].include?(opts[:type].to_s)
42
+ "#{@paypal_base_url}/webapps/adaptivepayment/flow/pay?expType=#{opts[:type]}&paykey=#{self['payKey']}"
43
+ else
44
+ base = @paypal_base_url
45
+ base = base + "/#{opts[:country]}" if opts[:country]
46
+ "#{base}/webscr?cmd=_ap-payment&paykey=#{self['payKey']}"
34
47
  end
35
-
36
- "#{@paypal_base_url}/webscr?cmd=_ap-payment&paykey=#{self['payKey']}"
37
48
  end
38
49
 
39
50
  def preapproval_paypal_payment_url
@@ -1,4 +1,4 @@
1
1
  module PaypalAdaptive
2
- VERSION = "0.3.3"
2
+ VERSION = "0.3.4"
3
3
  end
4
4
 
@@ -14,7 +14,8 @@ Gem::Specification.new do |s|
14
14
 
15
15
  s.add_dependency("json", "~>1.0")
16
16
  s.add_dependency("jsonschema", "~>2.0.0")
17
- s.add_dependency("rake")
17
+ s.add_development_dependency("rake")
18
+ s.add_development_dependency("activesupport", "~> 3.2.0")
18
19
 
19
20
  s.rubyforge_project = "paypal_adaptive"
20
21
 
@@ -9,6 +9,6 @@
9
9
  ]},
10
10
  "cancelUrl":"http://127.0.0.1:3000/payments/canceled_payment_request",
11
11
  "actionType":"PAY",
12
- "senderEmail": "a@a.com"
12
+ "senderEmail": "a@test.com"
13
13
  }
14
14
 
@@ -4,73 +4,35 @@ class PayRequestTest < Test::Unit::TestCase
4
4
  def setup
5
5
  @pay_request = PaypalAdaptive::Request.new("test")
6
6
  end
7
-
8
- def test_valid_simple_pay
9
- puts "-------"
10
- puts "simple"
11
-
12
- data_filepath = File.join(File.dirname(__FILE__),"..", "data","valid_simple_pay_request_1.json")
13
7
 
14
- data = read_json_file(data_filepath)
15
- pp_response = @pay_request.pay(data)
16
-
17
- puts "redirect url to\n #{pp_response.approve_paypal_payment_url}"
18
- assert_equal true, pp_response.success?
8
+ def test_valid_simple_pay
9
+ data = read_json_file("valid_simple_pay_request_1.json")
10
+ assert_success_response @pay_request.pay(data)
19
11
  end
20
12
 
21
13
  def test_invalid_simple_pay
22
- data_filepath = File.join(File.dirname(__FILE__),"..", "data","invalid_simple_pay_request_1.json")
23
-
24
- data = read_json_file(data_filepath)
25
- pp_response = @pay_request.pay(data)
26
- puts pp_response.errors
27
- assert_equal false, pp_response.success?
14
+ data = read_json_file("invalid_simple_pay_request_1.json")
15
+ assert_error_response "580022", @pay_request.pay(data)
28
16
  end
29
17
 
30
18
  def test_valid_chain_pay
31
- puts "-------"
32
- puts "chain"
33
- data_filepath = File.join(File.dirname(__FILE__),"..", "data","valid_chain_pay_request.json")
34
-
35
- data = read_json_file(data_filepath)
36
- pp_response = @pay_request.pay(data)
37
- puts "redirect url to\n #{pp_response.approve_paypal_payment_url}"
38
-
39
- unless pp_response.success?
40
- puts pp_response.errors
41
- end
42
-
43
- assert_equal true, pp_response.success?
19
+ data = read_json_file("valid_chain_pay_request.json")
20
+ assert_success_response @pay_request.pay(data)
44
21
  end
45
22
 
46
23
  def test_invalid_chain_pay
47
- data_filepath = File.join(File.dirname(__FILE__),"..", "data","invalid_chain_pay_request.json")
48
-
49
- data = read_json_file(data_filepath)
50
- pp_response = @pay_request.pay(data)
51
- puts pp_response.errors
52
- assert_equal false, pp_response.success?
24
+ data = read_json_file("invalid_chain_pay_request.json")
25
+ assert_error_response "579017", @pay_request.pay(data)
53
26
  end
54
27
 
55
28
  def test_valid_parallel_pay
56
- puts "-------"
57
- puts "parallel"
58
-
59
- data_filepath = File.join(File.dirname(__FILE__),"..", "data","valid_parallel_pay_request.json")
60
-
61
- data = read_json_file(data_filepath)
62
- pp_response = @pay_request.pay(data)
63
- puts "redirect url to\n #{pp_response.approve_paypal_payment_url}"
64
- assert_equal true, pp_response.success?
29
+ data = read_json_file("valid_parallel_pay_request.json")
30
+ assert_success_response @pay_request.pay(data)
65
31
  end
66
32
 
67
33
  def test_invalid_parallel_pay
68
- data_filepath = File.join(File.dirname(__FILE__),"..", "data","invalid_parallel_pay_request.json")
69
-
70
- data = read_json_file(data_filepath)
71
- pp_response = @pay_request.pay(data)
72
- puts pp_response.errors
73
- assert_equal false, pp_response.success?
34
+ data = read_json_file("invalid_parallel_pay_request.json")
35
+ assert_error_response "579040", @pay_request.pay(data)
74
36
  end
75
37
 
76
38
  def test_preapproval
@@ -93,8 +55,26 @@ class PayRequestTest < Test::Unit::TestCase
93
55
  #TODO
94
56
  end
95
57
 
96
- def read_json_file(filepath)
97
- File.open(filepath, "rb"){|f| JSON.parse(f.read)}
58
+ def read_json_file(filename)
59
+ JSON.parse(File.read(File.join(File.dirname(__FILE__),"..","data",filename)))
98
60
  end
61
+
62
+ APPROVE_URL_PATTERN = %r{^https://www.sandbox.paypal.com/webscr\?cmd=_ap-payment&paykey=AP-}
63
+ APPROVE_URL_PATTERN_JP = %r{^https://www.sandbox.paypal.com/jp/webscr\?cmd=_ap-payment&paykey=AP-}
64
+ MINI_APPROVE_URL_PATTERN = %r{^https://www.sandbox.paypal.com/webapps/adaptivepayment/flow/pay\?expType=mini&paykey=AP-}
99
65
 
66
+ def assert_success_response(pp_response)
67
+ assert_equal true, pp_response.success?, "expected success: #{pp_response.inspect}"
68
+ assert_match APPROVE_URL_PATTERN, pp_response.approve_paypal_payment_url
69
+ assert_match APPROVE_URL_PATTERN_JP, pp_response.approve_paypal_payment_url(:country => :jp)
70
+ assert_match MINI_APPROVE_URL_PATTERN, pp_response.approve_paypal_payment_url('mini')
71
+ assert_match MINI_APPROVE_URL_PATTERN, pp_response.approve_paypal_payment_url(:type => 'mini')
72
+ end
73
+
74
+ def assert_error_response(error_code, pp_response)
75
+ assert_equal false, pp_response.success?
76
+ pp_errors = pp_response.errors.first
77
+ assert_equal "Error", pp_errors["severity"]
78
+ assert_equal error_code, pp_errors["errorId"]
79
+ end
100
80
  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.3.3
4
+ version: 0.3.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -51,7 +51,7 @@ dependencies:
51
51
  - - ! '>='
52
52
  - !ruby/object:Gem::Version
53
53
  version: '0'
54
- type: :runtime
54
+ type: :development
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  none: false
@@ -59,6 +59,22 @@ dependencies:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: activesupport
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 3.2.0
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 3.2.0
62
78
  description: Lightweight wrapper for Paypal's Adaptive Payments API
63
79
  email:
64
80
  - tommy.chheng@gmail.com