Overview
Connect World integrates PayPal as an alternative payment method for users who prefer not to use credit cards. The implementation uses PayPal’s Orders API v2 for creating and capturing payments.Prerequisites
- PayPal Business account (sign up here)
- PayPal REST API credentials
- Next.js application with API routes
Environment Configuration
Create PayPal App
- Log in to the PayPal Developer Dashboard
- Navigate to Apps & Credentials
- Create a new app or use an existing one
- Copy your Client ID and Secret
Configure environment variables
Add these variables to your
.env.local file:.env.local
Sandbox vs Production:
- Sandbox:
https://api-m.sandbox.paypal.com - Production:
https://api-m.paypal.com
Server-Side Implementation
Authentication Helper
PayPal requires OAuth 2.0 authentication for API requests.Authentication
Create Order Endpoint
The create-order endpoint initializes a PayPal order with validated plan details. File:src/app/api/paypal/create-order/route.ts
Capture Order Endpoint
After the user approves the payment, capture the funds. File:src/app/api/paypal/capture-order/route.ts
Client-Side Implementation
Initialize PayPal Provider
Wrap your checkout component with the PayPal Script Provider.CheckoutModal.tsx
PayPal Buttons Implementation
CheckoutForm.tsx
Payment Flow
Server creates PayPal order
/api/paypal/create-order validates the plan and creates an order via PayPal APIClient captures payment
The
onApprove callback triggers /api/paypal/capture-order to finalize the paymentTesting
Sandbox Accounts
- Go to PayPal Sandbox
- Create a Personal account (buyer) for testing
- Use these credentials to test payments
Sandbox accounts have fake money pre-loaded. No real transactions occur during testing.
Test Credentials
Create test buyer accounts in the sandbox with these details:- Email:
[email protected] - Password: Set in the sandbox dashboard
Best Practices
Secure Credentials
Keep your client secret server-side only. Never expose it in client code.
Validate Server-Side
Always validate prices and plan details on the server before creating orders.
Handle Errors
PayPal errors include detailed response data. Log these for debugging.
Use Webhooks
For production, implement PayPal webhooks to handle asynchronous payment events.
Additional Resources
Troubleshooting
Common Issues
“Authentication failed”- Verify your client ID and secret are correct
- Ensure credentials match the environment (sandbox vs production)
- Check the
PAYPAL_BASE_URLmatches your credentials type
- Verify the amount format is a string with 2 decimal places
- Check that currency code is supported (USD, EUR, etc.)
- Review server logs for detailed PayPal error responses
- In sandbox: Use a test buyer account with sufficient balance
- In production: The buyer’s payment method was declined
