dominservice/przelewy24-laravel

Przelewy24 Laravel 库

1.0.0 2023-11-13 09:40 UTC

This package is auto-updated.

Last update: 2024-09-13 12:28:23 UTC


README

dominservice/przelewy24-php 提供 Laravel 包装器。

要求

  • PHP >=8.1
  • Laravel >=9.0

安装

composer require dominservice/przelewy24-laravel

配置

将以下内容添加到您的 .env 文件中

PRZELEWY24_MERCHANT_ID=12345
PRZELEWY24_REPORTS_KEY=f0ae...
PRZELEWY24_CRC=aef0...
PRZELEWY24_POS_ID=12345

PRZELEWY24_SANDBOX_MERCHANT_ID=12345
PRZELEWY24_SANDBOX_REPORTS_KEY=f0ae...
PRZELEWY24_SANDBOX_CRC=aef0...
PRZELEWY24_SANDBOX_POS_ID=12345

PRZELEWY24_LIVE=false

PRZELEWY24_LIVE 设置为 false 将使用 sandbox 环境。将其设置为 true 以使用生产/实时模式。

如果需要,也可以设置 Pos ID

PRZELEWY24_POS_ID=...

用法

以下是一个简单的示例,说明如何使用此包创建交易,监听 Przelewy24 的 webhook 并验证交易。使用依赖注入获取 Przelewy24 实例

<?php

use App\Models\Order;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Przelewy24\Enums\Currency;
use Przelewy24\Enums\Language;
use Przelewy24\Exceptions\Przelewy24Exception;
use Przelewy24\Przelewy24;

class MyController
{
    public function __construct(
        private readonly Przelewy24 $przelewy24,
    ) {}

    public function pay(Order $order): RedirectResponse
    {
        // Create a new transaction
        $register = $this->przelewy24->transactions()->register(
            sessionId: $order->id,
            amount: $order->amount,
            description: "Order #{$order->id}",
            email: $order->email,
            urlReturn: route('orders.success'),
            urlStatus: route('orders.webhook'),
            // client: 'Mateusz Domin',
            // currency: Currency::EUR,
            // language: Language::ENGLISH,
            // ...
        );

        $order->payment_id = $register->token();
        $order->save();

        // Redirect to Przelewy24's payment gateway
        return redirect(
            $register->gatewayUrl()
        );
    }

    /**
     * Method for route "orders.success". 
     */
    public function paymentSuccessful(): Response
    {
        return response('Payment successful!');
    }

    /**
     * Method for route "orders.webhook".
     * Must be POST and excluded from CSRF protection.
     */
    public function webhook(Request $request): Response
    {
        // Handle Przelewy24's webhook
        $webhook = $this->przelewy24->handleWebhook(
            $request->post()
        );

        // Find related order using webhook's session ID
        $order = Order::find(
            $webhook->sessionId()
        );

        // If you would like to verify that the webhook and its
        // signature are legitimate, you may use the following method:
        $isSignValid = $webhook->isSignValid(
            sessionId: $order->id,
            amount: $order->amount,
            originAmount: $order->amount,
            orderId: $webhook->orderId(),
            methodId: $webhook->methodId(),
            statement: "Order #{$order->id}",
            // currency: Currency::EUR,
        );

        if (!$isSignValid) {
            // Handle error ...

            abort(Response::HTTP_BAD_REQUEST);
        }

        // Verify the transaction / claim the payment
        try {
            $this->przelewy24->transactions()->verify(
                $order->id,
                $webhook->orderId(),
                $order->amount,
            );

            $order->status = 'paid';
            $order->save();
        } catch (Przelewy24Exception) {
            // Handle error ...
        }

        return response()->noContent();
    }
}

由于此包包装了 dominservice/przelewy24-php 包,所有方法都是相同的。要获取更详细的文档,请查看其 README。