I thought I would put this up for those who do not wish to use prepaid billing from other sources. If you are familiar with the asterisk extensions.conf then this will help.
-
I would only recommend going down this route if you have an established asterisk setup and do not wish to implement a2billing or astbilling or any other 3rd party tools which would mess up your entire setup.
-
if like me you have a live production system and do not want to change the setup for multiple servers then this will help.
In order to get a prepaid module working you will need the following:
- 1 test asterisk server (best to not have any addons or features like freePBX etc, if you do then I don’t know, you may have difficulties)
- phpagi (type it into google)
BTW: This is not a calling card setup, but similar.
once you’ve installed phpagi ensure you have the phpagi.conf in /etc/asterisk
Copy below to /var/lib/asterisk/agi-bin/credit.agi
Alter the database settings
[code]#!/usr/bin/php -q
<?php
include 'phpagi.php';
function connect_db()
{
mysql_connect ('192.168.0.291', 'sperm', 'shoot-her-the-eye') or die (mysql_error());
mysql_select_db('tomb-raid-her') or die (mysql_error());
}
connect_db();
# table which stores the live user details
$query = 'SELECT * FROM credit WHERE \''.$argv[1].'\' IN(vpexOutboundId,vpexExtension,vpexCallerId) and packPaymentMethod = \'CREDIT\'';
$rs = mysql_query($query);
#print $query;
#print "\n";
if(mysql_num_rows($rs))
{
# this must mean we have a el cheapo customer
$dialplan = array();
$row = (object) mysql_fetch_assoc($rs);
# find where they are calling
for($i = 0; $i < 9; $i++)
{
$query1 = 'SELECT * FROM dialplan WHERE destination = SUBSTR("'.$argv[2].'", 1,'.$i.')';
$dp = mysql_query($query1);
#print $query1;
if(mysql_num_rows($dp))
{
$rw = (object) mysql_fetch_assoc($dp);
$dialplan['destination'] = $rw->destination;
$dialplan['description'] = $rw->description;
$dialplan['peak'] = $rw->peak;
$dialplan['offpeak'] = $rw->offpeak;
$dialplan['connection_fee'] = $rw->connection_fee;
}
mysql_free_result($dp);
#print "\n";
}
if($row->cupkCredit > 0)
{
# now calulate how many seconds the caller has to make this call
$rate = (in_array(date('H'),array(0,1,2,3,4,5,21,22,23))) ? $dialplan['offpeak'] : $dialplan['peak'];
# uk only (VAT) + rate : because they don't pay it upfront damn el cheapos
$rate = ($rate * 0.15) + $rate;
# set the number of seconds
$seconds = ((($row->cupkCredit/$rate) * 60) - 360);
}
else
$seconds = 0;
#print $dialplan['description'].' '.$dialplan['destination']." seconds remaining = $seconds";
# load the agi agi
$agi = new AGI;
$agi->verbose($dialplan['description'].' '.$dialplan['destination']." seconds remaining = $seconds");
$agi->verbose($row->cupkCredit);
if($seconds <= 0)
{
$agi->exec('PlayBack','/var/lib/asterisk/sounds/not-enough-credit.gsm');
$agi->exec('Hangup');
exit;
}
else
{
# hangup the call
# executes a macro to set a absolute timeout
$agi->exec('Macro',"pre-pay|{$seconds}|{$argv[1]}|{$argv[2]}");
# but to be certain el cheapo doesn't get any more talk time to xxx girls while shooting her in the eye auto hangup
$agi->set_autohangup($seconds);
}
}
exit;
?>
[/code]
Now adjust this script however you need it with your tables.
The database tables are as follows
DROP TABLE IF EXISTS `tomb-raid-her`.`dialplan`;
CREATE TABLE `tomb-raid-her`.`dialplan` (
`destination` varchar(10) NOT NULL default '',
`description` varchar(250) NOT NULL,
`peak` double NOT NULL,
`offpeak` double NOT NULL,
`connection_fee` double NOT NULL,
PRIMARY KEY USING BTREE (`destination`,`description`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `tomb-raid-her`.`customerpackages`;
CREATE TABLE `tomb-raid-her`.`customerpackages` (
`cupkPackageId` int(10) unsigned NOT NULL auto_increment,
`cupkCustomerId` bigint(20) unsigned NOT NULL,
`cupkActive` tinyint(1) unsigned NOT NULL,
`cupkPackage` int(10) unsigned NOT NULL,
`cupkLastUpdated` datetime NOT NULL,
`cupkDateCreated` datetime NOT NULL,
`cupkAuditInfo` text NOT NULL,
`cupkPrice` decimal(10,2) NOT NULL,
`cupkCredit` decimal(10,2) NOT NULL,
PRIMARY KEY (`cupkPackageId`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;
# Make up your our package and customer tables
The next part is reall simple
on your outgoing route\context use the following, mine is below
[macro-dialout-trunk]
exten => s,n,AGI(credit.agi,${ARG1},${ARG2})
;exten => s,n, do what ever else you where going to do
the app in the above script pre-pay is the following:
[macro-pre-pay]
exten => s,1,NoOp(Making outbound call from ${ARG3} to ${ARG2})
exten => s,n,Set(TIMEOUT(absolute)=${ARG1})
exten => s,n,Answer
exten => s,n,Wait(2)
exten => s,n,Playback(not-enough-credit)
exten => s,n,Playtones(congestion)
exten => s,n,Congestion(5)
exten => h,1,NoOp(Hangup)
exten => i,1,NoOp(Invalid)
exten => t,1,NoOp(Timeout)
Thanks for all the help guys.