PHP code example of prinx / txtpay

1. Go to this page and download the library: Download prinx/txtpay 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/ */

    

prinx / txtpay example snippets




use Txtpay\MobileMoney;

$payment = new MobileMoney;

$amount = 1; // 1 GHC
$phone = '233...';
$network = 'MTN'; // MTN|AIRTEL|VODAFONE

$request = $payment->request($amount, $phone, $network);

$request = $payment->request($amount, $phone, $network, $voucherCode);

// or

$payment->setVoucherCode($voucherCode);
$request = $payment->request($amount, $phone, $network);

$request = $payment->request($amount, $phone, $network);

if ($request->isBeingProcessed()) {
    $status = $request->getStatus();

    // ...
} else {
    $error = $request->getError();

    // ...
}

// File: callback.php


$callback = new Callback;

// This is just an example.
// Write your controller the way you are used to with your favorite framework.

namespace Controllers;

use Txtpay\Callback;

class MobileMoneyCallbackController extends Controller
{
    public function processCallback()
    {
        $callback = new Callback;
    }
}

$callback->success(function (Callback $callback) {
    // Transaction was successful. Do stuff.
})->failure(function (Callback $callback) {
    // Transaction failed. Do stuff.
})->always(function (Callback $callback) {
    // Do stuff whether transaction was successful or not.
});

$callback->process();

$callback = new Callback;

$callback->success(function (Callback $callback) {
    //
})->failure(function (Callback $callback) {
    //
})->always(function (Callback $callback) {
    //
})->process();

$callback = new Callback;

$callback->process(function (Callback $callback) {
    //
});

use Txtpay\Callback;

$callback = new Callback;

$callback->on('000', function (Callback $callback) {
    //
})->on('101', function (Callback $callback) {
    //
})->on(['code' => '000', 'phone' => '233...'], function (Callback $callback) {
    //
})->success(function (Callback $callback){
    // We can still chain the success or failure methods.
})->failure(function (Callback $callback) {
    //
})->process();

$requestCode = $callback->getCode();

$requestStatus = $callback->getStatus(); // approved, declined...

$requestDetails = $callback->getDetails();

$transactionId = $callback->getId();

$phone = $callback->getPhone(); // 233...

$network = $callback->getNetwork(); // MTN, AIRTEL, VODAFONE, ...

$amount = $callback->getAmount();

$currency = $callback->getCurrency(); // GHS

$payload = $callback->getPayload();

$transactionId = $callback->getPayload('id');
$transactionCode = $callback->getPayload('code');

$message = $callback->getMessage();

[
    '000'     => 'Transaction successful. Your transaction ID is '.$transactionId,
    '101'     => 'Transaction failed. Insufficient fund in wallet.',
    '102'     => 'Transaction failed. Number non-registered for mobile money.',
    '103'     => 'Transaction failed. Wrong PIN. Transaction timed out.',
    '104'     => 'Transaction failed. Transaction declined',
    '114'     => 'Transaction failed. Invalid voucher',
    '909'     => 'Transaction failed. Duplicate transaction id.',
    'default' => 'Transaction failed.',
];

$callback->success(function (Callback $callback) {
    $message = $callback->getMessage();
});


// SmsService is your own implementation.
$sms = new SmsService();

$callback->success(function (Callback $callback) use ($sms) {
    $message = $callback->getMessage();
    $phone = $callback->getPhone();

    $sms->send($message, $phone);
});

class CallbackHandler
{
    public function callbacks(Callback $callback)
    {
        //
    }
}

class CallbackHandler
{
    public function callbacks(Callback $callback)
    {
        return [
            ['000', function (Callback $callback) {
                $message = $callback->getMessage();
            }],
            ['101', function (Callback $callback) {
                //
            }],
        ];
    }
}

class CallbackHandler
{
    public function callbacks(Callback $callback)
    {
        return [
            ['000', function (Callback $callback) {
                //
            }],
            ['000', function (Callback $callback) {
                // Another callback that will be run for the same code.
            }],
            ['101', function (Callback $callback) {
                //
            }],
        ];
    }
}

class CallbackHandler
{
    public function callbacks(Callback $callback)
    {
        return [
            ['000', [
                function (Callback $callback) {
                    //
                },
                function (Callback $callback) {
                    // Another callback that will be run for the same code.
                },
            ]],
            ['101', function (Callback $callback) {
                //
            }],
        ];
    }
}

class CallbackHandler
{
    public function callbacks(Callback $callback)
    {
        return [
            ['success', function (Callback $callback) {
                //
            }],
            ['failure', function (Callback $callback) {
                //
            }],
            ['always', function (Callback $callback) {
                //
            }],
        ];
    }
}

class CallbackHandler
{
    public function callbacks(Callback $callback)
    {
        return [
            [['code' => '000', 'phone' => '233...'], function (Callback $callback) {
                    //
            }],
        ];
    }
}

class CallbackHandler
{
    public function callbacks(Callback $callback)
    {
        return [
            ['000', function (Callback $callback) {
                //
            }],
            [['code' => '000', 'phone' => '233...'], function (Callback $callback) {
                //
            }],
            ['success', function (Callback $callback) {
                //
            }],
            ['always', function (Callback $callback) {
                //
            }],
        ];
    }
}

class CallbackHandler
{
    public function callbacks(Callback $callback)
    {
        return [
            ['000', 'sendSms'],
            ['101', ['notifyAdmin', 'sendSms']]
            ['always', 'log'],
        ];
    }

    public function notifyAdmin(Callback $callback)
    {
        // Notify admin...
    }

    public function sendSms(Callback $callback)
    {
        //
    }

    public function log(Callback $callback)
    {
        //
    }
}

// Do not forget to import the class from it namespace
use App\Helpers\CallbackHandler;

$callback = new Callback;

$callback->process(CallbackHandler::class);

$callback->setLogFolder($path);
ini
TXTPAY_LOCAL_LOG_ENABLED=false
ini
TXTPAY_SLACK_LOG_WEBHOOK=https://
ini
TXTPAY_SLACK_LOG_ENABLED=false
ini
TXTPAY_LOG_ENABLED=false