rekkopavel/laravel-unitpay

为 Laravel 的 UnitPay 支付

dev-master 2023-05-21 16:35 UTC

This package is auto-updated.

Last update: 2024-09-21 19:16:52 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 "RekkoPavel/laravel-unitpay"

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

RekkoPavel\UnitPay\UnitPayServiceProvider::class,

UnitPay 门面添加到您的门面数组中

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

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

php artisan vendor:publish --provider="RekkoPavel\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 了解详细信息。

安全

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

鸣谢

许可

MIT 许可证(MIT)。请参阅 许可文件 了解更多信息。