A Django package for integrating PayPal payments into your Django application.
- Easy PayPal integration for Django projects
- Support for PayPal Orders API v2
- Transaction tracking and management
- Django admin integration
- Sandbox and live mode support
- Secure OAuth2 authentication with token caching
pip install django-paypal-checkoutINSTALLED_APPS = [
...
'paypal',
]Add these settings to your settings.py:
# PayPal Configuration
PAYPAL_CLIENT_ID = 'your-paypal-client-id'
PAYPAL_CLIENT_SECRET = 'your-paypal-client-secret'
PAYPAL_MODE = 'sandbox' # Use 'live' for production
# Optional: Override default URLs
PAYPAL_RETURN_URL = 'https://yourdomain.com/payment/success/'
PAYPAL_CANCEL_URL = 'https://yourdomain.com/payment/cancel/'Include the django-paypal-checkout URLs in your project's urls.py:
from django.urls import path, include
urlpatterns = [
...
path('paypal/', include('paypal.urls')),
]python manage.py migrate paypalYou can create a payment in two ways:
Create a form in your template:
<form method="POST" action="{% url 'paypal:create_payment' %}">
{% csrf_token %}
<input type="hidden" name="amount" value="99.99">
<input type="hidden" name="currency" value="USD">
<input type="hidden" name="description" value="Product Purchase">
<button type="submit">Pay with PayPal</button>
</form>from paypal.client import PayPalClient
from paypal.models import PayPalTransaction
# Create transaction record
transaction = PayPalTransaction.objects.create(
user=request.user,
amount=99.99,
currency='USD',
description='Product Purchase'
)
# Create PayPal order
client = PayPalClient()
order = client.create_order(
amount=99.99,
currency='USD',
description='Product Purchase',
return_url='https://yourdomain.com/success/',
cancel_url='https://yourdomain.com/cancel/'
)
# Save order ID to transaction
transaction.paypal_order_id = order['id']
transaction.save()
# Redirect user to PayPal
approval_url = next(link['href'] for link in order['links'] if link['rel'] == 'approve')
return redirect(approval_url)Create views to handle success, failure, and cancellation:
from django.views.generic import TemplateView
class PaymentSuccessView(TemplateView):
template_name = 'payment_success.html'
class PaymentFailedView(TemplateView):
template_name = 'payment_failed.html'
class PaymentCancelledView(TemplateView):
template_name = 'payment_cancelled.html'Add these to your urls.py:
urlpatterns = [
...
path('payment/success/', PaymentSuccessView.as_view(), name='payment_success'),
path('payment/failed/', PaymentFailedView.as_view(), name='payment_failed'),
path('payment/cancelled/', PaymentCancelledView.as_view(), name='payment_cancelled'),
]The main model for tracking PayPal transactions:
Fields:
id: UUID primary keyuser: ForeignKey to User modelamount: Transaction amountcurrency: Currency code (default: USD)description: Transaction descriptionpaypal_order_id: PayPal order IDpaypal_payment_id: PayPal payment IDpayer_id: Payer IDpayer_email: Payer emailstatus: Transaction status (pending, completed, failed, cancelled, refunded)created_at: Creation timestampupdated_at: Last update timestampcompleted_at: Completion timestampmetadata: JSON field for additional data
Methods:
mark_completed(): Mark transaction as completedmark_failed(): Mark transaction as failedmark_cancelled(): Mark transaction as cancelled
The main client for interacting with PayPal API:
from paypal.client import PayPalClient
client = PayPalClient()
# Create an order
order = client.create_order(
amount=99.99,
currency='USD',
description='Product description'
)
# Capture an order
capture_data = client.capture_order(order_id='ORDER_ID')
# Get order details
order_details = client.get_order(order_id='ORDER_ID')- Go to PayPal Developer Dashboard
- Log in with your PayPal account
- Navigate to "Apps & Credentials"
- Create a new app or use an existing one
- Copy the Client ID and Secret
- Use sandbox credentials for testing, live credentials for production
The package uses PayPal's sandbox environment by default. To test:
- Create a sandbox account at PayPal Developer
- Use sandbox credentials in your settings
- Use sandbox buyer accounts for testing payments
- Never commit your PayPal credentials to version control
- Use environment variables for sensitive settings
- Always use HTTPS in production
- Validate transaction amounts server-side
Contributions are welcome! Please feel free to submit a Pull Request.
MIT License
For issues and questions, please open an issue on GitHub.
- Initial release
- PayPal Orders API v2 integration
- Transaction tracking
- Django admin integration
- Sandbox and live mode support