diff --git a/app/services/wallet_transactions/create_service.rb b/app/services/wallet_transactions/create_service.rb index c546b5ea73e..f40f480a92c 100644 --- a/app/services/wallet_transactions/create_service.rb +++ b/app/services/wallet_transactions/create_service.rb @@ -24,7 +24,7 @@ def call if params[:paid_credits] transaction = handle_paid_credits( wallet: result.current_wallet, - paid_credits: params[:paid_credits], + credits_amount: BigDecimal(params[:paid_credits]).floor(5), invoice_requires_successful_payment: ) wallet_transactions << transaction @@ -33,7 +33,7 @@ def call if params[:granted_credits] transaction = handle_granted_credits( wallet: result.current_wallet, - granted_credits: params[:granted_credits], + credits_amount: BigDecimal(params[:granted_credits]).floor(5), reset_consumed_credits: ActiveModel::Type::Boolean.new.cast(params[:reset_consumed_credits]), invoice_requires_successful_payment: ) @@ -43,7 +43,7 @@ def call if params[:voided_credits] void_result = WalletTransactions::VoidService.call( wallet: result.current_wallet, - credits: params[:voided_credits], + credits_amount: BigDecimal(params[:voided_credits]).floor(5), from_source: source, metadata: ) wallet_transactions << void_result.wallet_transaction @@ -61,16 +61,14 @@ def call attr_reader :organization, :params, :source, :metadata - def handle_paid_credits(wallet:, paid_credits:, invoice_requires_successful_payment:) - paid_credits_amount = BigDecimal(paid_credits) - - return if paid_credits_amount.zero? + def handle_paid_credits(wallet:, credits_amount:, invoice_requires_successful_payment:) + return if credits_amount.zero? wallet_transaction = WalletTransaction.create!( wallet:, transaction_type: :inbound, - amount: wallet.rate_amount * paid_credits_amount, - credit_amount: paid_credits_amount, + amount: wallet.rate_amount * credits_amount, + credit_amount: credits_amount, status: :pending, source:, transaction_status: :purchased, @@ -83,17 +81,15 @@ def handle_paid_credits(wallet:, paid_credits:, invoice_requires_successful_paym wallet_transaction end - def handle_granted_credits(wallet:, granted_credits:, invoice_requires_successful_payment:, reset_consumed_credits: false) - granted_credits_amount = BigDecimal(granted_credits) - - return if granted_credits_amount.zero? + def handle_granted_credits(wallet:, credits_amount:, invoice_requires_successful_payment:, reset_consumed_credits: false) + return if credits_amount.zero? ActiveRecord::Base.transaction do wallet_transaction = WalletTransaction.create!( wallet:, transaction_type: :inbound, - amount: wallet.rate_amount * granted_credits_amount, - credit_amount: granted_credits_amount, + amount: wallet.rate_amount * credits_amount, + credit_amount: credits_amount, status: :settled, settled_at: Time.current, source:, @@ -104,7 +100,7 @@ def handle_granted_credits(wallet:, granted_credits:, invoice_requires_successfu Wallets::Balance::IncreaseService.new( wallet:, - credits_amount: granted_credits_amount, + credits_amount:, reset_consumed_credits: ).call diff --git a/app/services/wallet_transactions/void_service.rb b/app/services/wallet_transactions/void_service.rb index 8490a06e368..2a31d4c7dcd 100644 --- a/app/services/wallet_transactions/void_service.rb +++ b/app/services/wallet_transactions/void_service.rb @@ -2,9 +2,9 @@ module WalletTransactions class VoidService < BaseService - def initialize(wallet:, credits:, from_source: :manual, metadata: {}) + def initialize(wallet:, credits_amount:, from_source: :manual, metadata: {}) @wallet = wallet - @credits = credits + @credits_amount = credits_amount @from_source = from_source @metadata = metadata @@ -34,10 +34,6 @@ def call private - attr_reader :wallet, :credits, :from_source, :metadata - - def credits_amount - @credits_amount ||= BigDecimal(credits) - end + attr_reader :wallet, :credits_amount, :from_source, :metadata end end diff --git a/spec/services/wallet_transactions/create_service_spec.rb b/spec/services/wallet_transactions/create_service_spec.rb index f7b20f4c7a7..6c52dc85647 100644 --- a/spec/services/wallet_transactions/create_service_spec.rb +++ b/spec/services/wallet_transactions/create_service_spec.rb @@ -112,5 +112,14 @@ expect(result.error.messages[:paid_credits]).to eq(['invalid_paid_credits']) end end + + context 'with decimal value' do + let(:paid_credits) { '4.399999' } + + it 'creates wallet transaction with floored value' do + result = create_service + expect(result.wallet_transactions.first.credit_amount).to eq(4.39999) + end + end end end diff --git a/spec/services/wallet_transactions/void_service_spec.rb b/spec/services/wallet_transactions/void_service_spec.rb index 555c825a23b..641a1031dcd 100644 --- a/spec/services/wallet_transactions/void_service_spec.rb +++ b/spec/services/wallet_transactions/void_service_spec.rb @@ -3,7 +3,7 @@ require 'rails_helper' RSpec.describe WalletTransactions::VoidService, type: :service do - subject(:void_service) { described_class.call(wallet:, credits:) } + subject(:void_service) { described_class.call(wallet:, credits_amount:) } let(:membership) { create(:membership) } let(:organization) { membership.organization } @@ -19,7 +19,7 @@ credits_ongoing_balance: 10.0 ) end - let(:credits) { '10.00' } + let(:credits_amount) { BigDecimal('10.00') } before do subscription @@ -27,7 +27,7 @@ describe '#call' do context 'when credits amount is zero' do - let(:credits) { '0.00' } + let(:credits_amount) { BigDecimal('0.00') } it 'does not create a wallet transaction' do expect { void_service }.not_to change(WalletTransaction, :count) @@ -35,7 +35,7 @@ end context 'when transaction have metadata' do - subject(:void_service) { described_class.call(wallet:, credits:, metadata:) } + subject(:void_service) { described_class.call(wallet:, credits_amount:, metadata:) } let(:metadata) { [{'key' => 'valid_value', 'value' => 'also_valid'}] }