killbill-litle 1.0.14 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -98,7 +98,9 @@ The plugin expects a `litle.yml` configuration file containing the following:
98
98
  :username: 'your-username'
99
99
  # Optional, if you are using PayPage
100
100
  :secure_page_url: 'litle-secure-page-url'
101
- :paypage_id: 'litle-paypage-id'
101
+ :paypage_id:
102
+ :USD: 'litle-paypage-id-USD'
103
+ :EUR: 'litle-paypage-id-EURO'
102
104
  :log_file: '/var/tmp/litle.log'
103
105
  # Switch to false for production
104
106
  :test: true
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.14
1
+ 1.1.0
@@ -1,7 +1,7 @@
1
1
  module Killbill::Litle
2
2
  class PaymentPlugin < Killbill::Plugin::Payment
3
3
  def start_plugin
4
- Killbill::Litle.initialize! @logger, @conf_dir
4
+ Killbill::Litle.initialize! @logger, @conf_dir, @kb_apis
5
5
  @gateway = Killbill::Litle.gateway
6
6
 
7
7
  super
@@ -113,7 +113,39 @@ module Killbill::Litle
113
113
  end
114
114
 
115
115
  def reset_payment_methods(kb_account_id, payment_methods)
116
- # No-op
116
+ return if payment_methods.nil?
117
+
118
+ litle_pms = LitlePaymentMethod.from_kb_account_id(kb_account_id.to_s)
119
+
120
+ payment_methods.delete_if do |payment_method_info_plugin|
121
+ should_be_deleted = false
122
+ litle_pms.each do |litle_pm|
123
+ # Do litle_pm and payment_method_info_plugin represent the same Litle payment method?
124
+ if litle_pm.external_payment_method_id == payment_method_info_plugin.external_payment_method_id
125
+ # Do we already have a kb_payment_method_id?
126
+ if litle_pm.kb_payment_method_id == payment_method_info_plugin.payment_method_id.to_s
127
+ should_be_deleted = true
128
+ break
129
+ elsif litle_pm.kb_payment_method_id.nil?
130
+ # We didn't have the kb_payment_method_id - update it
131
+ litle_pm.kb_payment_method_id = payment_method_info_plugin.payment_method_id.to_s
132
+ should_be_deleted = litle_pm.save
133
+ break
134
+ # Otherwise the same token points to 2 different kb_payment_method_id. This should never happen,
135
+ # but we cowardly will insert a second row below
136
+ end
137
+ end
138
+ end
139
+
140
+ should_be_deleted
141
+ end
142
+
143
+ # The remaining elements in payment_methods are not in our table (this should never happen?!)
144
+ payment_methods.each do |payment_method_info_plugin|
145
+ LitlePaymentMethod.create :kb_account_id => payment_method_info_plugin.account_id.to_s,
146
+ :kb_payment_method_id => payment_method_info_plugin.payment_method_id.to_s,
147
+ :litle_token => payment_method_info_plugin.external_payment_method_id
148
+ end
117
149
  end
118
150
 
119
151
  private
@@ -9,14 +9,30 @@ helpers do
9
9
  def plugin
10
10
  Killbill::Litle::PrivatePaymentPlugin.instance
11
11
  end
12
+
13
+ def required_parameter!(parameter_name, parameter_value, message='must be specified!')
14
+ halt 400, "#{parameter_name} #{message}" if parameter_value.blank?
15
+ end
12
16
  end
13
17
 
14
18
  # http://127.0.0.1:9292/plugins/killbill-litle
15
19
  get '/plugins/killbill-litle' do
20
+ kb_account_id = request.GET['kb_account_id']
21
+ required_parameter! :kb_account_id, kb_account_id
22
+
23
+ secure_page_url = Killbill::Litle.config[:litle][:secure_page_url]
24
+ required_parameter! :secure_page_url, secure_page_url, 'is not configured'
25
+
26
+ # Allow currency override if needed
27
+ currency = request.GET['currency'] || plugin.get_currency(kb_account_id)
28
+ paypage_id = Killbill::Litle.config[:litle][:paypage_id][currency.to_sym]
29
+ required_parameter! :paypage_id, paypage_id, "is not configured for currency #{currency.to_sym}"
30
+
16
31
  locals = {
17
- :secure_page_url => Killbill::Litle.config[:litle][:secure_page_url],
18
- :paypage_id => Killbill::Litle.config[:litle][:paypage_id],
19
- :kb_account_id => request.GET['kb_account_id'] || '1',
32
+ :currency => currency,
33
+ :secure_page_url => secure_page_url,
34
+ :paypage_id => paypage_id,
35
+ :kb_account_id => kb_account_id,
20
36
  :merchant_txn_id => request.GET['merchant_txn_id'] || '1',
21
37
  :order_id => request.GET['order_id'] || '1',
22
38
  :report_group => request.GET['report_group'] || 'Default Report Group',
@@ -30,7 +46,10 @@ post '/plugins/killbill-litle/checkout' do
30
46
  kb_account_id = request.POST['kb_account_id']
31
47
  response_paypage_registration_id = request.POST['response_paypage_registration_id']
32
48
 
33
- halt 400, "kb_account_id and response_paypage_registration_id must be specified!" if kb_account_id.blank? or response_paypage_registration_id.blank?
49
+ {
50
+ :kb_account_id => kb_account_id,
51
+ :response_paypage_registration_id => response_paypage_registration_id
52
+ }.each { |k, v| required_parameter! k, v }
34
53
 
35
54
  pm = plugin.register_token! kb_account_id, response_paypage_registration_id
36
55
  redirect "/plugins/killbill-litle/1.0/pms/#{pm.id}"
@@ -4,11 +4,13 @@ module Killbill::Litle
4
4
  mattr_reader :logger
5
5
  mattr_reader :config
6
6
  mattr_reader :gateway
7
+ mattr_reader :kb_apis
7
8
  mattr_reader :initialized
8
9
  mattr_reader :test
9
10
 
10
- def self.initialize!(logger=Logger.new(STDOUT), conf_dir=File.expand_path('../../../', File.dirname(__FILE__)))
11
+ def self.initialize!(logger=Logger.new(STDOUT), conf_dir=File.expand_path('../../../', File.dirname(__FILE__)), kb_apis = nil)
11
12
  @@logger = logger
13
+ @@kb_apis = kb_apis
12
14
 
13
15
  config_file = "#{conf_dir}/litle.yml"
14
16
  @@config = Properties.new(config_file)
@@ -28,4 +30,4 @@ module Killbill::Litle
28
30
 
29
31
  @@initialized = true
30
32
  end
31
- end
33
+ end
@@ -16,6 +16,8 @@ module Killbill::Litle
16
16
  :zip,
17
17
  :country
18
18
 
19
+ alias_attribute :external_payment_method_id, :litle_token
20
+
19
21
  def self.from_kb_account_id(kb_account_id)
20
22
  find_all_by_kb_account_id_and_is_deleted(kb_account_id, false)
21
23
  end
@@ -34,12 +36,8 @@ module Killbill::Litle
34
36
  end
35
37
 
36
38
  def to_payment_method_response
37
- external_payment_method_id = litle_token
38
- # No concept of default payment method in Litle
39
- is_default = false
40
-
41
39
  properties = []
42
- properties << Killbill::Plugin::Model::PaymentMethodKVInfo.new(false, "token", litle_token)
40
+ properties << Killbill::Plugin::Model::PaymentMethodKVInfo.new(false, 'token', litle_token)
43
41
 
44
42
  Killbill::Plugin::Model::PaymentMethodPlugin.new(external_payment_method_id,
45
43
  is_default,
@@ -60,13 +58,14 @@ module Killbill::Litle
60
58
  end
61
59
 
62
60
  def to_payment_method_info_response
63
- external_payment_method_id = litle_token
64
- # No concept of default payment method in Litle
65
- is_default = false
66
-
67
61
  Killbill::Plugin::Model::PaymentMethodInfoPlugin.new(kb_account_id, kb_payment_method_id, is_default, external_payment_method_id)
68
62
  end
69
63
 
64
+ def is_default
65
+ # No concept of default payment method in Litle
66
+ false
67
+ end
68
+
70
69
  def cc_name
71
70
  if cc_first_name and cc_last_name
72
71
  "#{cc_first_name} #{cc_last_name}"
@@ -14,6 +14,14 @@ module Killbill::Litle
14
14
  end
15
15
  end
16
16
 
17
+ def get_currency(kb_account_id_s)
18
+ kb_account_id = Killbill::Plugin::Model::UUID.new(kb_account_id_s)
19
+ account = kb_apis.get_account_by_id(kb_account_id)
20
+ account.currency.enum
21
+ rescue Killbill::Plugin::JKillbillApi::APINotAvailableError
22
+ 'USD'
23
+ end
24
+
17
25
  private
18
26
 
19
27
  def save_response(litle_response, api_call)
@@ -34,5 +42,10 @@ module Killbill::Litle
34
42
  # The logger should have been configured when the plugin started
35
43
  Killbill::Litle.logger
36
44
  end
45
+
46
+ def kb_apis
47
+ # The logger should have been configured when the plugin started
48
+ Killbill::Litle.kb_apis
49
+ end
37
50
  end
38
51
  end
data/pom.xml CHANGED
@@ -25,7 +25,7 @@
25
25
  <groupId>com.ning.killbill.ruby</groupId>
26
26
  <artifactId>litle-plugin</artifactId>
27
27
  <packaging>pom</packaging>
28
- <version>1.0.14</version>
28
+ <version>1.1.0</version>
29
29
  <name>litle-plugin</name>
30
30
  <url>http://github.com/killbill/killbill-litle-plugin</url>
31
31
  <description>Plugin for accessing Litle as a payment gateway</description>
@@ -8,10 +8,11 @@ describe Killbill::Litle::PaymentPlugin do
8
8
  :litle:
9
9
  :merchant_id: 'merchant_id'
10
10
  :password: 'password'
11
+ # As defined by spec_helper.rb
11
12
  :database:
12
13
  :adapter: 'sqlite3'
13
- :database: 'shouldntmatter.db'
14
- eos
14
+ :database: 'test.db'
15
+ eos
15
16
  file.close
16
17
 
17
18
  @plugin = Killbill::Litle::PaymentPlugin.new
@@ -23,7 +24,53 @@ eos
23
24
  end
24
25
  end
25
26
 
26
- it "should start and stop correctly" do
27
+ it 'should start and stop correctly' do
27
28
  @plugin.stop_plugin
28
29
  end
30
+
31
+ it 'should reset payment methods' do
32
+ kb_account_id = '129384'
33
+
34
+ @plugin.get_payment_methods(kb_account_id, false, nil).size.should == 0
35
+ verify_pms kb_account_id, 0
36
+
37
+ # Create a pm with a kb_payment_method_id
38
+ Killbill::Litle::LitlePaymentMethod.create :kb_account_id => kb_account_id,
39
+ :kb_payment_method_id => 'kb-1',
40
+ :litle_token => 'litle-1'
41
+ verify_pms kb_account_id, 1
42
+
43
+ # Add some in KillBill and reset
44
+ payment_methods = []
45
+ # Random order... Shouldn't matter...
46
+ payment_methods << Killbill::Plugin::Model::PaymentMethodInfoPlugin.new(kb_account_id, 'kb-3', false, 'litle-3')
47
+ payment_methods << Killbill::Plugin::Model::PaymentMethodInfoPlugin.new(kb_account_id, 'kb-2', false, 'litle-2')
48
+ payment_methods << Killbill::Plugin::Model::PaymentMethodInfoPlugin.new(kb_account_id, 'kb-4', false, 'litle-4')
49
+ @plugin.reset_payment_methods kb_account_id, payment_methods
50
+ verify_pms kb_account_id, 4
51
+
52
+ # Add a payment method without a kb_payment_method_id
53
+ Killbill::Litle::LitlePaymentMethod.create :kb_account_id => kb_account_id,
54
+ :litle_token => 'litle-5'
55
+ @plugin.get_payment_methods(kb_account_id, false, nil).size.should == 5
56
+
57
+ # Verify we can match it
58
+ payment_methods << Killbill::Plugin::Model::PaymentMethodInfoPlugin.new(kb_account_id, 'kb-5', false, 'litle-5')
59
+ @plugin.reset_payment_methods kb_account_id, payment_methods
60
+ verify_pms kb_account_id, 5
61
+
62
+ @plugin.stop_plugin
63
+ end
64
+
65
+ private
66
+
67
+ def verify_pms(kb_account_id, size)
68
+ pms = @plugin.get_payment_methods(kb_account_id, false, nil)
69
+ pms.size.should == size
70
+ pms.each do |pm|
71
+ pm.account_id.should == kb_account_id
72
+ pm.is_default.should == false
73
+ pm.external_payment_method_id.should == 'litle-' + pm.payment_method_id.split('-')[1]
74
+ end
75
+ end
29
76
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: killbill-litle
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.14
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-03 00:00:00.000000000 Z
12
+ date: 2013-06-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: killbill