jonhwu/laravel-unitpay

Laravel的UnitPay支付插件

v4.0.0 2021-04-02 12:57 UTC

README

Latest Stable Version Build Status StyleCI CodeFactor Total Downloads License

通过此Laravel框架包(Laravel)使用UnitPay(unitpay.ru)接受支付。

  • 仅添加两个回调即可接收支付

Laravel >= 8.*, PHP >= 7.3

要使用Laravel 7.*的包,请使用3.x分支

要使用Laravel 6.*的包,请使用2.x分支

要使用Laravel 5.*的包,请使用1.x分支

安装

使用composer安装此包。

composer require "jonhwu/laravel-unitpay"

如果您不使用自动发现,请将ServiceProvider添加到config/app.php中的providers数组中

JonhWu\UnitPay\UnitPayServiceProvider::class,

UnitPay外观添加到您的外观数组中

'UnitPay' => JonhWu\UnitPay\Facades\UnitPay::class,

使用发布命令将包配置复制到本地配置

php artisan vendor:publish --provider="JonhWu\UnitPay\UnitPayServiceProvider"

配置

发布配置文件后,请编辑config/unitpay.php中的配置文件。

  • unitpay.ru上创建一个账户
  • 添加您的项目,复制public_keysecret_key参数,并将其粘贴到config/unitpay.php
  • 配置发布后,编辑config/unitpay.php
  • searchOrderpaidOrder设置回调静态函数
  • 创建到您的控制器的路由,并调用UnitPay::handle方法

使用方法

  1. 生成支付URL或获取重定向
$amount = 100; // Payment`s amount

$email = "example@gmail.com"; // Your customer`s email

$description = "Test payment";

//

$url = UnitPay::getPayUrl($amount, $order_id, $email, $description, $currency);

$redirect = UnitPay::redirectToPayUrl($amount, $order_id, $email, $description, $currency);
  1. 处理来自UnitPay的请求
UnitPay::handle(Request $request)

重要

您必须在config/unitpay.php中定义回调以搜索订单并保存已支付订单。

'searchOrder' => null  // UnitPayController@searchOrder(Request $request)
'paidOrder' => null  // UnitPayController@paidOrder(Request $request, $order)

示例

处理方案

  1. 请求来自unitpay.ruGET请求http://yourproject.com/unitpay/result(带有参数)。
  2. 函数UnitPayController@handlePayment运行验证过程(自动验证请求参数)。
  3. 将调用searchOrder方法(参见config/unitpay.php中的searchOrder)以通过唯一ID搜索订单。
  4. 如果数据库中的当前订单状态不是paid,则将调用paidOrder方法(参见config/unitpay.php中的paidOrder)。

将路由添加到routes/web.php

 Route::get('/unitpay/result', 'UnitPayController@handlePayment');

注意:不要忘记在unitpay.ru上保存您项目的完整路由URL(例如,http://example.com/unitpay/result)。

创建以下控制器:/app/Http/Controllers/UnitPayController.php

class UnitPayController extends Controller
{
    /**
     * Search the order in your database and return that order
     * to paidOrder, if status of your order is 'paid'
     *
     * @param Request $request
     * @param $order_id
     * @return bool|mixed
     */
    public function searchOrder(Request $request, $order_id)
    {
        $order = Order::where('id', $order_id)->first();

        if($order) {
            $order['_orderSum'] = $order->sum;

            // If your field can be `paid` you can set them like string
            $order['_orderStatus'] = $order['status'];

            // Else your field doesn` has value like 'paid', you can change this value
            $order['_orderStatus'] = ('1' == $order['status']) ? 'paid' : false;

            return $order;
        }

        return false;
    }

    /**
     * When paymnet is check, you can paid your order
     *
     * @param Request $request
     * @param $order
     * @return bool
     */
    public function paidOrder(Request $request, $order)
    {
        $order->status = 'paid';
        $order->save();

        //

        return true;
    }

    /**
     * Start handle process from route
     *
     * @param Request $request
     * @return mixed
     */
    public function handlePayment(Request $request)
    {
        return UnitPay::handle($request);
    }
}

变更日志

有关最近更改的更多信息,请参阅CHANGELOG

贡献

有关详细信息,请参阅CONTRIBUTING

安全

如果您发现任何与安全相关的问题,请通过jonhwuua@gmail.com发送电子邮件,而不是使用问题跟踪器。

致谢

许可证

MIT许可证。请参阅许可证文件获取更多信息。