killbill 3.1.11 → 3.1.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/Jarfile +6 -6
  3. data/NEWS +6 -0
  4. data/VERSION +1 -1
  5. data/generators/active_merchant/templates/Jarfile.rb +7 -7
  6. data/generators/active_merchant/templates/db/ddl.sql.rb +2 -0
  7. data/generators/active_merchant/templates/db/schema.rb +2 -0
  8. data/generators/active_merchant/templates/lib/application.rb +1 -5
  9. data/generators/active_merchant/templates/lib/models/response.rb +2 -1
  10. data/generators/active_merchant/templates/lib/plugin.rb +1 -0
  11. data/generators/active_merchant/templates/lib/private_api.rb +7 -0
  12. data/generators/active_merchant/templates/plugin.gemspec.rb +3 -2
  13. data/generators/active_merchant/templates/spec/integration_spec.rb +103 -2
  14. data/killbill.gemspec +4 -1
  15. data/lib/killbill/gen/api/dry_run_arguments.rb +100 -0
  16. data/lib/killbill/gen/api/invoice_user_api.rb +5 -5
  17. data/lib/killbill/gen/api/killbill_api.rb +19 -18
  18. data/lib/killbill/gen/api/payment_options.rb +17 -5
  19. data/lib/killbill/gen/api/require_gen.rb +5 -0
  20. data/lib/killbill/gen/api/rolled_up_unit.rb +63 -0
  21. data/lib/killbill/gen/api/rolled_up_usage.rb +31 -35
  22. data/lib/killbill/gen/api/static_catalog.rb +8 -8
  23. data/lib/killbill/gen/api/subscription_usage_record.rb +75 -0
  24. data/lib/killbill/gen/api/unit_usage_record.rb +74 -0
  25. data/lib/killbill/gen/api/usage_record.rb +66 -0
  26. data/lib/killbill/gen/api/usage_user_api.rb +22 -56
  27. data/lib/killbill/gen/plugin-api/currency_plugin_api.rb +19 -18
  28. data/lib/killbill/gen/plugin-api/ext_bus_event.rb +19 -18
  29. data/lib/killbill/gen/plugin-api/gateway_notification.rb +19 -18
  30. data/lib/killbill/gen/plugin-api/hosted_payment_page_form_descriptor.rb +19 -18
  31. data/lib/killbill/gen/plugin-api/notification_plugin_api.rb +19 -18
  32. data/lib/killbill/gen/plugin-api/payment_method_info_plugin.rb +19 -18
  33. data/lib/killbill/gen/plugin-api/payment_plugin_api.rb +19 -18
  34. data/lib/killbill/gen/plugin-api/payment_plugin_api_exception.rb +19 -18
  35. data/lib/killbill/gen/plugin-api/payment_transaction_info_plugin.rb +19 -18
  36. data/lib/killbill/gen/plugin-api/require_gen.rb +19 -18
  37. data/lib/killbill/helpers/active_merchant/active_record/models/payment_method.rb +4 -0
  38. data/lib/killbill/helpers/active_merchant/active_record/models/response.rb +65 -34
  39. data/lib/killbill/helpers/active_merchant/active_record/models/streamy_result_set.rb +14 -2
  40. data/lib/killbill/helpers/active_merchant/active_record/models/transaction.rb +9 -5
  41. data/lib/killbill/helpers/active_merchant/killbill_spec_helper.rb +28 -23
  42. data/lib/killbill/helpers/active_merchant/payment_plugin.rb +66 -36
  43. data/lib/killbill/helpers/active_merchant/private_payment_plugin.rb +18 -31
  44. data/lib/killbill/helpers/active_merchant/sinatra.rb +7 -2
  45. data/lib/killbill/helpers/active_merchant/utils.rb +12 -0
  46. data/spec/killbill/helpers/killbill_spec_helper_spec.rb +24 -0
  47. data/spec/killbill/helpers/payment_method_spec.rb +5 -0
  48. data/spec/killbill/helpers/payment_plugin_spec.rb +33 -2
  49. data/spec/killbill/helpers/private_payment_plugin_spec.rb +83 -0
  50. data/spec/killbill/helpers/response_spec.rb +47 -24
  51. data/spec/killbill/helpers/streamy_result_set_spec.rb +38 -0
  52. data/spec/killbill/helpers/test_schema.rb +2 -0
  53. metadata +57 -8
  54. data/lib/killbill/gen/api/payment_attempt.rb +0 -117
  55. data/lib/killbill/gen/api/payment_method_kv_info.rb +0 -71
  56. data/lib/killbill/gen/plugin-api/billing_address.rb +0 -85
  57. data/lib/killbill/gen/plugin-api/customer.rb +0 -73
@@ -3,8 +3,10 @@ module Killbill
3
3
  module ActiveMerchant
4
4
  require 'action_controller'
5
5
  require 'action_view'
6
+ require 'active_merchant'
6
7
  require 'active_support'
7
8
  require 'cgi'
9
+ require 'offsite_payments'
8
10
 
9
11
  class PrivatePaymentPlugin < ::Killbill::Plugin::Payment
10
12
 
@@ -13,7 +15,7 @@ module Killbill
13
15
  include ::ActionController::RequestForgeryProtection
14
16
  include ::ActionView::Context
15
17
  include ::ActionView::Helpers::FormTagHelper
16
- include ::ActiveMerchant::Billing::Integrations::ActionViewHelper
18
+ include ::OffsitePayments::ActionViewHelper
17
19
 
18
20
  # For RequestForgeryProtection
19
21
  attr_reader :session
@@ -33,7 +35,7 @@ module Killbill
33
35
  #
34
36
  # Additionally, you can have a :html key which will be passed through to the link_to helper
35
37
  def payment_link_for(name, order_id, account_id, service, options = {})
36
- integration_module = ::ActiveMerchant::Billing::Integrations.const_get(service.to_s.camelize)
38
+ integration_module = ::OffsitePayments.integration(service.to_s.camelize)
37
39
  service_class = integration_module.const_get('Helper')
38
40
 
39
41
  link_options = options.delete(:html) || {}
@@ -69,36 +71,13 @@ module Killbill
69
71
  output_buffer
70
72
  end
71
73
 
72
- protected
74
+ def save_response_and_transaction(gw_response, api_call, kb_account_id, kb_tenant_id, payment_processor_account_id, kb_payment_id=nil, kb_payment_transaction_id=nil, transaction_type=nil, amount_in_cents=0, currency=nil)
75
+ logger.warn "Unsuccessful #{api_call}: #{gw_response.message}" unless gw_response.success?
73
76
 
74
- def reset_output_buffer
75
- @output_buffer = ''
76
- end
77
+ response, transaction = @response_model.create_response_and_transaction(@identifier, @transaction_model, api_call, kb_account_id, kb_payment_id, kb_payment_transaction_id, transaction_type, payment_processor_account_id, kb_tenant_id, gw_response, amount_in_cents, currency, {}, @response_model)
77
78
 
78
- def save_response(response, api_call)
79
- save_response_and_transaction(response, api_call)[0]
80
- end
79
+ logger.debug "Recorded transaction: #{transaction.inspect}" unless transaction.nil?
81
80
 
82
- def save_response_and_transaction(response, api_call, kb_payment_id=nil, amount_in_cents=0, currency=nil)
83
- logger.warn "Unsuccessful #{api_call}: #{response.message}" unless response.success?
84
-
85
- # Save the response to our logs
86
- response = @response_model.from_response(api_call, kb_payment_id, response)
87
- response.save!
88
-
89
- transaction = nil
90
- txn_id = response.txn_id
91
- if response.success and !kb_payment_id.blank? and !txn_id.blank?
92
- # Record the transaction
93
- transaction = response.send("create_#{@identifier}_transaction!",
94
- :amount_in_cents => amount_in_cents,
95
- :currency => currency,
96
- :api_call => api_call,
97
- :kb_payment_id => kb_payment_id,
98
- :txn_id => txn_id)
99
-
100
- logger.debug "Recorded transaction: #{transaction.inspect}"
101
- end
102
81
  return response, transaction
103
82
  end
104
83
 
@@ -106,13 +85,21 @@ module Killbill
106
85
  ::Killbill::Plugin::ActiveMerchant.kb_apis
107
86
  end
108
87
 
109
- def gateway
110
- ::Killbill::Plugin::ActiveMerchant.gateway
88
+ def gateway(payment_processor_account_id=:default)
89
+ gateway = ::Killbill::Plugin::ActiveMerchant.gateways[payment_processor_account_id.to_sym]
90
+ raise "Unable to lookup gateway for payment_processor_account_id #{payment_processor_account_id}, gateways: #{::Killbill::Plugin::ActiveMerchant.gateways}" if gateway.nil?
91
+ gateway
111
92
  end
112
93
 
113
94
  def logger
114
95
  ::Killbill::Plugin::ActiveMerchant.logger
115
96
  end
97
+
98
+ protected
99
+
100
+ def reset_output_buffer
101
+ @output_buffer = ''
102
+ end
116
103
  end
117
104
  end
118
105
  end
@@ -22,9 +22,14 @@ module Killbill
22
22
 
23
23
  after do
24
24
  # return DB connections to the Pool if required
25
- ::ActiveRecord::Base.connection.close
25
+ pool = ::ActiveRecord::Base.connection_pool
26
+ if pool.active_connection?
27
+ connection = ::ActiveRecord::Base.connection
28
+ pool.remove(connection)
29
+ connection.disconnect!
30
+ end
26
31
  end
27
32
  end
28
33
  end
29
34
  end
30
- end
35
+ end
@@ -17,6 +17,18 @@ module Killbill
17
17
  no_hyphens.insert(8, "-").insert(13, "-").insert(18, "-").insert(23, "-")
18
18
  end
19
19
 
20
+ def self.ip
21
+ first_public_ipv4 ? first_public_ipv4.ip_address : first_private_ipv4.ip_address
22
+ end
23
+
24
+ def self.first_private_ipv4
25
+ Socket.ip_address_list.detect { |intf| intf.ipv4_private? }
26
+ end
27
+
28
+ def self.first_public_ipv4
29
+ Socket.ip_address_list.detect { |intf| intf.ipv4? and !intf.ipv4_loopback? and !intf.ipv4_multicast? and !intf.ipv4_private? }
30
+ end
31
+
20
32
  class KBWiredumpDevice < IO
21
33
 
22
34
  # Required for compatibility, but unused
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe Killbill::Plugin::ActiveMerchant::RSpec do
4
+ include Killbill::Plugin::ActiveMerchant::RSpec
5
+
6
+ it 'should build payment method properties' do
7
+ overridden_city = SecureRandom.uuid
8
+
9
+ props1 = build_pm_properties
10
+ props2 = build_pm_properties(nil, {:city => overridden_city})
11
+ props3 = build_pm_properties(nil, {:city => overridden_city, :foo => :bar})
12
+
13
+ props1.size.should == props2.size
14
+ props3.size.should == props1.size + 1
15
+
16
+ city1 = props1.find { |p| p.key == 'city' }
17
+ city2 = props2.find { |p| p.key == 'city' }
18
+ city3 = props3.find { |p| p.key == 'city' }
19
+ city1.value.should_not == city2.value
20
+ city2.value.should == city3.value
21
+
22
+ (props3.find { |p| p.key == :foo }).value.should == :bar
23
+ end
24
+ end
@@ -149,6 +149,11 @@ describe Killbill::Plugin::ActiveMerchant::ActiveRecord::PaymentMethod do
149
149
  expect { ::Killbill::Test::TestPaymentMethod.from_kb_payment_method_id(SecureRandom.uuid, kb_tenant_id) }.to raise_error
150
150
  ::Killbill::Test::TestPaymentMethod.from_kb_payment_method_id(kb_payment_method_id, kb_tenant_id).kb_payment_method_id.should == kb_payment_method_id
151
151
 
152
+ # Retrieve by account id and token
153
+ pms = ::Killbill::Test::TestPaymentMethod.from_kb_account_id_and_token(token, kb_account_id, kb_tenant_id)
154
+ pms.size.should == 1
155
+ pms[0].kb_payment_method_id.should == kb_payment_method_id
156
+
152
157
  # Delete the payment method and verify we cannot find it anymore
153
158
  ::Killbill::Test::TestPaymentMethod.mark_as_deleted!(kb_payment_method_id, kb_tenant_id)
154
159
  ::Killbill::Test::TestPaymentMethod.from_kb_account_id(kb_account_id, kb_tenant_id).size.should == 0
@@ -10,7 +10,10 @@ describe Killbill::Plugin::ActiveMerchant::PaymentPlugin do
10
10
  file = File.new(File.join(dir, 'test.yml'), "w+")
11
11
  file.write(<<-eos)
12
12
  :test:
13
- :test: true
13
+ - :account_id: default
14
+ :test: true
15
+ - :account_id: something_non_standard
16
+ :test: true
14
17
  # As defined by spec_helper.rb
15
18
  :database:
16
19
  :adapter: 'sqlite3'
@@ -49,6 +52,12 @@ describe Killbill::Plugin::ActiveMerchant::PaymentPlugin do
49
52
  property.value = 'true'
50
53
  @properties = [property]
51
54
 
55
+ @ppai = ::Killbill::Plugin::Model::PluginProperty.new
56
+ @ppai.key = 'payment_processor_account_id'
57
+ @ppai.value = 'something_non_standard'
58
+ @properties_with_ppai = @properties.dup
59
+ @properties_with_ppai << @ppai
60
+
52
61
  token = ::Killbill::Plugin::Model::PluginProperty.new
53
62
  token.key = 'token'
54
63
  token.value = SecureRandom.uuid
@@ -101,9 +110,28 @@ describe Killbill::Plugin::ActiveMerchant::PaymentPlugin do
101
110
  @plugin.get_payment_methods(@kb_account_id, true, @properties, @call_context).size.should == 0
102
111
  end
103
112
 
113
+ it 'should support different payment_processor_account_ids' do
114
+ @plugin.get_payment_methods(@kb_account_id, true, @properties, @call_context).size.should == 0
115
+
116
+ @plugin.add_payment_method(@kb_account_id, @kb_payment_method_id, @payment_method_props, true, @properties, @call_context)
117
+ @plugin.get_payment_methods(@kb_account_id, true, @properties, @call_context).size.should == 1
118
+ ::Killbill::Test::TestPaymentMethod.where(:kb_payment_method_id => @kb_payment_method_id).first.token.should == @payment_method_props.properties[0].value
119
+
120
+ authorization_id = SecureRandom.uuid
121
+ @payment_api.add_payment(@kb_payment_id, authorization_id, SecureRandom.uuid, :AUTHORIZE)
122
+ authorization = @plugin.authorize_payment(@kb_account_id, @kb_payment_id, authorization_id, @kb_payment_method_id, @amount_in_cents, @currency, @properties_with_ppai, @call_context)
123
+ verify_transaction_info_plugin(authorization, authorization_id, :AUTHORIZE, 1, @ppai.value)
124
+
125
+ capture_id = SecureRandom.uuid
126
+ @payment_api.add_payment(@kb_payment_id, capture_id, SecureRandom.uuid, :CAPTURE)
127
+ # We omit the payment_processor_account_id to verify we can retrieve it
128
+ capture = @plugin.capture_payment(@kb_account_id, @kb_payment_id, capture_id, @kb_payment_method_id, @amount_in_cents, @currency, @properties, @call_context)
129
+ verify_transaction_info_plugin(capture, capture_id, :CAPTURE, 2, @ppai.value)
130
+ end
131
+
104
132
  private
105
133
 
106
- def verify_transaction_info_plugin(t_info_plugin, kb_transaction_id, type, transaction_nb)
134
+ def verify_transaction_info_plugin(t_info_plugin, kb_transaction_id, type, transaction_nb, payment_processor_account_id='default')
107
135
  t_info_plugin.kb_payment_id.should == @kb_payment_id
108
136
  t_info_plugin.kb_transaction_payment_id.should == kb_transaction_id
109
137
  t_info_plugin.transaction_type.should == type
@@ -116,6 +144,9 @@ describe Killbill::Plugin::ActiveMerchant::PaymentPlugin do
116
144
  end
117
145
  t_info_plugin.status.should == :PROCESSED
118
146
 
147
+ # Verify we routed to the right gateway
148
+ (t_info_plugin.properties.find { |kv| kv.key.to_s == 'payment_processor_account_id' }).value.to_s.should == payment_processor_account_id
149
+
119
150
  transactions = @plugin.get_payment_info(@kb_account_id, @kb_payment_id, [], @call_context)
120
151
  transactions.size.should == transaction_nb
121
152
  transactions[transaction_nb - 1].to_json.should == t_info_plugin.to_json
@@ -0,0 +1,83 @@
1
+ require 'spec_helper'
2
+ require 'spec/killbill/helpers/payment_method_spec'
3
+ require 'spec/killbill/helpers/response_spec'
4
+ require 'spec/killbill/helpers/transaction_spec'
5
+
6
+ describe Killbill::Plugin::ActiveMerchant::PrivatePaymentPlugin do
7
+
8
+ before(:each) do
9
+ @order_id = SecureRandom.uuid
10
+ @account_id = SecureRandom.uuid
11
+ @options = {:amount => 120, :country => 'US', :forward_url => 'http://kill-bill.org', :html => {:authenticity_token => false}}
12
+ @session = {:foo => :bar}
13
+
14
+ setup_public_plugin
15
+
16
+ @plugin = Killbill::Plugin::ActiveMerchant::PrivatePaymentPlugin.new(:test,
17
+ ::Killbill::Test::TestPaymentMethod,
18
+ ::Killbill::Test::TestTransaction,
19
+ ::Killbill::Test::TestResponse,
20
+ @session)
21
+ @plugin.session.should == @session
22
+ end
23
+
24
+ it 'should build payment links' do
25
+ link = @plugin.payment_link_for('Pay!', @order_id, @account_id, :bogus, @options)
26
+ link.should == "<a account=\"#{@account_id}\" amount=\"#{@options[:amount]}\" authenticity_token=\"false\" href=\"http://www.bogus.com?order=#{@order_id}&amp;account=#{@account_id}&amp;amount=#{@options[:amount]}\">Pay!</a>"
27
+ end
28
+
29
+ it 'should build payment forms' do
30
+ form = @plugin.payment_form_for(@order_id, @account_id, :bogus, @options) do |service|
31
+ service.token = 'Pay!'
32
+ end
33
+ form.should == "<form accept-charset=\"UTF-8\" action=\"http://www.bogus.com\" disable_authenticity_token=\"true\" method=\"post\"><div style=\"display:none\"></div>
34
+ Pay!
35
+ <input id=\"order\" name=\"order\" type=\"hidden\" value=\"#{@order_id}\" />
36
+ <input id=\"account\" name=\"account\" type=\"hidden\" value=\"#{@account_id}\" />
37
+ <input id=\"amount\" name=\"amount\" type=\"hidden\" value=\"#{@options[:amount]}\" />
38
+ </form>"
39
+ end
40
+
41
+ it 'should save responses and transactions' do
42
+ response, transaction = @plugin.save_response_and_transaction(::ActiveMerchant::Billing::Response.new(true, 'OK'), :custom_thinggy, @account_id, SecureRandom.uuid, :default, SecureRandom.uuid, SecureRandom.uuid, :op, 1242, 'USD')
43
+
44
+ response.api_call.should == :custom_thinggy
45
+ transaction.transaction_type.should == :op
46
+ end
47
+
48
+ it 'should access global variables' do
49
+ @plugin.kb_apis.is_a?(::Killbill::Plugin::KillbillApi).should be_true
50
+ @plugin.gateway.is_a?(::Killbill::Plugin::ActiveMerchant::Gateway).should be_true
51
+ @plugin.logger.respond_to?(:info).should be_true
52
+ end
53
+
54
+ private
55
+
56
+ def setup_public_plugin
57
+ Dir.mktmpdir do |dir|
58
+ file = File.new(File.join(dir, 'test.yml'), 'w+')
59
+ file.write(<<-eos)
60
+ :test:
61
+ :test: true
62
+ # As defined by spec_helper.rb
63
+ :database:
64
+ :adapter: 'sqlite3'
65
+ :database: 'test.db'
66
+ eos
67
+ file.close
68
+
69
+ plugin = ::Killbill::Plugin::ActiveMerchant::PaymentPlugin.new(Proc.new { |config| nil },
70
+ :test,
71
+ ::Killbill::Test::TestPaymentMethod,
72
+ ::Killbill::Test::TestTransaction,
73
+ ::Killbill::Test::TestResponse)
74
+ payment_api = ::Killbill::Plugin::ActiveMerchant::RSpec::FakeJavaPaymentApi.new
75
+ plugin.kb_apis = ::Killbill::Plugin::KillbillApi.new('test', {:payment_api => payment_api})
76
+ plugin.logger = Logger.new(STDOUT)
77
+ plugin.conf_dir = File.dirname(file)
78
+
79
+ # Start the plugin here - since the config file will be deleted
80
+ plugin.start_plugin
81
+ end
82
+ end
83
+ end
@@ -20,24 +20,26 @@ describe Killbill::Plugin::ActiveMerchant::ActiveRecord::Response do
20
20
  end
21
21
 
22
22
  it 'should construct responses correctly' do
23
- api_call = 'for debugging only'
24
- kb_account_id = SecureRandom.uuid
25
- kb_payment_id = SecureRandom.uuid
26
- kb_payment_transaction_id = SecureRandom.uuid
27
- transaction_type = :PURCHASE
28
- kb_tenant_id = SecureRandom.uuid
29
- response = ::ActiveMerchant::Billing::Response.new(true, 'Message', {}, {
23
+ api_call = 'for debugging only'
24
+ kb_account_id = SecureRandom.uuid
25
+ kb_payment_id = SecureRandom.uuid
26
+ kb_payment_transaction_id = SecureRandom.uuid
27
+ transaction_type = :PURCHASE
28
+ payment_processor_account_id = 'petit_poucet'
29
+ kb_tenant_id = SecureRandom.uuid
30
+ response = ::ActiveMerchant::Billing::Response.new(true, 'Message', {}, {
30
31
  :authorization => SecureRandom.uuid,
31
32
  :avs_result => ::ActiveMerchant::Billing::AVSResult.new(:code => 'P')
32
33
  })
33
34
 
34
- r = ::Killbill::Test::TestResponse.from_response(api_call, kb_account_id, kb_payment_id, kb_payment_transaction_id, transaction_type, kb_tenant_id, response, {}, ::Killbill::Test::TestResponse)
35
+ r = ::Killbill::Test::TestResponse.from_response(api_call, kb_account_id, kb_payment_id, kb_payment_transaction_id, transaction_type, payment_processor_account_id, kb_tenant_id, response, {}, ::Killbill::Test::TestResponse)
35
36
  r.api_call.should == api_call
36
- r.kb_account_id.should ==kb_account_id
37
- r.kb_payment_id.should ==kb_payment_id
38
- r.kb_payment_transaction_id.should ==kb_payment_transaction_id
39
- r.transaction_type.should ==transaction_type
40
- r.kb_tenant_id.should ==kb_tenant_id
37
+ r.kb_account_id.should == kb_account_id
38
+ r.kb_payment_id.should == kb_payment_id
39
+ r.kb_payment_transaction_id.should == kb_payment_transaction_id
40
+ r.transaction_type.should == transaction_type
41
+ r.payment_processor_account_id.should == payment_processor_account_id
42
+ r.kb_tenant_id.should == kb_tenant_id
41
43
  r.message.should == response.message
42
44
  r.authorization.should == response.authorization
43
45
  r.fraud_review.should == response.fraud_review?
@@ -63,26 +65,27 @@ describe Killbill::Plugin::ActiveMerchant::ActiveRecord::Response do
63
65
  ptip.gateway_error_code.should be_nil
64
66
  ptip.first_payment_reference_id.should be_nil
65
67
  ptip.second_payment_reference_id.should be_nil
66
- ptip.properties.size.should == 11
68
+ ptip.properties.size.should == 12
67
69
  end
68
70
 
69
71
  it 'should create responses and transactions correctly' do
70
- api_call = 'for debugging only'
71
- kb_account_id = SecureRandom.uuid
72
- kb_payment_id = SecureRandom.uuid
73
- kb_payment_transaction_id = SecureRandom.uuid
74
- transaction_type = :PURCHASE
75
- kb_tenant_id = SecureRandom.uuid
76
- success_response = ::ActiveMerchant::Billing::Response.new(true, 'Message', {}, {
72
+ api_call = 'for debugging only'
73
+ kb_account_id = SecureRandom.uuid
74
+ kb_payment_id = SecureRandom.uuid
75
+ kb_payment_transaction_id = SecureRandom.uuid
76
+ transaction_type = :PURCHASE
77
+ payment_processor_account_id = 'petit_poucet'
78
+ kb_tenant_id = SecureRandom.uuid
79
+ success_response = ::ActiveMerchant::Billing::Response.new(true, 'Message', {}, {
77
80
  :authorization => SecureRandom.uuid,
78
81
  :avs_result => ::ActiveMerchant::Billing::AVSResult.new(:code => 'P')
79
82
  })
80
- failure_response = ::ActiveMerchant::Billing::Response.new(false, 'Message', {}, {
83
+ failure_response = ::ActiveMerchant::Billing::Response.new(false, 'Message', {}, {
81
84
  :authorization => SecureRandom.uuid,
82
85
  :avs_result => ::ActiveMerchant::Billing::AVSResult.new(:code => 'P')
83
86
  })
84
87
 
85
- response, transaction = ::Killbill::Test::TestResponse.create_response_and_transaction('test', ::Killbill::Test::TestTransaction, api_call, kb_account_id, kb_payment_id, kb_payment_transaction_id, transaction_type, kb_tenant_id, success_response, 120, 'USD', {}, ::Killbill::Test::TestResponse)
88
+ response, transaction = ::Killbill::Test::TestResponse.create_response_and_transaction('test', ::Killbill::Test::TestTransaction, api_call, kb_account_id, kb_payment_id, kb_payment_transaction_id, transaction_type, payment_processor_account_id, kb_tenant_id, success_response, 120, 'USD', {}, ::Killbill::Test::TestResponse)
86
89
  found_response = ::Killbill::Test::TestResponse.find(response.id)
87
90
  found_response.should == response
88
91
  found_response.test_transaction.should == transaction
@@ -90,11 +93,31 @@ describe Killbill::Plugin::ActiveMerchant::ActiveRecord::Response do
90
93
  found_transaction.should == transaction
91
94
  found_transaction.test_response.should == response
92
95
 
93
- response, transaction = ::Killbill::Test::TestResponse.create_response_and_transaction('test', ::Killbill::Test::TestTransaction, api_call, kb_account_id, kb_payment_id, kb_payment_transaction_id, transaction_type, kb_tenant_id, failure_response, 120, 'USD', {}, ::Killbill::Test::TestResponse)
96
+ response, transaction = ::Killbill::Test::TestResponse.create_response_and_transaction('test', ::Killbill::Test::TestTransaction, api_call, kb_account_id, kb_payment_id, kb_payment_transaction_id, transaction_type, payment_processor_account_id, kb_tenant_id, failure_response, 120, 'USD', {}, ::Killbill::Test::TestResponse)
94
97
  transaction.should be_nil
95
98
  found_response = ::Killbill::Test::TestResponse.find(response.id)
96
99
  found_response.should == response
97
100
  found_response.test_transaction.should be_nil
101
+
102
+ # Lookup responses for kb_payment_id
103
+ responses = ::Killbill::Test::TestResponse.responses_from_kb_payment_id(transaction_type, kb_payment_id, kb_tenant_id)
104
+ responses.size.should == 2
105
+ responses[0].success.should be_true
106
+ responses[1].success.should be_false
107
+
108
+ # Lookup responses for kb_payment_transaction_id
109
+ responses = ::Killbill::Test::TestResponse.responses_from_kb_payment_transaction_id(transaction_type, kb_payment_transaction_id, kb_tenant_id)
110
+ responses.size.should == 2
111
+ responses[0].success.should be_true
112
+ responses[1].success.should be_false
113
+
114
+ # Dummy queries
115
+ ::Killbill::Test::TestResponse.responses_from_kb_payment_id(:foo, kb_payment_id, kb_tenant_id).size.should == 0
116
+ ::Killbill::Test::TestResponse.responses_from_kb_payment_id(transaction_type, SecureRandom.uuid, kb_tenant_id).size.should == 0
117
+ ::Killbill::Test::TestResponse.responses_from_kb_payment_id(transaction_type, kb_payment_id, SecureRandom.uuid).size.should == 0
118
+ ::Killbill::Test::TestResponse.responses_from_kb_payment_transaction_id(:foo, kb_payment_transaction_id, kb_tenant_id).size.should == 0
119
+ ::Killbill::Test::TestResponse.responses_from_kb_payment_transaction_id(transaction_type, SecureRandom.uuid, kb_tenant_id).size.should == 0
120
+ ::Killbill::Test::TestResponse.responses_from_kb_payment_transaction_id(transaction_type, kb_payment_transaction_id, SecureRandom.uuid).size.should == 0
98
121
  end
99
122
 
100
123
  it 'should generate the right SQL query' do
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+ require 'spec/killbill/helpers/payment_method_spec'
3
+
4
+ describe Killbill::Plugin::ActiveMerchant::ActiveRecord do
5
+
6
+ before(:all) do
7
+ ::Killbill::Test::TestPaymentMethod.delete_all
8
+ end
9
+
10
+ it 'should stream results per batch' do
11
+ 1.upto(35) do
12
+ ::Killbill::Test::TestPaymentMethod.create(:kb_account_id => SecureRandom.uuid,
13
+ :kb_payment_method_id => SecureRandom.uuid,
14
+ :kb_tenant_id => SecureRandom.uuid,
15
+ :token => SecureRandom.uuid)
16
+ end
17
+ ::Killbill::Test::TestPaymentMethod.count.should == 35
18
+
19
+ enum = ::Killbill::Plugin::ActiveMerchant::ActiveRecord::StreamyResultSet.new(40, 10) do |offset, limit|
20
+ ::Killbill::Test::TestPaymentMethod.where('kb_payment_method_id is not NULL')
21
+ .order('id ASC')
22
+ .offset(offset)
23
+ .limit(limit)
24
+ end
25
+
26
+ i = 0
27
+ enum.each do |results|
28
+ if i < 3
29
+ results.size.should == 10
30
+ elsif i == 3
31
+ results.size.should == 5
32
+ else
33
+ fail 'Too many results'
34
+ end
35
+ i += 1
36
+ end
37
+ end
38
+ end