定制 digital-threads/liqpay 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

digital-threads/liqpay

最新稳定版本:1.0.1

Composer 安装命令:

composer require digital-threads/liqpay

包简介

Laravel LiqPay Client

README 文档

README

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="//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

统计信息

  • 总下载量: 2.55k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-06-17