Code: Select all
function transfer_funds($instigator, $sender, $receiver, $funds, $reason, $server)
//Takes money from one account and put it in another.
//A session MUST be active to use this function. ie begin_session() in util.php.
//Returns 0 on success.
//Returns 1 on failure due to insufficient funds.
//2 is returned if the account for the reciever did not exist.
//3 if the sender doesn't exist.
//4 if trying to send to yourself.
//5 if funds to move are neg
//6 if the receiver is an Inactive account.
//7 if the instigator is an Inactive account.
//8 if we couldn't actualy make all the changes.
//Handles cleaning up all inputs.
//Handles error messages and logging.
//In terms of sql transactions, this function is self contained. If called as part
//of a transaction that has used begin_transaction() it will not interfer with that transaction.
//Other wise it will cause a new transaction to be started and will commmit or recall that transaction
//only based on what happens here.
//If used as part of a greater transaction ALWATS call begin_transaction() first
//and use commit_transaction() or abort_transaction() to complete.
$instigator // the account that is causing the transfer. Often the person sending the money.
$sender // What account are we pulling from?
$receiver // What account is the money going into.
$funds // How much are we moving.
$reason // A string explaining why we are moving the money.
$server // A connection to the server gained from bank_server_connect().
transfer_funds() makes use of the transaction ability in sql. ie if any part of the transfer fails, it doesn't happen.
If you plan to use transfer_funds() in a mod you must do it as follows.
Code: Select all
require_once ('../../require/config.php'); //needed for bank constants
require_once ('../../require/util.php'); //needed to connect to server and transaction
require_once ('../../require/bankops'); //needed for transfer_funds()
error_reporting(E_ALL); // I require this.
begin_session() //required to move money and useful info is in here. NEVER modify it. Consider all data safe or keep a local copy you have sanitized.
if you are going to do other modifications along with the transfer you must make sure it all works, or doesn't happen at all. read up on database transactions.
The following are the functions you will need.
begin_transaction($server)
commit_transaction()
abort_transaction().
Here is a basic idea to use:
Code: Select all
//all the requires and stuff
//anything you might need first.
$server = bank_server_connect();
begin_transaction($server)
//your sql queries here.
transfer_funds(); //with all the info it will require.
//next check if the all the queries succeeded. You can check as you go, or check it at the end in some way.
//everything went well
commit_transaction();
//something failed
abort_transaction();
Be sure that all your error checking is being done. We don't want money moving but without the service being rendered. Also make sure transfer_funds() succeeded if you are using begin_transaction(). Once you call that function you become responsible for making sure everything worked.