PayPal REST SDK for Python
The `paypalrestsdk` library provides Python APIs to integrate with PayPal's REST APIs for creating, processing, and managing payments. This library is officially deprecated and is no longer actively maintained. The last public version is 1.13.3, released in November 2023, and no new features or support are provided.
Warnings
- breaking The `paypalrestsdk` Python SDK is officially deprecated. PayPal is transitioning deprecated server-side SDKs to private, restricted access. Public GitHub repository access for this SDK will be restricted after April 29th (likely 2024), making it unavailable for new integrations or easy access to source code and issues.
- deprecated This library is no longer supported or receiving new features/security updates. Using it in new projects is strongly discouraged.
- gotcha Due to PCI compliance requirements, your server must support TLS 1.2 for all HTTPS connections to PayPal. Older TLS versions will result in connection warnings or failures.
- gotcha PayPal REST API no longer supports *new* direct credit card integrations. If your application requires direct credit card processing, consider using Braintree Direct, a PayPal service.
Install
-
pip install paypalrestsdk
Imports
- paypalrestsdk
import paypalrestsdk
- Payment
from paypalrestsdk import Payment
- Tokeninfo
from paypalrestsdk.openid_connect import Tokeninfo
Quickstart
import os
import paypalrestsdk
paypalrestsdk.configure({
"mode": os.environ.get("PAYPAL_MODE", "sandbox"), # "sandbox" or "live"
"client_id": os.environ.get("PAYPAL_CLIENT_ID", ""),
"client_secret": os.environ.get("PAYPAL_CLIENT_SECRET", "")
})
# Example: Create a simple payment (requires valid client_id and client_secret in sandbox/live)
payment = paypalrestsdk.Payment({
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "http://localhost:8000/payment/execute",
"cancel_url": "http://localhost:8000/payment/cancel"
},
"transactions": [{
"item_list": {
"items": [{
"name": "item",
"sku": "item",
"price": "1.00",
"currency": "USD",
"quantity": 1
}]
},
"amount": {
"total": "1.00",
"currency": "USD"
},
"description": "This is the payment transaction description."
}]
})
if payment.create():
print(f"Payment created successfully: {payment.id}")
for link in payment.links:
if link.rel == "approval_url":
print(f"Approval URL: {link.href}")
else:
print(f"Error creating payment: {payment.error}")