A Laravel application demonstrating Magpie Checkout integration for digital art sales using the Magpie PHP SDK.
- Laravel Framework: Built on Laravel 12 with modern PHP features
- Clean Architecture: MVC pattern with proper separation of concerns
- Magpie Integration: Complete checkout session creation and payment processing
- Responsive Design: Mobile-first, clean minimal design
- Error Handling: Comprehensive error handling with Laravel logging
- Configuration: Laravel config system for Magpie settings
- CSRF Protection: Built-in Laravel CSRF protection
- Route Model: Clean RESTful routes with named routes
- PHP 8.1 or higher
- Composer for dependency management
- Laravel 12.x
- Magpie account with API credentials
-
Clone or download this repository
git clone <repository-url> cd sample-magpie-laravel
-
Install dependencies
composer install
-
Set up environment
cp .env.example .env php artisan key:generate
-
Configure Magpie credentials
Edit the
.envfile and add your Magpie API credentials:MAGPIE_SECRET_KEY=your_magpie_secret_key_here
-
Run migrations (optional for this demo)
php artisan migrate
-
Sign up for a Magpie account
- Visit https://dashboard.magpie.im
- Create a new account or log in to your existing account
-
Create a new application
- Navigate to your Magpie dashboard
- Go to "Developers", under "API Keys" section
-
Get your API credentials
- Copy your Secret Key (used for server-side operations)
- Important: Keep your secret key secure and never expose it in client-side code
php artisan serveThen open your browser and visit: http://localhost:8000
For production deployment, follow standard Laravel deployment practices:
- Configure your web server (Apache/Nginx)
- Set
APP_ENV=productionin your.envfile - Run
php artisan config:cacheandphp artisan route:cache - Ensure proper file permissions
// Web routes (routes/web.php)
Route::get('/', [StoreController::class, 'index'])->name('store.index');
Route::post('/checkout', [CheckoutController::class, 'create'])->name('checkout.create');
Route::get('/success', [StoreController::class, 'success'])->name('store.success');StoreController (app/Http/Controllers/StoreController.php)
index(): Display the main product pagesuccess(): Display payment success page
CheckoutController (app/Http/Controllers/CheckoutController.php)
create(): Create Magpie checkout session using Laravel Facade and redirect
Magpie Config (config/magpie.php)
return [
'secret_key' => env('MAGPIE_SECRET_KEY'),
];Store Index (resources/views/store/index.blade.php)
- Product display with Picsum random images
- Buy Now button with CSRF protection
- Error message handling
Success Page (resources/views/store/success.blade.php)
- Payment confirmation
- Session ID display
- Back to store link
- Magpie settings in
config/magpie.php - Environment-based configuration
- Easy to override for different environments
- Laravel's built-in logging system
- Magpie errors logged with context
- Configurable log levels and channels
- Laravel exception handling
- Custom error messages for different scenarios
- Graceful fallbacks and redirects
- CSRF protection on all forms
- Environment-based configuration
- Laravel's built-in security features
- Named routes for easy URL generation
- RESTful route structure
- Middleware support ready
// Add to routes/web.php
Route::middleware(['auth'])->group(function () {
Route::post('/checkout', [CheckoutController::class, 'create']);
});// Create a Product model
php artisan make:model Product -m
// Store orders
php artisan make:model Order -m// Add to CheckoutController
$request->validate([
'product_id' => 'required|exists:products,id',
'quantity' => 'required|integer|min:1',
]);// Create a service for Magpie operations
php artisan make:class Services/MagpieService# Application
APP_NAME="Magpie Store"
APP_ENV=local
APP_DEBUG=true
APP_URL=http://localhost
# Magpie Configuration
MAGPIE_SECRET_KEY=your_magpie_secret_key_here
# Optional Magpie Settings
MAGPIE_TIMEOUT=30
MAGPIE_MAX_RETRIES=3
MAGPIE_DEBUG=falsephp artisan test// tests/Feature/CheckoutTest.php
public function test_checkout_creates_session()
{
$response = $this->post(route('checkout.create'));
$response->assertRedirect();
}- Automated deployment and server management
- Easy environment variable management
- Perfect for Laravel applications
- Serverless deployment on AWS Lambda
- Auto-scaling and cost-effective
- Great for variable traffic
- Standard Laravel deployment process
- Use Laravel Envoy for deployment automation
- Configure web server and PHP-FPM
-
"Magpie secret key not configured"
- Check
.envfile hasMAGPIE_SECRET_KEY - Run
php artisan config:clear - Verify the key starts with
sk_
- Check
-
CSRF Token Mismatch
- Ensure
@csrfis in your forms - Check if sessions are working properly
- Verify
APP_KEYis set in.env
- Ensure
-
Route not found
- Run
php artisan route:listto verify routes - Check controller namespaces
- Clear route cache:
php artisan route:clear
- Run
Enable debug mode in .env:
APP_DEBUG=true
LOG_LEVEL=debugCheck Laravel logs:
tail -f storage/logs/laravel.logphp artisan make:job ProcessMagpieWebhook
php artisan queue:workphp artisan make:event PaymentCompleted
php artisan make:listener SendOrderConfirmationphp artisan make:resource OrderResourceFor issues with:
- Laravel Framework: Check Laravel Documentation
- Magpie API: Contact Magpie support or check their documentation
- This application: Check the error logs and verify your configuration
This sample application is provided as-is for demonstration purposes.