dkrasilnikov/literato-payment-bundle
Installation Run:
composer require dkrasilnikov/literato-payment-bundle
Publish the bundle configuration (if needed):
php artisan vendor:publish --provider="LiteratoPaymentBundle\LiteratoPaymentBundleServiceProvider"
Service Provider & Facade
Ensure the bundle is registered in config/app.php under providers:
LiteratoPaymentBundle\LiteratoPaymentBundleServiceProvider::class,
Use the facade for quick access:
use LiteratoPaymentBundle\Facades\LiteratoPayment;
First Use Case: Processing a Payment
Initialize a payment gateway (e.g., Stripe) in config/literato_payment.php:
'gateways' => [
'stripe' => [
'key' => env('STRIPE_KEY'),
'secret' => env('STRIPE_SECRET'),
],
],
Process a payment in a controller:
$payment = LiteratoPayment::gateway('stripe')
->charge(1000, 'user@example.com', [
'description' => 'Premium Subscription',
]);
Gateway Abstraction
Use the LiteratoPayment facade to switch between gateways dynamically:
// Charge via Stripe
$result = LiteratoPayment::gateway('stripe')->charge(2000, 'user@example.com');
// Charge via PayPal (if configured)
$result = LiteratoPayment::gateway('paypal')->charge(2000, 'user@example.com');
Webhook Handling
Register a route in routes/web.php:
Route::post('/payment/webhook', [PaymentWebhookController::class, 'handle']);
Process webhooks in a controller:
public function handle(Request $request) {
$event = LiteratoPayment::gateway('stripe')->webhook($request->all());
// Handle $event (e.g., 'charge.succeeded', 'charge.failed')
}
Subscription Management Create a subscription:
$subscription = LiteratoPayment::gateway('stripe')
->subscribe('user@example.com', 1000, [
'interval' => 'month',
'trial_days' => 7,
]);
Middleware for Authenticated Payments Protect payment routes with middleware:
Route::middleware(['auth', 'verified'])->group(function () {
Route::post('/pay', [PaymentController::class, 'process']);
});
STRIPE_SECRET) in .env.config/literato_payment.php:
'debug' => env('APP_DEBUG', false),
$this->mock(LiteratoPayment::class)->shouldReceive('gateway')->andReturnSelf();
Missing Configuration
.env.php artisan vendor:publish and verify config/literato_payment.php.Webhook Signature Validation
'webhook' => [
'validate_signature' => true,
'secret' => env('STRIPE_WEBHOOK_SECRET'),
],
Currency Mismatch
Idempotency Keys
$payment = LiteratoPayment::gateway('stripe')
->charge(1000, 'user@example.com', [
'idempotency_key' => uniqid(),
]);
'debug' => true in config to log raw API responses.$response = LiteratoPayment::gateway('stripe')->charge(...);
dd($response->getRawResponse());
Custom Gateways
Extend the AbstractGateway class to support new providers:
namespace App\Providers;
use LiteratoPaymentBundle\Contracts\GatewayInterface;
use LiteratoPaymentBundle\AbstractGateway;
class CustomGateway extends AbstractGateway implements GatewayInterface {
// Implement charge(), webhook(), etc.
}
Register the gateway in config/literato_payment.php:
'gateways' => [
'custom' => [
'class' => App\Providers\CustomGateway::class,
'config' => [...],
],
],
Event Listeners
Listen for payment events (e.g., PaymentCharged, SubscriptionCreated):
// In EventServiceProvider
protected $listen = [
'LiteratoPaymentBundle\Events\PaymentCharged' => [
'App\Listeners\LogPayment',
],
];
Overriding Services
Bind custom implementations in AppServiceProvider:
$this->app->bind(GatewayInterface::class, function ($app) {
return new App\Providers\CustomGateway();
});
How can I help you explore Laravel packages today?