This package is to add Thawani support to Laravel
composer require phpawcom/thawani_laravel
Then migrate to add Thawani table:
php artisan migrate
php artisan vendor:publish --provider "S4D\Laravel\Thawani\ThawaniServiceProvider"
The S4D\Laravel\Thawani\ThawaniServiceProvider is auto-discovered and registered by default. If you want to register it yourself, add the ServiceProvider in config/app.php:
'providers' => [
S4D\Laravel\Thawani\ThawaniServiceProvider::class,
]
For alias:
'aliases' => [
S4D\Laravel\Thawani\Thawani::class,
]
First you need to add routes to your routes/web.php:
// To generate payment URL and redirect to Thawani
Route::get('pay', [\App\Http\Controllers\TestThawaniController::class, 'pay'])->name('thawani.pay');
// To check payment and update the status
Route::get('check/{session_id?}', [\App\Http\Controllers\TestThawaniController::class, 'check'])->name('thawani.check');
// To show cancellation message
Route::get('cancel/{session_id?}', [\App\Http\Controllers\TestThawaniController::class, 'cancel'])->name('thawani.cancel');
In your controller:
<?php
namespace App\Http\Controllers;
use S4D\Laravel\Thawani\Thawani;
class TestThawaniController extends Controller
{
public function pay(){
// Note that we use Routes names in setReturnUrls() method
Thawani::setClientReference(1)->setReturnUrls('thawani.check', 'thawani.cancel')->setProducts([
['name' => 'test test test test test test test test test test test test ', 'unit_amount' => 100, 'quantity' => 1],
['name' => 'test', 'unit_amount' => 100, 'quantity' => 1],
])->setMetadata([
'customer_name' => 'Fulan Al Fulani',
'customer_phone' => '90000000',
'customer_email' => '[email protected]',
])->redirectToPayment();
}
public function check($session_id){
return Response('Payment is '.(Thawani::paymentStatus($session_id) == 1? 'successful' : 'failed'));
}
public function cancel($session_id){
Thawani::cancelPayment($session_id);
return Response('has been cancelled');
}
}
Note that you can get the payment URL without redirecting in this way:
$url = Thawani::setClientReference(1)->setReturnUrls('thawani.check', 'thawani.cancel')->setProducts([
['name' => 'test test test test test test test test test test test test ', 'unit_amount' => 100, 'quantity' => 1],
['name' => 'test', 'unit_amount' => 100, 'quantity' => 1],
])->setMetadata([
'customer_name' => 'Fulan Al Fulani',
'customer_phone' => '90000000',
'customer_email' => '[email protected]',
])->getPaymentUrl();
If you don't specify URL in setReturnUrls() method, the package will use its default views