killbill-paypal-express 1.6.1 → 1.6.2

Sign up to get free protection for your applications and to get access to all the features.
data/NEWS CHANGED
@@ -1,3 +1,6 @@
1
+ 1.6.2
2
+ Add implementation for currency conversion
3
+
1
4
  1.6.1
2
5
  Update killbill version (1.8.3)
3
6
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.6.1
1
+ 1.6.2
data/db/ddl.sql CHANGED
@@ -20,6 +20,7 @@ CREATE TABLE `paypal_express_transactions` (
20
20
  `kb_payment_id` varchar(255) NOT NULL,
21
21
  `paypal_express_txn_id` varchar(255) NOT NULL,
22
22
  `amount_in_cents` int(11) NOT NULL,
23
+ `currency` char(3) NOT NULL,
23
24
  `created_at` datetime NOT NULL,
24
25
  `updated_at` datetime NOT NULL,
25
26
  PRIMARY KEY (`id`),
data/db/schema.rb CHANGED
@@ -21,6 +21,7 @@ ActiveRecord::Schema.define(:version => 20130311153635) do
21
21
  t.string "kb_payment_id", :null => false
22
22
  t.string "paypal_express_txn_id", :null => false
23
23
  t.integer "amount_in_cents", :null => false
24
+ t.string "currency", :null => false
24
25
  t.datetime "created_at", :null => false
25
26
  t.datetime "updated_at", :null => false
26
27
  end
@@ -34,9 +34,12 @@ module Killbill::PaypalExpress
34
34
  options[:reference_id] = payment_method.paypal_express_baid
35
35
  end
36
36
 
37
+ # Check for currency conversion
38
+ actual_amount, actual_currency = convert_amount_currency_if_required(amount_in_cents, currency, kb_payment_id)
39
+
37
40
  # Go to Paypal (DoReferenceTransaction call)
38
- paypal_response = @gateway.reference_transaction amount_in_cents, options
39
- response = save_response_and_transaction paypal_response, :charge, kb_payment_id, amount_in_cents
41
+ paypal_response = @gateway.reference_transaction actual_amount, options
42
+ response = save_response_and_transaction paypal_response, :charge, kb_payment_id, actual_amount, actual_currency
40
43
 
41
44
  response.to_payment_response
42
45
  end
@@ -59,9 +62,12 @@ module Killbill::PaypalExpress
59
62
 
60
63
  identification = paypal_express_transaction.paypal_express_txn_id
61
64
 
65
+ # Check for currency conversion
66
+ actual_amount, actual_currency = convert_amount_currency_if_required(amount_in_cents, currency, kb_payment_id)
67
+
62
68
  # Go to Paypal
63
- paypal_response = @gateway.refund amount_in_cents, identification, options
64
- response = save_response_and_transaction paypal_response, :refund, kb_payment_id, amount_in_cents
69
+ paypal_response = @gateway.refund actual_amount, identification, options
70
+ response = save_response_and_transaction paypal_response, :refund, kb_payment_id, actual_amount, actual_currency
65
71
 
66
72
  response.to_refund_response
67
73
  end
@@ -172,7 +178,31 @@ module Killbill::PaypalExpress
172
178
 
173
179
  private
174
180
 
175
- def save_response_and_transaction(paypal_express_response, api_call, kb_payment_id=nil, amount_in_cents=0)
181
+ def convert_amount_currency_if_required(input_amount, input_currency, kb_payment_id)
182
+
183
+ converted_currency = Killbill::PaypalExpress.converted_currency(input_currency)
184
+ return [input_amount, input_currency] if converted_currency.nil?
185
+
186
+ kb_payment = @kb_apis.payment_api.get_payment(kb_payment_id, false, @kb_apis.create_context)
187
+
188
+ currency_conversion = @kb_apis.currency_conversion_api.get_currency_conversion(input_currency, kb_payment.effective_date)
189
+ rates = currency_conversion.rates
190
+ found = rates.select do |r|
191
+ r.currency.to_s.upcase.to_sym == converted_currency.to_s.upcase.to_sym
192
+ end
193
+
194
+ if found.nil? || found.empty?
195
+ @logger.warn "Failed to find converted currency #{converted_currency} for input currency #{input_currency}"
196
+ return [input_amount, input_currency] if converted_currency.nil?
197
+ end
198
+
199
+ # conversion rounding ?
200
+ conversion_rate = found[0].value
201
+ output_amount = input_amount * conversion_rate
202
+ return [output_amount.to_i, converted_currency]
203
+ end
204
+
205
+ def save_response_and_transaction(paypal_express_response, api_call, kb_payment_id=nil, amount_in_cents=0, currency=nil)
176
206
  @logger.warn "Unsuccessful #{api_call}: #{paypal_express_response.message}" unless paypal_express_response.success?
177
207
 
178
208
  # Save the response to our logs
@@ -181,7 +211,11 @@ module Killbill::PaypalExpress
181
211
 
182
212
  if response.success and !kb_payment_id.blank? and !response.authorization.blank?
183
213
  # Record the transaction
184
- transaction = response.create_paypal_express_transaction!(:amount_in_cents => amount_in_cents, :api_call => api_call, :kb_payment_id => kb_payment_id, :paypal_express_txn_id => response.authorization)
214
+ transaction = response.create_paypal_express_transaction!(:amount_in_cents => amount_in_cents,
215
+ :currency => currency,
216
+ :api_call => api_call,
217
+ :kb_payment_id => kb_payment_id,
218
+ :paypal_express_txn_id => response.authorization)
185
219
  @logger.debug "Recorded transaction: #{transaction.inspect}"
186
220
  end
187
221
  response
@@ -7,6 +7,7 @@ module Killbill::PaypalExpress
7
7
  mattr_reader :paypal_sandbox_url
8
8
  mattr_reader :paypal_production_url
9
9
  mattr_reader :paypal_payment_description
10
+ mattr_reader :currency_conversions
10
11
  mattr_reader :initialized
11
12
  mattr_reader :test
12
13
 
@@ -27,6 +28,8 @@ module Killbill::PaypalExpress
27
28
  @@gateway = Killbill::PaypalExpress::Gateway.instance
28
29
  @@gateway.configure(@@config[:paypal])
29
30
 
31
+ @@currency_conversions = @@config[:currency_conversions]
32
+
30
33
  if defined?(JRUBY_VERSION)
31
34
  # See https://github.com/jruby/activerecord-jdbc-adapter/issues/302
32
35
  require 'jdbc/mysql'
@@ -38,4 +41,10 @@ module Killbill::PaypalExpress
38
41
 
39
42
  @@initialized = true
40
43
  end
44
+
45
+ def self.converted_currency(currency)
46
+ currency_sym = currency.to_s.upcase.to_sym
47
+ @@currency_conversions && @@currency_conversions[currency_sym]
48
+ end
49
+
41
50
  end
@@ -131,11 +131,13 @@ module Killbill::PaypalExpress
131
131
  if paypal_express_transaction.nil?
132
132
  # payment_info_grossamount is e.g. "100.00" - we need to convert it in cents
133
133
  amount_in_cents = payment_info_grossamount ? (payment_info_grossamount.to_f * 100).to_i : nil
134
+ currency = nill
134
135
  created_date = created_at
135
136
  first_payment_reference_id = nil
136
137
  second_payment_reference_id = nil
137
138
  else
138
139
  amount_in_cents = paypal_express_transaction.amount_in_cents
140
+ currency = paypal_express_transaction.currency
139
141
  created_date = paypal_express_transaction.created_at
140
142
  first_payment_reference_id = paypal_express_transaction.paypal_express_txn_id
141
143
  second_payment_reference_id = nil
@@ -148,6 +150,7 @@ module Killbill::PaypalExpress
148
150
  if type == :payment
149
151
  p_info_plugin = Killbill::Plugin::Model::PaymentInfoPlugin.new
150
152
  p_info_plugin.amount = BigDecimal.new(amount_in_cents.to_s) / 100.0 if amount_in_cents
153
+ p_info_plugin.amount = currency
151
154
  p_info_plugin.created_date = created_date
152
155
  p_info_plugin.effective_date = effective_date
153
156
  p_info_plugin.status = (success ? :PROCESSED : :ERROR)
@@ -159,6 +162,7 @@ module Killbill::PaypalExpress
159
162
  else
160
163
  r_info_plugin = Killbill::Plugin::Model::RefundInfoPlugin.new
161
164
  r_info_plugin.amount = BigDecimal.new(amount_in_cents.to_s) / 100.0 if amount_in_cents
165
+ r_info_plugin.amount = currency
162
166
  r_info_plugin.created_date = created_date
163
167
  r_info_plugin.effective_date = effective_date
164
168
  r_info_plugin.status = (success ? :PROCESSED : :ERROR)
@@ -2,6 +2,7 @@ module Killbill::PaypalExpress
2
2
  class PaypalExpressTransaction < ActiveRecord::Base
3
3
  belongs_to :paypal_express_response
4
4
  attr_accessible :amount_in_cents,
5
+ :currency,
5
6
  :api_call,
6
7
  :kb_payment_id,
7
8
  :paypal_express_txn_id
data/pom.xml CHANGED
@@ -25,7 +25,7 @@
25
25
  <groupId>com.ning.killbill.ruby</groupId>
26
26
  <artifactId>paypal-express-plugin</artifactId>
27
27
  <packaging>pom</packaging>
28
- <version>1.6.1</version>
28
+ <version>1.6.2</version>
29
29
  <name>paypal-express-plugin</name>
30
30
  <url>http://github.com/killbill/killbill-paypal-express-plugin</url>
31
31
  <description>Plugin for accessing paypal as a payment gateway</description>
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: killbill-paypal-express
3
3
  version: !ruby/object:Gem::Version
4
+ version: 1.6.2
4
5
  prerelease:
5
- version: 1.6.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Kill Bill core team
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-29 00:00:00.000000000 Z
12
+ date: 2013-10-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: killbill
@@ -213,9 +213,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
213
213
  - !ruby/object:Gem::Version
214
214
  segments:
215
215
  - 0
216
- hash: 2
217
216
  version: !binary |-
218
217
  MA==
218
+ hash: 2
219
219
  none: false
220
220
  requirements: []
221
221
  rubyforge_project: