PHP code example of unipay / unipay-bd

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

    

unipay / unipay-bd example snippets


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Unipay\BD\Facades\Payment;
use Unipay\BD\DTOs\PaymentRequest;

class CheckoutController extends Controller
{
    public function processCheckout(Request $request)
    {
        // Choose gateway driver dynamically ('bkash', 'nagad', 'rocket', 'upay', 'cellfin', 'sslcommerz', 'shurjopay')
        $gateway = $request->input('gateway', 'bkash');

        $paymentRequest = new PaymentRequest(
            amount: 1250.00,
            invoiceId: 'INV-' . time(),
            customerMobile: '01700000000',
            callbackUrl: route('checkout.callback')
        );

        $response = Payment::driver($gateway)->createPayment($paymentRequest);

        if ($response->isSuccessful() || $response->isPending()) {
            // Redirect customer to MFS / Gateway checkout page
            return redirect()->away($response->redirectUrl);
        }

        return back()->with('error', $response->message);
    }
}

use Unipay\BD\Events\PaymentSucceeded;
use Unipay\BD\Events\PaymentFailed;
use Unipay\BD\Events\PaymentRefunded;

protected $listen = [
    PaymentSucceeded::class => [
        \App\Listeners\MarkOrderAsPaid::class,
    ],
    PaymentFailed::class => [
        \App\Listeners\HandleFailedPayment::class,
    ],
];

namespace App\Listeners;

use Unipay\BD\Events\PaymentSucceeded;
use App\Models\Order;

class MarkOrderAsPaid
{
    public function handle(PaymentSucceeded $event)
    {
        $response = $event->response; // PaymentResponse DTO
        
        $order = Order::where('invoice_id', $response->invoiceId)->first();

        if ($order) {
            $order->update([
                'status' => 'paid',
                'transaction_id' => $response->transactionId,
                'gateway' => $response->gatewayName,
            ]);
        }
    }
}

use Unipay\BD\Facades\Payment;
use Unipay\BD\DTOs\RefundRequest;

$refundRequest = new RefundRequest(
    paymentId: 'PAY_12345678',
    transactionId: 'TRX_98765432',
    amount: 500.00,
    reason: 'Customer return request'
);

$response = Payment::driver('bkash')->refund($refundRequest);

if ($response->isSuccessful()) {
    // Refund complete
    $refundTrxId = $response->refundTransactionId;
}

use Unipay\BD\Facades\Payment;

$response = Payment::driver('nagad')->queryPayment('NAGAD_REF_1001');

if ($response->isSuccessful()) {
    $trxId = $response->transactionId;
    $amount = $response->amount;
}

use Unipay\BD\Facades\Payment;

Payment::extend('custom_mfs', function ($app) {
    return new CustomMfsGateway(config('unipay.gateways.custom_mfs'));
});

use Unipay\BD\Exceptions\UnipayException;

try {
    $response = Payment::driver('bkash')->createPayment($paymentRequest);
} catch (UnipayException $e) {
    logger()->error('UniPay Error: ' . $e->getMessage());
}
bash
php artisan unipay:install
php artisan migrate