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
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\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
});