PHP code example of maestrodimateo / simple-mobile-money

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

    

maestrodimateo / simple-mobile-money example snippets


$response = MobileMoney::driver('singpay')->pay(new PaymentRequest(
    amount: 500,                 // XAF
    reference: 'ORDER-1042',     // your unique reference
    msisdn: '074000000',         // customer's number
    operator: Operator::AIRTEL,
));

use Maestrodimateo\MobileMoney\Facades\MobileMoney;
use Maestrodimateo\MobileMoney\Data\PaymentRequest;
use Maestrodimateo\MobileMoney\Enums\Operator;

class CheckoutController
{
    public function pay(Request $request)
    {
        $response = MobileMoney::driver('singpay')->pay(new PaymentRequest(
            amount: 500,
            reference: 'ORDER-'.$order->id,
            msisdn: $request->input('phone'),
            operator: Operator::AIRTEL,
            description: "Order #{$order->id}",
        ));

        // Hosted flow → redirect the customer
        if ($response->needsRedirect()) {
            return redirect()->away($response->redirectUrl);
        }

        // USSD-push flow → customer confirms on their phone
        return view('checkout.pending', ['reference' => $response->reference]);
    }
}

use Maestrodimateo\MobileMoney\Events\PaymentSucceeded;

class FulfilOrder
{
    public function handle(PaymentSucceeded $event): void
    {
        $order = Order::where('reference', $event->result->reference)->firstOrFail();

        if ($order->isPaid()) {
            return; // idempotent: the same webhook may arrive more than once
        }

        $order->markPaid();
    }
}

use Maestrodimateo\MobileMoney\Data\PayoutRequest;
use Maestrodimateo\MobileMoney\Enums\Operator;
use Maestrodimateo\MobileMoney\Enums\PayoutType;

$response = MobileMoney::driver('ebilling')->payout(new PayoutRequest(
    amount: 5000,
    reference: 'PAYOUT-42',
    msisdn: '074000000',
    operator: Operator::AIRTEL,
    type: PayoutType::REFUND,       // refund | cashback | withdrawal
));

$response->status;  // PaymentStatus::SUCCESS (synchronous)

MobileMoney::driver('ebilling')->balance();                 // ['airtelmoney' => 305394, ...]
MobileMoney::driver('ebilling')->payoutStatus('PAYOUT-42'); // PaymentStatus

MobileMoney::driver(?string $provider = null): Gateway   // null = default provider
MobileMoney::fake(): MobileMoneyFake                      // test double (see Testing)

$status->isFinal();       // true for SUCCESS / FAILED / CANCELLED / EXPIRED
$status->isSuccessful();  // true only for SUCCESS

$operator->label();  // "Airtel Money" / "Moov Money"

use Maestrodimateo\MobileMoney\Facades\MobileMoney;

it('places an order', function () {
    MobileMoney::fake();

    $this->post('/checkout', ['amount' => 500])->assertOk();

    MobileMoney::assertPaid(fn ($request, $provider) => $request->amount === 500);
});

use Maestrodimateo\MobileMoney\Enums\PaymentStatus;

it('handles a declined payment', function () {
    MobileMoney::fake()->respondToPayments(PaymentStatus::FAILED);

    // ... assert your code reacts to the FAILED response
});

use Illuminate\Support\Facades\Http;

Http::fake([
    'gateway.singpay.ga/*' => Http::response([
        'transaction' => ['id' => 'tx-1', 'status' => 'Start'],
    ]),
]);

$response = MobileMoney::driver('singpay')->pay(/* ... */);
expect($response->providerReference)->toBe('tx-1');
bash
php artisan vendor:publish --tag=mobile-money-config
php artisan migrate