PHP code example of satnam-singh / sbi-epay

1. Go to this page and download the library: Download satnam-singh/sbi-epay 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/ */

    

satnam-singh / sbi-epay example snippets


->withMiddleware(function (Middleware $middleware) {
    $middleware->validateCsrfTokens(except: [
        'payment/sbi/callback',
    ]);
})

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use SbiEpay\SbiEpay\Facades\SbiEpay;
use SbiEpay\SbiEpay\DTOs\PaymentRequest;

class CheckoutController extends Controller
{
    public function initiatePayment()
    {
        $initiation = SbiEpay::initiate(new PaymentRequest(
            orderId: 'ORDER_' . uniqid(),
            amount: 1500.00,
            successUrl: route('payment.sbi.callback'),
            failUrl: route('payment.sbi.callback'),
            otherDetails: 'Order Payment #1024',
            customerEmail: '[email protected]',
            customerMobile: '9876543210',
        ));

        // Returns an auto-submitting POST view that redirects the user to SBI ePay
        return $initiation->redirect();
    }
}

> return response()->json([
>     'action' => $initiation->paymentUrl,
>     'form_data' => $initiation->formData(),
> ]);
> 

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use SbiEpay\SbiEpay\Facades\SbiEpay;

class CheckoutController extends Controller
{
    public function handleCallback(Request $request)
    {
        try {
            // Decrypt and parse the response
            $response = SbiEpay::handleCallback($request->input('encData'));

            if ($response->isSuccess()) {
                // Highly recommended: Perform server-to-server Double Verification
                $verification = SbiEpay::verify($response->orderId, $response->amount);

                if ($verification->isSuccess()) {
                    // Mark order as paid in your database
                    // $order = Order::where('order_id', $response->orderId)->first();
                    // $order->markAsPaid($response->sbiTransactionId);

                    return redirect()->route('order.success', [
                        'order_id' => $response->orderId,
                    ])->with('success', 'Payment completed successfully!');
                }
            }

            return redirect()->route('order.failed')->with('error', 'Payment verification failed.');

        } catch (\Exception $e) {
            return redirect()->route('order.failed')->with('error', $e->getMessage());
        }
    }
}

$response->isSuccess();           // bool: true if status is SUCCESS
$response->isFailed();            // bool: true if status is FAIL/FAILED
$response->isPending();           // bool: true if status is PENDING/AWAITED
$response->orderId;               // string: your original order ID
$response->status;                // string: 'SUCCESS', 'FAIL', etc.
$response->amount;                // float: paid amount
$response->sbiTransactionId;      // ?string: SBI reference ID
$response->bankReferenceNo;       // ?string: Bank reference number
$response->bankName;              // ?string: Paying bank name (e.g. State Bank of India)
$response->transactionDate;       // ?string: Transaction timestamp
$response->merchantId;            // ?string: SBI merchant ID
$response->rawResponse;           // string: raw pipe-delimited decrypted string
$response->toArray();             // array<string, mixed>

use SbiEpay\SbiEpay\Facades\SbiEpay;

$verification = SbiEpay::verify(
    orderId: 'ORDER_12345',
    amount: 1500.00
);

if ($verification->isSuccess()) {
    // Verified by SBI server
    $sbiRef = $verification->sbiTransactionId;
}

$initiation = SbiEpay::merchant('MERCHANT_BRANCH_02', 'KEY_BRANCH_02')
    ->initiate($paymentRequest);

use SbiEpay\SbiEpay\Facades\SbiEpay;

// Encrypt string with configured merchant key
$cipherText = SbiEpay::encrypt('Hello World');

// Decrypt back to plaintext
$plainText = SbiEpay::decrypt($cipherText);
bash
php artisan vendor:publish --tag="sbi-epay-config"