PHP code example of alsharie / jawali-payment
1. Go to this page and download the library: Download alsharie/jawali-payment 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/ */
alsharie / jawali-payment example snippets
return [
'auth' => [
// For "LOGIN TO SYSTEM" API
'username' => env('JAWALI_MERCHANT_USERNAME'),
'password' => env('JAWALI_MERCHANT_PASSWORD'),
// For PAYWA/PAYAG header: signonDetail
'org_id' => env('JAWALI_MERCHANT_ORG_ID'),
'user_id' => env('JAWALI_MERCHANT_USER_ID'),
'external_user' => env('JAWALI_MERCHANT_EXTERNAL_USER'),
// For PAYWA.WALLETAUTHENTICATION body & PAYAG body
'wallet_identifier' => env('JAWALI_MERCHANT_WALLET'),
'wallet_password' => env('JAWALI_MERCHANT_WALLET_PASSWORD'),
],
'url' => [
'base' => env('JAWALI_BASE_URL', 'https://82.114.179.89:9493/paygate'),
'disable_ssl_verification' => env('JAWALI_DISABLE_SSL_VERIFICATION', false),
],
'timeout' => env('JAWALI_TIMEOUT', 30),
'retry' => [
'enabled' => env('JAWALI_RETRY_ENABLED', true),
'max_attempts' => env('JAWALI_RETRY_MAX_ATTEMPTS', 2),
'status_codes' => [400, 401],
],
];
use Alsharie\Jawali\Facades\Jawali;
use Alsharie\Jawali\Exceptions\JawaliApiException;
// Example: Login to System
try {
$loginResponse = Jawali::loginToSystem();
if ($loginResponse->isSuccess()) {
// Access token is automatically stored for future requests
echo "Login successful! Access token: " . $loginResponse->getAccessToken();
} else {
echo "Login failed!";
}
} catch (JawaliApiException $e) {
// Handle API error
// $e->getMessage(), $e->getApiStatus(), $e->getData()
logger()->error('Jawali Login Error: ' . $e->getMessage(), ['response' => $e->getData()]);
}
// Example: Wallet Authentication
// Optional header overrides if needed for a specific call
$headerOverrides = [
'signonDetail' => [
'orgID' => '22000001688', // Example, could be different
'userID' => 'school.branch.api.test',
'externalUser' => 'user1',
]
];
try {
$walletAuthResponse = Jawali::walletAuthentication($headerOverrides);
if ($walletAuthResponse->isSuccess()) {
// Wallet token is automatically stored for future requests
echo "Wallet authentication successful!";
} else {
echo "Wallet authentication failed!";
}
} catch (JawaliApiException $e) {
logger()->error('Jawali Wallet Auth Error: ' . $e->getMessage(), ['response' => $e->getData()]);
}
// Example: Ecommerce Inquiry
// Note: No need to pass tokens - they are managed automatically
try {
$voucher = '3360714';
$receiverMobile = '711029220';
$purpose = 'test bill payment';
$inquiryResponse = Jawali::ecommerceInquiry($voucher, $receiverMobile, $purpose, $headerOverrides);
if ($inquiryResponse->isSuccess()) {
echo "Inquiry successful!";
echo "Amount: " . $inquiryResponse->getAmount();
echo "Currency: " . $inquiryResponse->getCurrency();
echo "State: " . $inquiryResponse->getState();
echo "Transaction Reference: " . $inquiryResponse->getTransactionRef();
// Check if the transaction is in PENDING state
if ($inquiryResponse->getState() === 'PENDING') {
// Proceed with cashout
}
} else {
echo "Inquiry failed!";
}
} catch (JawaliApiException $e) {
logger()->error('Jawali Inquiry Error: ' . $e->getMessage(), ['response' => $e->getData()]);
}
// Example: Ecommerce Cashout
try {
$voucher = '2383314';
$receiverMobile = '711029220';
$purpose = 'test bill payment';
$cashoutResponse = Jawali::ecommerceCashout($voucher, $receiverMobile, $purpose, $headerOverrides);
if ($cashoutResponse->isSuccess()) {
echo "Cashout successful!";
echo "Amount: " . $cashoutResponse->getAmount();
echo "Currency: " . $cashoutResponse->getCurrency();
echo "Transaction Reference: " . $cashoutResponse->getTransactionRef();
} else {
echo "Cashout failed!";
}
} catch (JawaliApiException $e) {
logger()->error('Jawali Cashout Error: ' . $e->getMessage(), ['response' => $e->getData()]);
}
// Complete End-to-End Example
// This example shows how to perform an inquiry and then a cashout if the conditions are met
function processPayment($voucher, $receiverMobile, $purpose, $expectedAmount, $expectedCurrency)
{
try {
// Step 1: Perform an ecommerce inquiry
$inquiryResponse = Jawali::ecommerceInquiry($voucher, $receiverMobile, $purpose);
if ($inquiryResponse->isSuccess()) {
// Step 2: Check if the state is "PENDING" and the amount and currency are correct
if ($inquiryResponse->getState() === 'PENDING') {
if ($inquiryResponse->getAmount() == $expectedAmount &&
$inquiryResponse->getCurrency() === $expectedCurrency) {
// Step 3: Perform cashout
$cashoutResponse = Jawali::ecommerceCashout($voucher, $receiverMobile, $purpose);
if ($cashoutResponse->isSuccess()) {
return [
'message' => 'Payment processed successfully',
'success' => true,
'data' => $cashoutResponse->getData(),
];
} else {
return [
'message' => 'Error during cashout',
'success' => false,
'data' => $cashoutResponse->getData(),
];
}
} else {
return [
'message' => 'Amount or currency mismatch',
'success' => false,
'expected' => [
'amount' => $expectedAmount,
'currency' => $expectedCurrency,
],
'actual' => [
'amount' => $inquiryResponse->getAmount(),
'currency' => $inquiryResponse->getCurrency(),
],
];
}
} else {
return [
'message' => 'Transaction is not in PENDING state',
'success' => false,
'state' => $inquiryResponse->getState(),
];
}
} else {
return [
'message' => 'Inquiry failed',
'success' => false,
'error' => $inquiryResponse->getErrorMessage(),
];
}
} catch (JawaliApiException $e) {
return [
'message' => 'API Exception',
'success' => false,
'error' => $e->getMessage(),
'status' => $e->getApiStatus(),
];
} catch (\Exception $e) {
return [
'message' => 'General Exception',
'success' => false,
'error' => $e->getMessage(),
];
}
}
use Alsharie\Jawali\Facades\Jawali;
use Alsharie\Jawali\Exceptions\JawaliApiException;
try {
$inquiryResponse = Jawali::ecommerceInquiry($voucher, $receiverMobile, $purpose);
// Handle successful response
} catch (JawaliApiException $e) {
// Get detailed error information
$errorDetails = $e->getApiErrorDetails();
// Access specific error information
$statusCode = $e->getApiStatus();
$apiResponse = $e->getData();
$userFriendlyMessage = $e->getUserFriendlyMessage();
// Check error type
if ($e->isTokenError()) {
// Handle token-related errors
logger()->warning('Token error occurred', $errorDetails);
} elseif ($e->isRetryable()) {
// Handle retryable errors
logger()->info('Retryable error occurred', $errorDetails);
}
// Log the error with context
logger()->error('Jawali API Error', [
'error_details' => $errorDetails,
'voucher' => $voucher,
'mobile' => $receiverMobile,
]);
}
try {
$response = Jawali::ecommerceInquiry($voucher, $mobile);
// Success case
$data = $response->getData();
$amount = $response->getResponseBody('amount');
$error = $response->getErrorMessage(); // null if successful
} catch (JawaliApiException $e) {
// Error case - SAME method names!
$data = $e->getData();
$errorDetail = $e->getResponseBody('error');
$error = $e->getErrorMessage();
}
// In your .env file
JAWALI_LOGGING_ENABLED=true
bash
php artisan vendor:publish --provider="Alsharie\Jawali\JawaliServiceProvider" --tag="jawali-config"