1. Go to this page and download the library: Download bayarcash/php-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/ */
bayarcash / php-sdk example snippets
use Bayarcash\Bayarcash;
$bayarcash = new Bayarcash('YOUR_API_TOKEN');
$bayarcash->useSandbox(); // remove this line in production
use Bayarcash\Bayarcash;
$bayarcash = app(Bayarcash::class);
$bayarcash->useSandbox(); // remove this line in production
$bayarcash
->useSandbox() // switch to the sandbox environment
->setApiVersion('v3') // 'v2' (default) or 'v3'
->setTimeout(60); // request timeout in seconds (default 30)
$bayarcash->getApiVersion(); // read back the current version
use Bayarcash\Bayarcash;
use Bayarcash\Fpx;
$bayarcash = new Bayarcash('YOUR_API_TOKEN');
$bayarcash->useSandbox();
$apiSecretKey = 'YOUR_API_SECRET_KEY';
// 1. Build the payment request
$data = [
'portal_key' => 'your_portal_key',
'payment_channel' => Bayarcash::FPX,
'order_number' => 'INV-1001',
'amount' => '10.00',
'payer_name' => 'Ahmad bin Abdullah',
'payer_email' => '[email protected]',
'payer_telephone_number' => '0123456789',
'return_url' => 'https://your-site.com/payment/return',
'callback_url' => 'https://your-site.com/payment/callback',
];
// 2. Sign it (recommended)
$data['checksum'] = $bayarcash->createPaymentIntentChecksumValue($apiSecretKey, $data);
// 3. Create the payment intent and redirect the payer to Bayarcash
$paymentIntent = $bayarcash->createPaymentIntent($data);
header('Location: ' . $paymentIntent->url);
exit;
$callbackData = $_POST; // Laravel: $request->all()
// Transaction callback (sent to your callback_url)
if ($bayarcash->verifyTransactionCallbackData($callbackData, $apiSecretKey)) {
// Data is authentic — safe to process.
}
// Payer redirect (sent to your return_url)
if ($bayarcash->verifyReturnUrlCallbackData($callbackData, $apiSecretKey)) {
// ...
}
// Pre-transaction callback (sent before the transaction record)
if ($bayarcash->verifyPreTransactionCallbackData($callbackData, $apiSecretKey)) {
// ...
}
use Bayarcash\Exceptions\ValidationException;
use Bayarcash\Exceptions\FailedActionException;
use Bayarcash\Exceptions\NotFoundException;
use Bayarcash\Exceptions\RateLimitExceededException;
try {
$paymentIntent = $bayarcash->createPaymentIntent($data);
} catch (ValidationException $e) {
// 422 — invalid request data
$errors = $e->errors();
} catch (NotFoundException $e) {
// 404 — resource not found
} catch (RateLimitExceededException $e) {
// 429 — too many requests
$resetAt = $e->rateLimitResetsAt; // unix timestamp or null
} catch (FailedActionException $e) {
// 400 — request failed
$message = $e->getMessage();
}
$paymentIntent->url; // checkout URL to redirect the payer to
$paymentIntent->id;
$paymentIntent->status;
$paymentIntent->amount;
$paymentIntent->orderNumber;
$paymentIntent->payerName;
$paymentIntent->payerEmail;
$transaction->id;
$transaction->status; // int status code — see Fpx constants
$transaction->statusDescription;
$transaction->amount;
$transaction->orderNumber;
$transaction->exchangeReferenceNumber;
$transaction->payerName;
$transaction->payerEmail;