PHP code example of rhaima / larakonnect

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

    

rhaima / larakonnect example snippets


protected $except = [
    'konnect/webhook',
];

use Rhaima\LaraKonnect\Facades\LaraKonnect;

// Create a payment link (amount in TND)
$result = LaraKonnect::createPaymentLink(
    amountTND: 150.500,
    orderId: 'ORDER-2024-001',
    description: 'iPhone repair service',
    customer: [
        'first_name' => 'Mohamed',
        'last_name' => 'Rhaima',
        'email' => '[email protected]',
        'phone' => '22123456',
    ]
);

if ($result->success) {
    return redirect($result->payUrl);
}

// Handle error
return back()->with('error', $result->error);

use Rhaima\LaraKonnect\Facades\LaraKonnect;
use Rhaima\LaraKonnect\Services\KonnectClient;

// Convert TND to millimes (1 TND = 1000 millimes)
$amountMillimes = KonnectClient::toMillimes(150.500); // 150500

// Initialize payment with full options
$result = LaraKonnect::initPayment($amountMillimes, 'ORDER-123', [
    'description' => 'Payment description',
    'acceptedPaymentMethods' => ['bank_card', 'e-DINAR'],
    'lifespan' => 30, // minutes
    'theme' => 'dark',
    'firstName' => 'Mohamed',
    'lastName' => 'Rhaima',
    'email' => '[email protected]',
    'phoneNumber' => '22123456',
    'successUrl' => 'https://yoursite.com/payment/success',
    'failUrl' => 'https://yoursite.com/payment/fail',
]);

// Check payment status
$payment = LaraKonnect::getPayment($paymentRef);

if ($payment->isCompleted()) {
    // Payment successful
}

// Quick status check
if (LaraKonnect::isCompleted($paymentRef)) {
    // ...
}

use Rhaima\LaraKonnect\Models\KonnectPayment;

// Create and initiate payment with tracking
$result = KonnectPayment::createAndInitiate(
    amountTnd: 150.500,
    orderId: 'ORDER-123',
    payable: $order, // Your model (Order, Intervention, etc.)
    customerInfo: [
        'first_name' => 'Mohamed',
        'email' => '[email protected]',
    ]
);

if ($result['success']) {
    return redirect($result['payUrl']);
}

// Query payments
$pendingPayments = KonnectPayment::pending()->get();
$completedPayments = KonnectPayment::completed()->get();
$orderPayments = KonnectPayment::forOrder('ORDER-123')->get();

use Rhaima\LaraKonnect\Traits\HasKonnectPayments;

class Intervention extends Model
{
    use HasKonnectPayments;

    // Optional: customize the order ID
    public function getKonnectOrderId(): string
    {
        return 'INT-' . $this->reference;
    }

    // Optional: auto-fill customer info
    public function getKonnectCustomerInfo(): array
    {
        return [
            'first_name' => $this->client->prenom,
            'last_name' => $this->client->nom,
            'phone' => $this->client->telephone,
        ];
    }
}

$intervention = Intervention::find(1);

// Initiate payment
$result = $intervention->initiateKonnectPayment(150.500);

// Check payment status
if ($intervention->isPaidViaKonnect()) {
    // Already paid
}

// Get payment history
$payments = $intervention->konnectPayments;
$latestPayment = $intervention->latestKonnectPayment;

use Rhaima\LaraKonnect\Events\PaymentInitiated;
use Rhaima\LaraKonnect\Events\PaymentCompleted;
use Rhaima\LaraKonnect\Events\PaymentFailed;

protected $listen = [
    PaymentCompleted::class => [
        UpdateOrderStatus::class,
        SendPaymentConfirmation::class,
    ],
    PaymentFailed::class => [
        NotifyAdminOfFailedPayment::class,
    ],
];

class UpdateOrderStatus
{
    public function handle(PaymentCompleted $event): void
    {
        $order = Order::where('reference', $event->orderId)->first();
        
        if ($order) {
            $order->update([
                'payment_status' => 'paid',
                'paid_at' => now(),
            ]);
            
            // Send notification
            $order->client->notify(new PaymentReceived($order));
        }
    }
}
bash
php artisan larakonnect:install
bash
# Install the package
php artisan larakonnect:install

# Check configuration status
php artisan larakonnect:status

# Check specific payment
php artisan larakonnect:status PAYMENT_REF
bash
php artisan vendor:publish --tag=larakonnect-views
bash
php artisan vendor:publish --tag=larakonnect-config