spatie/payment
UNMAINTAINED. Laravel package to accept payments via payment gateways, with a Europabank e-commerce integration. Provides a payment form and configurable secrets/UID/MPI settings via a published config file.
Installation:
composer require spatie/payment
Register the service provider in config/app.php (Laravel ≤4.2) or config/app.php under providers for Laravel 5.x:
Spatie\Payment\PaymentServiceProvider::class,
Publish Config (if needed):
php artisan vendor:publish --provider="Spatie\Payment\PaymentServiceProvider"
Configure config/payment.php with your Europabank credentials (only supported gateway).
First Use Case: Create a payment intent and verify the callback:
use Spatie\Payment\Payment;
// Create a payment
$payment = Payment::create([
'amount' => 100.00,
'description' => 'Order #12345',
'reference' => 'order_12345',
'currency' => 'EUR',
]);
// Handle callback (e.g., in a webhook route)
$payment = Payment::findByReference('order_12345');
if ($payment->isPaid()) {
// Fulfill order logic
}
Payment Creation:
Use Payment::create() with required fields (amount, description, reference, currency). Extend with custom data via additionalData:
Payment::create([
'amount' => 50.00,
'reference' => 'invoice_67890',
'additionalData' => ['customer_id' => 42],
]);
Callback Handling:
paymentId or reference in your database.Payment::findByReference() or Payment::find($paymentId).if ($payment->isPaid()) { /* Success */ }
if ($payment->isFailed()) { /* Retry or notify */ }
Webhook Integration:
PaymentWebhookController).Spatie\Payment\Payment::verifyCallback():
public function handleCallback(Request $request) {
$payment = Payment::verifyCallback($request->all());
if ($payment) {
// Process payment
}
}
Retry Logic: Implement a job to retry failed payments (e.g., using Laravel Queues):
use Spatie\Payment\Payment;
$payment = Payment::find($failedPaymentId);
if ($payment->isFailed() && $payment->canRetry()) {
$payment->retry();
}
payments table. Publish migrations if needed:
php artisan vendor:publish --provider="Spatie\Payment\PaymentServiceProvider" --tag=migrations
Payment::fake() for unit tests:
Payment::fake([
'payment_success' => true,
]);
config/payment.php for troubleshooting:
'debug' => env('PAYMENT_DEBUG', false),
Deprecated Package:
laravel-cashier or omnipay.Single Gateway Limitation:
Spatie\Payment\Gateways\Gateway class to add new gateways (see below).Callback Verification:
$request->all()) and manually verify signatures if needed.Laravel Version Mismatch:
PaymentServiceProvider or use a wrapper class.No Webhook Signing:
// Example: Validate Europabank's signature
$expectedSignature = hash_hmac('sha256', $request->getContent(), config('payment.europabank.secret'));
if (!hash_equals($expectedSignature, $request->header('X-Signature'))) {
abort(403);
}
Enable Debug Mode:
'debug' => true, // in config/payment.php
Logs will appear in storage/logs/laravel.log.
Check Raw Responses: Dump the raw gateway response in a callback handler:
\Log::debug('Raw callback data:', $request->all());
Test Locally: Use Europabank’s sandbox environment (if available) to test callbacks without real transactions.
Add New Gateways:
Extend the Gateway class and register it in the service provider:
// app/Providers/PaymentServiceProvider.php
public function register() {
$this->app->bind('gateway.stripe', function() {
return new \App\Gateways\StripeGateway();
});
}
Custom Payment Models:
Use the Payment model as a base and extend it:
class CustomPayment extends \Spatie\Payment\Payment {
protected $table = 'custom_payments';
}
Bind the custom model in the service provider:
$this->app->bind('payment.model', function() {
return new CustomPayment();
});
Override Callback Logic:
Replace the verifyCallback method in a custom service:
class CustomPaymentService {
public function verifyCallback(array $data) {
// Custom logic here
return Payment::findByReference($data['reference']);
}
}
Missing .env Support:
The package lacks .env variable support for credentials. Hardcode or use Laravel’s config() helper:
'europabank' => [
'username' => config('services.europabank.username'),
'password' => config('services.europabank.password'),
],
Amount Precision:
Ensure amounts use 2 decimal places (e.g., 100.00 for €100). The package may truncate or round values.
Reference Uniqueness:
The reference field must be unique in the payments table. Use UUIDs or composite keys for high-volume systems:
$payment = Payment::create([
'reference' => Str::uuid()->toString(),
// ...
]);
How can I help you explore Laravel packages today?