1. Go to this page and download the library: Download sitehandy/omnipay-toyyibpay 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/ */
// FPX (Online Banking) only
$response = $gateway->purchase([
'billPaymentChannel' => 1, // FPX only
// ... other parameters
]);
// Credit/Debit Card only
$response = $gateway->purchase([
'billPaymentChannel' => 2, // Card only
// ... other parameters
]);
// All payment methods (default)
$response = $gateway->purchase([
'billPaymentChannel' => 0, // All methods
// ... other parameters
]);
$gateway = Omnipay::create('ToyyibPay');
// Use sandbox mode for testing
$gateway->setTestMode(1);
// You need to pass the following parameters to generate bill code
$options = [
'userSecretKey' => 'YOURSECRETKEY',
'categoryCode' => 'CATEGORYCODE',
'billName' => 'Product Name',
'billDescription' => 'Product Description',
'billPriceSetting'=> 0,
'billPayorInfo'=> 1,
'billAmount'=> 1,
'billReturnUrl'=>'https://yourwebsite.com/returnurl',
'billCallbackUrl'=>'https://yourwebsite.com/callbackurl',
'billExternalReferenceNo' => 'ORDER123',
'billTo'=>'Customer Name',
'billEmail'=>'[email protected]',
'billPhone'=>'0123456789',
'billSplitPayment'=> 0,
'billSplitPaymentArgs'=>'',
'billPaymentChannel'=> 0,
'billDisplayMerchant'=> 1,
'billContentEmail' => 'Sample email content',
'billChargeToCustomer' => 2
];
// Send a purchase request to create a bill
$response = $gateway->purchase($options)->send();
// Available response method
// $response->isSuccessful(); // is the response successful?
// $response->isRedirect(); // is the response a redirect?
// $response->getTransactionReference(); // a reference generated by the payment gateway
// $response->getTransactionId(); // the reference set by the originating website if available.
// $response->getMessage(); // a message generated by the payment gateway
// Now redirect the customer to the toyyibPay bill payment page
if ($response->isRedirect())
{
// Do whatever you want here for example saving the data to the database etc
// and then redirect the customer to the offsite payment gateway
$response->redirect();
}
else
{
// Display error message
exit($response->getMessage());
}
$gateway = Omnipay::create('ToyyibPay');
// Parameters Send a complete purchase request to get the bill transactions
$response = $gateway->completePurchase($options)->send();
// Available response method
// $response->isSuccessful(); // is the response successful?
// $response->isRedirect(); // is the response a redirect?
// $response->getTransactionReference(); // a reference generated by the payment gateway
// $response->getTransactionId(); // the reference set by the originating website if available.
// $response->getMessage(); // a message generated by the payment gateway
if ($response->isSuccessful())
{
// Do whatever you want here for example saving the data to the database etc
echo $responsive->getTransactionReference();
}
elseif ($response->isRedirect())
{
// If the payment is not successful, redirect the customer to the payment page for completion
$response->redirect();
}
else
{
// Display error message
exit($response->getMessage());
}
// Test with sandbox environment
$gateway = Omnipay::create('ToyyibPay');
$gateway->setTestMode(true); // Always use test mode for testing
// Use test credentials (get these from toyyibPay sandbox)
$testOptions = [
'userSecretKey' => 'sandbox-secret-key',
'categoryCode' => 'sandbox-category-code',
'billName' => 'Test Payment - ' . date('Y-m-d H:i:s'),
'billDescription' => 'Integration test payment',
'billAmount' => 1.00, // Use small amount for testing
'billReturnUrl' => 'https://httpbin.org/get?return=true',
'billCallbackUrl' => 'https://httpbin.org/post',
'billExternalReferenceNo' => 'TEST-' . uniqid(),
'billTo' => 'Test Customer',
'billEmail' => '[email protected]',
'billPhone' => '0123456789'
];
$response = $gateway->purchase($testOptions)->send();
if ($response->isRedirect()) {
echo "Test payment created successfully!\n";
echo "Payment URL: " . $response->getRedirectUrl() . "\n";
echo "Bill Code: " . $response->getTransactionReference() . "\n";
} else {
echo "Test failed: " . $response->getMessage() . "\n";
}
// Use environment variables for sensitive data
$gateway = Omnipay::create('ToyyibPay');
$gateway->setTestMode(getenv('TOYYIBPAY_TEST_MODE') === 'true');
$options = [
'userSecretKey' => getenv('TOYYIBPAY_SECRET_KEY'), // Never hardcode
'categoryCode' => getenv('TOYYIBPAY_CATEGORY_CODE'),
// ... other options
];
// In your callback handler
function handleCallback()
{
// Verify the callback is from toyyibPay
$billCode = $_POST['billcode'] ?? null;
$status = $_POST['status_id'] ?? null;
if (!$billCode || !$status) {
http_response_code(400);
exit('Invalid callback data');
}
// Verify payment status with toyyibPay API
$gateway = Omnipay::create('ToyyibPay');
$response = $gateway->completePurchase(['billCode' => $billCode])->send();
if ($response->isSuccessful()) {
// Update order status in your database
updateOrderStatus($billCode, 'paid');
// Send confirmation email
sendPaymentConfirmation($billCode);
}
// Always respond with 200 OK to acknowledge receipt
http_response_code(200);
echo 'OK';
}
function createPayment($orderData)
{
// Validate and sanitize input data
$billAmount = filter_var($orderData['amount'], FILTER_VALIDATE_FLOAT);
if ($billAmount === false || $billAmount <= 0) {
throw new InvalidArgumentException('Invalid amount');
}
$billEmail = filter_var($orderData['email'], FILTER_VALIDATE_EMAIL);
if ($billEmail === false) {
throw new InvalidArgumentException('Invalid email address');
}
$billPhone = preg_replace('/[^0-9+]/', '', $orderData['phone']);
if (strlen($billPhone) < 10) {
throw new InvalidArgumentException('Invalid phone number');
}
// Sanitize text fields
$billName = htmlspecialchars(trim($orderData['product_name']), ENT_QUOTES, 'UTF-8');
$billTo = htmlspecialchars(trim($orderData['customer_name']), ENT_QUOTES, 'UTF-8');
return [
'billAmount' => $billAmount,
'billEmail' => $billEmail,
'billPhone' => $billPhone,
'billName' => $billName,
'billTo' => $billTo
];
}
// Debug: Check your credentials
$gateway = Omnipay::create('ToyyibPay');
$gateway->setTestMode(true); // Make sure this matches your secret key environment
// Test with minimal esting credentials',
'billAmount' => 1.00,
'billReturnUrl' => 'https://example.com/return',
'billCallbackUrl' => 'https://example.com/callback',
'billExternalReferenceNo' => 'DEBUG-' . time(),
'billTo' => 'Test',
'billEmail' => '[email protected]',
'billPhone' => '0123456789'
])->send();
// Debug callback handler
function debugCallback()
{
// Log all incoming data
error_log('Callback received: ' . print_r($_POST, true));
error_log('Headers: ' . print_r(getallheaders(), true));
// Always respond with 200
http_response_code(200);
echo 'OK';
}