Skip to content

magpieimdev/sample-magpie-laravel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sample Magpie Laravel Checkout Application

A Laravel application demonstrating Magpie Checkout integration for digital art sales using the Magpie PHP SDK.

Features

  • 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

Requirements

  • PHP 8.1 or higher
  • Composer for dependency management
  • Laravel 12.x
  • Magpie account with API credentials

Installation

  1. Clone or download this repository

    git clone <repository-url>
    cd sample-magpie-laravel
  2. Install dependencies

    composer install
  3. Set up environment

    cp .env.example .env
    php artisan key:generate
  4. Configure Magpie credentials

    Edit the .env file and add your Magpie API credentials:

    MAGPIE_SECRET_KEY=your_magpie_secret_key_here
  5. Run migrations (optional for this demo)

    php artisan migrate

Getting Magpie API Credentials

  1. Sign up for a Magpie account

  2. Create a new application

    • Navigate to your Magpie dashboard
    • Go to "Developers", under "API Keys" section
  3. 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

Running the Application

Development Server

php artisan serve

Then open your browser and visit: http://localhost:8000

Production Deployment

For production deployment, follow standard Laravel deployment practices:

  • Configure your web server (Apache/Nginx)
  • Set APP_ENV=production in your .env file
  • Run php artisan config:cache and php artisan route:cache
  • Ensure proper file permissions

Application Structure

Routes

// 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');

Controllers

StoreController (app/Http/Controllers/StoreController.php)

  • index(): Display the main product page
  • success(): Display payment success page

CheckoutController (app/Http/Controllers/CheckoutController.php)

  • create(): Create Magpie checkout session using Laravel Facade and redirect

Configuration

Magpie Config (config/magpie.php)

return [
    'secret_key' => env('MAGPIE_SECRET_KEY'),
];

Views

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

Laravel-Specific Features

Configuration Management

  • Magpie settings in config/magpie.php
  • Environment-based configuration
  • Easy to override for different environments

Logging

  • Laravel's built-in logging system
  • Magpie errors logged with context
  • Configurable log levels and channels

Error Handling

  • Laravel exception handling
  • Custom error messages for different scenarios
  • Graceful fallbacks and redirects

Security

  • CSRF protection on all forms
  • Environment-based configuration
  • Laravel's built-in security features

Routing

  • Named routes for easy URL generation
  • RESTful route structure
  • Middleware support ready

Customization

Adding Middleware

// Add to routes/web.php
Route::middleware(['auth'])->group(function () {
    Route::post('/checkout', [CheckoutController::class, 'create']);
});

Database Integration

// Create a Product model
php artisan make:model Product -m

// Store orders
php artisan make:model Order -m

Validation

// Add to CheckoutController
$request->validate([
    'product_id' => 'required|exists:products,id',
    'quantity' => 'required|integer|min:1',
]);

Service Classes

// Create a service for Magpie operations
php artisan make:class Services/MagpieService

Environment Variables

# 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=false

Testing

Run Laravel Tests

php artisan test

Feature Tests Example

// tests/Feature/CheckoutTest.php
public function test_checkout_creates_session()
{
    $response = $this->post(route('checkout.create'));
    $response->assertRedirect();
}

Deployment Options

Laravel Forge

  • Automated deployment and server management
  • Easy environment variable management
  • Perfect for Laravel applications

Laravel Vapor (AWS Serverless)

  • Serverless deployment on AWS Lambda
  • Auto-scaling and cost-effective
  • Great for variable traffic

Traditional VPS

  • Standard Laravel deployment process
  • Use Laravel Envoy for deployment automation
  • Configure web server and PHP-FPM

Troubleshooting

Common Issues

  1. "Magpie secret key not configured"

    • Check .env file has MAGPIE_SECRET_KEY
    • Run php artisan config:clear
    • Verify the key starts with sk_
  2. CSRF Token Mismatch

    • Ensure @csrf is in your forms
    • Check if sessions are working properly
    • Verify APP_KEY is set in .env
  3. Route not found

    • Run php artisan route:list to verify routes
    • Check controller namespaces
    • Clear route cache: php artisan route:clear

Debug Mode

Enable debug mode in .env:

APP_DEBUG=true
LOG_LEVEL=debug

Logs

Check Laravel logs:

tail -f storage/logs/laravel.log

Additional Laravel Features

Queues (for webhook processing)

php artisan make:job ProcessMagpieWebhook
php artisan queue:work

Events & Listeners

php artisan make:event PaymentCompleted
php artisan make:listener SendOrderConfirmation

API Resources (for API endpoints)

php artisan make:resource OrderResource

Support

For 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

License

This sample application is provided as-is for demonstration purposes.

About

Laravel application with Magpie PHP SDK integration.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages