jack-walter-smith/bepaid-laravel

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

v1.0.8 2020-12-13 09:53 UTC

This package is auto-updated.

Last update: 2024-09-16 04:17:44 UTC


README

bepaid-laravel

Build Status Codecov branch

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

文档

需求

PHP >=7.1

Laravel >= 5.7

安装

  • 添加到 composer.json
{
  "repositories": [
    {
      "type": "vcs",
      "url": "git@github.com:Jack-Walter-Smith/begateway-api-php.git",
      "no-api": true
    }
  ],
  "require": {
    "jack-walter-smith/bepaid-laravel": "^1.0"
  }
}
  • 运行 composer update

  • 发布配置

php artisan vendor:publish --provider="JackWalterSmith\BePaidLaravel\Providers\BePaidServiceProvider"

用法

基础知识

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

以下是一个简单的示例

<?php

namespace App\Services;

use JackWalterSmith\BePaidLaravel\Refund;

class PaymentService 
{
    /** @var Refund */
    private $refund;

    public function __construct(Refund $refund) {
        $this->refund = $refund;
    }

    public function refund()
    {
        // Create new DTO
        $refundDto = new \JackWalterSmith\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,这使您能够访问原始对象。这是在包功能不足以达到目标的情况下。您可以查看原始的 以查看所有可用方法。

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

订阅事件

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

最重要的是 notifications。在成功验证场景下,如果发生错误,BePaid Laravel 会验证传入的请求是否来自 BePaid。它会触发 bepaid.event.notification.successbepaid.event.notification.fail

如何处理所有这些?

BePaid Laravel 附带了一个抽象类 BePaidSubscriber,您需要扩展它。

创建并注册一个新的 Event Subscriber

<?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 JackWalterSmith\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
    }
}