PHP code example of kalkulus / thetellara

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

    

kalkulus / thetellara example snippets


'providers' => [
    ...
    Kalkulus\Thetellara\ThetellaraServiceProvider::class,,
    ...
]

'aliases' => [
    ...
    'Thetellara' => Kalkulus\Thetellara\ThetellaraServiceProvider::class,
    ...
]



return [

    /**
     * Your environment. Either Production (prod) or Test (test)
     *
     */
    'tellerEnv' => getenv('THETELLER_ENV', 'test'),

    /**
     * Merchant ID From Theteller Dashboard
     *
     */
    'merchantId' => getenv('THETELLER_MERCHANT_ID'),

    /**
     * Theteller API Username
     *
     */
    'apiUsername' => getenv('THETELLER_API_USERNAME'),

    /**
     * Theteller API Key
     *
     */
    'apiKey' => getenv('THETELLER_API_KEY'),

    /**
     * Theteller Redirect Url
     *
     */
    'redirectUrl' => getenv('THETELLER_REDIRECT_URL'),
];


THETELLER_ENV=xxxx
THETELLER_MERCHANT_ID=xxxxxxxxxxxx
THETELLER_REDIRECT_URL=http://baseurl/payment/callback
THETELLER_API_USERNAME=xxxxxxxxxxxxxx
THETELLER_API_KEY=xxxxxxxxxxxxxxxxxx

// Laravel 8
Route::post('/pay', [App\Http\Controllers\PaymentController::class, 'redirectToGateway'])->name('pay');

Route::get('/payment/callback', 'PaymentController@handleGatewayCallback');




namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Kalkulus\Thetellara\Thetellara;
use Illuminate\Support\Facades\Redirect;

class PaymentController extends Controller
{
    /**
     * Redirect the User to Theteller Payment Page
     * @return Url
     */

    public function redirectToGateway()
    {
        $transactionId = mt_rand(100000000000,999999999999); // Must be unique for all transactions... 
        // You may use the function I wrote below for unique transaction id `genTransactionId()` 
        // In that case you keep track of the transaction IDs in payments table in the database. 
        // Hence call it as $transactionId = $this->genTransactionId();
        $email = request()->input('email'); //Customer email
        $amount = request()->input('amount');; //Amount with is in pesewas. So 200 is actually GHS 2.00
        $desc = request()->input('desc');
        try{
            $theteller = new Thetellara();
            $response =  $theteller->initialize($transactionId, $email, $amount, $desc);
            return redirect($response->checkout_url);

        }catch(\Exception $e) {
            return Redirect::back()->withMessage(['error'=>'The transaction has expired']);
        }
    }

    /**
     * Get Theteller payment information
     * @return void
     */
    public function handleGatewayCallback()
    {
        $theteller = new Thetellara();
        $response = $theteller->getPaymentDetails();
        dd($response);

        // Now you have the payment details,
        // you can then redirect or do whatever you want
    }


    /**
     * Get unique transaction ID
     * @return string
     */
    private function genTransactionId(){
        $this->transactionId = [
            'transactionId' => mt_rand(100000000000,999999999999)
        ];

        $rules = ['transactionId' => 'unique:payments'];

        $validate = Validator::make($this->transactionId, $rules)->passes();

        return $validate ? $this->transactionId['transactionId'] : $this->genTransactionId();
    }
}

bash
apt-get install php-curl
bash
php artisan vendor:publish --provider="Kalkulus\Thetellara\ThetellaraServiceProvider"