Billing Event v1 - pavlyuts/portabilling GitHub Wiki

BillingEvent class

Purpose

The class is intended for easy hanlding of PortaBilling events. External system provisioning framework (ESPF) is used for handle events and then call external system with webhooks. BillingEvent class authentificates the call, decodes data and dispach the event to the proper handling method with event data.

Prerequisite

  • setup and configure web-server to hanldle events
    • Web-server must be configured properly to accept webhook calls on the desired endpoint
  • Enable and configure ESPF
    • ESPF must be enabled according to ESPF configuration handbook
    • IMPORTANT: API version 1 must be set in ESPF configuration due of "simplified" API (aka V2) is not supported.
    • ESPF webhook address must be set properly.
    • The desired event set should be allowed and EvenSender handler must be enabled and subscribed for these events.

Usage

  • Define the MyEvent class, extending BillingEvent and add a method for every event you want to handle. The metod name derived from the event name by removing "/" and camelcase it, except of starting wih DID and IP abreviatons. The full list of available events/method names is here.
  • Create the MyEvent class istance in the index.php on the desired endpoit, pass it username, password, log instance and error handling flag.
  • Check the log on error/info/debug.

Example

Assume you configure ESPF to sent your events to http://myeventserver/event/ with username myuser and password mypass. The server has PHP and the package installed. The project structure:

project
  |--vendor
  |--lib
  |  |--MyEvents.php
  |--public
     |--event
        |--index.php

Web server root set to project/public and index to index.php. Composer configured for psr-4 autoload namespace MyApp from directory lib like this:

    "require": {
        "monolog/monolog": "^2.0",
	"pavlyuts/portabilling": "*",
    },
    "autoload": {
        "psr-4": {
            "MyApp\\": "lib/"
        }
    }

File MyEvents.php:

<?php
namespace MyApp;

use Portaone\BillingEvent;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

//extending the class with custom __construct and handling methods
class MyEvents extends BillingEvent {

    public function __construct() {
        //Create logging instance
        $log = new Logger('Events');
        $log->pushHandler(new StreamHandler('event.log', Logger::DEBUG));
        
        //Run the class with credentials and logger
        parent::__construct(array ('user' => 'testuser', 'password' => 'testpassword'), $log);
    }

    //Event CustomField/Changed will be dispatched here with it's data passed as stdClass parameter
    function customFieldChanged($data) {
        $this->logDebug('CAlled handler for CustomField/Changed', array ($data));
        //Business logic must be here, but just log it for now
    }

}

File index.php

<?php

namespace MyApp;

require '../../vendor/autoload.php';

//And jest create the instance!
$a = new MyEvents();

//That's all! )))