PHP code example of tnmdev / ussd

1. Go to this page and download the library: Download tnmdev/ussd library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tnmdev / ussd example snippets


$this->addPayload('key', $this->value());

$this->payload('key');

$this->addPayload('products', $array, true);

public function options(): array 
{
    return $this->map('humanized', 'products');
}

$this->addPayload('chosenProduct', $this->find('id', 'products'));

$this->payload('products', true);

namespace App\Screens;

use TNM\USSD\Screen;
use TNM\USSD\Http\Validates;

class EnterPhoneNumber extends Screen
{
    use Validates;

    protected function message() : string
    {
        return 'Enter your phone number';
    }
    
    //...
    
    protected function execute()
    {
        $this->validate($this->request, 'phone');
        $this->addPayload('phone', $this->value());
        return (new NextScreen($this->request))->render();
    }

    protected function rules() : string
    {
        return 'regex:/(088)[0-9]{7}/';
    }
}      


use TNM\USSD\Http\UssdRequestInterface;

class TruRouteRequest implements UssdRequestInterface
{
    /**
     * @var array
     */
    private $request;

    public function __construct()
    {
        $this->request = json_decode(json_encode(simplexml_load_string(request()->getContent())), true);
    }

    public function getMsisdn(): string
    {
        return $this->request['msisdn'];
    }
    // ...
}

use TNM\USSD\Http\UssdResponseInterface;

use TNM\USSD\Screen;

class TruRouteResponse extends XMLResponse
{
    protected function getPayload(): array
    {
        return [
            'type' => $this->screen->type(),
            'message' => $this->screen->getResponseMessage(),
        ];
    }

    protected function getTemplate(): string
    {
        return __DIR__ . '/response.xml';
    }
}

namespace TNM\USSD\Factories;

class RequestFactory
{
    public function make(): UssdRequestInterface
    {
        return match (request()->route('adapter')) {
            'flares' => resolve(FlaresRequest::class),
            default => resolve(TruRouteRequest::class),
        };
    }
}

namespace TNM\USSD\Factories;

class ResponseFactory
{
    public function make(): UssdResponseInterface
    {
        return match (request()->route('adapter')) {
            'flares' => resolve(FlaresResponse::class),
            default => resolve(TruRouteResponse::class),
        };
    }
}

$this->request->trail->setLocale('en');

public function message(): string 
{
    return __("screens.welcome_message");
}

public function execute()
{
    $locale = $this->value() == 'English' ? 'en' : 'fr';
    $this->request->trail->setLocale($locale);
    return (new NextScreen($this->request))->render();
}

// app/Screens/Subscribe.php

namespace App\Screens;

use TNM\USSD\Screen;

class Subscribe extends Screen
{
    public function message(): string
    {
        return "Please select a plan you want to subscribe to";
    }

    public function options(): array
    {
        return ['Plan 1', 'Plan 2', 'Plan 3'];
    }

    public function execute()
    {
        // save the request value to session object 
        // to access it in the next screen with $this->payload($key) 
        $this->addPayload('plan', $this->value());

        return (new ConfirmSubscription($this->request))->render();
    }
        
    public function previous(): Screen
    {
        return new Welcome($this->request);
    }
}

// app/Screens/ConfirmSubscription.php

namespace App\Screens;

use Exception;use TNM\USSD\Screen;
use TNM\USSD\Exceptions\UssdException;

class ConfirmSubscription extends Screen
{
    public function message(): string
    {
        return sprintf("Please confirm subscription to %s", $this->payload('plan'));
    }

    public function options(): array
    {
        return ['Confirm', 'Cancel'];
    }

    public function execute()
    {
        if ($this->value() === 'Cancel') return $this->previous()->render();
        
        $service = new SubscriptionService($this->request->msisdn);

        try {
       
            $service->subscribe($this->payload('plan'));
            return (new Subscribed($this->request))->render();
            
        } catch (Exception $exception) {
            throw new UssdException($this->request, "Subscription failed. Please try again later");
        }
    }
    
    public function previous(): Screen
    {
        return new Subscribe($this->request);
    }
}

php artisan vendor:publish --provider="TNM\USSD\USSDServiceProvider"

php artisan ussd:install

php artisan make:ussd <name>

php artisan ussd:list <phone>

php artisan ussd:audit <session-id>
bash
php artisan ussd:clean-up --days=30