PHP code example of srustamov / laravel-azericard
1. Go to this page and download the library: Download srustamov/laravel-azericard 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/ */
srustamov / laravel-azericard example snippets
// 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']);
});
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');
}
}
bash
php artisan vendor:publish --provider="Srustamov\Azericard\AzericardServiceProvider" --tag="config"