Install and Configure
composer require baks-dev/payment
php artisan vendor:publish --provider="Baks\Payment\PaymentServiceProvider" --tag="config"
php artisan doctrine:migrations:migrate
Register Service Provider
Add to config/app.php under providers:
Baks\Payment\PaymentServiceProvider::class,
First Use Case: Create a Payment Method
use Baks\Payment\Facades\Payment;
// Define a payment method (e.g., YooMoney)
$paymentMethod = Payment::method('yoomoney')
->setConfig([
'secret_key' => config('payment.yoomoney.secret'),
'account_id' => 'your_account_id'
]);
// Create a transaction
$transaction = Payment::createTransaction()
->setAmount(1000) // 1000 RUB
->setCurrency('RUB')
->setMethod($paymentMethod)
->setDescription('Order #12345');
// Authorize payment
$result = $transaction->authorize();
Verify Webhook Setup
Add a route in routes/api.php:
Route::post('/payment/webhook', [PaymentWebhookController::class, 'handle']);
Payment Method Abstraction
Extend the PaymentMethod interface for custom providers:
namespace App\Payment;
use Baks\Payment\Contracts\PaymentMethod;
class CustomBankTransfer implements PaymentMethod {
public function authorize($transaction) {
// Custom logic (e.g., API call to bank)
return ['success' => true, 'transaction_id' => 'bank_ref_123'];
}
}
Transaction Lifecycle
// Create and process transaction
$transaction = Payment::transaction()
->setAmount(5000)
->setMethod('card')
->setData(['card_number' => '4242424242424242'])
->authorize()
->capture();
// Handle refunds
$transaction->refund(2000);
Webhook Handling
// PaymentWebhookController.php
public function handle(Request $request) {
$payload = $request->validate([
'signature' => 'required',
'data' => 'required|array'
]);
Payment::webhook()
->validateSignature($payload['signature'])
->process($payload['data']);
}
Batch Processing
// Process multiple payments asynchronously
Payment::batch()
->addTransaction($transaction1)
->addTransaction($transaction2)
->processOnQueue('payments');
Queue Integration Use Laravel queues for non-blocking operations:
Payment::transaction()->authorize()->onQueue('payments');
Event Listeners
Bind to payment events in EventServiceProvider:
public function boot() {
event(new \Baks\Payment\Events\PaymentAuthorized($transaction));
}
Middleware for Security Protect payment routes:
Route::middleware(['auth:sanctum', 'throttle:60'])->group(function () {
Route::post('/payments', [PaymentController::class, 'store']);
});
Doctrine vs. Eloquent Conflicts
Webhook Signature Validation
$valid = hash_equals(config('payment.yoomoney.secret'), $payload['signature']);
Synchronous Blocking
Payment::transaction()->authorize()->onQueue('payments');
Currency and Locale Handling
PaymentFormatter class for custom locales:
class CustomFormatter extends PaymentFormatter {
public function formatAmount($amount) {
return number_format($amount, 2, ',', ' ');
}
}
Enable Debug Logging
Add to config/payment.php:
'debug' => env('PAYMENT_DEBUG', false),
Test with Sandbox Providers Use test modes for payment methods (e.g., YooMoney sandbox):
$paymentMethod->setConfig(['mode' => 'test']);
Check Queue Jobs Monitor failed jobs in Laravel Horizon or Telescope:
php artisan queue:work --queue=payments
Custom Payment Methods
Implement the PaymentMethod interface and register via service provider:
$this->app->bind(\Baks\Payment\Contracts\PaymentMethod::class, \App\Payment\CustomMethod::class);
Override Transaction Logic
Extend the Transaction class:
class CustomTransaction extends \Baks\Payment\Transaction {
public function authorize() {
// Custom logic before parent::authorize()
return parent::authorize();
}
}
Add Custom Fields
Extend the Transaction model:
class ExtendedTransaction extends \Baks\Payment\Transaction {
protected $customField = null;
public function setCustomField($value) {
$this->customField = $value;
return $this;
}
}
Modify Webhook Processing
Override the WebhookProcessor:
class CustomWebhookProcessor extends \Baks\Payment\WebhookProcessor {
public function process(array $data) {
// Custom logic
parent::process($data);
}
}
How can I help you explore Laravel packages today?