1. Go to this page and download the library: Download rusdyahmad/bayarcash 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/ */
rusdyahmad / bayarcash example snippets
use Bayarcash\Laravel\Facades\Bayarcash;
$transaction = Bayarcash::getTransaction('trx_123456');
use Bayarcash\Support\Configuration;
$config = new Configuration(
'your-personal-access-token',
'your-api-secret-key',
'your-portal-key',
true, // sandbox mode
'v3', // API version
false, // debug mode
'FPX', // default channel
'https://your-site.com/payment/return', // return URL
'https://your-site.com/payment/callback' // callback URL
);
use Bayarcash\Laravel\Facades\Bayarcash;
// The client is automatically created using your configuration
$paymentIntent = Bayarcash::createPaymentIntent([
'amount' => 100.00,
'order_number' => 'ORDER-123',
// ...
]);
use Bayarcash\Bayarcash;
use Bayarcash\Support\Configuration;
// Create a configuration object
$config = new Configuration(
'your-personal-access-token',
'your-api-secret-key',
'your-portal-key'
);
// Create a client
$bayarcash = new Bayarcash($config);
// Create a payment intent
$paymentIntent = $bayarcash->createPaymentIntent([
'amount' => 100.00,
'order_number' => 'ORDER-123',
// ...
]);
// Using the FPX channel
$paymentIntent = $bayarcash->channel('FPX')->createPaymentIntent([
'amount' => 100.00,
'order_number' => 'ORDER-123',
// ...
]);
// Using the DuitNow DOBW channel
$paymentIntent = $bayarcash->channel('DuitnowDobw')->createPaymentIntent([
'amount' => 100.00,
'order_number' => 'ORDER-123',
// ...
]);
// Create a payment intent using FPX
$paymentIntent = $bayarcash->createPaymentIntent([
'payment_channel' => 1, // FPX channel ID (integer)
'portal_key' => 'your-portal-key', // Portal key from your Bayarcash account
'order_number' => 'ORDER-' . time(),
'amount' => 1000, // Amount in cents (RM 10.00)
'payer_name' => 'John Doe',
'payer_email' => '[email protected]',
'payer_telephone_number' => '60123456789', // Malaysia number format
'payer_bank_code' => 'ABB0234', // Optional, bank code from getBanks()
'return_url' => 'https://your-site.com/payment/return', // Server to browser redirect
'callback_url' => 'https://your-site.com/payment/callback', // Server to server callback
]);
// Get the payment URL
$paymentUrl = $paymentIntent->getPaymentUrl();
// Redirect the user to the payment page
header("Location: {$paymentUrl}");
exit;
// Get a transaction by ID
$transaction = $bayarcash->getTransaction('trx_123456');
// Check the transaction status
if ($transaction->isSuccessful()) {
// Process successful payment
echo "Payment successful!";
echo "Amount: " . $transaction->getAmount() . " " . $transaction->getCurrency();
echo "Transaction ID: " . $transaction->getId();
} else {
// Handle failed payment
echo "Payment failed: " . $transaction->getStatusDescription();
}
// Get the list of FPX banks
$banks = $bayarcash->getBanks();
// Display bank information
foreach ($banks as $bank) {
echo $bank->getDisplayName() . ' (' . $bank->getCode() . ')';
echo $bank->isAvailable() ? ' - Available' : ' - Not Available';
echo PHP_EOL;
}
// Get the list of DuitNow DOBW banks
$banks = $bayarcash->getDuitNowDobwBanks();
// Display bank information
foreach ($banks as $bank) {
echo $bank->getName() . ' (' . $bank->getCode() . ')';
echo $bank->isAvailable() ? ' - Available' : ' - Not Available';
echo PHP_EOL;
}
// Get the list of portals (first page)
$portalCollection = $bayarcash->getPortals();
// Access portal information
foreach ($portalCollection->getPortals() as $portal) {
echo "Portal: " . $portal->getPortalName() . " (" . $portal->getPortalKey() . ")\n";
echo "URL: " . $portal->getUrl() . "\n";
// Access payment channels for each portal
echo "Supported payment channels:\n";
foreach ($portal->getPaymentChannels() as $channel) {
echo "- " . $channel->getName() . " (" . $channel->getCode() . ")\n";
}
echo "\n";
}
// Access pagination information
echo "Page " . $portalCollection->getCurrentPage() . " of " . $portalCollection->getLastPage() . "\n";
echo "Total portals: " . $portalCollection->getTotal() . "\n";
// Get a specific page
$portalCollection = $bayarcash->getPortals(2);
// Create a basic portal
$portal = $bayarcash->createPortal('My New Portal');
// Create a portal with all options
$portal = $bayarcash->createPortal(
'My Custom Portal',
'https://mycustomportal.com',
'[email protected]',
'Pay Now with Bayarcash',
[1, 3, 4] // Enable FPX, Direct Debit, and FPX Line of Credit
);
// Access the created portal information
echo "Portal created: " . $portal->getPortalName() . "\n";
echo "Portal Key: " . $portal->getPortalKey() . "\n";
echo "Portal URL: " . $portal->getUrl() . "\n";
// Create a direct debit mandate using FPX
$mandate = $bayarcash->createDirectDebitMandate([
'payment_channel' => 5, // FPX Direct Debit channel ID (integer)
'portal_key' => 'your-portal-key', // Portal key from your Bayarcash account
'payer_name' => 'John Doe',
'payer_id_type' => 1, // 1 for NRIC, 2 for Passport, 3 for Army ID, 4 for Police ID
'payer_id' => '910109021234', // ID number
'payer_email' => '[email protected]',
'payer_telephone_number' => '60123456789', // Malaysia number format
'payer_bank_code' => 'ABB0234', // Bank code from getBanks()
'order_number' => 'DD-' . time(),
'amount' => 3000, // Amount in cents (RM 30.00)
'application_type' => 'Subscription',
'application_reason' => 'Monthly subscription',
'frequency_mode' => 'MT', // MT for Monthly, YR for Yearly, etc.
'effective_date' => '2025-05-01',
'expiry_date' => '2026-05-01',
'return_url' => 'https://your-site.com/mandate/return', // Server to browser redirect
'callback_url' => 'https://your-site.com/mandate/callback', // Server to server callback
]);
// Get the authorization URL
$authorizationUrl = $mandate->getAuthorizationUrl();
// Redirect the user to the authorization page
header("Location: {$authorizationUrl}");
exit;
use Bayarcash\Callbacks\TransactionCallback;
use Bayarcash\Exceptions\InvalidCallbackException;
// In a controller method
public function handleCallback(Request $request)
{
try {
// Create a transaction callback instance from the request data
$callback = TransactionCallback::fromRequest($request->all());
// Verify the checksum using your API secret key
$apiSecretKey = config('bayarcash.api_secret_key');
if (!$callback->verifyChecksum($apiSecretKey)) {
Log::warning('Invalid Bayarcash callback checksum', $callback->toArray());
return response()->json(['status' => 'error', 'message' => 'Invalid checksum'], 400);
}
// Process the transaction based on its status
if ($callback->isSuccessful()) {
// Handle successful payment
$order = Order::where('order_number', $callback->getOrderNumber())->first();
if ($order) {
$order->markAsPaid();
$order->transaction_id = $callback->getTransactionId();
$order->save();
}
} elseif ($callback->isFailed()) {
// Handle failed payment
// ...
}
// Return 200 OK response as
use Bayarcash\Callbacks\FpxDirectDebitAuthorizationCallback;
use Bayarcash\Callbacks\FpxDirectDebitBankApprovalCallback;
use Bayarcash\Callbacks\FpxDirectDebitTransactionCallback;
use Bayarcash\Exceptions\InvalidCallbackException;
// In a controller method
public function handleDirectDebitCallback(Request $request)
{
$data = $request->all();
$apiSecretKey = config('bayarcash.api_secret_key');
try {
// Determine callback type based on record_type
if ($data['record_type'] === 'authorization') {
$callback = FpxDirectDebitAuthorizationCallback::fromRequest($data);
} elseif ($data['record_type'] === 'bank_approval') {
$callback = FpxDirectDebitBankApprovalCallback::fromRequest($data);
} elseif ($data['record_type'] === 'transaction') {
$callback = FpxDirectDebitTransactionCallback::fromRequest($data);
} else {
throw new InvalidCallbackException("Unknown record type");
}
// Verify the checksum
if (!$callback->verifyChecksum($apiSecretKey)) {
Log::warning('Invalid FPX Direct Debit callback checksum', $callback->toArray());
return response()->json(['status' => 'error', 'message' => 'Invalid checksum'], 400);
}
// Process the callback based on its type
// ...
// Return 200 OK response as