furkanmeclis / paytr-link
PayTR Link API integration for Laravel
Fund package maintenance!
furkanmeclis
Installs: 8
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/furkanmeclis/paytr-link
Requires
- php: ^8.1
- illuminate/contracts: ^11.0||^12.0
- illuminate/http: ^11.0||^12.0
- illuminate/support: ^11.0||^12.0
- spatie/laravel-data: ^4.0
- spatie/laravel-package-tools: ^1.16
- spatie/laravel-settings: ^3.0
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^10.0.0||^9.0.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- spatie/laravel-ray: ^1.35
README
Laravel package for PayTR Link API integration. This package allows you to easily integrate with PayTR Link API.
Features
- ✅ All PayTR Link API endpoints
- ✅ Type-safe Data Transfer Objects (Spatie Laravel Data)
- ✅ Settings management (Spatie Laravel Settings)
- ✅ Event system (Link creation, deletion, SMS/Email sending, Callback)
- ✅ Facade support for easy usage
- ✅ Comprehensive test coverage
- ✅ PHP 8.1+ support
Installation
Install the package via Composer:
composer require furkanmeclis/paytr-link
Publish the config file:
php artisan vendor:publish --tag="paytr-link-config"
If you're going to use Spatie Laravel Settings:
php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsServiceProvider" --tag="migrations" php artisan migrate
Configuration
Add your PayTR credentials to your .env file:
PAYTR_MERCHANT_ID=your_merchant_id PAYTR_MERCHANT_KEY=your_merchant_key PAYTR_MERCHANT_SALT=your_merchant_salt PAYTR_DEBUG_ON=1
You can also configure via the config file (config/paytr-link.php).
Usage
Creating a Link
use FurkanMeclis\PayTRLink\Facades\PayTRLink; use FurkanMeclis\PayTRLink\Data\CreateLinkData; use FurkanMeclis\PayTRLink\Enums\CurrencyEnum; use FurkanMeclis\PayTRLink\Enums\LinkTypeEnum; $data = CreateLinkData::from([ 'name' => 'Web Design Service', 'price' => 1500.00, // in TL 'currency' => CurrencyEnum::TL, 'link_type' => LinkTypeEnum::Product, 'max_installment' => 12, 'lang' => 'tr', 'expiry_date' => '2025-12-31 23:59:59', ]); $response = PayTRLink::create($data); if ($response->isSuccess()) { $link = $response->link; $linkId = $response->id; }
Creating a Collection Link
$data = CreateLinkData::from([ 'name' => 'Bulk Payment', 'price' => 5000.00, 'currency' => CurrencyEnum::TL, 'link_type' => LinkTypeEnum::Collection, 'email' => 'customer@example.com', // Required for Collection 'max_installment' => 12, ]); $response = PayTRLink::create($data);
Deleting a Link
use FurkanMeclis\PayTRLink\Data\DeleteLinkData; $response = PayTRLink::delete(DeleteLinkData::from([ 'link_id' => 'link_id_here', ])); // Or directly with a string $response = PayTRLink::delete('link_id_here');
Sending SMS
use FurkanMeclis\PayTRLink\Data\SendSmsData; $response = PayTRLink::sendSms(SendSmsData::from([ 'link_id' => 'link_id_here', 'phone' => '5551234567', ]));
Sending Email
use FurkanMeclis\PayTRLink\Data\SendEmailData; $response = PayTRLink::sendEmail(SendEmailData::from([ 'link_id' => 'link_id_here', 'email' => 'customer@example.com', ]));
Callback Validation
use Illuminate\Http\Request; public function handleCallback(Request $request) { if (PayTRLink::validateCallback($request->all())) { // Transaction successful $callbackData = \FurkanMeclis\PayTRLink\Data\CallbackData::from($request->all()); if ($callbackData->status === 'success') { // Payment successful, update the transaction echo "OK"; } else { // Payment failed echo "OK"; } } else { return response('Invalid hash', 400); } }
Service Injection
You can also use dependency injection instead of Facade:
use FurkanMeclis\PayTRLink\PayTRLinkService; class PaymentController { public function __construct( protected PayTRLinkService $paytrLink ) {} public function createLink(CreateLinkData $data) { $response = $this->paytrLink->create($data); return response()->json($response); } }
Spatie Laravel Settings Integration
The package works integrated with Spatie Laravel Settings. You can store settings in the database using Settings:
use FurkanMeclis\PayTRLink\Settings\PayTRSettings; // Assign values to Settings $settings = app(PayTRSettings::class); $settings->merchant_id = 'your_merchant_id'; $settings->merchant_key = 'your_merchant_key'; $settings->merchant_salt = 'your_merchant_salt'; $settings->debug_on = true; $settings->save(); // Reading values from Settings (also reads from config with fallback) $settings = app(PayTRSettings::class); $merchantId = $settings->getMerchantId(); $merchantKey = $settings->getMerchantKey(); $merchantSalt = $settings->getMerchantSalt(); $debugMode = $settings->getDebugOn();
When Settings is used, settings values are used instead of config values. The getMerchantId(), getMerchantKey(), getMerchantSalt(), and getDebugOn() methods automatically read from config if no value exists in settings.
Price Conversion
PayTR API expects prices in kuruş (cents). The package automatically converts TL prices to kuruş:
// 1500.00 TL is automatically converted to 150000 kuruş $data = CreateLinkData::from([ 'name' => 'Product', 'price' => 1500.00, // TL // ... ]);
Events
The package dispatches events for various operations. You can track operations and perform actions when needed by listening to these events:
Event List
FurkanMeclis\PayTRLink\Events\LinkCreated- When a link is createdFurkanMeclis\PayTRLink\Events\LinkDeleted- When a link is deletedFurkanMeclis\PayTRLink\Events\SmsSent- When SMS is sentFurkanMeclis\PayTRLink\Events\EmailSent- When email is sentFurkanMeclis\PayTRLink\Events\CallbackReceived- When callback is received
Event Usage
Creating an Event Listener
Create a listener in the app/Listeners folder:
// app/Listeners/HandleLinkCreated.php namespace App\Listeners; use FurkanMeclis\PayTRLink\Events\LinkCreated; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Queue\InteractsWithQueue; class HandleLinkCreated implements ShouldQueue { use InteractsWithQueue; public function handle(LinkCreated $event): void { $linkData = $event->createLinkData; $response = $event->response; // Actions to perform when link is created if ($response->isSuccess()) { logger()->info('Link created successfully', [ 'link_id' => $response->id, 'link' => $response->link, 'name' => $linkData->name, 'price' => $linkData->price, ]); // Example: Save to database // Link::create([ // 'paytr_link_id' => $response->id, // 'link' => $response->link, // ... // ]); } } }
Registering in Event Service Provider
In the app/Providers/EventServiceProvider.php file:
use App\Listeners\HandleLinkCreated; use FurkanMeclis\PayTRLink\Events\LinkCreated; use FurkanMeclis\PayTRLink\Events\LinkDeleted; use FurkanMeclis\PayTRLink\Events\SmsSent; use FurkanMeclis\PayTRLink\Events\EmailSent; use FurkanMeclis\PayTRLink\Events\CallbackReceived; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; class EventServiceProvider extends ServiceProvider { protected $listen = [ LinkCreated::class => [ HandleLinkCreated::class, ], LinkDeleted::class => [ // Your listeners ], SmsSent::class => [ // Your listeners ], EmailSent::class => [ // Your listeners ], CallbackReceived::class => [ // Your listeners ], ]; }
Callback Event Usage
// app/Listeners/HandleCallbackReceived.php namespace App\Listeners; use FurkanMeclis\PayTRLink\Events\CallbackReceived; class HandleCallbackReceived { public function handle(CallbackReceived $event): void { $callbackData = $event->callbackData; $isValid = $event->isValid; if ($isValid && $callbackData->status === 'success') { // Payment successful - update order logger()->info('Payment successful', [ 'merchant_oid' => $callbackData->merchant_oid, 'total_amount' => $callbackData->total_amount, ]); } else { // Payment failed logger()->warning('Payment failed', [ 'merchant_oid' => $callbackData->merchant_oid, 'status' => $callbackData->status, ]); } } }
Listening to Events with Closures
You can use closures instead of creating event listeners:
use FurkanMeclis\PayTRLink\Events\LinkCreated; use Illuminate\Support\Facades\Event; Event::listen(LinkCreated::class, function (LinkCreated $event) { if ($event->response->isSuccess()) { // Actions to perform when link is created logger()->info('Link created: ' . $event->response->link); } });
Exception Handling
use FurkanMeclis\PayTRLink\Exceptions\PayTRRequestException; use FurkanMeclis\PayTRLink\Exceptions\PayTRValidationException; try { $response = PayTRLink::create($data); } catch (PayTRRequestException $e) { // API request failed logger()->error('PayTR API Error', [ 'message' => $e->getMessage(), 'response' => $e->response, ]); } catch (PayTRValidationException $e) { // Validation error logger()->error('PayTR Validation Error', [ 'errors' => $e->errors, ]); }
Testing
composer test
Test with coverage:
composer test-coverage
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Security Vulnerabilities
If you discover a security vulnerability, please send an e-mail to furkanmeclis@icloud.com. All security vulnerabilities will be promptly addressed.
Credits
License
The MIT License (MIT). Please see License File for more information.