serenity_technologies / ecobank-unified-api
Laravel wrapper for Ecobank Unified API
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Forks: 0
pkg:composer/serenity_technologies/ecobank-unified-api
Requires
- php: ^8.1
- ext-json: *
- illuminate/http: ^8.0|^9.0|^10.0|^11.0|^12.0
- illuminate/support: ^8.0|^9.0|^10.0|^11.0|^12.0
Requires (Dev)
- orchestra/testbench: ^6.0|^7.0|^8.0|^9.0
- phpunit/phpunit: ^9.0|^10.0
README
This package provides a Laravel wrapper for the Ecobank Unified API, allowing you to easily integrate Ecobank's financial services into your Laravel applications.
Installation
You can install the package via composer:
composer require serenity_technologies/ecobank-unified-api
Configuration
Publish the configuration file:
php artisan vendor:publish --tag=ecobank-config
This will create a config/ecobank.php file. You should modify this file to match your Ecobank credentials:
return [
'base_url' => env('ECOBANK_BASE_URL', 'https://developer.ecobank.com'),
'sandbox' => env('ECOBANK_SANDBOX', true),
'credentials' => [
'user_id' => env('ECOBANK_USER_ID'),
'password' => env('ECOBANK_PASSWORD'),
'lab_key' => env('ECOBANK_LAB_KEY'),
'client_id' => env('ECOBANK_CLIENT_ID'),
],
'affiliate_code' => env('ECOBANK_AFFILIATE_CODE', 'EGH'),
'request_token' => env('ECOBANK_REQUEST_TOKEN', '/4mZF42iofzo7BDu0YtbwY6swLwk46Z91xItybhYwQGFpaZNOpsznL/9fca5LkeV'),
'source_code' => env('ECOBANK_SOURCE_CODE', 'ECOBANK_QR_API'),
'source_channel_id' => env('ECOBANK_SOURCE_CHANNEL_ID', 'KANZAN'),
'defaults' => [
'currency' => env('ECOBANK_DEFAULT_CURRENCY', 'GHS'),
'country' => env('ECOBANK_DEFAULT_COUNTRY', 'GH'),
],
'timeout' => env('ECOBANK_TIMEOUT', 30),
];
Add your Ecobank credentials to your .env file:
ECOBANK_USER_ID=your_user_id
ECOBANK_PASSWORD=your_password
ECOBANK_LAB_KEY=your_lab_key
ECOBANK_CLIENT_ID=your_client_id
ECOBANK_AFFILIATE_CODE=EGH
ECOBANK_BASE_URL=https://developer.ecobank.com
ECOBANK_SANDBOX=true
Usage
Using the Facade
use SerenityTechnologies\Ecobank\UnifiedApi\Facades\UnifiedApi;
// Get biller list
$response = UnifiedApi::collections()->getBillerList('REQUEST123');
// Get account balance
$response = UnifiedApi::accounts()->getAccountBalance('REQUEST123', '1234567890', 'Company Name');
// Process card payment
$cardPaymentData = [
'paymentDetails' => [
'requestId' => 'REQ123',
'productCode' => '2310',
'amount' => '100.00',
'currency' => 'USD',
'locale' => 'en_US',
'orderInfo' => 'Order #123',
'returnUrl' => 'https://yoursite.com/return'
],
'merchantDetails' => [
'accessCode' => 'access_code',
'merchantID' => 'merchant_id',
'secureSecret' => 'secure_secret'
]
];
$response = UnifiedApi::cardPayments()->processPayment($cardPaymentData);
Using the Helper Function
// Get biller list
$response = unifiedapi()->collections()->getBillerList('REQUEST123');
// Get account balance
$response = unifiedapi()->accounts()->getAccountBalance('REQUEST123', '1234567890', 'Company Name');
// Process card payment
$cardPaymentData = [
'paymentDetails' => [
'requestId' => 'REQ123',
'productCode' => '2310',
'amount' => '100.00',
'currency' => 'USD',
'locale' => 'en_US',
'orderInfo' => 'Order #123',
'returnUrl' => 'https://yoursite.com/return'
],
'merchantDetails' => [
'accessCode' => 'access_code',
'merchantID' => 'merchant_id',
'secureSecret' => 'secure_secret'
]
];
$response = unifiedapi()->cardPayments()->processPayment($cardPaymentData);
Response Handling
All API responses are returned as response objects with helpful methods for accessing data:
// Get biller list
$response = unifiedapi()->collections()->getBillerList('REQUEST123');
if ($response->isSuccess()) {
$billerList = $response->getBillerList();
foreach ($billerList as $biller) {
echo $biller['billerName'];
}
} else {
echo "Error: " . $response->getResponseMessage();
}
// Get account balance
$accountResponse = unifiedapi()->accounts()->getAccountBalance('REQUEST123', '1234567890', 'Company Name');
if ($accountResponse->isSuccess()) {
echo "Available Balance: " . $accountResponse->getAvailableBalance();
echo "Account Name: " . $accountResponse->getAccountName();
echo "Account Number: " . $accountResponse->getAccountNumber();
} else {
echo "Error: " . $accountResponse->getResponseMessage();
}
// Process card payment
$cardPaymentData = [
'paymentDetails' => [
'requestId' => 'REQ123',
'productCode' => '2310',
'amount' => '100.00',
'currency' => 'USD',
'locale' => 'en_US',
'orderInfo' => 'Order #123',
'returnUrl' => 'https://yoursite.com/return'
],
'merchantDetails' => [
'accessCode' => 'access_code',
'merchantID' => 'merchant_id',
'secureSecret' => 'secure_secret'
]
];
$cardResponse = unifiedapi()->cardPayments()->processPayment($cardPaymentData);
if ($cardResponse->isSuccess()) {
$redirectUrl = $cardResponse->getPaymentRedirectUrl();
$timestamp = $cardResponse->getPaymentTimestamp();
// Redirect user to the payment page
header("Location: " . $redirectUrl);
exit;
} else {
echo "Error: " . $cardResponse->getResponseMessage();
}
Dynamic Method Access
All response objects now support dynamic method access to response data. This allows you to access response fields as methods:
$response = unifiedapi()->accounts()->getAccountBalance('REQUEST123', '1234567890', 'Company Name');
// Traditional access
$responseCode = $response->getResponseCode();
$responseMessage = $response->getResponseMessage();
// Dynamic method access (new feature)
$responseCode = $response->response_code();
$responseMessage = $response->response_message();
$responseTimestamp = $response->response_timestamp();
// For nested associative arrays, you can chain methods:
$responseContent = $response->response_content();
if ($responseContent) {
$hostHeaderInfo = $responseContent->hostHeaderInfo();
if ($hostHeaderInfo) {
$sourceCode = $hostHeaderInfo->sourceCode();
$affiliateCode = $hostHeaderInfo->affiliateCode();
}
}
This feature makes it easier to access deeply nested response data without having to manually traverse arrays. It also provides better IDE support with autocompletion when using modern IDEs.
Request Validation
All service methods automatically validate required fields before making API calls. If any required fields are missing, an EcobankException will be thrown with a descriptive error message:
try {
// This will automatically validate that requestId, accountNo, and companyName are provided
$response = unifiedapi()->accounts()->getAccountBalance('REQUEST123', '1234567890', 'Company Name');
} catch (EcobankException $e) {
echo "Validation Error: " . $e->getMessage();
echo "Error Timestamp: " . $e->getTimestamp(); // New: All exceptions now include a timestamp
}
Available Services
- Token Service - For generating access tokens
- Collection Service - For bill payments and collections
- Remittance Service - For cross-border payments
- Account Service - For account management
- Status Service - For checking transaction statuses
- Payment Service - For various payment types
- QR Service - For QR code generation and management
- Card Payment Service - For processing card payments
Response Objects
Each service returns a specific response object with helpful methods:
BaseResponse (Base class for all responses)
isSuccess()- Check if the request was successfulgetResponseCode()- Get the response codegetResponseMessage()- Get the response messagegetResponseContent()- Get the response contentgetResponseTimestamp()- Get the response timestampgetRawResponse()- Get the raw response arraytoArray()- Convert response to array- Dynamic method access to any response field (new feature)
CollectionResponse
getBillerList()- Get list of billersgetBillerDetails()- Get biller detailsgetFormData()- Get form datagetBillerProductInfo()- Get biller product informationgetValidationData()- Get validation data
RemittanceResponse
getInstitutionList()- Get list of institutionsgetAccountDetails()- Get account detailsgetRates()- Get exchange ratesgetTransactionDetails()- Get transaction details
AccountResponse
getAccountBalance()- Get account balance informationgetAccountDetails()- Get account detailsgetAccountName()- Get account namegetAvailableBalance()- Get available balancegetCurrentBalance()- Get current balancegetAccountNumber()- Get account numbergetAccountStatus()- Get account statusgetCurrency()- Get account currencygetStatement()- Get account statementgetAccountCreationDetails()- Get account creation detailsgetAccountBalanceData()- Get structured account balance data modelgetAccountInquiryData()- Get structured account inquiry data modelgetThirdPartyAccountInquiryData()- Get structured third party account inquiry data modelgetStatementTransactions()- Get array of structured statement transaction data models
StatusResponse
getTransactionStatus()- Get transaction statusgetEtokenStatus()- Get etoken statusisTransactionSuccessful()- Check if transaction was successfulgetTransactionStatusCode()- Get transaction status codegetTransactionStatusReason()- Get transaction status reason
PaymentResponse
getPaymentDetails()- Get payment detailsgetTransactionReference()- Get transaction referenceisPaymentSuccessful()- Check if payment was successful
QrResponse
getQrCode()- Get QR code base64 stringgetTerminalId()- Get terminal IDgetMerchantCode()- Get merchant codegetSecretKey()- Get secret keygetDynamicQr()- Get dynamic QR stringgetDynamicQrBase64()- Get dynamic QR base64 stringgetMccList()- Get MCC list
CardPaymentResponse
getPaymentRedirectUrl()- Get payment redirect URLgetPaymentTimestamp()- Get payment timestampgetPaymentDetails()- Get payment details
TokenResponse
getToken()- Get access tokengetUsername()- Get usernameisTokenValid()- Check if token is valid
Data Models
The package now includes structured data models for easier data access:
Account Data Models
AccountBalanceData- Structured data model for account balance responsesAccountInquiryData- Structured data model for account inquiry responsesThirdPartyAccountInquiryData- Structured data model for third party account inquiry responsesStatementTransaction- Structured data model for individual statement transactions
Bills Payment Data Models
BillFormData- Structured data model for bill form dataBiller- Structured data model for biller informationBillerDetails- Structured data model for biller detailsBillerProductInfo- Structured data model for biller product informationFormDataValue- Structured data model for form data values
Host Header Info Data Model
HostHeaderInfo- Structured data model for host header information
Available Methods
Token Service
generateToken()- Generate access token
Collection Service
getBillerList($requestId)- Get list of billersgetBillerDetails($requestId, $billerCode)- Get biller detailsvalidateBiller($data)- Validate a billerpostBillPayment($paymentData)- Post a bill payment
Remittance Service
getInstitutionList($requestId)- Get list of institutionsnameEnquiry($requestId, $accountNo, $destinationCountry)- Perform name enquirygetRates($requestId)- Get exchange ratestransactionEnquiry($requestId, $transactionReference)- Enquire about a transactioncrossBorderPayment($paymentData)- Make cross-border payment
Account Service
getAccountBalance($requestId, $accountNo, $companyName)- Get account balanceaccountEnquiry($requestId, $accountNo, $companyName)- Account enquiryaccountEnquiryThirdParty($requestId, $accountNo, $destinationBankCode, $companyName)- Third party account enquirygenerateStatement($data)- Generate account statementcreateExpressAccount($data)- Create express accountcreateAccount($data)- Create internal account
Status Service
checkTransactionStatus($requestId)- Check transaction statuscheckEtokenStatus($requestId)- Check etoken status
Payment Service
makePayment($paymentData)- Make various types of payments
QR Service
getMcc($requestId)- Get Merchant Category CodecreateQr($data)- Create QR codegenerateDynamicQr($data)- Generate dynamic QR paymentaddQrTerminal($data)- Add QR terminal
Card Payment Service
processPayment($paymentData)- Process card paymentcreatePayment($requestId, $productCode, $amount, $currency, $locale, $orderInfo, $returnUrl, $accessCode, $merchantID, $secureSecret)- Create card payment with individual parameters
Response Structure
All responses follow the Ecobank API structure:
[
'response_code' => 200, // HTTP response code or Ecobank status code
'response_message' => 'success', // Response message
'response_content' => [...], // Actual response data
'response_timestamp' => '...' // Timestamp of response
]
Exception Handling
The package now includes enhanced exception handling with timestamp information:
try {
$response = unifiedapi()->accounts()->getAccountBalance('REQUEST123', '1234567890', 'Company Name');
} catch (EcobankException $e) {
// All exceptions now include a timestamp in the message
// Format: [YYYYMMDDHHmmss] Error message
echo "Error: " . $e->getMessage();
// You can also access the timestamp separately
echo "Timestamp: " . $e->getTimestamp();
}
Security
If you discover any security related issues, please email security@ecobank.com instead of using the issue tracker.
License
The MIT License (MIT). Please see License File for more information.