laravel-przelewy24 maintained by lukasz-adamski
Przelewy24 payment processor for Laravel
Laravel package which provides Przelewy24 payment processor support.
Installation
- Install composer package using command:
composer require lukasz-adamski/laravel-przelewy24
- Add Service Provider in
config/app.php:
Adams\Przelewy24\Przelewy24ServiceProvider::class,
- Add Facade in
config/app.php:
'Przelewy24' => Adams\Przelewy24\Facades\Facade::class,
- Publish configuration file to your project:
php artisan vendor:publish --provider="Adams\Przelewy24\Przelewy24ServiceProvider"
- If you have
package_routessetting enabled you need to except/webhook/przelewy24route from CSRF verification inapp/Http/Middleware/VerifyCsrfToken.php:
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array
*/
protected $except = [
'/webhook/przelewy24'
];
Environment
You can setup these environment variables to configure Przelewy24 API access:
PRZELEWY24_MODE- Current API mode, by default this value is set tosandboxto test implementation. On production you need to set this value tolive,PRZELEWY24_MERCHANT_ID- Your merchant identifier received from payment provider,PRZELEWY24_POS_ID- Shop identifier to process payments. If you don't want to process incoming payments to given shop enter here merchant identifier and payments will not be classified to any shop,PRZELEWY24_CRC- Random string received from payment provider to sign API requests.
Testing
To run predefined test set use:
php vendor/bin/phpunit
Usage
Below you have example controller implementation:
<?php
namespace App\Http\Controllers;
use Przelewy24;
use Adams\Przelewy24\Transaction;
use App\Http\Controllers\Controller;
use Illuminate\Support\Str;
class ExampleController extends Controller
{
/**
* Redirect user to payment provider.
*
* @return Response
*/
public function pay()
{
$payload = new Transaction();
$payload->setSessionId(Str::random(30));
$payload->setAmount($item->price * 100);
$payload->setDescription('My item description');
$payload->setEmail('customer@example.com');
$payload->setUrlReturn(url('/'));
return Przelewy24::redirect($payload);
}
}
Events
When transaction receiver from this package is enabled (package_routes setting) you can listen for predefined events dispatched by default webhook controller:
\Adams\Przelewy24\Events\TransactionReceived::class- transaction was successfully received (signature is valid). If you don't use automatic verification you need to do it manually to charge prepaid money,\Adams\Przelewy24\Events\TransactionVerified::class- transaction was successfully received and verified via provider's API. After dispatching this event, prepaid money is already added to your account.
Example
- Below you can see example code of
TransactionVerifiedevent listener created byphp artisan make:listener ReceivePayment:
<?php
namespace App\Listeners;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Adams\Przelewy24\Events\TransactionVerified;
class ReceivePayment
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param TransactionVerified $event
* @return void
*/
public function handle(TransactionVerified $event)
{
//
}
}
- You also need to register new listener in
app/Providers/EventServiceProvider.phpfile.
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
\Adams\Przelewy24\Events\TransactionVerified::class => [
\App\Listeners\ReceivePayment::class,
],
];