PHP code example of farayaz / larapay
1. Go to this page and download the library: Download farayaz/larapay 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/ */
farayaz / larapay example snippets
use Farayaz\Larapay\Exceptions\LarapayException;
use Larapay;
$gateway = 'ZarinPal';
$config = [
'merchant_id' => 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee',
];
$amount = 10000; // Rial | ریال
$id = 1230; // Transaction ID | شماره تراکنش
$callbackUrl = route('payment.callback', $id);
$nationalId = '1234567890';
$mobile = '09131234567';
try {
$result = Larapay::gateway($gateway, $config)
->request(
id: $id,
amount: $amount,
callbackUrl: $callbackUrl,
nationalId: $nationalId,
mobile: $mobile,
);
} catch (LarapayException $e) {
// handle error
}
// Save token for later verification
$token = $result['token'];
$fee = $result['fee'];
try {
return Larapay::gateway($gateway, $config)
->redirect(id: $id, token: $token, callbackUrl: $callbackUrl);
} catch (LarapayException $e) {
// handle error
}
$params = $request->all();
try {
$result = Larapay::gateway($gateway, $config)
->verify(
id: $id,
amount: $amount,
nationalId: $nationalId,
mobile: $mobile,
token: $token,
params: $params,
);
} catch (LarapayException $e) {
// Transaction failed
}
// Transaction successful
$result['result']; // Status message
$result['reference_id']; // Gateway reference ID
$result['tracking_code'];// Tracking code
$result['card']; // Masked card number
$result['fee']; // Transaction fee
namespace App\Http\Controllers;
use Farayaz\Larapay\Exceptions\LarapayException;
use Illuminate\Http\Request;
use Larapay;
class PaymentController extends Controller
{
protected string $gateway = 'ZarinPal';
protected array $config = [
'merchant_id' => env('ZARINPAL_MERCHANT_ID'),
];
public function pay()
{
$amount = 100000; // Rial
$id = rand(1000, 9999); // Unique transaction ID
$callbackUrl = route('payment.callback', $id);
$nationalId = '1234567890';
$mobile = '09131234567';
try {
$result = Larapay::gateway($this->gateway, $this->config)
->request(
id: $id,
amount: $amount,
callbackUrl: $callbackUrl,
nationalId: $nationalId,
mobile: $mobile,
);
} catch (LarapayException $e) {
return back()->with('error', $e->getMessage());
}
// Store the payment details for verification.
session(['payment_token' => $result['token'], 'payment_id' => $id, 'amount' => $amount]);
return Larapay::gateway($this->gateway, $this->config)
->redirect($id, $result['token'], $callbackUrl);
}
public function callback(Request $request, int $id)
{
$token = session('payment_token');
$amount = session('amount');
try {
$result = Larapay::gateway($this->gateway, $this->config)
->verify(
id: $id,
amount: $amount,
nationalId: '1234567890',
mobile: '09131234567',
token: $token,
params: $request->all(),
);
} catch (LarapayException $e) {
return view('payment.failed', ['message' => $e->getMessage()]);
}
return view('payment.success', [
'tracking_code' => $result['tracking_code'],
'reference_id' => $result['reference_id'],
'card' => $result['card'],
]);
}
}
try {
$result = Larapay::gateway($gateway, $config)
->refund(
id: $transactionId,
amount: $amount,
referenceId: $referenceId,
reason: 'Customer request',
);
} catch (LarapayException $e) {
// Refund failed
}
try {
Larapay::gateway($gateway, $config)
->bulkCheck(
successCallback: function (\Farayaz\Larapay\Data\TransactionIdentity $identity, array $data) {
// Find the local transaction by $identity->id or gateway + $identity->token.
// $data contains: result, card, tracking_code, reference_id, amount, fee
},
unsuccessCallback: function (\Farayaz\Larapay\Data\TransactionIdentity $identity) {
// Find the local transaction and mark it failed.
},
);
} catch (LarapayException $e) {
// handle error
}
use DateTimeImmutable;
use Farayaz\Larapay\Data\TransactionQuery;
$gateway = Larapay::gateway('JetPay', $config);
$page = $gateway->listTransactions(new TransactionQuery(
page: 1,
size: 50,
fromDate: new DateTimeImmutable('2026-05-01'),
toDate: new DateTimeImmutable('2026-05-31'),
nationalId: '1234567890',
));
foreach ($page->items as $transaction) {
$token = $transaction->identity->token;
$referenceId = $transaction->identity->referenceId;
$status = $transaction->status;
$amount = $transaction->amount;
}
$gateway = Larapay::gateway($gateway, $config);
if ($gateway->supports('refund')) {
// Gateway supports refund
}
if ($gateway->supports('bulk_check')) {
// Gateway supports bulk check
}
if ($gateway->supports('transaction_list')) {
// Gateway supports transaction lists
}
use Farayaz\Larapay\Exceptions\LarapayException;
try {
// ...
} catch (LarapayException $e) {
$message = $e->getMessage(); // Persian or English error message
$code = $e->getCode(); // Optional error code
}