Skip to main content

PayPal payment integration for Streamlit apps

Project description

💳 Streamlit PayPal

Python Streamlit License

Secure and elegant PayPal payment integration for Streamlit apps

Easily integrate PayPal payments into your Streamlit applications with a secure, popup-based payment flow.

This project is forked from streamlit-oauth, focusing on PayPal payment integration.

🚀 Quick Start

Installation

pip install streamlit-paypal

Basic Usage

Create a .env file with your PayPal credentials:

PAYPAL_CLIENT_ID=your_client_id
PAYPAL_CLIENT_SECRET=your_client_secret

Then use the component in your Streamlit app:

import streamlit as st
from streamlit_paypal import PayPalComponent
import os

# Initialize PayPal component
paypal = PayPalComponent(
    client_id=os.getenv('PAYPAL_CLIENT_ID'),
    client_secret=os.getenv('PAYPAL_CLIENT_SECRET'),
    mode='sandbox'  # Use 'live' for production
)

# Create payment button
if 'payment' not in st.session_state:
    result = paypal.payment_button(
        name="Pay $10 USD",
        amount=10.00,
        currency='USD',
        description='Product Purchase',
        return_url='https://yourapp.streamlit.app'  # Required!
    )

    if result:
        st.session_state.payment = result
        st.rerun()
else:
    st.success(f"Payment successful! Order ID: {st.session_state.payment['order_id']}")

⚠️ Production Notice: This component uses Streamlit session state for immediate interaction. For reliable order processing in production (handling network interruptions, browser closures, etc.), configure PayPal Webhooks to receive payment notifications server-side and persist order states.

📚 API Reference

PayPalComponent

paypal = PayPalComponent(
    client_id: str,           # PayPal Client ID
    client_secret: str,       # PayPal Client Secret
    mode: str = 'sandbox'     # 'sandbox' or 'live'
)

result = paypal.payment_button(
    name: str,                # Button text
    amount: float,            # Payment amount
    currency: str,            # Currency code (USD, EUR, TWD, etc.)
    description: str,         # Order description
    return_url: str           # Post-payment return URL (required)
)

Return Value

On successful payment, returns a dictionary:

{
    'order_id': 'xxx',        # PayPal Order ID
    'status': 'COMPLETED',    # Order status
    'payer_email': 'xxx',     # Payer's email
    'amount': '10.00',        # Payment amount
    'currency': 'USD'         # Currency code
}

🔒 Security Features

Feature Description
Client Secret Protection ✅ Secret stays server-side, zero frontend exposure
CSRF Protection ✅ Order ID verification mechanism
Timeout Control ✅ 5-minute auto-cancellation
Order Verification ✅ Can only capture self-created orders
Replay Attack Protection ✅ Order state tracking

🛠️ Development

Setup

# Clone repository
git clone https://github.com/TEENLU/streamlit-paypal.git
cd streamlit-paypal

# Install in development mode
pip install -e .

# Run example
streamlit run examples/paypal_basic.py

Frontend Development

cd streamlit_paypal/frontend
npm install
npm run dev

Testing

python test_paypal_component.py

📊 Architecture Design

Why Popup Mode?

  1. Simpler URL Handling: Returns Python dict directly, no callback URL parsing needed
  2. Better UX: Dedicated window feels more professional, doesn't interrupt main app flow
  3. State Management: Auto-integrates with Streamlit session state
  4. Enhanced Security: Reduces URL parameter exposure risks

Production Architecture

This package provides the frontend interaction layer for immediate payment experiences.

Recommended production setup:

Streamlit App (this package)  →  Real-time UI, payment buttons, user experience
         ↓
PayPal Orders API             →  Create orders, popup payment flow
         ↓
Your Backend + Webhooks       →  Receive PAYMENT.CAPTURE.COMPLETED
                                  Persist orders, fulfillment, authorization

Why use Webhooks?

  • ✅ Reliability: Process payments even if user closes browser
  • ✅ Security: Server-to-Server verification
  • ✅ Completeness: Receive all payment events (success, failure, refund, etc.)

Reference: PayPal Webhooks Documentation

🙏 Acknowledgments

This project is forked from dnplus/streamlit-oauth. Special thanks to the original author for the excellent popup mechanism architecture.

📝 License

MIT License - see LICENSE file for details


Version: 1.0.0 Status: 🟢 Active Development

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

streamlit_paypal-1.0.0.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

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

streamlit_paypal-1.0.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file streamlit_paypal-1.0.0.tar.gz.

File metadata

  • Download URL: streamlit_paypal-1.0.0.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for streamlit_paypal-1.0.0.tar.gz
Algorithm Hash digest
SHA256 3665d12e01e0461f03ab384a7594bdd27e32008f31a1bd7d55b0d4e27c06bac8
MD5 dd0bdf3f9260d834be97273e57d81dce
BLAKE2b-256 e395a411015d6ad6b3f47e2e173bd2d29223f627a587f22ffc29f0794e8a7238

See more details on using hashes here.

File details

Details for the file streamlit_paypal-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_paypal-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 45b0e405e397f0270d0d41960da97681005143aa9404ef469bd2b773b6cf6257
MD5 118c9c9b69344739a9d7caa0b7a9f10c
BLAKE2b-256 ee41456647e30a6fcf01ad3a3476c3323811fdcdced987cbec0873e9c150e253

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