zaenalrfn/laravel-paypal-checkout
Composer 安装命令:
composer require zaenalrfn/laravel-paypal-checkout
包简介
A secure and production-ready PayPal Checkout integration for Laravel with DTO-based payloads and verified webhooks
README 文档
README
A modern, clean, and well-tested PayPal payment gateway integration for Laravel 11 & 12.
Features
✅ Simple & Intuitive API - Fluent interface for creating orders and processing payments
✅ Webhook Support - Secure webhook verification and event handling
✅ Type-Safe DTOs - Immutable data transfer objects for better code quality
✅ Comprehensive Logging - Dedicated PayPal log channel for debugging
✅ Test Coverage - Well-tested codebase with Pest PHP
✅ Laravel 12 Ready - Compatible with Laravel 11 and 12
Installation
Install via Composer:
composer require zaenalrfn/laravel-paypal-checkout
Configuration
1. Publish Configuration
php artisan vendor:publish --tag=paypal-config
This creates config/paypal.php with all available options.
2. Environment Variables
Add your PayPal credentials to .env:
# PayPal Mode (sandbox or live) PAYPAL_MODE=sandbox # Sandbox Credentials PAYPAL_SANDBOX_CLIENT_ID=your-sandbox-client-id PAYPAL_SANDBOX_CLIENT_SECRET=your-sandbox-client-secret # Production Credentials (when ready) PAYPAL_LIVE_CLIENT_ID=your-live-client-id PAYPAL_LIVE_CLIENT_SECRET=your-live-client-secret # Webhook ID (get from PayPal Dashboard) PAYPAL_WEBHOOK_ID=your-webhook-id # Optional: Disable webhook verification in development PAYPAL_VERIFY_WEBHOOK=true
3. Get PayPal Credentials
- Go to PayPal Developer Dashboard
- Create an app or select existing one
- Copy Client ID and Secret
- For webhooks: Go to Webhooks → Create webhook → Copy Webhook ID
Usage
Creating a PayPal Order
use Zaenalrfn\LaravelPayPal\Facades\PayPal; use Zaenalrfn\LaravelPayPal\DTO\{CheckoutOrderData, PurchaseUnitData, AmountData}; // Build order data $order = CheckoutOrderData::capture() ->addPurchaseUnit( new PurchaseUnitData( new AmountData('USD', '10.00'), 'ORDER-123' // Optional reference ID ) ) ->withReturnUrl('https://yoursite.com/payment/success') ->withCancelUrl('https://yoursite.com/payment/cancel'); // Create order with PayPal $response = PayPal::checkout()->create($order); // Get approval URL $approvalUrl = collect($response['links']) ->firstWhere('rel', 'approve')['href']; // Redirect user to PayPal return redirect($approvalUrl);
Capturing a Payment
After user approves payment on PayPal:
use Zaenalrfn\LaravelPayPal\Facades\PayPal; // Get order ID from PayPal callback $orderId = $request->query('token'); // Capture the payment $response = PayPal::checkout()->capture($orderId); if ($response['status'] === 'COMPLETED') { // Payment successful! $captureId = $response['purchase_units'][0]['payments']['captures'][0]['id']; // Store in database, fulfill order, etc. }
Processing Refunds
use Zaenalrfn\LaravelPayPal\Facades\PayPal; $response = PayPal::payment()->refund($captureId, [ 'amount' => [ 'currency_code' => 'USD', 'value' => '10.00' ], 'note_to_payer' => 'Refund for order #123' ]);
Webhook Setup
1. Create Webhook in PayPal Dashboard
- Go to PayPal Developer Dashboard
- Select your app
- Click Webhooks → Add Webhook
- Webhook URL:
https://yoursite.com/api/paypal/webhook - Select events:
PAYMENT.CAPTURE.COMPLETEDPAYMENT.CAPTURE.DENIEDPAYMENT.CAPTURE.REFUNDED
- Save and copy the Webhook ID
- Add to
.env:PAYPAL_WEBHOOK_ID=your-webhook-id
2. Webhook Route
The package automatically registers the webhook route:
POST /api/paypal/webhook
3. Handle Webhook Events
Extend the WebhookHandler to customize event handling:
namespace App\PayPal; use Zaenalrfn\LaravelPayPal\Webhooks\WebhookHandler as BaseHandler; use Zaenalrfn\LaravelPayPal\Support\PayPalLogger; class CustomWebhookHandler extends BaseHandler { protected function paymentCompleted(array $event): void { $captureId = $event['resource']['id'] ?? null; // Update your database \App\Models\Payment::where('capture_id', $captureId) ->update(['status' => 'completed']); PayPalLogger::info('Payment completed', ['capture_id' => $captureId]); } }
Then bind it in your AppServiceProvider:
$this->app->bind( \Zaenalrfn\LaravelPayPal\Webhooks\WebhookHandler::class, \App\PayPal\CustomWebhookHandler::class );
Testing
Running Tests
cd packages/laravel-paypal
./vendor/bin/pest
Development Mode
For local testing without valid webhook signatures:
PAYPAL_VERIFY_WEBHOOK=false
⚠️ WARNING: Never disable webhook verification in production!
Logging
All PayPal interactions are logged to a dedicated channel:
storage/logs/paypal-{date}.log
View logs:
tail -f storage/logs/paypal-$(date +%Y-%m-%d).log
Troubleshooting
Webhook 500 Error
Problem: Webhook returns 500 Internal Server Error
Solutions:
- Wrong Webhook ID - Verify
PAYPAL_WEBHOOK_IDmatches PayPal Dashboard - Environment Mismatch - Sandbox webhook ID only works with sandbox credentials
- Development Testing - Set
PAYPAL_VERIFY_WEBHOOK=falsefor local testing
Authentication Failed
Problem: "PayPal authentication failed" error
Solutions:
- Verify
PAYPAL_SANDBOX_CLIENT_IDandPAYPAL_SANDBOX_CLIENT_SECRETare correct - Check
PAYPAL_MODEmatches your credentials (sandbox vs live) - Ensure credentials are from the correct PayPal app
Order Creation Failed
Problem: Order creation returns 400 error
Solutions:
- Verify amount format: must be string with 2 decimals (e.g., "10.00")
- Check currency code is valid (USD, EUR, GBP, etc.)
- Ensure at least one purchase unit is added
Security
- ✅ Webhook signature verification enabled by default
- ✅ No credentials in code (environment variables only)
- ✅ Dedicated exception handling with context
- ✅ Comprehensive logging for audit trails
License
The MIT License (MIT). Please see License File for more information.
Credits
Support
- Issues: GitHub Issues
- Documentation: PayPal API Docs
zaenalrfn/laravel-paypal-checkout 适用场景与选型建议
zaenalrfn/laravel-paypal-checkout 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 01 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「payment」 「checkout」 「gateway」 「paypal」 「laravel」 「laravel-paypal」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 zaenalrfn/laravel-paypal-checkout 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 zaenalrfn/laravel-paypal-checkout 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 zaenalrfn/laravel-paypal-checkout 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Payyo Gateway for the Omnipay payment processing library
Adds a 'checkout' to a SilverStripe site which links to an Estimate and adds the ability to setup and pay
Rich payment solutions for Laravel framework. Paypal, payex, authorize.net, be2bill, omnipay, recurring paymens, instant notifications and many more
Client library to send SMS using Comilio SMS Gateway API (https://www.comilio.it)
Add a simple webshop to your Laravel project
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-29