Installation
composer require srmklive/paypal
For Laravel, publish the config:
php artisan vendor:publish --provider="Srmklive\PayPal\Providers\PayPalServiceProvider" --tag="config"
Configuration
Update .env with PayPal credentials:
PAYPAL_CLIENT_ID=your_client_id
PAYPAL_SECRET=your_secret
PAYPAL_MODE=sandbox # or 'live'
First Use Case: Create a Payment
use Srmklive\PayPal\Services\PayPal;
$paypal = app('paypal');
$payment = $paypal->payment()->create([
'intent' => 'sale',
'payer' => [
'payment_method' => 'paypal',
],
'transactions' => [
[
'amount' => [
'total' => '10.00',
'currency' => 'USD',
],
'description' => 'Test Payment',
],
],
'redirect_urls' => [
'return_url' => route('paypal.success'),
'cancel_url' => route('paypal.cancel'),
],
]);
Redirect User
return redirect()->away($payment->getApprovalLink());
// After user returns from PayPal
$paymentId = request('paymentID');
$payerId = request('PayerID');
$payment = $paypal->payment()->get($paymentId);
$execution = $paypal->payment()->execute($paymentId, [
'payer_id' => $payerId,
]);
$plan = $paypal->plan()->create([
'name' => 'Premium',
'description' => 'Monthly Subscription',
'billing_cycles' => [
'price' => '9.99',
'frequency' => 'MONTH',
'tenure_type' => 'REGULAR',
'sequence' => 1,
],
]);
$subscription = $paypal->subscription()->create([
'plan_id' => $plan->getId(),
'start_time' => now()->addDay()->format('Y-m-d\TH:i:s\Z'),
'subscriber' => [
'name' => 'John Doe',
'email_address' => 'john@example.com',
],
]);
$capture = $paypal->capture()->create($paymentId, [
'amount' => '5.00',
]);
$refund = $paypal->refund()->create($capture->getId(), [
'amount' => [
'total' => '2.50',
'currency' => 'USD',
],
]);
use Srmklive\PayPal\Services\Webhook;
$webhook = app(Webhook::class);
$event = $webhook->verifyAndParse(request()->all());
Route::post('/paypal/webhook', [PayPalController::class, 'handleWebhook']);
use Srmklive\PayPal\Facades\PayPal;
$payment = PayPal::payment()->create([...]);
payment_id, subscription_id) for later reference.payment.succeeded).$this->mock(PayPal::class)->shouldReceive('payment()->create')->andReturn($mockPayment);
Sandbox vs. Live Mode
PAYPAL_MODE=sandbox in .env.Redirect URLs Must Match
return_url and cancel_url. Ensure they are HTTPS and accessible.https://yourdomain.com/paypal/success).Idempotency Keys
idempotency_key to avoid duplicate processing:
$paypal->subscription()->create([...], 'unique_key_123');
Webhook Verification
Webhook::verify() to prevent spoofing.POST requests to your configured webhook URL.Currency and Amount Formatting
10.00 not 10).number_format($amount, 2, '.', '') to ensure consistency.Rate Limits
Deprecated Methods
createOrder() (older method). Use payment()->create() for classic PayPal flows or order()->create() for newer PayPal Checkout.Enable Logging
Add to config/paypal.php:
'log' => [
'enabled' => true,
'file' => storage_path('logs/paypal.log'),
],
Logs API requests/responses for troubleshooting.
Check HTTP Status Codes
400 for invalid requests, 401 for auth issues, and 403 for forbidden actions.try-catch to handle exceptions:
try {
$payment = $paypal->payment()->create([...]);
} catch (\Srmklive\PayPal\Exceptions\PayPalConnectionException $e) {
Log::error($e->getMessage());
}
PayPal Developer Dashboard
Common Errors
transactions array includes item_list if selling items.payer object structure (e.g., payment_method must be paypal).Customize Requests/Responses
Override the PayPal service binding in AppServiceProvider:
$this->app->bind('paypal', function () {
$paypal = new \Srmklive\PayPal\Services\PayPal();
$paypal->setConfig(['timeout' => 30]); // Custom timeout
return $paypal;
});
Add Custom API Endpoints
Extend the PayPal class to support unsupported endpoints:
$paypal->custom()->post('/v1/custom-endpoint', $data);
Webhook Middleware Create middleware to process webhooks before they reach your controller:
public function handle($request, Closure $next) {
$event = Webhook::verifyAndParse($request->all());
// Process event (e.g., update DB, send notifications)
return $next($request);
}
Laravel Cashier Integration Use PayPal subscriptions with Cashier for unified billing:
$user->newSubscription('premium', $planId)->create($paypalToken);
Standalone PHP Usage Initialize PayPal without Laravel:
$paypal = new \Srmklive\PayPal\Services\PayPal();
$paypal->setConfig([
'mode' => 'sandbox',
'client_id' => 'your_client_id',
'secret' => 'your_secret',
]);
How can I help you explore Laravel packages today?