PHP code example of mnastalski / przelewy24-laravel

1. Go to this page and download the library: Download mnastalski/przelewy24-laravel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mnastalski / przelewy24-laravel example snippets




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 Nastalski',
            // 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();
    }
}