PHP code example of aaronkatema / laravel-smilepay
1. Go to this page and download the library: Download aaronkatema/laravel-smilepay 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/ */
// Leg 1
$result = SmilePay::smileCash(
PaymentRequest::make('ORDER-1', 25.00, 'USD')
->withItem('Premium plan')
->withMsisdn('0711111111')
);
session(['smilepay_txn' => $result->transactionReference]);
// Leg 2 — after the customer types the code
$confirmed = SmilePay::confirmOtp(
transactionReference: session('smilepay_txn'),
otp: $request->input('otp'),
method: PaymentMethod::WALLETPLUS,
orderReference: 'ORDER-1',
);
$result = SmilePay::card(
PaymentRequest::make('ORDER-1', 10.00, 'USD')->withItem('Widget'),
CardDetails::fromExpiryString('5123450000000008', '01/39', '100')
);
// Prefer the structured challenge over ZB's redirectHtml
if ($result->challenge?->hasStructuredChallenge()) {
return view('checkout.3ds', [
'html' => $result->challenge->toSafeHtml(target: '3ds-frame'),
]);
}
use AaronKatema\SmilePay\Events\PaymentSucceeded;
class FulfilOrder
{
public function handle(PaymentSucceeded $event): void
{
$order = Order::where('reference', $event->orderReference())->firstOrFail();
$order->markPaid(
amount: $event->snapshot->amount,
fee: $event->snapshot->merchantFee,
);
}
}
class PollSmilePayTransaction implements ShouldQueue
{
public function __construct(private string $orderReference) {}
public function handle(SmilePay $smilepay): void
{
$snapshot = $smilepay->poll($this->orderReference, timeoutSeconds: 120);
if ($snapshot->isPending()) {
$this->release(60);
}
}
}
use AaronKatema\SmilePay\Facades\SmilePay;
it('fulfils an order once paid', function () {
$fake = SmilePay::fake()->willSucceed('ORDER-1');
$this->post('/checkout', ['reference' => 'ORDER-1', 'amount' => 25.00]);
$fake->assertInitiated('ORDER-1')
->assertMethodUsed('ORDER-1', PaymentMethod::ECOCASH)
->assertPaid('ORDER-1');
});