2016-07-05 13:20:25 +02:00
|
|
|
class WalletService
|
|
|
|
def initialize(user: nil, wallet: nil)
|
|
|
|
@user = user
|
|
|
|
@wallet = wallet
|
|
|
|
end
|
|
|
|
|
|
|
|
def credit(amount)
|
|
|
|
if @wallet.credit(amount)
|
2016-07-05 19:07:50 +02:00
|
|
|
transaction = WalletTransaction.create(user: @user, wallet: @wallet, transaction_type: 'credit', amount: amount)
|
|
|
|
|
|
|
|
NotificationCenter.call type: 'notify_user_wallet_is_credited',
|
|
|
|
receiver: @wallet.user,
|
|
|
|
attached_object: transaction
|
|
|
|
NotificationCenter.call type: 'notify_admin_user_wallet_is_credited',
|
|
|
|
receiver: User.admins,
|
|
|
|
attached_object: transaction
|
2016-07-05 13:20:25 +02:00
|
|
|
return true
|
|
|
|
end
|
2016-07-07 15:57:06 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
def debit(amount, transactable)
|
|
|
|
if @wallet.debit(amount)
|
|
|
|
WalletTransaction.create(user: @user, wallet: @wallet, transaction_type: 'debit', amount: amount, transactable: transactable)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
2016-07-05 13:20:25 +02:00
|
|
|
end
|
|
|
|
end
|