diff --git a/app/jobs/invoices/prepaid_credit_job.rb b/app/jobs/invoices/prepaid_credit_job.rb index 7e6b7a4bb21..4658833df95 100644 --- a/app/jobs/invoices/prepaid_credit_job.rb +++ b/app/jobs/invoices/prepaid_credit_job.rb @@ -7,7 +7,8 @@ class PrepaidCreditJob < ApplicationJob unique :until_executed, on_conflict: :log def perform(invoice) - Wallets::ApplyPaidCreditsService.new.call(invoice) + wallet_transaction = invoice.fees.find_by(fee_type: 'credit')&.invoiceable + Wallets::ApplyPaidCreditsService.call(wallet_transaction:) end end end diff --git a/app/services/wallets/apply_paid_credits_service.rb b/app/services/wallets/apply_paid_credits_service.rb index 3cb7f76edd8..5b9721062a6 100644 --- a/app/services/wallets/apply_paid_credits_service.rb +++ b/app/services/wallets/apply_paid_credits_service.rb @@ -2,15 +2,25 @@ module Wallets class ApplyPaidCreditsService < BaseService - def call(invoice) - wallet_transaction = invoice.fees.find_by(fee_type: 'credit')&.invoiceable + def initialize(wallet_transaction:) + @wallet_transaction = wallet_transaction + super + end - return unless wallet_transaction - return if wallet_transaction.status == 'settled' + def call + return result unless wallet_transaction + return result if wallet_transaction.status == 'settled' WalletTransactions::SettleService.new(wallet_transaction:).call Wallets::Balance::IncreaseService .new(wallet: wallet_transaction.wallet, credits_amount: wallet_transaction.credit_amount).call + + result.wallet_transaction = wallet_transaction + result end + + private + + attr_reader :wallet_transaction end end diff --git a/spec/services/wallets/apply_paid_credits_service_spec.rb b/spec/services/wallets/apply_paid_credits_service_spec.rb index b9968a87697..c0f3f7d00f4 100644 --- a/spec/services/wallets/apply_paid_credits_service_spec.rb +++ b/spec/services/wallets/apply_paid_credits_service_spec.rb @@ -3,43 +3,24 @@ require 'rails_helper' RSpec.describe Wallets::ApplyPaidCreditsService, type: :service do - subject(:service) { described_class.new } + subject(:service) { described_class.new(wallet_transaction:) } describe '.call' do - let(:invoice) { create(:invoice, customer:, organization: customer.organization) } - let(:customer) { create(:customer) } - let(:subscription) { create(:subscription, customer:) } - let(:wallet) { create(:wallet, customer:, balance_cents: 1000, credits_balance: 10.0) } + let(:wallet) { create(:wallet, balance_cents: 1000, credits_balance: 10.0) } let(:wallet_transaction) do create(:wallet_transaction, wallet:, amount: 15.0, credit_amount: 15.0, status: 'pending') end - let(:fee) do - create( - :fee, - fee_type: 'credit', - invoiceable_type: 'WalletTransaction', - invoiceable_id: wallet_transaction.id, - invoice: - ) - end - - before do - wallet_transaction - fee - subscription - invoice.update(invoice_type: 'credit') - end it 'updates wallet balance' do - service.call(invoice) + service.call expect(wallet.reload.balance_cents).to eq 2500 end it 'settles the wallet transaction' do - service.call(invoice) + result = service.call - expect(wallet_transaction.reload.status).to eq('settled') + expect(result.wallet_transaction.status).to eq('settled') end end end