PHP code example of sirmekus / ego

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

    

sirmekus / ego example snippets


$paymentFactory = new PaymentFactory();
$data = [
    'amount' => 1000,
    'email' => '[email protected]',
    'callback_url' => 'http://localhost/webhook',
    'reference' => 'randomized',
];
$response = $paymentFactory->pay($data);

// Sets the configuration/credentials for the underlying payment gateway
public function setKey(string|array $key): void;

// Builds the appropriate payload from an array of values the target gateway expects.
// The underlying payment gateway class determines which fields it extracts.
public function prepareForPayment(array $data): array;

// Builds the appropriate transfer payload from an array of values.
public function prepareForTransfer(array $data): array;

// Initiates a payment or deposit
public function pay(array $array): array;

/**
 * Verifies a payment, deposit, or transfer. The return value is dependent on
 * the underlying payment gateway.
 *
 * $paymentType is optional and can be defined by your implementation. Enums are
 * intentionally avoided so each implementation can define its own. An error will
 * typically be thrown if the payment type is not supported.
 */
public function verifyPayment(array|string $array, ?string $paymentType = null): array;

// Verifies an incoming webhook. If the webhook is valid, execution continues;
// otherwise it fails with a 401/404 response.
public function verifyWebhook(Request $request): void;

// Fetches a list of banks supported by the underlying payment gateway
public function getBanks(string $countryCode = ""): array;

// Verifies an account number
public function verifyAccountNumber(array $request): array;

// Runs a transfer/withdrawal transaction
public function transfer(array $data): array;

// Returns the crafted payload if magic methods were used to build it
public function getPayload(): array;

$paymentFactory = new PaymentFactory();
$paymentFactory->setAmount($amount);
$paymentFactory->setCurrency($currency);
$paymentFactory->setReference($reference);
$paymentFactory->setMetadata($metadata);

$response = $paymentFactory->pay();

$paymentFactory = new PaymentFactory();
// Assuming a validated Laravel request
$paymentFactory->prepareForPayment($request->validated());
$response = $paymentFactory->pay();

$paymentFactory = new PaymentFactory();
$gateway = $paymentFactory->getGatewayInstance();
// Now you can use the actual payment gateway class
$gateway->someGatewaySpecificMethod();

return [
    // Your app's default payment gateway. Must exist in the 'providers' section
    // below and have its credentials set up.
    'default' => 'paystack',

    // Whether to verify webhook authenticity. Recommended to keep true.
    'verify_webhook' => env('EGO_VERIFY_WEBHOOK', true),

    'credentials' => [
        'paystack' => [
            'secret_key' => env('PAYSTACK_SECRET_KEY'),
        ],
        'flutterwave' => [
            'secret_key' => env('FLUTTERWAVE_SECRET_KEY'),
        ],
        'stripe' => [
            'secret_key'      => env('STRIPE_SECRET_KEY'),
            'signing_secret'  => env('STRIPE_SIGNING_SECRET'),
            'account_id'      => env('STRIPE_ACCOUNT_ID'),
            'client_id'       => env('STRIPE_CLIENT_ID'),
        ],
        'nomba' => [
            'client_id'     => env('NOMBA_CLIENT_ID'),
            'secret_key'    => env('NOMBA_SECRET_KEY'),
            'account_id'    => env('NOMBA_ACCOUNT_ID'),
            'signature_key' => env('NOMBA_SIGNATURE_KEY'),
            'base_url'      => env('NOMBA_BASE_URL'),
        ],
        'budpay' => [
            'secret_key' => env('BUDPAY_SECRET_KEY'),
            'public_key' => env('BUDPAY_PUBLIC_KEY'),
        ],
    ],

    'providers' => [
        'paystack'    => Emmy\Ego\Gateway\Paystack\Paystack::class,
        'flutterwave' => Emmy\Ego\Gateway\Flutterwave\Flutterwave::class,
        'stripe'      => Emmy\Ego\Gateway\Stripe\Stripe::class,
        'nomba'       => Emmy\Ego\Gateway\Nomba\Nomba::class,
        'budpay'      => Emmy\Ego\Gateway\BudPay\BudPay::class,
    ],
];

// Default: redirect to checkout page
$paymentFactory = new PaymentFactory('nomba');
$response = $paymentFactory->pay([
    'email'        => '[email protected]',
    'amount'       => 5000,
    'currency'     => 'NGN',
    'reference'    => 'unique-ref-001',
    'callback_url' => 'https://yourapp.com/payment/callback',
]);
// $response['url'] contains the checkout link

// Tokenized: charge card directly
$response = $paymentFactory->pay([
    'email'     => '[email protected]',
    'amount'    => 5000,
    'currency'  => 'NGN',
    'reference' => 'unique-ref-002',
    'token'     => 'card-token-from-nomba',
]);

// Verify a customer payment
$status = $paymentFactory->verifyPayment('unique-ref-001', 'transaction');

// Verify a bank transfer
$status = $paymentFactory->verifyPayment('unique-ref-002', 'bank_transfer');

[
    'status'    => 'success' | 'pending' | 'failed',
    'message'   => 'Transaction description or narration',
    'data'      => [...],  // Full transaction data from Nomba
    'reference' => 'order-reference-string',
]

$paymentFactory = new PaymentFactory('nomba');
$response = $paymentFactory->transfer([
    'account_number' => '0123456789',
    'account_name'   => 'John Doe',
    'bank_code'      => '058',
    'amount'         => 10000,
    'narration'      => 'Payment for services',
    'sender_name'    => 'My Business',
    'reference'      => 'transfer-ref-001',
]);

$result = $paymentFactory->verifyAccountNumber([
    'accountNumber' => '0123456789',
    'bankCode'      => '058',
]);

// Returns:
// [
//     'success'       => true,
//     'accountNumber' => '0123456789',
//     'bankCode'      => '058',
//     'accountName'   => 'John Doe',
// ]

$paymentFactory = new PaymentFactory('budpay');

$payload = $paymentFactory->prepareForPayment([
    'email'        => '[email protected]',
    'amount'       => '5000',
    'currency'     => 'NGN',
    'reference'    => 'order-ref-001',
    'callback_url' => 'https://yourapp.com/payment/callback',
]);

$response = $paymentFactory->pay($payload);

$status = $paymentFactory->verifyPayment('order-ref-001');

$status = $paymentFactory->verifyPayment('order-ref-001', 'transaction');

$status = $paymentFactory->verifyPayment('order-ref-001', 'payout');

$paymentFactory = new PaymentFactory('budpay');

$banks = $paymentFactory->getBanks();
$nigerianBanks = $paymentFactory->getGatewayInstance()->getBanksByCurrency('NGN');

$paymentFactory = new PaymentFactory('budpay');

$banks = $paymentFactory->getBanks();
$nigerianBanks = $paymentFactory->getBanksByCurrency('NGN');

$result = $paymentFactory->verifyAccountNumber([
    'bank_code'      => '000013',
    'account_number' => '0050883605',
]);

$paymentFactory = new PaymentFactory('budpay');

$payload = $paymentFactory->prepareForTransfer([
    'currency'       => 'NGN',
    'amount'         => '10000',
    'bank_code'      => '000013',
    'bank_name'      => 'GUARANTY TRUST BANK',
    'account_number' => '0050883605',
    'narration'      => 'Vendor payment',
]);

$response = $paymentFactory->transfer($payload);

$fee = $paymentFactory->getGatewayInstance()->calculateTransferFee([
    'currency' => 'NGN',
    'amount'   => '10000',
]);

$fee = $paymentFactory->calculateTransferFee([
    'currency' => 'NGN',
    'amount'   => '10000',
]);

$paymentFactory = new PaymentFactory('budpay');

$payload = $paymentFactory->prepareForTransfer([
    'currency' => 'NGN',
    'transfers' => [
        [
            'amount'         => '20000',
            'bank_code'      => '000013',
            'bank_name'      => 'GUARANTY TRUST BANK',
            'account_number' => '0050883605',
            'narration'      => 'January salary',
        ],
    ],
]);

$response = $paymentFactory->transfer($payload);

$status = $paymentFactory->verifyPayment('trf_reference', 'payout');

// web.php
Route::post('money/na/water/webhook/{gateway}', App\Http\Controllers\Dashboard\WebhookController::class);

class WebhookController extends Controller
{
    public function __invoke(Request $request, string $gateway)
    {
        $gateway = new PaymentFactory($gateway);
        $gateway->verifyWebhook($request);

        $payload = $request->json()->all();

        // Handle the payload. Ideally, fire an event to avoid blocking
        // on long-running tasks.

        return response()->json();
    }
}
bash
php artisan vendor:publish --provider="Emmy\Ego\Provider\EgoProvider"