From 022f85a203fde191510e1960fede47755403f565 Mon Sep 17 00:00:00 2001 From: Jason Webster Date: Thu, 24 Nov 2016 09:50:46 -0500 Subject: [PATCH] Worldpay: Format non-fractional currency amounts correctly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/activemerchant/active_merchant/pull/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 --- CHANGELOG | 1 + lib/active_merchant/billing/gateways/worldpay.rb | 2 +- test/unit/gateways/worldpay_test.rb | 13 ++++++++++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index b65075d8fb3..6f9cbafe103 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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] diff --git a/lib/active_merchant/billing/gateways/worldpay.rb b/lib/active_merchant/billing/gateways/worldpay.rb index b6e2137e104..f8236c450a8 100644 --- a/lib/active_merchant/billing/gateways/worldpay.rb +++ b/lib/active_merchant/billing/gateways/worldpay.rb @@ -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 } diff --git a/test/unit/gateways/worldpay_test.rb b/test/unit/gateways/worldpay_test.rb index 7b286ce1612..43c09f3135e 100644 --- a/test/unit/gateways/worldpay_test.rb +++ b/test/unit/gateways/worldpay_test.rb @@ -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'},