Class: Bscf::Core::TransactionService
- Inherits:
-
Object
- Object
- Bscf::Core::TransactionService
- Defined in:
- app/services/bscf/core/transaction_service.rb
Class Method Summary collapse
-
.cancel(transaction) ⇒ Boolean
Cancel a transaction.
-
.create_adjustment(account_id:, amount:, is_debit: true, description: nil) ⇒ VirtualAccountTransaction
Create an adjustment to an account.
-
.create_deposit(to_account_id:, amount:, description: nil) ⇒ Array<VirtualAccountTransaction>
Create a deposit to an account.
-
.create_fee(from_account_id:, amount:, description: nil) ⇒ Array<VirtualAccountTransaction>
Create a fee transaction.
- .create_paired_transaction(from_account_id:, to_account_id:, amount:, transaction_type:, description: nil) ⇒ Object
-
.create_transfer(from_account_id:, to_account_id:, amount:, description: nil) ⇒ Array<VirtualAccountTransaction>
Create a transfer between two accounts.
-
.create_withdrawal(from_account_id:, amount:, description: nil) ⇒ Array<VirtualAccountTransaction>
Create a withdrawal from an account.
- .ensure_system_account_balance(account, required_amount) ⇒ Object
- .find_or_create_system_account(account_identifier) ⇒ Object
- .find_or_create_system_user ⇒ Object
- .generate_reference_number ⇒ Object
-
.process(transaction) ⇒ Boolean
Process a transaction.
Class Method Details
.cancel(transaction) ⇒ Boolean
Cancel a transaction
104 105 106 |
# File 'app/services/bscf/core/transaction_service.rb', line 104 def self.cancel(transaction) transaction.cancel! end |
.create_adjustment(account_id:, amount:, is_debit: true, description: nil) ⇒ VirtualAccountTransaction
Create an adjustment to an account
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'app/services/bscf/core/transaction_service.rb', line 80 def self.create_adjustment(account_id:, amount:, is_debit: true, description: nil) reference_number = generate_reference_number VirtualAccountTransaction.create!( transaction_type: :adjustment, entry_type: is_debit ? :debit : :credit, account_id: account_id, amount: amount, reference_number: reference_number, description: description, status: :pending ) end |
.create_deposit(to_account_id:, amount:, description: nil) ⇒ Array<VirtualAccountTransaction>
Create a deposit to an account
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'app/services/bscf/core/transaction_service.rb', line 25 def self.create_deposit(to_account_id:, amount:, description: nil) system_account = find_or_create_system_account("DEPOSIT_ACCOUNT") # Ensure system account has sufficient balance for deposits ensure_system_account_balance(system_account, amount) create_paired_transaction( from_account_id: system_account.id, to_account_id: to_account_id, amount: amount, transaction_type: :deposit, description: description ) end |
.create_fee(from_account_id:, amount:, description: nil) ⇒ Array<VirtualAccountTransaction>
Create a fee transaction
62 63 64 65 66 67 68 69 70 71 72 |
# File 'app/services/bscf/core/transaction_service.rb', line 62 def self.create_fee(from_account_id:, amount:, description: nil) fee_account = find_or_create_system_account("FEE_ACCOUNT") create_paired_transaction( from_account_id: from_account_id, to_account_id: fee_account.id, amount: amount, transaction_type: :fee, description: description ) end |
.create_paired_transaction(from_account_id:, to_account_id:, amount:, transaction_type:, description: nil) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'app/services/bscf/core/transaction_service.rb', line 110 def self.create_paired_transaction(from_account_id:, to_account_id:, amount:, transaction_type:, description: nil) reference_number = generate_reference_number ActiveRecord::Base.transaction do # Debit entry (from account) debit = VirtualAccountTransaction.create!( transaction_type: transaction_type, entry_type: :debit, account_id: from_account_id, amount: amount, reference_number: reference_number, description: description, status: :pending ) # Credit entry (to account) credit = VirtualAccountTransaction.create!( transaction_type: transaction_type, entry_type: :credit, account_id: to_account_id, amount: amount, reference_number: reference_number, description: description, status: :pending, paired_transaction_id: debit.id ) # Update the paired_transaction_id for the debit entry debit.update!(paired_transaction_id: credit.id) [ debit, credit ] end end |
.create_transfer(from_account_id:, to_account_id:, amount:, description: nil) ⇒ Array<VirtualAccountTransaction>
Create a transfer between two accounts
10 11 12 13 14 15 16 17 18 |
# File 'app/services/bscf/core/transaction_service.rb', line 10 def self.create_transfer(from_account_id:, to_account_id:, amount:, description: nil) create_paired_transaction( from_account_id: from_account_id, to_account_id: to_account_id, amount: amount, transaction_type: :transfer, description: description ) end |
.create_withdrawal(from_account_id:, amount:, description: nil) ⇒ Array<VirtualAccountTransaction>
Create a withdrawal from an account
45 46 47 48 49 50 51 52 53 54 55 |
# File 'app/services/bscf/core/transaction_service.rb', line 45 def self.create_withdrawal(from_account_id:, amount:, description: nil) system_account = find_or_create_system_account("WITHDRAWAL_ACCOUNT") create_paired_transaction( from_account_id: from_account_id, to_account_id: system_account.id, amount: amount, transaction_type: :withdrawal, description: description ) end |
.ensure_system_account_balance(account, required_amount) ⇒ Object
163 164 165 166 167 168 |
# File 'app/services/bscf/core/transaction_service.rb', line 163 def self.ensure_system_account_balance(account, required_amount) # Top up the system account if needed if account.balance < required_amount account.update!(balance: account.balance + required_amount * 2) end end |
.find_or_create_system_account(account_identifier) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'app/services/bscf/core/transaction_service.rb', line 144 def self.find_or_create_system_account(account_identifier) # Find or create a system account for external transactions account = VirtualAccount.find_or_create_by!(account_number: account_identifier) do |account| # Set default values for a new system account account.user_id = find_or_create_system_user.id account.cbs_account_number = account_identifier account.branch_code = "SYSTEM" account.product_scheme = "SAVINGS" account.voucher_type = "REGULAR" account.interest_rate = 0 account.interest_type = :simple account.balance = 100000.00 # Initialize with a large balance account.locked_amount = 0 account.status = :active end account end |
.find_or_create_system_user ⇒ Object
170 171 172 173 174 175 176 |
# File 'app/services/bscf/core/transaction_service.rb', line 170 def self.find_or_create_system_user User.find_or_create_by!(phone_number: "SYSTEM_USER") do |user| user.first_name = "System" user.last_name = "User" user.password = SecureRandom.hex(8) end end |
.generate_reference_number ⇒ Object
178 179 180 181 182 |
# File 'app/services/bscf/core/transaction_service.rb', line 178 def self.generate_reference_number = Time.current.strftime("%Y%m%d%H%M%S") random = SecureRandom.hex(3) "TXN#{timestamp}#{random}" end |
.process(transaction) ⇒ Boolean
Process a transaction
97 98 99 |
# File 'app/services/bscf/core/transaction_service.rb', line 97 def self.process(transaction) transaction.process! end |