PHP code example of islam016 / laravel-safe-checkout

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

    

islam016 / laravel-safe-checkout example snippets


use IslamGaber\SafeCheckout\Facades\Checkout;
use IslamGaber\SafeCheckout\Money;

public function pay(Order $order)
{
    $payment = Checkout::start(
        Money::fromMajor($order->total, 'SAR'),   // "150.75" -> 15075 halalas
        idempotencyKey: "order-{$order->id}",     // same order = same checkout, never two
        payable: $order,
        options: ['description' => "Order #{$order->id}"],
    );

    return redirect()->away($payment->checkout_url);
}

Checkout::start(Money::of(15075), "order-{$order->id}", $order, [
    'gateway' => 'tap',
    'customer' => ['first_name' => $user->name, 'email' => $user->email],
]);

use IslamGaber\SafeCheckout\Events\PaymentSucceeded;
use IslamGaber\SafeCheckout\Events\PaymentFailed;

Event::listen(function (PaymentSucceeded $event) {
    $event->payment->payable->markAsPaid();   // fired exactly once per payment
});

Event::listen(function (PaymentFailed $event) {
    // failed, expired, or amount_mismatch (see $event->payment->failure_reason)
});

$payment = Checkout::reconcile(Payment::where('uuid', $uuid)->firstOrFail());

return $payment->isPaid() ? view('checkout.thanks') : view('checkout.pending');

// routes/console.php
Schedule::command('safe-checkout:reconcile --older-than=10')->everyFiveMinutes();

Checkout::extend('hyperpay', fn (string $name, array $config) => new HyperPayGateway($name, $config));