PHP code example of bowphp / payment

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

    

bowphp / payment example snippets


use Bow\Payment\Payment;

return [
    'default' => [
        'gateway' => Payment::ORANGE,
        'country' => 'ivory_coast',
    ],
    
    'ivory_coast' => [
        'orange' => [
            'client_key' => env('ORANGE_CLIENT_KEY'),
            'client_secret' => env('ORANGE_CLIENT_SECRET'),
            'webhook_secret' => env('ORANGE_WEBHOOK_SECRET'),
        ],
        'mtn' => [
            'subscription_key' => env('MTN_SUBSCRIPTION_KEY'),
            'api_user' => env('MTN_API_USER'),
            'api_key' => env('MTN_API_KEY'),
            'environment' => 'sandbox', // or 'production'
            'webhook_secret' => env('MTN_WEBHOOK_SECRET'),
        ],
        // Other providers...
    ],
];

use Bow\Payment\Payment;

// Configure the payment gateway
$gateway = Payment::configure($config);

// Make a payment
$gateway->payment([
    'amount' => 1000,
    'phone_number' => '+225070000001',
    'reference' => 'ORDER-123',
    'options' => [
        'notif_url' => 'https://your-app.com/webhook',
        'return_url' => 'https://your-app.com/success',
        'cancel_url' => 'https://your-app.com/cancel',
    ]
]);

// Verify a transaction
$status = $gateway->verify([
    'reference' => 'ORDER-123',
    'options' => [
        // 
    ]
]);

if ($status->isSuccess()) {
    // Payment successful
}

use Bow\Payment\UserPayment;

class User extends Model
{
    use UserPayment;
}

// Now you can use payment methods on your user model
$user->payment(1000, 'ORDER-123');
$user->transfer(5000, 'TRANSFER-456');

use Bow\Payment\Support\RetryHandler;

$retry = new RetryHandler(
    maxAttempts: 3,
    retryDelay: 1000,
    exponentialBackoff: true
);

$result = $retry->execute(function() use ($payment) {
    return $payment->pay($amount);
});

use Bow\Payment\Support\RateLimiter;

$limiter = new RateLimiter(
    maxRequests: 60,
    timeWindow: 60
);

if ($limiter->isAllowed('orange')) {
    $limiter->hit('orange');
    // Make API call
}

use Bow\Payment\Support\TransactionLogger;

$logger = new TransactionLogger('/path/to/logs');

// Logs are automatically created with detailed context
$logger->logPaymentRequest('mtn', [
    'amount' => 1000,
    'reference' => 'ORDER-123'
]);

$logger->logPaymentResponse('mtn', true, $response);

use Bow\Payment\Webhook\WebhookHandler;

$handler = new WebhookHandler('orange', $config['webhook_secret']);
$request = WebhookHandler::parseRequest();

$event = $handler->handle($request['payload'], $request['signature']);

if ($event->isPaymentSuccess()) {
    $transactionId = $event->getTransactionId();
    $amount = $event->getAmount();
    // Update order status
}

use Bow\Payment\Exceptions\PaymentRequestException;
use Bow\Payment\Exceptions\RateLimitException;
use Bow\Payment\Exceptions\TokenGenerationException;

try {
    Payment::payment($data);
} catch (RateLimitException $e) {
    // Rate limit exceeded
    $retryAfter = $e->getCode();
} catch (PaymentRequestException $e) {
    // Payment request failed
    Log::error($e->getMessage());
} catch (TokenGenerationException $e) {
    // Token generation failed
}

Payment::configure([
    'default' => [
        'gateway' => Payment::ORANGE,
        'country' => 'ci',
    ],
    'ivory_coast' => [
        'orange' => [
            'client_key' => 'YOUR_CLIENT_KEY',
            'client_secret' => 'YOUR_CLIENT_SECRET',
        ],
    ],
]);

$result = Payment::payment([
    'amount' => 1000,
    'reference' => 'ORDER-123',
    'options' => [
        'notif_url' => 'https://your-app.com/webhook',
        'return_url' => 'https://your-app.com/success',
        'cancel_url' => 'https://your-app.com/cancel',
    ],
]);

Payment::configure([
    'default' => [
        'gateway' => Payment::MTN,
        'country' => 'ci',
    ],
    'ivory_coast' => [
        'mtn' => [
            'subscription_key' => 'YOUR_SUBSCRIPTION_KEY',
            'api_user' => 'YOUR_API_USER',
            'api_key' => 'YOUR_API_KEY',
            'environment' => 'sandbox',
        ],
    ],
]);

$result = Payment::payment([
    'amount' => 1000,
    'reference' => 'ORDER-123',
    'phone_number' => '0707070707',
]);

// Verify transaction
$status = Payment::verify(['reference' => $result['reference']]);

// Check balance
$balance = Payment::balance();

// Start with Orange Money
Payment::configure($config);

// Switch to MTN for a specific transaction
Payment::useProvider('ci', Payment::MTN);
Payment::payment($data);