Code Monkey home page Code Monkey logo

przelewy24-php's Introduction

Przelewy24 PHP library

PHP wrapper for Przelewy24.

If you are using Laravel, check out mnastalski/przelewy24-laravel.

Przelewy24's API documentation is available at https://developers.przelewy24.pl/.

Requirements

  • PHP >=8.1

For lower PHP versions, check the 0.x versions.

Installation

composer require mnastalski/przelewy24-php

Usage

Creating an instance

use Przelewy24\Przelewy24;

$przelewy24 = new Przelewy24(
    merchantId: 12345,
    reportsKey: 'f0ae...',
    crc: 'aef0...',
    isLive: false,
);

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

Creating a transaction

$transaction = $przelewy24->transactions()->register(
    // Required parameters:
    sessionId: 'unique order identifier from your application',
    amount: 125,
    description: 'transaction description',
    email: 'buyer email address',
    urlReturn: 'url to return to after transaction',

    // Optional parameters:
    urlStatus: 'url to which the transaction status webhook will be sent',

    // client: 'Mateusz Nastalski',
    // currency: \Przelewy24\Enums\Currency::EUR,
    // language: Language::ENGLISH,
    // ...
);

Note that amount is passed as an integer, so if the actual amount is 1.25 PLN you will need to pass 125 as value.

For the complete list of available parameters check the signature of TransactionRequests::register().

Return the transaction's token:

$transaction->token();

Return the URL to the payment gateway:

$transaction->gatewayUrl();

Listening for transaction status webhook

To parse the webhook's payload, pass the whole request's POST data as an array to handleWebhook():

// $requestData = $request->request->all();
// $requestData = $request->post();
// $requestData = json_decode(file_get_contents('php://input'), true);

$webhook = $przelewy24->handleWebhook($requestData);

handleWebhook() returns TransactionStatusNotification::class, which has a bunch of useful methods you can use to check the transaction's data, as well as verify the webhook's signature:

$webhook->amount();
$webhook->currency();
$webhook->orderId();
...
$webhook->isSignValid(...);

If you would like to make sure the incoming request's IP address belongs to Przelewy24 then a list of valid IPs is available in the \Przelewy24\Constants\IpAddresses::V4 constant. A helper method that accepts a string with an IP address and returns a boolean is also available: \Przelewy24\Constants\IpAddresses::isValid($ip).

Verifying a transaction

$przelewy24->transactions()->verify(
    sessionId: 'unique order identifier from your application',
    orderId: $webhook->orderId(),
    amount: 125,
);

Similarly to registering a transaction, the amount is passed as an integer.

Error handling

Should Przelewy24's API return an erroneous response, an ApiResponseException::class (which extends Przelewy24Exception::class) will be thrown. You can therefore use a try/catch block to handle any errors:

use Przelewy24\Exceptions\Przelewy24Exception;

try {
    $przelewy24->transactions()->verify([
        // ...
    ]);
} catch (Przelewy24Exception $e) {
    // Handle the error...
}

przelewy24-php's People

Contributors

dbojdo avatar kamilwojtalak avatar mnastalski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

przelewy24-php's Issues

Weryfikacja transakcji

Witam. Jak sprawdzić czy weryfikacja transakcji przebiegła pomyślnie? Gdyby ApiResponse::hasError była metodą publiczną możnaby jej użyć do weryfikacji:

$verified = $p24->verify([
                'session_id' => $order->getSessionId(), //unique order identifier from your application
                'order_id' => $webhook->orderId(),  // przelewy24 order id
                'amount' => round($order->getTotalSum() * 100), //transaction amount as an integer (1.25 PLN = 125)
            ]);
if (!$verified->hasError()) {
//$order->setAsPaid(); etc...
}

... ale metoda hasError jest chroniona. Może jest jakiś inny sposób?

Transaction verification issue

Hello,

Thanks for native PHP module but It seems there is an error during transaction verification waiting for Przelewy24 incoming request.

In transaction "url_status" i set url with php code:

$webhook = $przelewy24->handleWebhook(); <-- but this doesnt return any parameters or objects

$przelewy24->verify([
'session_id' => '12343', // my test order_id
'order_id' => $webhook->orderId(), // <-- for some reason $webhook is null :/
'amount' => 'transaction amount as an integer (1.25 PLN = 125)',
]);

Method "$przelewy24->verify" throw error cause $webhook is null.

Brak z zgodności z nową wersją API p24

Kod w obecnej wersji pracuje na starym API więc nie ma możliwości integracji (zawłaszcza obsługi notyfikacji a co za tym idzie weryfikacji ) bazując na tej wersji API

Question regarding additional parameters on transaction

Hi,

While creating transaction with Yours composer library I am in need to pass argument 'waitForResult' => true.

However because it doesnt work as described on przelewy24 docs I started to wonder if from transaction data that I am passing inside I am using proper parameters.

In docs there is sessionId and here I am passing session_id. Maybe the same should be with "waitForResult" parameter?

$transaction = $payment->transaction([
'session_id' => $order_name,
'url_return' => ''
'url_status' => '',
'waitForResult' => true,
'amount' => '',
'description' => '',
'email' => '',
]);

Regards
Łukasz

Undefined array key "p24_order_id"

I have an error while payment

ErrorException Undefined array key "p24_order_id"

Illuminate\Foundation\Bootstrap\HandleExceptions::handleError
...\vendor\mnastalski\przelewy24-php\src\TransactionStatusNotification.php:49

    /**
    * @return int
    */
    public function orderId(): int
    {
        return $this->parameters['p24_order_id'];             // <-- error
    }

my code is like in your example:

$przelewy24 = new Przelewy24([
                        'merchant_id' => $merchantId,
                         'crc'         => $crc,     
                         'live'        => false, 
                         ]);


$transaction = $przelewy24->transaction([
                        'session_id'  => $sessionId,
                        'url_return'  => $urlReturn,
                        'url_status'  => '',
                         'amount'      => $amount,
                         'description' => $description,
                         'email'       => $customer_email,
                         ]);

$transaction->token();

$transaction->redirectUrl();

$webhook = $przelewy24->handleWebhook();

$przelewy24->verify([
                        'session_id' => $sessionId,
                        'order_id'   => $webhook->orderId(),    
                        'amount'     => $amount,
                    ]);

And by the way: is it possible to choose a currency in your code?

Błąd p24_api_version:Incorrect api version

Hej,
Biblioteka na niektórych serwerach przekazuje pole p24_api_version jako float 3,2 (z przecinkiem) i przelewy24 zwracają p24_api_version:Incorrect api version.
Sugeruje numer wersji API trzymać jako string '3.2' - to rozwiąże problem.

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.