axcherednikov/laravel-bepaid

用于 Laravel 的 https://github.com/begateway/begateway-api-php 的包装器

1.6.1 2022-06-28 13:31 UTC

This package is auto-updated.

Last update: 2024-09-28 18:47:54 UTC


README

为 Laravel(非官方)的 BeGateway 的包装器 BeGateway

文档

安装

  • 运行
$ composer require axcherednikov/laravel-bepaid
  • 发布配置
$ php artisan vendor:publish --provider="Excent\BePaidLaravel\Providers\BePaidServiceProvider"

使用

基础

您只需要创建一个新的 DTO 并用提供的值填写原始对象。

这里有一个简单的示例

<?php

namespace App\Services;

use Excent\BePaidLaravel\Refund;

class PaymentService 
{
    public function __construct(private Refund $refund)
    {
    }

    public function refund()
    {
        // Create new DTO
        $refundDto = new \Excent\BePaidLaravel\Dtos\RefundDto([
            'reason' => 'Purchase returns',
            'parent_uid' => 'payment_uid',
            'money' => [
                'amount' => 333.33,
            ],
        ]);
        
        $response = $this->refund
            ->fill($refundDto)
            ->submit();
        
        // OR even shorter
        // $response = $this->refund->submit($refundDto);

        // ... process the $response
    }
}

下表说明了 BeGateway 中的哪些对象等于 BePaid Laravel 中的对象。下面列出的所有 BePaid Laravel 包中的对象都具有公共字段 $operation,这使您能够访问原始对象。如果包的功能不足以达到目标,您可以检查原始的 以查看所有可用方法。

关于 StatusQueryChildTransaction 对象的一些话。它们也有公共字段 $operation,但是有一些细微差别。这取决于您将传递给 fill()submit() 方法的 DTO。假设您想要通过 uid 查询交易,在这种情况下,您将创建 new StatusQueryByUidDto([...]),然后 $operation 字段变为 \BeGateway\QueryByUid 的实例。

订阅事件

BePaid Laravel 提供了预配置的路由,可以在请求中使用。以下是它的列表

最重要的是 notificationsBePaid Laravel 已经验证了传入的请求是否由 BePaid 发送。在验证成功的情况下,如果出现错误,它将触发 bepaid.event.notification.successbepaid.event.notification.fail

如何处理所有这些内容?

BePaid Laravel 伴随有一个抽象类 BePaidSubscriber,您需要扩展它。

创建并注册一个新的事件订阅者

<?php

namespace App\Providers;

use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        //
    ];

    /**
     * The subscriber classes to register.
     *
     * @var array
     */
    protected $subscribe = [
        'App\Listeners\PaymentNotificationEventSubscriber',
    ];
}

现在只需扩展 BePaidSubscriber 并定义所有必需的方法。就是这样。

<?php

namespace App\Listeners;

use Excent\BePaidLaravel\Contracts\BePaidSubscriber;
use Illuminate\Http\Request;

class PaymentNotificationEventSubscriber extends BePaidSubscriber
{
    public function onNotificationSuccess(Request $request)
    {
        // ... process the request
    }

    public function onNotificationFail(Request $request)
    {
        // ... process the request
    }

    public function onSuccess(Request $request)
    {
        // ... process the request
    }

    public function onFail(Request $request)
    {
        // ... process the request
    }

    public function onReturn(Request $request)
    {
        // ... process the request
    }

    public function onCancel(Request $request)
    {
        // ... process the request
    }

    public function onDecline(Request $request)
    {
        // ... process the request
    }
}