Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apple Pay Displays Incorrect Amount for Non-Decimal Currencies (x100 Multiplication) in Spree Gateway Payment Method #412

Open
gulzarhussain112 opened this issue Feb 12, 2024 · 1 comment

Comments

@gulzarhussain112
Copy link

This issue reports an incorrect displayed amount in the Apple Pay popup when using the Spree::Gateway::StripeApplePayGateway payment method provided by the Spree Gateway gem.

Problem:

When using Apple Pay with the mentioned payment method, the displayed amount in the Apple Pay popup is incorrect. Instead of showing the actual amount, it shows the amount multiplied by 100 (e.g., for ¥8000, it shows ¥800,000).

Investigation:

Identified potential cause: The issue might be related to the amount_in_cents method in the Spree Gateway gem, which converts monetary amounts to cents. The additional multiplication by 100 could lead to this discrepancy.

Expected Behaviour:

The Apple Pay popup should display the correct monetary amount without any unintended multiplications.

Reproduction Steps:

Configure the Spree::Gateway::StripeApplePayGateway payment method in Spree Commerce.
Add items to the cart and proceed to checkout.
Select the Spree::Gateway::StripeApplePayGateway payment method.
Use Apple Pay to attempt payment.
Observe the incorrect amount displayed in the Apple Pay popup.

Additional Information:
Please find the relevant code snippet in the following file
strpe_apple_pay.html.erb
This file contains the logic for calculating and displaying amounts in the Apple Pay popup.

  • It's important to note that the incorrect amount is only shown in the Apple Pay popup. The actual charged amount by Stripe is correct, as demonstrated by the localized_amount method in the activemerchant-1.133.0 (for reference, see its code snippet below).
def localized_amount(money, currency)
  amount = amount(money)

  return amount unless non_fractional_currency?(currency) || three_decimal_currency?(currency)

  if non_fractional_currency?(currency)
     if self.money_format == :cents
        sprintf('%.0f', amount.to_f / 100)
     else
       amount.split('.').first
     end
  elsif three_decimal_currency?(currency)
     if self.money_format == :cents
       amount.to_s
     else
       sprintf('%.3f', (amount.to_f / 10))
     end
   end
end



@rahul2103
Copy link

hey @gulzarhussain112 , I also faced the same issue, and figured it out and added the patch solution for same.

strpe_apple_pay.html.erb

  var paymentRequest = stripeApplePay.paymentRequest({
    country: '<%= payment_method.preferred_country_code.try(:upcase) %>',
    currency: '<%= @order.currency.downcase %>',
    displayItems: [
      <% @order.line_items.each do |line_item| %>
      {
        label: '<%= line_item.name %> x <%= line_item.quantity %>',
        amount: <%= actual_amount(line_item.total, line_item.currency) %>
      },
      <% end %>
      <% if @order.tax_total != 0 %>
      {
        label: '<%= Spree.t(:tax) %>',
        amount: <%= actual_amount(@order.tax_total, @order.currency) %>
      },
      <% end %>
      <% if @order.shipment_total != 0 %>
      {
        label: '<%= Spree.t(:shipment) %>',
        amount: <%= actual_amount(@order.shipment_total, @order.currency) %>
      }
      <% end %>
    ],
    total: {
      label: '<%= Spree.t(:total) %>',
      amount: <%= actual_amount(@order.total, @order.currency) %>
    },
    requestPayerName: false,
    requestPayerEmail: false,
    requestPayerPhone: false
  });

frontend_helper.rb

    def actual_amount(amount, currency)
      money = Spree::Money.new(amount, currency: currency)

      if non_decimal_currencies.include?('JPY')
        money.amount_in_cents / 100
      else
        money.amount_in_cents
      end
    end

    def non_decimal_currencies
      %w(JPY)                               # add all non decimal currencies here.
    end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants