Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thefinn93/ed9b7f638f3d4af1b06f to your computer and use it in GitHub Desktop.
Save thefinn93/ed9b7f638f3d4af1b06f to your computer and use it in GitHub Desktop.

Pretending to be the non-free billing app

FusionPBX doesn't do billing or LCR out of the box, but one guy will sell you closed, proprietary, non-free billing and LCR modules that will integrate nicely into FusionPBX. To help with this, there are a number of places in the free FusionPBX codebase that run things that these non-free apps need. Obviously these are all completely undocumented, until now.

These work by checking for an app named "billing". Specifically, they look for the app_config.php file in the billing folder. The conditional usually looks like this:

if (file_exists($_SERVER['DOCUMENT_ROOT'].PROJECT_PATH."/app/billing/app_config.php")) {

Then it proceeds to assume a number of functions, files, database tables and columns, and other things exist. There is absolutely no checking of most of these, some of them are checked but in ways that create PHP errors. I have been finding these mostly making an app named billing and watching the PHP and Postgres error logs.

Files and Functions

There are several files who's paths are hard-coded into FusionPBX's source and are require'd from different places, all of which will break if you don't have these files.

currency.php

app/billing/resources/functions/currency.php is called in 3 places:

  • app/extensions/extension_edit.php
  • app/fax/fax_edit.php
  • app/destinations/destination_edit.php

It is expected to provide the following functions:

I have not found any functons that currency.php is expected to provide (yet)

rating.php

app/billing/resources/functions/rating.php is called in 5 places:

  • app/extensions/extension_edit.php
  • app/xml_cdr/xml_cdr.php
  • app/xml_cdr/v_xml_cdr_import.php
  • app/fax/fax_edit.php
  • app/destinations/destination_edit.php

It is expected to provide the following functions:

call_cost($lcr_rate, $lcr_first_increment, $lcr_second_increment, $time)

Returns the cost of a given call. Only called from app/xml_cdr/v_xml_cdr_import.php. It's called twice from there to determine the cost of the call. I think: Once for the cost to the user, eg how much you should bill for, and once for the cost to you, eg how much you're going to be billed for the call.

currency_convert($price, $source_currency, $dest_currency) Called from a number of places when it needs to convert a price in one currency into another.

number_series($destination_number) I have no idea what this does. I found that returning 0 makes it generate valid SQL in app/xml_cdr/xml_cdr.php, which builds an SQL query string using the return value and just ramming it in there without any escaping.

It also seems to need to define $_SESSION['billing']['currency']['text']. There is some code to the strlen of $_SESSION['billing']['currency']['text'] and defaults to "USD", but then you get PHP warnings.

Database

Tables

There are several tables that FusionPBX expects the billing and LCR modules to create:

Table: v_billings

This is referenced from several places, seems to be expected to have the following columns:

Column Name Type What I think it is Notes
domain uuid The UUID of the domain that this entry corresponds to
lcr_profile ? - Setting the type to text seems to work
type_value text The name of the domain that this entry corresponds to
currency text The three letter abbreviation for the type of currency used in this entry
balance numeric The current balance in the given account
old_balance numeric Who knows...

Table: v_lcr

Column Name Type What I think it is Notes
carrier_uuid uuid I assume 'carrier' is an origination or termination provider See v_carriers table
currency text The three letter abbreviation for the type of currency used in this entry
enabled bool If this LCR route is valid bool is an assumption
lcr_direction text If this is for inbound or outbound routing This is either the text "inbound" or "outbound"
digits numeric
talk_increment numeric LCR seems to have different rates for different parts of the call or something? idk
connect_increment numeric
rate numeric The amount it costs to use this route I assume rate is per minute
lcr_profile uuid
date_start date The time that this route price is valid from
date_end date The time that this route price expires I have no idea how LCR works so these could be totally wrong

Table: v_carriers

Column Name Type What I think it is Notes
carrier_uuid uuid A UUID to identify this carrier I assume a 'carrier' is an origination or termination provider
carrier_name text A human-friendly name for this carrier

Columns

There are also some changes to existing tables that seem to be expected:

Table Column Type
v_xml_cdr call_sell numeric
v_xml_cdr call_buy numeric
v_xml_cdr carrier_name text

XML CDR

The XML CDR that gets generated with every call needs to have the following elements:

  • variables->lcr_carrier
  • variables->lcr_rate
  • variables->lcr_user_rate
  • variables->lcr_query_digits

There may be more that I have not found yet.

@daniel-lucio
Copy link

Hi here. Go here lhttps://okay.network/communications/billing-for-fusionpbx-with-freeswitch.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment