srustamov / laravel-azericard
Azericard for laravel framework
v3.0.1
2023-07-19 10:16 UTC
Requires
- php: >=8.0
- ext-curl: *
- ext-openssl: *
- laravel/framework: ^8.0|^9.0|^10.0
Requires (Dev)
- orchestra/testbench: ^6.0|^7.0|^8.0
- phpunit/phpunit: ~9.5|^10.1
README
Requirements
- Laravel ^8|^9
- PHP ^8.0
Installation
You can install the package via composer:
composer require srustamov/laravel-azericard
For Laravel < 8 and PHP < 8
composer require srustamov/laravel-azericard:^1.0.1
Publish config file
php artisan vendor:publish --provider="Srustamov\Azericard\AzericardServiceProvider" --tag="config"
Credits
Example
Routes
// routes Route::prefix('azericard')->group(function () { Route::get('/create-order',[\App\Http\Controllers\AzericardController::class,'createOrder']); Route::post('/callback',[\App\Http\Controllers\AzericardController::class,'callback']); Route::get('/result/{orderId}',[\App\Http\Controllers\AzericardController::class,'result']); });
Controller
use App\Models\Payment\Transaction; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Event; use Srustamov\Azericard\Azericard; use Srustamov\Azericard\DataProviders\RefundData; use Srustamov\Azericard\Events\OrderCompleted; use Srustamov\Azericard\Exceptions\AzericardException; use Srustamov\Azericard\Exceptions\FailedTransactionException; use Srustamov\Azericard\Options; class AzericardController extends Controller { public function __construct() { Event::listen(OrderCompleted::class, function (OrderCompleted $event) { // do something //example logger()->info('Order completed', ['request' => $event->request]); }); } public function createOrder(Azericard $azericard, Request $request) { $order = auth()->user()->transactions()->create([ 'amount' => $request->post('amount'), 'currency' => 'AZN', 'status' => Transaction::PENDING, 'type' => Transaction::TYPE_PAYMENT, 'payment_method' => Transaction::PAYMENT_METHOD_AZERICARD, ]); $formParams = $azericard->setOrder($order->id) ->setAmount($order->amount) ->setMerchantUrl(route('azericard.result',['order' => $order]) //->debug($request->has('test')) ->createOrder(); return response()->json($formParams); } public function callback(Azericard $azericard, Request $request) { $transaction = Trasaction::findByAzericard($request->get(Options::ORDER)); if(!$transaction->isPending()){ return response()->json(['message' => 'Order already processed'], 409); } DB::beginTransaction(); try { if ($azericard->completeOrder($request->all())) { $transaction->update([ 'status' => Trasaction::SUCCESS, 'rrn' => $request->get(Options::RRN), 'int_ref' => $request->get(Options::INT_REF), 'process_at' => now(), ]); $transaction->user->increment('balance', $transaction->amount); DB::commit(); $transaction->user->notify(new TransactionSuccess($transaction)); return response()->json(['message' => 'Order processed successfully'], 200); } else { $transaction->update([ 'status' => Trasaction::FAILED, 'process_at' => now(), ]); DB::commit(); logger()->error('Azericard payment failed', $request->all()); return response()->json(['message' => 'Order processed failed'], 500); } } catch (FailedTransactionException $e) { DB::rollBack(); logger()->error('Azericard | Message: '.$e->getMessage(), $request->all()); //do something } catch (AzericardException $e) { DB::rollBack(); //do something } catch (Exception $e) { DB::rollBack(); } finally { info('Azericard payment callback called', $request->all()); } } public function refund(Request $request,Azericard $azericard) { $transaction = Trasaction::findOrFail($request->post('transaction_id')); try { $order = Transaction::createForRefund( amount : $amount = $request->post('amount'), parent_id: $transaction->id ); $data = new RefundData( rrn: $transaction->rrn, int_ref: $transaction->int_ref, created_at: $transaction->process_at ); if ($azericard->setAmount($amount)->setOrder($order->id)->refund($data)) { // refund success } else { // fail } } catch (FailedTransactionException $e) { //info($e->getMessage(),$e->getParams()); } catch (AzericardException $e) { // payment fail } catch (Exception $e) { // payment fail } } public function result($orderId) { $transaction = Transaction::findByAzericard($orderId); if($transaction->isSuccess()){ return view('payment.success'); } elseif ($transaction->isPending()){ return view('payment.pending'); } return view('payment.failed'); } }
Testing
composer test
License
The MIT License (MIT). Please see License File for more information.