PHP code example of uddoktapay / laravel-sdk

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

    

uddoktapay / laravel-sdk example snippets


use UddoktaPay\LaravelSDK\UddoktaPay;

$uddoktapay = UddoktaPay::make(env('UDDOKTAPAY_API_KEY'), env('UDDOKTAPAY_API_URL'));

use UddoktaPay\LaravelSDK\Requests\CheckoutRequest;

try {
    $checkoutRequest = CheckoutRequest::make()
        ->setFullName('John Doe')
        ->setEmail('[email protected]')
        ->setAmount('10')
        ->addMetadata('order_id', '12345')
        ->setRedirectUrl(route('uddoktapay.verify'))
        ->setCancelUrl(route('uddoktapay.cancel'))
        ->setWebhookUrl(route('uddoktapay.ipn'));

    $response = $uddoktapay->checkout($checkoutRequest);

    if ($response->failed()) {
        dd($response->message());
    }

    return redirect($response->paymentURL());
} catch (\UddoktaPay\LaravelSDK\Exceptions\UddoktaPayException $e) {
    dd("Initialization Error: " . $e->getMessage());
}

try {
    $response = $uddoktapay->verify($request);

    if ($response->success()) {
        // Handle successful status
        dd($response->toArray()); // Handle success
    } elseif ($response->pending()) {
        // Handle pending status
    } elseif ($response->failed()) {
        // Handle failure
    }
} catch (\UddoktaPay\LaravelSDK\Exceptions\UddoktaPayException $e) {
    dd("Verification Error: " . $e->getMessage());
}

try {
    $response = $uddoktapay->ipn($request);

    if ($response->success()) {
        // Handle successful IPN
    } elseif ($response->pending()) {
        // Handle pending IPN
    } elseif ($response->failed()) {
        // Handle failed IPN
    }
} catch (\UddoktaPay\LaravelSDK\Exceptions\UddoktaPayException $e) {
    dd("IPN Error: " . $e->getMessage());
}

use UddoktaPay\LaravelSDK\Requests\RefundRequest;

try {
    $refundRequest = RefundRequest::make()
        ->setAmount('10')
        ->setTransactionId('12345')
        ->setPaymentMethod('bkash')
        ->setProductName('Sample Product')
        ->setReason('Customer Request');

    $response = $uddoktapay->refund($refundRequest);

    if ($response->success()) {
        // Handle refund success
    } elseif ($response->failed()) {
        // Handle refund failure
    }
} catch (\UddoktaPay\LaravelSDK\Exceptions\UddoktaPayException $e) {
    dd("Refund Error: " . $e->getMessage());
}

use App\Http\Controllers\UddoktaPayController;

Route::get('/checkout', [UddoktaPayController::class, 'checkout'])->name('uddoktapay.checkout');
Route::get('/verify', [UddoktaPayController::class, 'verify'])->name('uddoktapay.verify');
Route::get('/cancel', [UddoktaPayController::class, 'cancel'])->name('uddoktapay.cancel');
Route::post('/ipn', [UddoktaPayController::class, 'ipn'])->name('uddoktapay.ipn');
Route::post('/refund', [UddoktaPayController::class, 'refund'])->name('uddoktapay.refund');