Skip to content

Commit

Permalink
Worldpay: Format non-fractional currency amounts correctly
Browse files Browse the repository at this point in the history
#2084 introduced a
regression to the Worldpay gateway wherein it was no longer implementing
the correct contract for non-fractional currencies.

Our [base gateway
tests](https://github.com/jasonwebster/active_merchant/blob/07e28c4936c2ae46f0470d878c19f36564c3df6e/test/unit/gateways/gateway_test.rb#L83-L91)
and [Stripe
tests](https://github.com/jasonwebster/active_merchant/blob/07e28c4936c2ae46f0470d878c19f36564c3df6e/test/unit/gateways/stripe_test.rb#L431-L440)
for example, which is another gateway with a `:cents` money format,
ensure that you want to charge ¥100, you actually have to pass in
`10000`, as if the non-fractional currency _was_ fractional. That's the
external contract of Active Merchant--all transaction amounts accepted
by the public API methods are expected to be in cents, period.

Closes #2267
  • Loading branch information
jasonwebster committed Dec 5, 2016
1 parent e71cdd2 commit 022f85a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* Authorize.net: Add line item fields and additional transaction settings [shasum]
* Authorize.net: Pass through `header_email_receipt` [shasum]
* Stripe: Scrub additional network tokenization related sensitive data [jasonwebster] #2251
* Applying: Worldpay: Format non-fractional currency amounts correctly [jasonwebster] #2267

== Version 1.61.0 (November 7, 2016)
* Add codes AQ, BQ, SX, and SS to list of countries and update SD numeric code [zxlin]
Expand Down
2 changes: 1 addition & 1 deletion lib/active_merchant/billing/gateways/worldpay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def add_amount(xml, money, options)
currency = options[:currency] || currency(money)

amount_hash = {
:value => amount(money),
:value => localized_amount(money, currency),
'currencyCode' => currency,
'exponent' => non_fractional_currency?(currency) ? 0 : 2
}
Expand Down
13 changes: 12 additions & 1 deletion test/unit/gateways/worldpay_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,20 @@ def test_amount_handling
end.respond_with(successful_authorize_response)
end

def test_non_fractional_amount_handling_with_moneylike
amount = OpenStruct.new(cents: 10000)
stub_comms do
@gateway.authorize(amount, @credit_card, @options.merge(currency: 'JPY'))
end.check_request do |endpoint, data, headers|
assert_tag_with_attributes 'amount',
{'value' => '100', 'exponent' => '0', 'currencyCode' => 'JPY'},
data
end.respond_with(successful_authorize_response)
end

def test_currency_exponent_handling
stub_comms do
@gateway.authorize(100, @credit_card, @options.merge(currency: :JPY))
@gateway.authorize(10000, @credit_card, @options.merge(currency: :JPY))
end.check_request do |endpoint, data, headers|
assert_tag_with_attributes 'amount',
{'value' => '100', 'exponent' => '0', 'currencyCode' => 'JPY'},
Expand Down

0 comments on commit 022f85a

Please sign in to comment.