LaravelPackages.net
Acme Inc.
Toggle sidebar
digital-threads/liqpay

Laravel LiqPay Client

2.696
1
1.0.1
About digital-threads/liqpay

digital-threads/liqpay is a Laravel package for laravel liqpay client. It currently has 1 GitHub stars and 2.696 downloads on Packagist (latest version 1.0.1). Install it with composer require digital-threads/liqpay. Discover more Laravel packages by digital-threads or browse all Laravel packages to compare alternatives.

Last updated

Laravel Liqpay Client

Code Coverage License Code Coverage

Installation

Run composer require digital-threads/liqpay

Configurations

LiqPay client requires following configurations to be set in your environment:

| Key | Description | | ------------------------- | ----------------------------------------------------------------------------------- | | LIQPAY_PUBLIC_KEY | LiqPay Public Key | | LIQPAY_PRIVATE_KEY | LiqPay Private Key | | LIQPAY_DEFAULT_CURRENCY | Default order currency that will be used if none will be specified for each request |

Alternatively you can publish package configurations and specify your own boundaries:

php artisan vendor:publish --provider='DigitalThreads\LiqPay\LiqPayServiceProvider' --tag='config'

Usage

After package configurations were specified you can use DigitalThreads\LiqPay\LiqPay facade for your payment operations.

Checkout

In order to render LiqPay form you may want to securly recieve Checkout encoded form parameters from your backend API like following:

PaymentController.php

<?php

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(),
        ]);
    }
}

api.php

<?php

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

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

Then you can render the form with your favorite front-end framework like VueJS:

<template>
  <form method="POST" action="{{ form.action }}" accept-charset="utf-8">
    <input type="hidden" name="data" value="{{ form.data }}" />
    <input type="hidden" name="signature" value="{{ form.signature }}" />
    <input
      type="image"
      src="https://raw.githubusercontent.com/Digital-Threads/Liqpay/refs/heads/main//static.liqpay.ua/buttons/p1en.radius.png"
      name="btn_text"
    />
  </form>
</template>

<script>
  export default {
    data() {
      return {
        orderId: 1,
        form: {
          action: null,
          data: null,
          signature: null,
        },
      };
    },
    async mounted() {
      const response = await fetch(`{your-api-url}/${this.orderId}/checkout`);
      this.form = response.json();
    },
  };
</script>

Callback Validation

During payment processing your API will recieve a Callback post request with url that was specified as server_url in the PaymentController. You will need to register callback handler route in order to update order status according to the data in the request.

PaymentController.php

<?php

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);
        }
    }
}

api.php

<?php

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');

LiqPay::validateCallback method will take care of the request validation and signature checks and will return an instance of LiqPayPaymentDetailsInterface, use it to extract order details data for your needs.

Credits

Comments