Installation
composer require backndev/paypal
Ensure your project uses Symfony (Laravel is not natively supported, but can be adapted via Symfony Bridge).
Configuration
Add PayPal credentials to config/services.php (or equivalent in Laravel):
'paypal' => [
'client_id' => env('PAYPAL_CLIENT_ID'),
'client_secret' => env('PAYPAL_CLIENT_SECRET'),
'mode' => env('PAYPAL_MODE', 'sandbox'), // 'live' for production
],
First Use Case: Create an Order
use Backndev\Paypal\Paypal;
$paypal = new Paypal(config('paypal'));
$order = $paypal->createOrder([
'intent' => 'CAPTURE',
'purchase_units' => [
[
'amount' => [
'currency_code' => 'USD',
'value' => '10.00',
],
],
],
]);
Order Creation & Capture
createOrder() to generate a PayPal order ID.$paypal->getApprovalLink($orderId).$paypal->captureOrder($orderId);
Webhooks
PAYMENT.CAPTURE.COMPLETED).Route::post('/paypal/webhook', function (Request $request) {
$paypal = new Paypal(config('paypal'));
$verified = $paypal->verifyWebhook($request->getContent());
if ($verified) {
$event = $paypal->parseWebhook($request->getContent());
// Handle event (e.g., update order status)
}
});
Refunds
$paypal->createRefund($captureId, ['amount' => ['currency_code' => 'USD', 'value' => '5.00']]);
HttpClient via Laravel’s HTTP client or bridge packages like symfony/http-client-bridge.$paypal->setLogger(new Monolog\Logger('paypal'));
Symfony Dependency: The package assumes Symfony’s HttpClient and HttpFoundation. For Laravel:
symfony/http-client and symfony/http-foundation via Composer.Request for webhooks using Symfony\Component\HttpFoundation\Request::createFromGlobals().Webhook Verification:
$paypal->verifyWebhook() to prevent spoofing.webhook_id and webhook_event_id must match your configured endpoints.Sandbox vs. Live:
mode: 'sandbox'). Sandbox credentials are different from live ones.curl to inspect PayPal API responses if debugging fails:
curl -v -X POST https://api.sandbox.paypal.com/v2/checkout/orders \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"intent":"CAPTURE", ...}'
detail field of responses. Log these for troubleshooting.$paypal->getAccessToken(); // Auto-refreshes if expired
handleResponse() method in the Paypal class to transform API responses (e.g., flatten arrays for Laravel Eloquent).public function createSubscription(array $data) { ... }
Paypal class to Laravel’s container for dependency injection:
$this->app->singleton(Paypal::class, function ($app) {
return new Paypal(config('paypal'));
});
How can I help you explore Laravel packages today?