dnsoftware/laravel-unitpay

Laravel 的 UnitPay 支付

2.0.3 2021-05-03 11:00 UTC

This package is not auto-updated.

Last update: 2024-10-01 04:13:13 UTC


README

Latest Stable Version Build Status SensioLabsInsight Quality Score Total Downloads License

通过此 Laravel 框架包使用 UnitPay 接受支付(unitpay.money 或 unitpay.ru),使用以下链接:

  • 接收支付,只需添加两个回调函数
  • 通过电子邮件或 Slack 接收支付通知

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

Laravel 8.0, 5.4, PHP >= 5.6.4

安装

您可以通过 Composer 安装此包

composer require actionm/laravel-unitpay

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

'providers' => [

    ActionM\UnitPay\UnitPayServiceProvider::class,
    
]

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

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

发布配置文件和视图

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

仅发布配置文件

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

仅发布视图

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

配置

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

  • unitpay.moneyunitpay.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.moneyunitpay.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.moneyunitpay.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

安全

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

鸣谢

许可证

麻省理工学院许可证(MIT)。请参阅许可文件获取更多信息。