diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a66091ac..2aad0136d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - Updated ffi to 1.15.1 - Updated GitHub issue templates +- Fix a bug: zero-decimal currencies were charged multiplied by 100 - Fix a bug: fablab:fix_invoices task fails to recreate the data if the date is in english ## v4.7.10 2021 May 25 diff --git a/app/controllers/api/payments_controller.rb b/app/controllers/api/payments_controller.rb index 350377e91..2d5df145e 100644 --- a/app/controllers/api/payments_controller.rb +++ b/app/controllers/api/payments_controller.rb @@ -26,7 +26,7 @@ class API::PaymentsController < API::ApiController intent = Stripe::PaymentIntent.create( { payment_method: params[:payment_method_id], - amount: amount[:amount], + amount: StripeService.stripe_amount(amount[:amount]), currency: Setting.get('stripe_currency'), confirmation_method: 'manual', confirm: true, diff --git a/app/services/stripe_service.rb b/app/services/stripe_service.rb index fdf10443d..7c4713d46 100644 --- a/app/services/stripe_service.rb +++ b/app/services/stripe_service.rb @@ -50,7 +50,7 @@ class StripeService if coupon.type == 'percent_off' stp_coupon[:percent_off] = coupon.percent_off elsif coupon.type == 'amount_off' - stp_coupon[:amount_off] = coupon.amount_off + stp_coupon[:amount_off] = stripe_amount(coupon.amount_off) stp_coupon[:currency] = Setting.get('stripe_currency') end @@ -61,6 +61,13 @@ class StripeService Stripe::Coupon.create(stp_coupon, api_key: Setting.get('stripe_secret_key')) end + def stripe_amount(amount) + currency = Setting.get('stripe_currency') + return amount / 100 if zero_decimal_currencies.any? { |s| s.casecmp(currency).zero? } + + amount + end + private def subscription_invoice_items(payment_schedule, subscription, first_item, reservable_stp_id) @@ -91,7 +98,7 @@ class StripeService def create_price(amount, stp_product_id, name, monthly: false) params = { - unit_amount: amount, + unit_amount: stripe_amount(amount), currency: Setting.get('stripe_currency'), product: stp_product_id, nickname: name @@ -107,5 +114,10 @@ class StripeService customer_id = payment_schedule.invoicing_profile.user.stp_customer_id Stripe::Customer.update(customer_id, { balance: -payment_schedule.wallet_amount }, { api_key: Setting.get('stripe_secret_key') }) end + + # @see https://stripe.com/docs/currencies#zero-decimal + def zero_decimal_currencies + %w[BIF CLP DJF GNF JPY KMF KRW MGA PYG RWF UGX VND VUV XAF XOF XPF] + end end end