Class: Bscf::Core::TransactionService

Inherits:
Object
  • Object
show all
Defined in:
app/services/bscf/core/transaction_service.rb

Class Method Summary collapse

Class Method Details

.cancel(transaction) ⇒ Boolean

Cancel a transaction

Parameters:

Returns:

  • (Boolean)

    Success or failure



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

Parameters:

  • account_id (Integer)

    Account to adjust

  • amount (Decimal)

    Adjustment amount

  • is_debit (Boolean) (defaults to: true)

    Whether this is a debit (true) or credit (false)

  • description (String) (defaults to: nil)

    Optional description

Returns:



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: ,
    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

Parameters:

  • to_account_id (Integer)

    Destination account ID

  • amount (Decimal)

    Amount to deposit

  • description (String) (defaults to: nil)

    Optional description

Returns:



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)
   = ("DEPOSIT_ACCOUNT")

  # Ensure system account has sufficient balance for deposits
  (, amount)

  create_paired_transaction(
    from_account_id: .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

Parameters:

  • from_account_id (Integer)

    Account to charge

  • amount (Decimal)

    Fee amount

  • description (String) (defaults to: nil)

    Optional description

Returns:



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")

  create_paired_transaction(
    from_account_id: ,
    to_account_id: .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: ,
      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: ,
      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

Parameters:

  • from_account_id (Integer)

    Source account ID

  • to_account_id (Integer)

    Destination account ID

  • amount (Decimal)

    Amount to transfer

  • description (String) (defaults to: nil)

    Optional description

Returns:



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: ,
    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

Parameters:

  • from_account_id (Integer)

    Source account ID

  • amount (Decimal)

    Amount to withdraw

  • description (String) (defaults to: nil)

    Optional description

Returns:



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)
   = ("WITHDRAWAL_ACCOUNT")

  create_paired_transaction(
    from_account_id: ,
    to_account_id: .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.(, required_amount)
  # Top up the system account if needed
  if .balance < required_amount
    .update!(balance: .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 a system account for external transactions
   = VirtualAccount.find_or_create_by!(account_number: ) do ||
    # Set default values for a new system account
    .user_id = find_or_create_system_user.id
    . = 
    .branch_code = "SYSTEM"
    .product_scheme = "SAVINGS"
    .voucher_type = "REGULAR"
    .interest_rate = 0
    .interest_type = :simple
    .balance = 100000.00  # Initialize with a large balance
    .locked_amount = 0
    .status = :active
  end

  
end

.find_or_create_system_userObject



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_numberObject



178
179
180
181
182
# File 'app/services/bscf/core/transaction_service.rb', line 178

def self.generate_reference_number
  timestamp = Time.current.strftime("%Y%m%d%H%M%S")
  random = SecureRandom.hex(3)
  "TXN#{timestamp}#{random}"
end

.process(transaction) ⇒ Boolean

Process a transaction

Parameters:

Returns:

  • (Boolean)

    Success or failure



97
98
99
# File 'app/services/bscf/core/transaction_service.rb', line 97

def self.process(transaction)
  transaction.process!
end