forked from Simnation/Main
Update billing_server.lua
This commit is contained in:
parent
7b2d064f63
commit
a3269e304b
1 changed files with 45 additions and 12 deletions
|
@ -75,9 +75,26 @@ lib.callback.register('billing:server:handleBillResponse', function(source, data
|
||||||
if data.action == 'pay' then
|
if data.action == 'pay' then
|
||||||
-- Process payment based on selected account
|
-- Process payment based on selected account
|
||||||
if data.accountId == 'personal' then
|
if data.accountId == 'personal' then
|
||||||
-- Pay from personal bank account
|
-- Pay from personal bank account using ps-banking's payBill function
|
||||||
local success = lib.callback.await('ps-banking:server:payBill', src, data.billId)
|
local billResult = MySQL.query.await('SELECT * FROM ps_banking_bills WHERE id = ?', {data.billId})
|
||||||
return success
|
if not billResult or #billResult == 0 then return false end
|
||||||
|
|
||||||
|
local bill = billResult[1]
|
||||||
|
local amount = tonumber(bill.amount)
|
||||||
|
|
||||||
|
-- Check if player has enough money in their bank account
|
||||||
|
if player.PlayerData.money["bank"] < amount then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Process payment manually instead of using callback to avoid issues
|
||||||
|
player.Functions.RemoveMoney("bank", amount, "bill-payment")
|
||||||
|
MySQL.query.await('DELETE FROM ps_banking_bills WHERE id = ?', {data.billId})
|
||||||
|
|
||||||
|
-- Process the payment to the recipient's account
|
||||||
|
ProcessBillPayment(data.billId)
|
||||||
|
|
||||||
|
return true
|
||||||
else
|
else
|
||||||
-- Pay from shared account
|
-- Pay from shared account
|
||||||
local success = PayBillFromSharedAccount(src, data.billId, data.accountId)
|
local success = PayBillFromSharedAccount(src, data.billId, data.accountId)
|
||||||
|
@ -119,7 +136,7 @@ function PayBillFromSharedAccount(source, billId, accountId)
|
||||||
if not billResult or #billResult == 0 then return false end
|
if not billResult or #billResult == 0 then return false end
|
||||||
|
|
||||||
local bill = billResult[1]
|
local bill = billResult[1]
|
||||||
local amount = bill.amount
|
local amount = tonumber(bill.amount)
|
||||||
|
|
||||||
-- Get account details
|
-- Get account details
|
||||||
local accountResult = MySQL.query.await('SELECT * FROM ps_banking_accounts WHERE id = ?', {accountId})
|
local accountResult = MySQL.query.await('SELECT * FROM ps_banking_accounts WHERE id = ?', {accountId})
|
||||||
|
@ -146,7 +163,7 @@ function PayBillFromSharedAccount(source, billId, accountId)
|
||||||
if not hasAccess then return false end
|
if not hasAccess then return false end
|
||||||
|
|
||||||
-- Check if account has enough balance
|
-- Check if account has enough balance
|
||||||
if tonumber(account.balance) < tonumber(amount) then return false end
|
if tonumber(account.balance) < amount then return false end
|
||||||
|
|
||||||
-- Process payment
|
-- Process payment
|
||||||
MySQL.update.await('UPDATE ps_banking_accounts SET balance = balance - ? WHERE id = ?', {amount, accountId})
|
MySQL.update.await('UPDATE ps_banking_accounts SET balance = balance - ? WHERE id = ?', {amount, accountId})
|
||||||
|
@ -161,10 +178,31 @@ end
|
||||||
-- Add a callback for paying bill from selected account
|
-- Add a callback for paying bill from selected account
|
||||||
lib.callback.register('billing:server:payBillFromAccount', function(source, data)
|
lib.callback.register('billing:server:payBillFromAccount', function(source, data)
|
||||||
local src = source
|
local src = source
|
||||||
|
local player = QBCore.Functions.GetPlayer(src)
|
||||||
|
|
||||||
|
if not player then return false end
|
||||||
|
|
||||||
if data.accountId == 'personal' then
|
if data.accountId == 'personal' then
|
||||||
-- Pay from personal bank account
|
-- Pay from personal bank account
|
||||||
return lib.callback.await('ps-banking:server:payBill', src, data.billId)
|
local billResult = MySQL.query.await('SELECT * FROM ps_banking_bills WHERE id = ?', {data.billId})
|
||||||
|
if not billResult or #billResult == 0 then return false end
|
||||||
|
|
||||||
|
local bill = billResult[1]
|
||||||
|
local amount = tonumber(bill.amount)
|
||||||
|
|
||||||
|
-- Check if player has enough money in their bank account
|
||||||
|
if player.PlayerData.money["bank"] < amount then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Process payment manually
|
||||||
|
player.Functions.RemoveMoney("bank", amount, "bill-payment")
|
||||||
|
MySQL.query.await('DELETE FROM ps_banking_bills WHERE id = ?', {data.billId})
|
||||||
|
|
||||||
|
-- Process the payment to the recipient's account
|
||||||
|
ProcessBillPayment(data.billId)
|
||||||
|
|
||||||
|
return true
|
||||||
else
|
else
|
||||||
-- Pay from shared account
|
-- Pay from shared account
|
||||||
return PayBillFromSharedAccount(src, data.billId, data.accountId)
|
return PayBillFromSharedAccount(src, data.billId, data.accountId)
|
||||||
|
@ -180,7 +218,7 @@ function ProcessBillPayment(billId)
|
||||||
local bill = billData[1]
|
local bill = billData[1]
|
||||||
local receiverId = bill.sender_id
|
local receiverId = bill.sender_id
|
||||||
local accountId = bill.account_id
|
local accountId = bill.account_id
|
||||||
local amount = bill.amount
|
local amount = tonumber(bill.amount)
|
||||||
|
|
||||||
-- If it's a personal account
|
-- If it's a personal account
|
||||||
if accountId == 'personal' then
|
if accountId == 'personal' then
|
||||||
|
@ -210,11 +248,6 @@ function ProcessBillPayment(billId)
|
||||||
end
|
end
|
||||||
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
|
-- Create the necessary database tables if they don't exist
|
||||||
MySQL.ready(function()
|
MySQL.ready(function()
|
||||||
MySQL.query.await([[
|
MySQL.query.await([[
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue