PHP code example of nikba / laravel-maib

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

    

nikba / laravel-maib example snippets


return [
    'project_id'     => env('MAIB_PROJECT_ID'),
    'project_secret' => env('MAIB_PROJECT_SECRET'),
    'signature_key'  => env('MAIB_SIGNATURE_KEY'),
    'environment'    => env('MAIB_ENV', 'production'),
    'base_url'       => 'https://api.maibmerchants.md/v1',
    'callback_route' => env('MAIB_CALLBACK_ROUTE', '/maib/callback'),
    'cache_driver'   => env('MAIB_CACHE_DRIVER', null),
    'cache_prefix'   => 'maib_token',
];

use Nikba\LaravelMaib\Facades\Maib;
use Nikba\LaravelMaib\Dto\Requests\DirectPaymentRequest;
use Nikba\LaravelMaib\Dto\PaymentItem;
use Nikba\LaravelMaib\Enums\Currency;
use Nikba\LaravelMaib\Enums\Language;

$response = Maib::directPayment(
    new DirectPaymentRequest(
        amount:      99.99,
        currency:    Currency::MDL,
        clientIp:    $request->ip(),
        language:    Language::RO,
        orderId:     'ORDER-001',
        description: 'Order #001',
        callbackUrl: route('maib.callback'),
        okUrl:       route('payment.success'),
        failUrl:     route('payment.fail'),
        items: [
            new PaymentItem(id: '1', name: 'Product A', price: 99.99, quantity: 1),
        ],
    )
);

return redirect($response->payUrl);

use Nikba\LaravelMaib\Dto\Requests\CapturePaymentRequest;
use Nikba\LaravelMaib\Dto\Requests\HoldPaymentRequest;

// Step 1: Hold
$hold = Maib::hold(new HoldPaymentRequest(
    amount:   99.99,
    currency: Currency::MDL,
    clientIp: $request->ip(),
    language: Language::RO,
));

return redirect($hold->payUrl);

// Step 2: Capture (after callback confirms hold)
$capture = Maib::capture(new CapturePaymentRequest(
    payId:         $payId,
    confirmAmount: 99.99, // optional — omit to capture full hold amount
));

use Nikba\LaravelMaib\Dto\Requests\ExecuteRecurringRequest;
use Nikba\LaravelMaib\Dto\Requests\SaveCardRecurringRequest;

// Registration (once per customer — redirect to 3DS checkout)
$save = Maib::savecardRecurring(new SaveCardRecurringRequest(
    billerExpiry: '1228',
    clientIp:     $request->ip(),
    currency:     Currency::MDL,
    language:     Language::RO,
    email:        '[email protected]',
));

return redirect($save->payUrl);
// Callback will return billerId — store it for the customer.

// Server-side execution (no customer redirect)
$response = Maib::executeRecurring(new ExecuteRecurringRequest(
    billerId: $user->maib_biller_id,
    amount:   29.99,
    currency: Currency::MDL,
    orderId:  'SUBSCRIPTION-' . now()->format('Y-m'),
));

if ($response->status === \Nikba\LaravelMaib\Enums\TransactionStatus::OK) {
    // subscription renewed
}

use Nikba\LaravelMaib\Dto\Requests\ExecuteOneclickRequest;
use Nikba\LaravelMaib\Dto\Requests\SaveCardOneclickRequest;

// Registration
$save = Maib::savecardOneclick(new SaveCardOneclickRequest(
    billerExpiry: '1228',
    clientIp:     $request->ip(),
    currency:     Currency::MDL,
    language:     Language::RO,
    email:        '[email protected]',
));

return redirect($save->payUrl);

// Execution (redirects customer to checkout for 3DS confirmation)
$response = Maib::executeOneclick(new ExecuteOneclickRequest(
    billerId:  $user->maib_biller_id,
    amount:    49.99,
    currency:  Currency::MDL,
    clientIp:  $request->ip(),
    language:  Language::RO,
));

return redirect($response->payUrl);

use Nikba\LaravelMaib\Dto\Requests\RefundRequest;

$response = Maib::refund(new RefundRequest(
    payId:        $payId,
    refundAmount: 30.00, // optional — omit to refund the full amount
));

$info = Maib::payInfo($payId);

echo $info->status->value;   // 'OK', 'FAILED', etc.
echo $info->cardNumber;

$response = Maib::deleteCard($billerId);

// In App\Providers\EventServiceProvider (or using #[AsEventListener])
use Nikba\LaravelMaib\Events\MaibCardSaved;
use Nikba\LaravelMaib\Events\MaibPaymentFailed;
use Nikba\LaravelMaib\Events\MaibPaymentReceived;

Event::listen(MaibPaymentReceived::class, function (MaibPaymentReceived $event) {
    $payload = $event->payload;

    Order::where('maib_pay_id', $payload->payId)
        ->update([
            'status'      => 'paid',
            'rrn'         => $payload->rrn,
            'approval'    => $payload->approval,
            'card_number' => $payload->cardNumber,
        ]);
});

Event::listen(MaibCardSaved::class, function (MaibCardSaved $event) {
    // Store the token for future recurring / one-click charges.
    User::where('id', $event->payload->orderId)
        ->update(['maib_biller_id' => $event->payload->billerId]);
});

Event::listen(MaibPaymentFailed::class, function (MaibPaymentFailed $event) {
    Order::where('maib_pay_id', $event->payload->payId)
        ->update(['status' => 'failed']);
});
bash
php artisan vendor:publish --tag=maib-config