daaner/laravel-unitpay

Laravel 的 UnitPay 支付

8.0 2021-06-14 13:29 UTC

This package is auto-updated.

Last update: 2024-09-14 20:20:17 UTC


README

Total Downloads License

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

基于 ActionM

  • 只需添加两个回调即可接收支付
  • 通过您的电子邮件或 Slack 接收支付通知

您可以通过 Yandex.Money、QIWI、WebMoney、PayPal、信用卡等使用 Unitpay 接受支付。

Laravel 5.5+, PHP >= 7.1+

安装

您可以通过 Composer 安装此包

composer require daaner/laravel-unitpay

将服务提供者添加到 config/app.php 中的 providers 数组

'providers' => [

    Daaner\UnitPay\UnitPayServiceProvider::class,

]

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

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

发布配置文件和视图

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

仅发布配置文件

php artisan vendor:publish --provider="Daaner\UnitPay\UnitPayServiceProvider" --tag=config

仅发布视图

php artisan vendor:publish --provider="Daaner\UnitPay\UnitPayServiceProvider" --tag=views

配置

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

  • unitpay.ru 上创建一个帐户
  • 添加您的项目,复制 PUBLIC KEYSECRET KEY 参数,并将其粘贴到 config/unitpay.php
  • 配置发布后,编辑 config/unitpay.php
  • searchOrderFilterpaidOrderFilter 设置回调静态函数
  • 设置通知通道(电子邮件和/或 Slack)和 Slack webhook_url

用法

  1. 生成带有启用支付方式的 HTML 支付表单
$payment_amount = Order amount

$payment_no = Unique order ID in your project

$user_email = User email

$item_name = Name of your order item

$currency =  'RUB' or 'UAH','BYR','EUR','USD'
UnitPay::generatePaymentForm($payment_amount, $payment_no, $user_email, $item_name, $currency);

在发布的视图中自定义 HTML 支付表单

app/resources/views/vendor/unitpay/payment_form.blade.php

  1. 处理来自 UnitPay 的请求
UnitPay::payOrderFromGate(Request $request)

重要

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

 'searchOrderFilter' => null  // ExampleController:searchOrderFilter($request)
 'paidOrderFilter' => null  // ExampleController::paidOrderFilter($request,$order)

示例

处理方案

  1. 请求来自 unitpay.ru GET http://yourproject.com/unitpay/result(带有参数)。
  2. 函数ExampleController@payOrderFromGate 运行验证过程(自动验证请求参数)。
  3. 将调用静态函数 searchOrderFilter(请参阅 config/unitpay.php 中的 searchOrderFilter)以按唯一 ID 搜索订单。
  4. 如果您的数据库中的当前订单状态不是 已付款,则将调用静态函数 paidOrderFilter(请参阅 config/unitpay.php 中的 paidOrderFilter)。

将路由添加到 routes/web.php

 Route::get('/unitpay/result', 'ExampleController@payOrderFromGate');

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

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

class ExampleController extends Controller
{

    /**
     * Search the order if the request from unitpay is received.
     * Return the order with required details for the unitpay request verification.
     *
     * @param Request $request
     * @param $order_id
     * @return mixed
     */
    public static function searchOrderFilter(Request $request, $order_id) {

        // If the order with the unique order ID exists in the database
        $order = Order::where('unique_id', $order_id)->first();

        if ($order) {
            $order['UNITPAY_orderSum'] = $order->amount; // from your database
            $order['UNITPAY_orderCurrency'] = 'RUB';  // from your database

            // if the current_order is already paid in your database, return strict "paid";
            // if not, return something else
            $order['UNITPAY_orderStatus'] = $order->order_status; // from your database
            return $order;
        }

        return false;
    }

    /**
     * When the payment of the order is received from unitpay, you can process the paid order.
     * !Important: don't forget to set the order status as "paid" in your database.
     *
     * @param Request $request
     * @param $order
     * @return bool
     */
    public static function paidOrderFilter(Request $request, $order)
    {
        // Your code should be here:
        YourOrderController::saveOrderAsPaid($order);

        // Return TRUE if the order is saved as "paid" in the database or FALSE if some error occurs.
        // If you return FALSE, then you can repeat the failed paid requests on the unitpay website manually.
        return true;
    }

    /**
     * Process the request from the UnitPay route.
     * searchOrderFilter is called to search the order.
     * If the order is paid for the first time, paidOrderFilter is called to set the order status.
     * If searchOrderFilter returns the "paid" order status, then paidOrderFilter will not be called.
     *
     * @param Request $request
     * @return mixed
     */
    public function payOrderFromGate(Request $request)
    {
        return UnitPay::payOrderFromGate($request);
    }

变更日志

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

测试

$ composer test

贡献

有关详细信息,请参阅 CONTRIBUTING

致谢

许可

MIT 许可证(MIT)。有关更多信息,请参阅 许可文件