PHP code example of digital-threads / liqpay

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

    

digital-threads / liqpay example snippets




namespace App\Http\Controllers\Api;

use App\Models\Order;
use DigitalThreads\LiqPay\LiqPay;
use App\Http\Controllers\Controller;

class PaymentController extends Controller
{
    public function checkout($orderId)
    {
        $order = Order::findOrFail($orderId);

        $prerequisites = LiqPay::getCheckoutFormPrerequisites([
            'amount' => $order->amount,
            'description' => $order->description,
            'order_id' => $order->id,
            'result_url' => route('web.checkout'),
            'server_url' => route('api.liqpay_callback'), // The url that wil be used for order webhook notification
            'currency' => $order->currency, // Optional. If not set - default currency will be used.
        ]);

        return new JsonResponse([
            'action' => $prerequisites->getAction(),
            'data' => $prerequisites->getData(),
            'signature' => $prerequisites->getSignature(),
        ]);
    }
}



use Illuminate\Support\Facades\Route;
use  App\Http\Controllers\Api\PaymentController;

Route::get('{order}/checkout', [PaymentController::class, 'checkout']);



namespace App\Http\Controllers\Api;

use App\Models\Order;
use Illuminate\Http\Request;
use DigitalThreads\LiqPay\LiqPay;
use App\Http\Controllers\Controller;
use DigitalThreads\LiqPay\Exceptions\InvalidCallbackRequestException;

class PaymentController extends Controller
{
    public function checkout($orderId)
    {
        $order = Order::findOrFail($orderId);

        $prerequisites = LiqPay::getCheckoutFormPrerequisites([
            'amount' => $order->amount,
            'description' => $order->description,
            'order_id' => $order->id,
            'result_url' => route('web.checkout'),
            'server_url' => route('api.liqpay_callback'), // The url that wil be used for order webhook notification
            'currency' => $order->currency, // Optional. If not set - default currency will be used.
        ]);

        return new JsonResponse([
            'action' => $prerequisites->getAction(),
            'data' => $prerequisites->getData(),
            'signature' => $prerequisites->getSignature(),
        ]);
    }

    public function callback(Request $request)
    {
        try {
            $payload = LiqPay::validateCallback($request);
            $order = Order::findOrFail($payload->get('order_id'));

            $order->update(['status' => $payload->get('status')]);
        } catch (InvalidCallbackRequestException $e) {
            return new JsonResponse(['error' => $e->getMessage()], 400);
        }
    }
}



use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Api\PaymentController;

Route::get('{order}/checkout', [PaymentController::class, 'checkout']);
Route::post('callback', [PaymentController::class, 'callback'])->name('liqpay_callback');