PHP code example of elgiosoft / elgiopay-php-sdk

1. Go to this page and download the library: Download elgiosoft/elgiopay-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/ */

    

elgiosoft / elgiopay-php-sdk example snippets


use ElgioPay\SDK\ElgioPayClient;

// Automatically reads from ELGIOPAY_API_KEY and ELGIOPAY_ENV
$client = new ElgioPayClient();

use ElgioPay\SDK\ElgioPayClient;

// For testing (sandbox)
$client = new ElgioPayClient('sandbox', 'pk_test_your_api_key');

// For production (live)
$client = new ElgioPayClient('prod', 'pk_live_your_api_key');

// Signature
$client->initiatePayment(array $paymentData): array

// Payload shape
// [
//     'amount'          => float,
//     'currency'        => string,   // 'XAF' | 'XOF' | 'EUR' | 'USD'
//     'payment_method'  => string,   // 'mtn_mobile_money' | 'orange_money'
//     'channel_code'    => string,   // e.g. 'ORANGE_CMR' — alternative to payment_method
//     'customer_phone'  => string,   // E.164, e.g. '+237677123456'
//     'customer_name'   => string,   // optional
//     'customer_email'  => string,   // optional
//     'reference'       => string,   // optional
//     'metadata'        => array,    // optional
//     'surcharge'       => float,    // optional — SURCHARGE wallet add-on
// ]

$result = $client->initiatePayment([
    'amount'         => 1000,
    'channel_code'   => 'ORANGE_CMR',
    'customer_phone' => '+237699000000',
]);

try {
    $result = $client->initiatePayment([
        'amount'         => 1000.00,
        'currency'       => 'XAF',
        'payment_method' => 'mtn_mobile_money',
        'customer_phone' => '+237677123456',
        'customer_name'  => 'John Doe',
        'customer_email' => '[email protected]',
        'reference'      => 'ORDER-123',
        'metadata'       => [
            'order_id' => 123,
            'product'  => 'Premium Plan',
        ],
    ]);

    echo "Transaction ID: " . $result['transaction_id'];
    echo "Status: " . $result['status'];
} catch (\ElgioPay\SDK\ElgioPayException $e) {
    echo "Payment failed: " . $e->getMessage();
}

try {
    $result = $client->initiatePayment([
        'amount'         => 5000.00,
        'currency'       => 'XAF',
        'payment_method' => 'orange_money',
        'customer_phone' => '+237677123456',
        'customer_name'  => 'Jane Doe',
        'reference'      => 'INV-456',
    ]);

    echo "Payment URL: " . $result['payment_url'];
} catch (\ElgioPay\SDK\ElgioPayException $e) {
    echo "Payment failed: " . $e->getMessage();
}

$phone = $client->normalizeCameroonPhone('677123456'); // → +237677123456

$result = $client->initiatePayment([
    'amount'         => 1000.00,
    'currency'       => 'XAF',
    'payment_method' => 'mtn_mobile_money',
    'customer_phone' => $phone,
]);

// All channels
$channels = $client->listChannels();

// Filtered: payout-capable mobile-money channels in Cameroon
$payoutChannels = $client->listChannels([
    'type'      => 'mobile_money',
    'direction' => 'payout',
    'country'   => 'CMR',
]);
// $payoutChannels['data'] => [ ['code' => 'ORANGE_CMR', 'currency' => 'XAF', ...], ... ]

// Filters: type, direction, country (ISO-3), category, name

$connected = $client->connectedMerchants();

// 1. Onboard. Provide `email` plus company_name OR first_name.
$merchant = $connected->create([
    'email' => '[email protected]',
    'company_name' => 'Boutique Mballa',
    'metadata' => ['external_id' => 'shop_42'],
]);
// => ['merchant_id' => 'mch_…', 'kyc_status' => 'pending', 'status' => 'created', ...]

// 2. Charge on its behalf — the net amount credits ITS balance, not your float.
$client->initiatePayment([
    'amount' => 5000,
    'customer_phone' => '+237677389120',
    'sub_merchant_id' => $merchant['merchant_id'],
]);

// Card charges route the same way. The card settlement hold applies to the
// sub-merchant's ledger rather than your float.
$client->cards()->processPayment([
    'amount' => 100,
    'currency' => 'USD',
    'customer_name' => 'Marie Tchana',
    'payment_method_id' => 'pm_123',
    'sub_merchant_id' => $merchant['merchant_id'],
]);

// 3. Read and settle.
$balance = $connected->getBalance($merchant['merchant_id']);
$connected->createPayout($merchant['merchant_id'], [
    'amount' => 4000,
    'payout_method' => 'mtn_mobile_money',
    'recipient_name' => 'Jean Mballa',
    'recipient_phone' => '677389120',
]);

// Listing + lifecycle
$page = $connected->list(['per_page' => 25]);   // ['data' => [...], 'meta' => [...]]
$connected->get($merchant['merchant_id']);
$connected->deactivate($merchant['merchant_id']); // freeze
$connected->reactivate($merchant['merchant_id']);

try {
    $connected->create(['email' => '[email protected]']);
} catch (\ElgioPay\SDK\ElgioPayException $e) {
    if (($e->getResponse()['error'] ?? null) === 'NOT_A_PLATFORM') {
        // This app's merchant isn't a platform.
    }
}

try {
    $status = $client->getPaymentStatus('txn_abc123');
    
    echo "Status: " . $status['status'];
    echo "Amount: " . $status['amount'];
} catch (\ElgioPay\SDK\ElgioPayException $e) {
    echo "Status check failed: " . $e->getMessage();
}

try {
    $verification = $client->verifyPayment('txn_abc123');
    
    if ($verification['verified']) {
        echo "Payment verified successfully!";
    } else {
        echo "Payment verification failed.";
    }
} catch (\ElgioPay\SDK\ElgioPayException $e) {
    echo "Verification failed: " . $e->getMessage();
}

try {
    $result = $client->createPaymentWithRetry([
        'amount' => 2000.00,
        'payment_method' => 'mtn_mobile_money',
        'customer_phone' => '+237677123456',
        'currency' => 'XAF'
    ], maxRetries: 3);
    
    echo "Payment created: " . $result['transaction_id'];
} catch (\ElgioPay\SDK\ElgioPayException $e) {
    echo "Payment failed after retries: " . $e->getMessage();
}

use ElgioPay\SDK\ElgioPayClient;

$client = new ElgioPayClient('sandbox', 'pk_test_your_api_key');

// Simple MTN payment in XAF
$result = $client->initiatePayment([
    'amount'         => 5000.00, // 5000 XAF
    'currency'       => 'XAF',
    'payment_method' => 'mtn_mobile_money',
    'customer_phone' => $client->normalizeCameroonPhone('677123456'),
    'customer_name'  => 'Jean Dupont',
    'reference'      => 'FACTURE-001',
]);

// Simple Orange payment in XAF
$result = $client->initiatePayment([
    'amount'         => 2500.00, // 2500 XAF
    'currency'       => 'XAF',
    'payment_method' => 'orange_money',
    'customer_phone' => '+237677123456',
    'reference'      => 'CMD-002',
]);

// Sandbox environment (for testing)
$client = new ElgioPayClient('sandbox', 'pk_test_your_sandbox_api_key');

// Live environment (for production) 
$client = new ElgioPayClient('prod', 'pk_live_your_live_api_key');

$client = new ElgioPayClient();

try {
    $result = $client->initiatePayment($paymentData);
} catch (\ElgioPay\SDK\ElgioPayException $e) {
    $errorMessage = $e->getMessage();
    $errorCode = $e->getCode();
    $responseData = $e->getResponse(); // API response if available
    
    // Handle error appropriately
}

// In your webhook endpoint
$data = json_decode(file_get_contents('php://input'), true);

if ($data['status'] === 'completed') {
    // Payment successful
    $transactionId = $data['transaction_id'];
    // Update your order status
} elseif ($data['status'] === 'failed') {
    // Payment failed
    $reason = $data['failure_reason'];
    // Handle failed payment
}

// Testing setup
$client = new ElgioPayClient('sandbox', 'pk_test_your_sandbox_key');

// Test payment
$result = $client->initiatePayment([
    'amount'         => 1000.00,
    'currency'       => 'XAF',
    'payment_method' => 'mtn_mobile_money',
    'customer_phone' => '+237677123456',
]);
bash
composer