actionm/laravel-unitpay

Laravel 的 UnitPay 支付解决方案

v1.0.6 2017-07-11 15:45 UTC

This package is not auto-updated.

Last update: 2024-09-28 20:56:04 UTC


README

Latest Stable Version Build Status SensioLabsInsight Quality Score Total Downloads License

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

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

您可以通过 Yandex.Money、QIWI、WebMoney、PayPal、信用卡等方式通过 Unitpay 接受付款

Laravel 5.3, 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.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.ruGET 请求 http://yourproject.com/unitpay/result(带有参数)。
  2. 函数 ExampleController@payOrderFromGate 运行验证过程(自动验证请求参数)。
  3. 将调用静态函数 searchOrderFilter(参见 config/unitpay.php 中的 searchOrderFilter),以通过唯一 ID 搜索订单。
  4. 如果当前订单状态在您的数据库中不是 paid,则将调用静态函数 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

安全

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

致谢

许可

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