Thursday, March 31, 2016

Installation of A2billing version 2.2 on AsteriskNow 6

After compiling my last post on installing A2billing, i noticed i installed the old version (1.94), not only it was old but also has a lot of complications and bugs, so i think i was worth compiling another with include installation of latest version of a2billing. This howto's has steps of installing the lastest version of A2billing as the time of writing of this post.

This howto's was tested on
AsteriskNOW6.12
FreePBX v12
A2billing v2.2
Asterisk v11

All the command below should be run as root user (#)

1. Prepare the server
Prepare the server by updating it and installing additional packages
yum -y update && yum -y groupinstall core && yum -y groupinstall base && yum -y install epel-release
yum -y install php-mcrypt php-xml perl-DBD-Pg git wget

2. a. Get A2billing from source

cd /usr/src
wget --no-check-certificate https://github.com/Star2Billing/a2billing/archive/master.tar.gz
tar zxvf master.tar.gz 
mv a2billing-master/ a2billing

2.b. Get vendor packages using composer.
cd /usr/src/a2billing
curl -sS https://getcomposer.org/installer | php
php composer.phar update
php composer.phar install

3.a. Create MySQL root password
If you did not create a mysql root password during the asterisk install you should create one now.
mysqladmin -u root password 'mysql-root-password'

3.b. Create A2Billing Database
cd /usr/src/a2billing
mysql -u root -p < DataBase/mysql-5.x/a2billing-createdb-user.sql
which, after you enter your root password, will create:

Database name: mya2billing
Database user: a2billinguser
Database user password: a2billing

Now run script to create tables and insert some basic configuration data
cd /usr/src/a2billing/DataBase/mysql-5.x
./install-db.sh
Answer questions as follows:

Enter Database Name: mya2billing
Enter Hostname: localhost
Enter UserName: root
Enter Password: {mysql-root-password}

Copy a2billing.conf file to /etc/ and edit it.
cp /usr/src/a2billing/a2billing.conf /etc/

Make sure the following parameters are set as shown.
vi /etc/a2billing.conf
[database]
hostname = localhost
port = 3306
user = a2billinguser
password = a2billing
dbname = mya2billing
dbtype = mysql

4. Create files and set permissions

Only required if not using Asterisk Realtime.  If unsure then proceed as if you are not using Asterisk Realtime.

Some of these files assume you have FreePBX installed and not just Asterisk.  If you just have Asterisk installed then use the files specified in the INSTALL.rst instructions included with A2billing source files.
touch /etc/asterisk/additional_a2billing_iax.conf
touch /etc/asterisk/additional_a2billing_sip.conf
echo \#include additional_a2billing_sip.conf >> /etc/asterisk/sip_custom.conf
echo \#include additional_a2billing_iax.conf >> /etc/asterisk/iax_custom.conf
chown -Rf asterisk:asterisk /etc/asterisk/additional_a2billing_iax.conf
chown -Rf asterisk:asterisk /etc/asterisk/additional_a2billing_sip.conf
chmod 777 /etc/asterisk

5. Run sound installation script
cd /usr/src/a2billing/addons/sounds
./install_a2b_sounds.sh
chown -R asterisk:asterisk /var/lib/asterisk/sounds/
Add the following to /etc/asterisk/manager_custom.conf

You can also do it via the Freepbx Asterisk API module.  If you don't have Freepbx installed then you should follow the INSTALL.rst instructions included with the a2billing source files.
[myasterisk]
secret=mycode
deny=0.0.0.0/0.0.0.0
permit=127.0.0.1/255.255.255.0
read=system,call,log,verbose,command,agent,user
write=system,call,log,verbose,command,agent,user

Note:
Am changing default a2billing root folder from /var/www/html/a2billing to /var/www/html/voipbilling due to security reasons since hackers are aware to that directory so it's one step to server security

Add fwconsole blacklist

This is very important otherwise a fwconsole restart or fwconsole chown command will delete necessary symlinks in a2billing.
vim /etc/asterisk/freepbx_chown.conf
Add the following and save
[blacklist]
item=/var/www/html/voipbilling

6. Set up a2billing web folders

A2billing has 3 GUI sections.  admin, agent, and customer.
mkdir /var/www/html/voipbilling
cp -rf /usr/src/a2billing/admin /var/www/html/voipbilling
cp -rf /usr/src/a2billing/agent /var/www/html/voipbilling
cp -rf /usr/src/a2billing/customer /var/www/html/voipbilling
cp -rf /usr/src/a2billing/common /var/www/html/voipbilling
cp -rf /usr/src/a2billing/vendor /var/www/html/voipbilling
chown -R asterisk:asterisk /var/www/html/voipbilling
chmod 755 /var/www/html/voipbilling/admin/templates_c
chmod 755 /var/www/html/voipbilling/customer/templates_c
chmod 755 /var/www/html/voipbilling/agent/templates_c
Prevent public access to /common and /vendor folders.
echo 'Deny from all' > /var/www/html/voipbilling/common/.htaccess
echo 'Deny from all' > /var/www/html/voipbilling/vendor/.htaccess
Check admin web interface

From a web browser go to http://{my-ip-address}/voipbilling/admin and make sure you get the authentication screen.  Log in as follows.

user: root
password: changepassword

Once logged in click change password link at bottom left and change it.

7. Set up AGI
mkdir /var/lib/asterisk/agi-bin
cd /usr/src/a2billing/AGI             
cp a2billing.php /var/lib/asterisk/agi-bin
cp a2billing_monitoring.php /var/lib/asterisk/agi-bin
chown -R asterisk:asterisk /var/lib/asterisk/agi-bin
chmod 755 /var/lib/asterisk/agi-bin/a2billing.php
chmod 755 /var/lib/asterisk/agi-bin/a2billing_monitoring.php
ln -s /var/www/html/voipbilling/common/lib /var/lib/asterisk/agi-bin/lib
ln -s /var/www/html/voipbilling/vendor /var/lib/asterisk/vendor
Add the following extensions to /etc/asterisk/extensions_custom.conf

If not using Freepbx then this would go into /etc/asterisk/extensions.conf.  These are just examples.  Depending on what you are doing and how you are doing it some may not apply and some may not work.

Customizing this to your needs is considered beyond the scope of this document.  Search for documentation on Asterisk custom extensions and dialplans.  If possible try find examples specific to A2Billing.

Open the below file
vim  /etc/asterisk/extensions_custom.conf

And add the contents below and save.
[macro-dialout-trunk-predial-hook]
exten => s,1,GotoIf($["${OUT_${DIAL_TRUNK}:4:4}" = "A2B/"]?custom-freepbx-a2billing,${OUTNUM},1:2)
exten => s,2,MacroExit

[custom-freepbx-a2billing] exten => _X.,1,DeadAGI(a2billing.php,${OUT_${DIAL_TRUNK}:8}) exten => _X.,n,Hangup()
[a2billing] exten => _X.,1,Answer exten => _X.,n,Wait(1) exten => _X.,n,deadAGI(a2billing.php,1) exten => _X.,n,Hangup
[a2billing-callback] exten => _X.,1,deadAGI(a2billing.php,1,callback) exten => _X.,n,Hangup
[a2billing-cid-callback] exten => _X.,1,deadAGI(a2billing.php,1,cid-callback,34) ;last parameter is the callback area code exten => _X.,n,Hangup
[a2billing-all-callback] exten => _X.,1,deadAGI(a2billing.php,1,all-callback,34) ;last parameter is the callback area code exten => _X.,n,Hangup
[a2billing-did] exten => _X.,1,deadAGI(a2billing.php,1,did) exten => _X.,2,Hangup
[a2billing_callingcard] ; CallingCard application exten => _X.,1,NoOp(A2Billing Start) exten => _X.,n,DeadAgi(a2billing.php|1) exten => _X.,n,Hangup
[a2billing_voucher] exten => _X.,1,Answer(1) exten => _X.,n,DeadAgi(a2billing.php|1|voucher) ;exten => _X.,n,AGI(a2billing.php|1|voucher|44) ; will add 44 in front of the callerID exten => _X.,n,Hangup
[custom-a2billing-did] exten => _X.,1,deadAGI(a2billing.php,1,did) exten => _X.,2,Hangup
[custom-a2billing] exten => _X.,1,deadAGI(a2billing.php,1) exten => _X.,n,Hangup

8. Connect FreePBX with A2billing

8.a. Add custom Trunk

Go into FreePBX GUI>Connectivity>Trunks>Add Trunk>Add Custom Trunk give it a name and add the following custom dial string:

A2B/1
This is the trunk that is used to send calls out via A2Billing.  Simply select this trunk in outbound routes.  The /1 refers to which agi-conf is going to be used.

8.b. Add custom destinations

Add custom destinations to FreePBX via FreePBX GUI>Admin>Custom Destinations

These destinations are referring to the custom extensions created above in /etc/asterisk/extensions_custom.conf

Target: custom-a2billing,${EXTEN},1
Description: A2Billing - Callthrough

Target: custom-a2billing-did,${EXTEN},1
Description: A2Billing - DID

Start or restart FreePBX
fwconsole restart
Recurring Services
Recurring services are handled via the /etc/crontab. Make directory for A2Billing cron PID
mkdir -p /var/run/a2billing
chown asterisk:asterisk /var/run/a2billing

Copy cron files to some permanent location such as /usr/local
mkdir -p /usr/local/a2billing
cp -R /usr/src/a2billing/Cronjobs /usr/local/a2billing/
ln -sf /var/www/html/voipbilling/common/lib /usr/local/a2billing/Cronjobs/lib
chown -R asterisk:asterisk /usr/local/a2billing

9. Add cron

Add the cron jobs to /var/spool/cron/asterisk as follows
Run command below:
crontab -e -u asterisk

Copy and paste the contents below to opened cron file.
# update the currency table
0 6 * * * php /usr/local/a2billing/Cronjobs/currencies_update_yahoo.php
# manage the monthly services subscription
0 6 1 * * php /usr/local/a2billing/Cronjobs/a2billing_subscription_fee.php
# To check account of each Users and send an email if the balance is less than the user have choice.
0 * * * * php /usr/local/a2billing/Cronjobs/a2billing_notify_account.php
# this script will browse all the DID that are reserve and check if the customer need to pay for it.
# bill them or warn them per email to know if they want to pay in order to keep their DIDs.
0 2 * * * php /usr/local/a2billing/Cronjobs/a2billing_bill_diduse.php
# This script will take care of the recurring service.
0 12 * * * php /usr/local/a2billing/Cronjobs/a2billing_batch_process.php
# To generate invoices and for each user.
0 6 * * * php /usr/local/a2billing/Cronjobs/a2billing_batch_billing.php
# to proceed the autodialer
*/5 * * * * php /usr/local/a2billing/Cronjobs/a2billing_batch_autodialer.php
# manage alarms
0 * * * * php /usr/local/a2billing/Cronjobs/a2billing_alarm.php
# manage archive
0 12 * * * php /usr/local/a2billing/Cronjobs/a2billing_archive_data_cront.php
#autorefill
0 10 21 * * php /usr/local/a2billing/Cronjobs/a2billing_autorefill.php
15 * * * * php /usr/local/a2billing/Cronjobs/a2billing_batch_cache.php

10. Add log files

mkdir -p /var/log/a2billing
touch /var/log/a2billing/cront_a2b_alarm.log
touch /var/log/a2billing/cront_a2b_autorefill.log
touch /var/log/a2billing/cront_a2b_batch_process.log
touch /var/log/a2billing/cront_a2b_archive_data.log 
touch /var/log/a2billing/cront_a2b_bill_diduse.log 
touch /var/log/a2billing/cront_a2b_subscription_fee.log 
touch /var/log/a2billing/cront_a2b_currency_update.log 
touch /var/log/a2billing/cront_a2b_invoice.log 
touch /var/log/a2billing/cront_a2b_check_account.log 
touch /var/log/a2billing/a2billing_paypal.log
touch /var/log/a2billing/a2billing_epayment.log
touch /var/log/a2billing/a2billing_api_ecommerce_request.log
touch /var/log/a2billing/a2billing_api_callback_request.log
touch /var/log/a2billing/a2billing_api_card.log
touch /var/log/a2billing/a2billing_agi.log
chown -R asterisk:asterisk /var/log/a2billing

Add index file to prevent browsing of folders

touch /var/www/html/voipbilling/index.html

Log into the webpage

http://{my-ip-address}/voipbilling/admin
user:  root
pass: mynewpassword

Or use default password changepassword if you have not changed it yet and then click the change password link at the bottom left.

11. Set Asterisk version in A2Billing

Go into System settings>Global list.  Go to the bottom left and set to display all.  From your web browser search for "asterisk_version".  It will find that setting twice.  One for Global group and one for agi-conf1 group.  Change both accordingly.  If using Asterisk v11 then use "1_11"

Disable Asterisk Realtime if not setting up the following Asterisk Realtime procedure. Go to System settings>Global search for "realtime".  Change that setting to "no".

That's all, please let me know if you face any difficult in implementing the steps above, credits should also go to composer of original howto found here,  where i customized it a bit to fits my needs.