PHP code example of infinitypaul / laravel-cbs

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

    

infinitypaul / laravel-cbs example snippets


'aliases' => [
    ...
    'Cbs' => Infinitypaul\Cbs\Facades\Cbs::class,
    ...
]

return [
    /**
     * Client ID From CBS 
     *
     */
    'clientId' => getenv('CBS_CLIENT_ID'),

    /**
     * Secret Key From CBS 
     *
     */
    'secret' => getenv('CBS_SECRET'),

    /**
     * switch to live or test
     *
     */
    'mode' => getenv('CBS_MODE', 'test'),

    /**
     * CBS Test Payment URL
     *
     */
    'testUrl' => getenv('CBS_TEST_BASE_URL'),

    /**
     * CBS Live Payment URL
     *
     */
    'liveURL' => getenv('CBS_LIVE_BASE_URL'),


    /**
     * Revenue Head
     *
     */
    'revenueHead' => getenv('CBS_REVENUE_HEAD'),

    /**
     * Revenue Head
     *
     */
    'categoryId' => getenv('CBS_CATEGORY_ID'),
];

// Laravel 5.1.17 and above
Route::post('/pay', 'InvoiceController@redirectToGateway')->name('pay'); 

Route::post('/pay', [
    'uses' => 'InvoiceController@redirectToGateway',
    'as' => 'pay'
]);

Route::post('/pay2', [
    'uses' => 'InvoiceController@getInvoice',
    'as' => 'data'
]);

Route::get('/payment/callback', 'InvoiceController@handleGatewayCallback')->name('callback');

// Laravel 5.0
Route::get('payment/callback', [
    'uses' => 'InvoiceController@handleGatewayCallback'
]); 



namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Cbs;

class InvoiceController extends Controller
{

    /**
     * Redirect the User to Cbs Payment Page
     * @return Url
     */
    public function redirectToGateway()
    {
        return Cbs::setInvoice()->redirectNow();
    }

    /**
     * Get The Invoice Information
     * @return array
     */
    public function getInvoice()
    {
        return Cbs::setInvoice()->getData();
    }

    /**
     * Obtain Cbs payment information
     * @return void
     */
    public function handleGatewayCallback()
    {
        $paymentDetails = Cbs::getPaymentData();

        dd($paymentDetails);
        // Now you have the payment details,
        // you can store the authorization_code in your db to allow for recurrent subscriptions
        // you can then redirect or do whatever you want
    }
}

/**
 *  This fluent method does all the dirty work of sending a POST request with the form data
 *  to Cbs Api, then it gets the payment Url and redirects the user to Cbs
 *  Payment Page. I abstracted all of it, so you don't have to worry about that.
 *  Just eat your cookies while coding!
 */
Cbs::setInvoice()->redirectNow();

/**
*  SetInvoice can also accept an array instead of a request object and you are good to go, it will be in this format
 */

        $data = [
            "payerID" => "ST-000408",
  "amount" => "230",
  "description" => "Testing",
  "callback" => route('callback'),
  "quantity" => "5",
            'externalRefNumber' => time()
];
        $data = Cbs::setInvoice($data)->getData();


/**
 * This fluent method does all the dirty work of verifying that the just concluded transaction was actually valid,
 */
Cbs::getPaymentData();

/**
 * This method gets the invoice information generated on Cbs
 * @returns array
 */
Cbs::setInvoice()->getData();

bash
php artisan vendor:publish --provider="Infinitypaul\Cbs\CbsServiceProvider"
 php
CBS_CLIENT_ID=****
CBS_SECRET=****
CBS_REVENUE_HEAD=***
CBS_CATEGORY_ID=***
CBS_LIVE_BASE_URL=***
CBS_TEST_BASE_URL=***