Code Monkey home page Code Monkey logo

przelewy24-laravel's Introduction

Przelewy24 Laravel library

Laravel wrapper for mnastalski/przelewy24-php.

Requirements

  • PHP >=8.1
  • Laravel >=9.0

Installation

composer require mnastalski/przelewy24-laravel

Configuration

Add the following to your .env file:

PRZELEWY24_MERCHANT_ID=12345
PRZELEWY24_REPORTS_KEY=f0ae...
PRZELEWY24_CRC=aef0...
PRZELEWY24_IS_LIVE=false

Setting PRZELEWY24_IS_LIVE to false will use the sandbox environment. Set it to true to use production/live mode.

Pos ID may also be set if necessary:

PRZELEWY24_POS_ID=...

Usage

Here is a simple example of how the package can be used to create a transaction, listen for Przelewy24's webhook and verify the transaction. Dependency injection is used to get an instance of 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 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: $webhook->statement(),
            // 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();
    }
}

As this package wraps the mnastalski/przelewy24-php package, all methods are the same. For a more in-depth documentation, check its README.

przelewy24-laravel's People

Contributors

kamilkozak avatar mnastalski avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

przelewy24-laravel's Issues

Niespójności dokumentacji

Cześć,

Implementuje po raz pierwszy płatności z Przelewy24, i też poznaje flow akurat tego dostawcy, nie mniej, w README są pewne błędy/niespójności:

$order->payment_id = $register->orderId();

Ta klasa nie ma w ogóle metody orderId a samo Przelewy24 nie zwraca tej informacji.

        $order = Order::find(
            $webhook->orderId()
        );

Nie bardzo rozumiem w jaki sposób orderId z przelewów miało by akurat odpowiadać kluczowi głównemu naszego modelu?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.