PHP code example of prevailexcel / laravel-bani

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

    

prevailexcel / laravel-bani example snippets


'providers' => [
    ...
    PrevailExcel\Bani\BaniServiceProvider::class,
    ...
]

'aliases' => [
    ...
    'Bani' => PrevailExcel\Bani\Facades\Bani::class,
    ...
]



return [

    /**
     * Tribe Account Ref Key From Bani Dashboard
     *
     */
    'tribeAccountRef' => getenv('BANI_TRIBE_ACCOUNT_REF'),

    /**
     * Public Key From Bani Dashboard
     *
     */
    'publicKey' => getenv('BANI_PUBLIC_KEY'),

    /**
     * Merchant Key From Bani Dashboard
     *
     */
    'merchantKey' => getenv('BANI_PRIVATE_KEY'),

    /**
     * Merchant Key From Bani Dashboard
     *
     */
    'accessToken' => getenv('BANI_ACCESS_TOKEN'),

    /**
     * You enviroment can either be live or stage.
     * Make sure to add the appropriate API key after changing the enviroment in .env
     *
     */
    'env' => env('BANI_ENV', 'stage'),

    /**
     * BANI Live URL
     *
     */
    'liveUrl' => env('BANI_LIVE_URL', "https://live.getbani.com/api/v1"),

    /**
     * BANI Stage URL
     *
     */
    'stageUrl' => env('BANI_STAGE_URL', "https://stage.getbani.com/api/v1"),
];

BANI_TRIBE_ACCOUNT_REF=**-**************************
BANI_PUBLIC_KEY=***_****_*********************
BANI_PRIVATE_KEY=**********************
BANI_ACCESS_TOKEN=****************************************************
BANI_ENV=stage

// Laravel 5.1.17 and above
Route::post('/pay', 'PaymentController@createPayment')->name('pay');
Route::callback(PaymentController::class, 'handleGatewayCallback');
Route::webhook(WebhookController::class, 'handleWebhook');

// Laravel 8 & 9
Route::post('/pay', [PaymentController::class, 'createPayment'])->name('pay');
Route::callback(PaymentController::class, 'handleGatewayCallback');
Route::webhook(WebhookController::class, 'handleWebhook');



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PrevailExcel\Bani\Facades\Bani;

class PaymentController extends Controller
{  
    /**
     * You collect data from your blade form
     * and this returns the Bani Pop Widget
     */
    public function createPayment()
    {
        try {
            return Bani::payWithwidget();
        } catch (\Exception $e) {
            return redirect()->back()->withMessage(['msg' => $e->getMessage(), 'type' => 'error']);
        }
    }
    
    public function handleGatewayCallback()
    {
        // verify transaction and get data
        $data = bani()->getPaymentData();

        // Do anything you want
        dd($data);
    }
}



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PrevailExcel\Bani\Facades\Bani;

class PaymentController extends Controller
{  
    /**
     * You collect data from your blade form
     * and this returns the Account details for payment
     */
    public function createPayment()
    {
        try {
                $data = [
                    "pay_amount" => 10000,
                    "holder_account_type" => "temporary",
                    "customer_ref" => "MC-69941173958782368244",
                    "custom_data" => ["id" => 1, "color" => "white"],
                ];
                
                // You can use the global helper bani()>method() or the Facade Bani:: method().
                return bani()->bankTransfer($data);
        } catch (\Exception $e) {
            return redirect()->back()->withMessage(['msg' => $e->getMessage(), 'type' => 'error']);
        }
    }

    public function handleWebhook()
    {
        // verify webhook and get data
        bani()->getWebhookData()->proccessData(function ($data) {
            // Do something with $data
            logger($data);
            // If you have heavy operations, dispatch your queued jobs for them here
            // OrderJob::dispatch($data);
        });
        
        // Acknowledge you received the response
        return http_response_code(200);
    }
}

protected $except = [
    'bani/webhook',
];

// For all methods, you can get data from request()

/**
 * Check if the customer already exist
 * @returns array
 */
Bani::customer(?string $phone, ?string $ref);
// Or
request()->phone;
bani()->customer();


/**
 * Create a customer object
 * @returns array
 */
Bani::createCustomer();
// Or
bani()->createCustomer();


/**
 * Update a customer object
 * @returns array
 */
Bani::updateCustomer();
// Or
bani()->updateCustomer();


/**
 * Fetch your customer delivery/billing address(es).
 */
Bani::listBillingAddress();
// Or
bani()->listBillingAddress();


/**
 * Add customer delivery/billing address(es)
 * @returns array
 */
Bani::addBillingAddress();
// Or
bani()->addBillingAddress();


/**
 * Uupdate customer's delivery/billing address
 * @returns array
 */
Bani::updateBillingAddress();
// Or
bani()->updateBillingAddress();


/**
 * Delete customer's delivery/billing address
 * @returns array
 */
Bani::deleteBillingAddress();
// Or
bani()->deleteBillingAddress();

/**
 * Accept payments from customers via bank transfer
 * @returns array
 */
Bani::bankTransfer();

/**
 * Accept payments via mobile money from customers
 * @returns array
 */
Bani::mobileMoney();

/**
 * Accept payments from customers via crypto and get settled in fiat
 * @returns array
 */
Bani::payWithCrypto();

/**
 * generate a deposit address to accept strictly DLT payments 
 * and get settled with the same currency
 */
Bani::getWalletAddress();

/**
 * Accept one-click payment from customers via bani shopper wallet
 * @returns array
 */
Bani::startPayWithBaniShopper();

/**
 * Complete 2nd step in payment from customers via bani shopper wallet
 * @returns array
 */
Bani::completePayWithBaniShopper();

/**
 * The fastest and most secure way to verify mobile numbers using USSD instead of SMS OTPs.
 * @returns array
 */
Bani::verifyPhone(?string $phone);

/**
 * This endpoint is used to confirmed if the customer phone number has been verified
 * @returns array
 */
Bani::checkUssdStatus(?string $verification_reference);

/**
 * This endpoint can be used to carry out a lookup on customer account
 * @returns array
 */
Bani::confirmEwallet($wallet, $tag, ?string $type);
bash
php artisan vendor:publish --provider="PrevailExcel\Bani\BaniServiceProvider"