This commit is contained in:
Nordi98 2025-08-05 14:27:19 +02:00
parent fb02bf36f5
commit 7b2d064f63
34 changed files with 250 additions and 56 deletions

View file

@ -73,15 +73,15 @@ lib.callback.register('billing:server:handleBillResponse', function(source, data
if not player then return false end
if data.action == 'pay' then
-- Process payment
local success = lib.callback.await('ps-banking:server:payBill', src, data.billId)
if success then
-- Payment successful
return true
-- Process payment based on selected account
if data.accountId == 'personal' then
-- Pay from personal bank account
local success = lib.callback.await('ps-banking:server:payBill', src, data.billId)
return success
else
-- Payment failed (likely insufficient funds)
return false
-- Pay from shared account
local success = PayBillFromSharedAccount(src, data.billId, data.accountId)
return success
end
elseif data.action == 'decline' then
-- Mark as declined in our system
@ -107,10 +107,72 @@ lib.callback.register('billing:server:handleBillResponse', function(source, data
return false
end)
-- Event handler for when a bill is paid
RegisterNetEvent('ps-banking:server:billPaid', function(billId)
-- Function to pay bill from a shared account
function PayBillFromSharedAccount(source, billId, accountId)
local src = source
local player = QBCore.Functions.GetPlayer(src)
if not player then return false end
-- Get bill details
local billResult = MySQL.query.await('SELECT * FROM ps_banking_bills WHERE id = ?', {billId})
if not billResult or #billResult == 0 then return false end
local bill = billResult[1]
local amount = bill.amount
-- Get account details
local accountResult = MySQL.query.await('SELECT * FROM ps_banking_accounts WHERE id = ?', {accountId})
if not accountResult or #accountResult == 0 then return false end
local account = accountResult[1]
-- Check if player has access to this account
local hasAccess = false
local accountOwner = json.decode(account.owner)
local accountUsers = json.decode(account.users)
if accountOwner.identifier == player.PlayerData.citizenid then
hasAccess = true
else
for _, user in ipairs(accountUsers) do
if user.identifier == player.PlayerData.citizenid then
hasAccess = true
break
end
end
end
if not hasAccess then return false end
-- Check if account has enough balance
if tonumber(account.balance) < tonumber(amount) then return false end
-- Process payment
MySQL.update.await('UPDATE ps_banking_accounts SET balance = balance - ? WHERE id = ?', {amount, accountId})
MySQL.query.await('DELETE FROM ps_banking_bills WHERE id = ?', {billId})
-- Process the payment to the recipient's account
ProcessBillPayment(billId)
return true
end
-- Add a callback for paying bill from selected account
lib.callback.register('billing:server:payBillFromAccount', function(source, data)
local src = source
if data.accountId == 'personal' then
-- Pay from personal bank account
return lib.callback.await('ps-banking:server:payBill', src, data.billId)
else
-- Pay from shared account
return PayBillFromSharedAccount(src, data.billId, data.accountId)
end
end)
-- Function to process bill payment to recipient
function ProcessBillPayment(billId)
-- Find the bill in our custom table
local billData = MySQL.query.await('SELECT * FROM billing_accounts WHERE bill_id = ?', {billId})
@ -146,6 +208,11 @@ RegisterNetEvent('ps-banking:server:billPaid', function(billId)
-- Update the bill status
MySQL.update.await('UPDATE billing_accounts SET paid = 1, paid_at = NOW() WHERE bill_id = ?', {billId})
end
end
-- Event handler for when a bill is paid
RegisterNetEvent('ps-banking:server:billPaid', function(billId)
ProcessBillPayment(billId)
end)
-- Create the necessary database tables if they don't exist
@ -214,7 +281,7 @@ AddEventHandler('oxmysql:query', function(query, params)
if billId then
-- Small delay to ensure the deletion completes
SetTimeout(100, function()
TriggerEvent('ps-banking:server:billPaid', billId)
ProcessBillPayment(billId)
end)
end
end