Skip to main content

Minimal PayPal Checkout SDK (with Pydantic models)

Project description

PayPal SDK

Minimal PayPal Checkout SDK (with Pydantic models)

PyPI version [Python versions]

A minimalistic Python SDK for PayPal Checkout using Pydantic models and HTTPX.

Features

  • OAuth2 authentication with token caching and auto-refresh
  • Pydantic v2 models for request and response payloads
  • Synchronous HTTP client powered by HTTPX
  • Support for PayPal Orders API (create, capture, retrieve)
  • Custom exception types for error handling

Installation

pip install paypal-checkout-sdk

Requires Python 3.11+, Pydantic 2.x, HTTPX 0.27+

Quick Start

Initialize client

from paypal_sdk import PayPalClient
from paypal_sdk.enums import Environment

client = PayPalClient(
    client_id="YOUR_CLIENT_ID",
    client_secret="YOUR_CLIENT_SECRET",
    environment=Environment.SANDBOX,  # Use Environment.LIVE for production
)

You can also use the client as a context manager:

with PayPalClient(... ) as client:
    # use client

Create an Order

from paypal_sdk.models.orders import CreateOrderRequest, PurchaseUnitRequest
from paypal_sdk.models.base import Money

order_request = CreateOrderRequest(
    intent="CAPTURE",
    purchase_units=[
        PurchaseUnitRequest(
            amount=Money(currency_code="USD", value="100.00"),
            reference_id="PU1",
        )
    ],
)

order = client.orders.create_order(order_request)
print("Order ID:", order.id)
print("Status:", order.status)

# Extract approval URL
for link in order.links:
    if link.rel == "approve":
        print("Approval URL:", link.href)
        break

After the buyer approves the payment, capture the order:

capture = client.orders.capture_order(order.id)
print("Capture status:", capture.purchase_units[0].payments.captures[0].status)

Retrieve an Order

order = client.orders.get_order(order_id)
print(order)

Error Handling

All SDK errors inherit from paypal_sdk.exceptions.PayPalError:

  • AuthenticationError: failed to authenticate or refresh token
  • APIError: PayPal API returned an error (status code 4xx or 5xx)
  • RequestError: network or HTTP request issues
  • ConfigurationError: SDK configuration errors
from paypal_sdk.exceptions import APIError, AuthenticationError, RequestError

try:
    client.orders.get_order("INVALID_ID")
except AuthenticationError as e:
    print("Auth failed:", e)
except APIError as e:
    print("API error:", e)
except RequestError as e:
    print("Request error:", e)

API Reference

PayPalClient

PayPalClient(client_id: str, client_secret: str, environment: Environment = Environment.SANDBOX, timeout: float = 30.0, max_retries: int = 3)

Main client for interacting with the PayPal REST API:

  • orders: OrdersService instance for Orders API methods
  • close(): close underlying HTTP connection
  • Context manager support (__enter__ / __exit__)

OrdersService

Located at paypal_sdk.services.orders.OrdersService:

  • create_order(order_request: CreateOrderRequest, paypal_request_id: Optional[str] = None, prefer: str = "return=representation") -> CreateOrderResponse
  • capture_order(order_id: str, capture_request: Optional[CaptureOrderRequest] = None, paypal_request_id: Optional[str] = None, prefer: str = "return=representation") -> CaptureOrderResponse
  • get_order(order_id: str) -> Order

Models

Pydantic v2 models are defined under paypal_sdk.models:

  • CreateOrderRequest, CaptureOrderRequest, PurchaseUnitRequest, Money, Order, etc.

Exceptions

Custom exceptions in paypal_sdk.exceptions:

  • PayPalError
  • AuthenticationError
  • APIError
  • RequestError
  • ConfigurationError

References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

paypal_checkout_sdk-1.2.2.tar.gz (22.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

paypal_checkout_sdk-1.2.2-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

Details for the file paypal_checkout_sdk-1.2.2.tar.gz.

File metadata

  • Download URL: paypal_checkout_sdk-1.2.2.tar.gz
  • Upload date:
  • Size: 22.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.12

File hashes

Hashes for paypal_checkout_sdk-1.2.2.tar.gz
Algorithm Hash digest
SHA256 1ffe7e54087094087e655380bb7edbc599edfea5c1e4cc1bffb20d07e18511f8
MD5 213d928852ef8f38e3da37fab472ccac
BLAKE2b-256 d42ae24f50b4e9a521187dbe35502c986cf88e579e061ae848e06a838d8fd544

See more details on using hashes here.

File details

Details for the file paypal_checkout_sdk-1.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for paypal_checkout_sdk-1.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9419b6dba26a0cbf73c0d35d36b1ed58d4a5d3312345442d7f3cf878262cfbe7
MD5 46a010ea5970175de3d3d68b77a3acd1
BLAKE2b-256 e0d20269d20001fa0236b97d913d694bdfeeb7d97a3a6f59d321426937bf8d3e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page