paypal_adaptive 0.2.6 → 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -92,6 +92,9 @@ If you don't set ssl_cert_file then paypal_adaptive will check for certificates
92
92
  this location exists, otherwise falling back to the cacert.pem file included with paypal_adaptive.
93
93
 
94
94
  ## Changelog
95
+ 0.2.7
96
+ Refactored config file handling for better non-Rails support, thanks mreinsch and dave-thompson.
97
+
95
98
  0.2.6
96
99
  Fix for using correct status field in pre-approval, thanks nbibler. Fix for ruby 1.9.3, thanks deepj.
97
100
 
@@ -15,23 +15,12 @@ module PaypalAdaptive
15
15
  :beta_sandbox => "https://svcs.beta-sandbox.paypal.com"
16
16
  } unless defined? API_BASE_URL_MAPPING
17
17
 
18
- attr_accessor :config_filepath, :paypal_base_url, :api_base_url, :headers, :ssl_cert_path, :ssl_cert_file
18
+ attr_accessor :paypal_base_url, :api_base_url, :headers, :ssl_cert_path, :ssl_cert_file
19
19
 
20
- def initialize(env=nil, config_override=nil)
21
- if env
22
- #non-rails env
23
- @config_filepath = File.join(File.dirname(__FILE__), "..", "..", "config", "paypal_adaptive.yml")
24
- load(env, config_override)
25
- else
26
- @config_filepath = File.join(Rails.root, "config", "paypal_adaptive.yml")
27
- load(Rails.env, config_override)
28
- end
29
- end
30
-
31
- def load(environment, config_override)
32
- config = YAML.load(ERB.new(File.new(@config_filepath).read).result)[environment]
33
- config.merge!(config_override) unless config_override.nil?
20
+ def initialize(env=nil, config_override={})
21
+ config = YAML.load(ERB.new(File.new(config_filepath).read).result)[env]
34
22
  raise "Could not load settings from config file" unless config
23
+ config.merge!(config_override) unless config_override.nil?
35
24
 
36
25
  if config["retain_requests_for_test"] == true
37
26
  @retain_requests_for_test = true
@@ -42,6 +31,9 @@ module PaypalAdaptive
42
31
  @ssl_cert_file = nil
43
32
  @paypal_base_url = PAYPAL_BASE_URL_MAPPING[pp_env]
44
33
  @api_base_url = API_BASE_URL_MAPPING[pp_env]
34
+
35
+ # http.rb requires headers to be strings. Protect against ints in paypal_adaptive.yml
36
+ config.update(config){ |key,v| v.to_s }
45
37
  @headers = {
46
38
  "X-PAYPAL-SECURITY-USERID" => config['username'],
47
39
  "X-PAYPAL-SECURITY-PASSWORD" => config['password'],
@@ -53,17 +45,36 @@ module PaypalAdaptive
53
45
 
54
46
  if config['ssl_cert_file'] && config['ssl_cert_file'].length > 0
55
47
  @ssl_cert_file = config['ssl_cert_file']
56
- elsif File.exists?("/etc/ssl/certs")
57
- @ssl_cert_path = "/etc/ssl/certs"
58
48
  else
59
49
  @ssl_cert_file = File.join(File.dirname(__FILE__), "..", "..", "cacert.pem")
60
50
  end
51
+ puts "warning: coulf not find file: #{@ssl_cert_file}" unless File.exists?(@ssl_cert_file.to_s)
52
+ end
53
+ end
54
+
55
+ def config_filepath
56
+ if defined?(Rails)
57
+ Rails.root.join("config", "paypal_adaptive.yml")
58
+ else
59
+ File.join(File.dirname(__FILE__), "..", "..", "config", "paypal_adaptive.yml")
61
60
  end
62
61
  end
63
62
 
64
63
  def retain_requests_for_test?
65
64
  !!@retain_requests_for_test
66
65
  end
66
+ end
67
+
68
+ def self.config(env = nil)
69
+ env ||= default_env_for_config
70
+ raise "Please provide an environment" unless env
71
+ @configs ||= Hash.new
72
+ @configs[env] ||= Config.new(env)
73
+ end
74
+
75
+ private
67
76
 
77
+ def self.default_env_for_config
78
+ defined?(Rails) ? Rails.env : nil
68
79
  end
69
80
  end
@@ -6,23 +6,22 @@ module PaypalAdaptive
6
6
  class IpnNotification
7
7
 
8
8
  def initialize(env=nil)
9
- @env = env
10
- @@config ||= PaypalAdaptive::Config.new(@env)
11
- @@paypal_base_url ||= @@config.paypal_base_url
12
- @@ssl_cert_path ||= @@config.ssl_cert_path
13
- @@ssl_cert_file ||= @@config.ssl_cert_file
9
+ config = PaypalAdaptive.config(env)
10
+ @paypal_base_url = config.paypal_base_url
11
+ @ssl_cert_path = config.ssl_cert_path
12
+ @ssl_cert_file = config.ssl_cert_file
14
13
  end
15
14
 
16
15
  def send_back(data)
17
16
  data = "cmd=_notify-validate&#{data}"
18
- url = URI.parse @@paypal_base_url
17
+ url = URI.parse @paypal_base_url
19
18
  http = Net::HTTP.new(url.host, 443)
20
19
  http.use_ssl = true
21
20
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
22
- http.ca_path = @@ssl_cert_path unless @@ssl_cert_path.nil?
23
- http.ca_file = @@ssl_cert_file unless @@ssl_cert_file.nil?
21
+ http.ca_path = @ssl_cert_path unless @ssl_cert_path.nil?
22
+ http.ca_file = @ssl_cert_file unless @ssl_cert_file.nil?
24
23
 
25
- path = "#{@@paypal_base_url}/cgi-bin/webscr"
24
+ path = "#{@paypal_base_url}/cgi-bin/webscr"
26
25
  response_data = http.post(path, data).body
27
26
 
28
27
  @verified = response_data == "VERIFIED"
@@ -9,20 +9,20 @@ module PaypalAdaptive
9
9
  class Request
10
10
  def initialize(env = nil)
11
11
  @env = env
12
- @@config ||= PaypalAdaptive::Config.new(@env)
13
- @@api_base_url ||= @@config.api_base_url
14
- @@headers ||= @@config.headers
15
- @@ssl_cert_path ||= @@config.ssl_cert_path
16
- @@ssl_cert_file ||= @@config.ssl_cert_file
12
+ config = PaypalAdaptive.config(env)
13
+ @api_base_url = config.api_base_url
14
+ @headers = config.headers
15
+ @ssl_cert_path = config.ssl_cert_path
16
+ @ssl_cert_file = config.ssl_cert_file
17
17
  end
18
18
 
19
19
  def validate
20
20
  #TODO the receiverList field not validating properly
21
21
 
22
- # @@schema_filepath = "../lib/pay_request_schema.json"
23
- # @@schema = File.open(@@schema_filepath, "rb"){|f| JSON.parse(f.read)}
22
+ # @schema_filepath = "../lib/pay_request_schema.json"
23
+ # @schema = File.open(@schema_filepath, "rb"){|f| JSON.parse(f.read)}
24
24
  # see page 42 of PP Adaptive Payments PDF for explanation of all fields.
25
- #JSON::Schema.validate(@data, @@schema)
25
+ #JSON::Schema.validate(@data, @schema)
26
26
  end
27
27
 
28
28
  def pay(data)
@@ -78,14 +78,14 @@ module PaypalAdaptive
78
78
  def post(data, path)
79
79
  #hack fix: JSON.unparse doesn't work in Rails 2.3.5; only {}.to_json does..
80
80
  api_request_data = JSON.unparse(data) rescue data.to_json
81
- url = URI.parse @@api_base_url
81
+ url = URI.parse @api_base_url
82
82
  http = Net::HTTP.new(url.host, 443)
83
83
  http.use_ssl = true
84
84
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
85
- http.ca_path = @@ssl_cert_path unless @@ssl_cert_path.nil?
86
- http.ca_file = @@ssl_cert_file unless @@ssl_cert_file.nil?
85
+ http.ca_path = @ssl_cert_path unless @ssl_cert_path.nil?
86
+ http.ca_file = @ssl_cert_file unless @ssl_cert_file.nil?
87
87
 
88
- response_data = http.post(path, api_request_data, @@headers).body
88
+ response_data = http.post(path, api_request_data, @headers).body
89
89
 
90
90
  JSON.parse(response_data)
91
91
  end
@@ -1,45 +1,43 @@
1
1
  module PaypalAdaptive
2
2
  class Response < Hash
3
3
  def initialize(response, env=nil)
4
- @@config ||= PaypalAdaptive::Config.new(env)
5
- @@paypal_base_url ||= @@config.paypal_base_url
4
+ config = PaypalAdaptive.config(env)
5
+ @paypal_base_url = config.paypal_base_url
6
6
 
7
7
  self.merge!(response)
8
8
  end
9
9
 
10
10
  def success?
11
- self['responseEnvelope']['ack'].to_s =~ /^Success$/i &&
12
- !(self['paymentExecStatus'].to_s =~ /^ERROR$/i)
11
+ !! (self['responseEnvelope']['ack'].to_s =~ /^Success$/i &&
12
+ !(self['paymentExecStatus'].to_s =~ /^ERROR$/i))
13
13
  end
14
14
 
15
15
  def errors
16
16
  if success?
17
17
  return []
18
18
  else
19
- self['error']
19
+ errors = self['error']
20
+ errors ||= self['payErrorList']['payError'].collect { |e| e['error'] } rescue nil
21
+ errors
20
22
  end
21
23
  end
22
24
 
23
25
  def error_message
24
- if success?
25
- return nil
26
- else
27
- self['error'].first['message'] rescue nil
28
- end
26
+ message = errors.first['message'] rescue nil
29
27
  end
30
28
 
31
29
  def approve_paypal_payment_url(type=nil)
32
30
  if self['payKey'].nil?
33
31
  return nil
34
32
  elsif ['mini', 'light'].include?(type.to_s)
35
- return "#{@@paypal_base_url}/webapps/adaptivepayment/flow/pay?expType=#{type.to_s}&paykey=#{self['payKey']}"
33
+ return "#{@paypal_base_url}/webapps/adaptivepayment/flow/pay?expType=#{type.to_s}&paykey=#{self['payKey']}"
36
34
  end
37
35
 
38
- "#{@@paypal_base_url}/webscr?cmd=_ap-payment&paykey=#{self['payKey']}"
36
+ "#{@paypal_base_url}/webscr?cmd=_ap-payment&paykey=#{self['payKey']}"
39
37
  end
40
38
 
41
39
  def preapproval_paypal_payment_url
42
- self['preapprovalKey'].nil? ? nil : "#{@@paypal_base_url}/webscr?cmd=_ap-preapproval&preapprovalkey=#{self['preapprovalKey']}"
40
+ self['preapprovalKey'].nil? ? nil : "#{@paypal_base_url}/webscr?cmd=_ap-preapproval&preapprovalkey=#{self['preapprovalKey']}"
43
41
  end
44
42
 
45
43
  # workaround for rails 3.1.1, see https://github.com/tc/paypal_adaptive/issues/23
@@ -1,4 +1,4 @@
1
1
  module PaypalAdaptive
2
- VERSION = "0.2.6"
2
+ VERSION = "0.2.7"
3
3
  end
4
4
 
@@ -0,0 +1,22 @@
1
+ {
2
+ "responseEnvelope":{
3
+ "timestamp":"2011-11-28T08:02:09.070-08:00",
4
+ "ack":"Success",
5
+ "correlationId":"XXXXXXXXXXXXX",
6
+ "build":"2279004"},
7
+ "payKey":"XX-XXXXXXXXXXXXXXXXX",
8
+ "paymentExecStatus":"ERROR",
9
+ "payErrorList":{
10
+ "payError":[{
11
+ "receiver":{
12
+ "amount":"25.0",
13
+ "email":"test@test.test"},
14
+ "error":{
15
+ "errorId":"580036",
16
+ "domain":"PLATFORM",
17
+ "severity":"Error",
18
+ "category":"Application",
19
+ "message":"This transaction cannot be processed. Please enter a valid credit card number and type"}
20
+ }]
21
+ }
22
+ }
@@ -15,7 +15,7 @@ class PayRequestTest < Test::Unit::TestCase
15
15
  pp_response = @pay_request.pay(data)
16
16
 
17
17
  puts "redirect url to\n #{pp_response.approve_paypal_payment_url}"
18
- assert pp_response.success?
18
+ assert_equal true, pp_response.success?
19
19
  end
20
20
 
21
21
  def test_invalid_simple_pay
@@ -24,7 +24,7 @@ class PayRequestTest < Test::Unit::TestCase
24
24
  data = read_json_file(data_filepath)
25
25
  pp_response = @pay_request.pay(data)
26
26
  puts pp_response.errors
27
- assert pp_response.success? == false
27
+ assert_equal false, pp_response.success?
28
28
  end
29
29
 
30
30
  def test_valid_chain_pay
@@ -40,7 +40,7 @@ class PayRequestTest < Test::Unit::TestCase
40
40
  puts pp_response.errors
41
41
  end
42
42
 
43
- assert pp_response.success?
43
+ assert_equal true, pp_response.success?
44
44
  end
45
45
 
46
46
  def test_invalid_chain_pay
@@ -49,7 +49,7 @@ class PayRequestTest < Test::Unit::TestCase
49
49
  data = read_json_file(data_filepath)
50
50
  pp_response = @pay_request.pay(data)
51
51
  puts pp_response.errors
52
- assert pp_response.success? == false
52
+ assert_equal false, pp_response.success?
53
53
  end
54
54
 
55
55
  def test_valid_parallel_pay
@@ -61,7 +61,7 @@ class PayRequestTest < Test::Unit::TestCase
61
61
  data = read_json_file(data_filepath)
62
62
  pp_response = @pay_request.pay(data)
63
63
  puts "redirect url to\n #{pp_response.approve_paypal_payment_url}"
64
- assert pp_response.success?
64
+ assert_equal true, pp_response.success?
65
65
  end
66
66
 
67
67
  def test_invalid_parallel_pay
@@ -70,7 +70,7 @@ class PayRequestTest < Test::Unit::TestCase
70
70
  data = read_json_file(data_filepath)
71
71
  pp_response = @pay_request.pay(data)
72
72
  puts pp_response.errors
73
- assert pp_response.success? == false
73
+ assert_equal false, pp_response.success?
74
74
  end
75
75
 
76
76
  def test_preapproval
@@ -48,6 +48,18 @@ class PreapprovalTest < Test::Unit::TestCase
48
48
  assert pp_response.success? == false
49
49
  end
50
50
 
51
+ def test_erred_preapproval_payment_message
52
+ puts "-------"
53
+ puts "invalid"
54
+ data_filepath = File.join(File.dirname(__FILE__),"..", "data","invalid_preapproval_payment.json")
55
+
56
+ data = read_json_file(data_filepath)
57
+ pp_response = @preapproval_request.preapproval(data)
58
+ puts "error message is #{pp_response.error_message}"
59
+
60
+ assert pp_response.error_message == "This transaction cannot be processed. Please enter a valid credit card number and type"
61
+ end
62
+
51
63
  def read_json_file(filepath)
52
64
  File.open(filepath, "rb"){|f| JSON.parse(f.read)}
53
65
  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.2.6
4
+ version: 0.2.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-23 00:00:00.000000000 Z
12
+ date: 2012-01-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
16
- requirement: &70119519086160 !ruby/object:Gem::Requirement
16
+ requirement: &70239312828620 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.6.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70119519086160
24
+ version_requirements: *70239312828620
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: jsonschema
27
- requirement: &70119519085000 !ruby/object:Gem::Requirement
27
+ requirement: &70239312840100 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 2.0.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70119519085000
35
+ version_requirements: *70239312840100
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &70119519084120 !ruby/object:Gem::Requirement
38
+ requirement: &70239312838340 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0.8'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70119519084120
46
+ version_requirements: *70239312838340
47
47
  description: Lightweight wrapper for Paypal's Adaptive Payments API
48
48
  email:
49
49
  - tommy.chheng@gmail.com
@@ -71,6 +71,7 @@ files:
71
71
  - test/data/invalid_parallel_pay_request.json
72
72
  - test/data/invalid_preapproval.json
73
73
  - test/data/invalid_preapproval_error_execstatus.json
74
+ - test/data/invalid_preapproval_payment.json
74
75
  - test/data/invalid_simple_pay_request_1.json
75
76
  - test/data/valid_chain_pay_request.json
76
77
  - test/data/valid_get_payment_options_request.json
@@ -117,6 +118,7 @@ test_files:
117
118
  - test/data/invalid_parallel_pay_request.json
118
119
  - test/data/invalid_preapproval.json
119
120
  - test/data/invalid_preapproval_error_execstatus.json
121
+ - test/data/invalid_preapproval_payment.json
120
122
  - test/data/invalid_simple_pay_request_1.json
121
123
  - test/data/valid_chain_pay_request.json
122
124
  - test/data/valid_get_payment_options_request.json