digkill / yookassa-laravel

YooKassa for Laravel

v1.1.1 2024-01-27 11:18 UTC

This package is auto-updated.

Last update: 2024-10-01 00:17:40 UTC


README

安装

composer require digkill/yookassa-laravel
php artisan vendor:publish --tag=yookassa.migrations
php artisan migrate
php artisan vendor:publish --tag=yookassa.config
php artisan vendor:publish --provider=Digkill\YooKassaLaravel\YooKassaServiceProvider
// Facade
use Digkill\YooKassaLaravel\Facades\YooKassaFacade;
$yookassaPayment = YookassaFacade::createPayment(10000, 'test payment'); 

// Service
$service = app(Digkill\YooKassaLaravel\Services\PaymentService::class);
$yookassaPayment = $service->createPayment(10000, 'test payment');

要接收请求,您需要添加URL

./assets/img.jpg

并订阅事件 Digkill\YooKassaLaravel\Events\YookassaPaymentNotification

namespace App\Providers;
class EventServiceProvider extends ServiceProvider
...
    protected $listen = [
        YookassaPaymentNotification::class => [
        YookassaPaymentStatus::class
    ]
];
...
namespace App\Listeners;
class YookassaPaymentStatus implements ShouldQueue
...
public function handle(YookassaPaymentNotification $event): void
{
    if ($event->payment->status === PaymentStatus::SUCCEEDED->value) {
    }
}

如果使用doctrine,实体位于Entities文件夹

.env

YOOKASSA_SHOP_ID=
YOOKASSA_SECRET_KEY=
YOOKASSA_REDIRECT=

使用通知

        $yooKassa = new YooKassa([]);
        $paymentRepository = new PaymentRepository();
        $paymentService = new PaymentService($yooKassa, $paymentRepository);

        $requestBody = $request->all();
        if (!isset($requestBody['event'])) {
            throw new \Exception('event not found');
        }

        if (($requestBody['event'] === NotificationEventType::PAYMENT_SUCCEEDED)) {
            $notification = new NotificationSucceeded($requestBody);
        } elseif ($requestBody['event'] === NotificationEventType::PAYMENT_WAITING_FOR_CAPTURE) {
            $notification = new NotificationWaitingForCapture($requestBody);
        } else {
            $notification = new NotificationCanceled($requestBody);
        }

        $payment = $notification->getObject();
        $status = PaymentStatus::tryFrom($payment->getStatus());
        $paymentService->setStatus($payment->getId(), $status);
        $yooKassaPayment = $paymentService->findByPaymentId($payment->getId());
        YookassaPaymentNotification::dispatch($yooKassaPayment);

        return response()->json();

MIT许可证

版权所有 © 2024 Vitaliy Edifanov

在此特此授予任何获得本软件及其相关文档副本(以下简称“软件”)的人免费使用软件的权利,不受任何限制,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本的权利,并允许获得软件的人从事上述活动,前提是遵守以下条件

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

本软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、特定用途的适用性和非侵权性保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任承担责任,无论此类责任是基于合同、侵权或其他原因,无论此类责任是否因软件或其使用或其他方式产生。