Skip to main content

Asynchronous client for PayOS API

Project description

PayOS Python SDK

Một thư viện Python bất đồng bộ (asynchronous) để tương tác với API cổng thanh toán PayOS. Thư viện này giúp bạn dễ dàng tích hợp các dịch vụ thanh toán của PayOS vào ứng dụng Python của mình.

Mục lục

Tính năng

  • Tạo liên kết thanh toán cho đơn hàng.
  • Truy xuất thông tin chi tiết về liên kết thanh toán.
  • Huỷ bỏ liên kết thanh toán.
  • Xác nhận URL webhook với PayOS.
  • Xác minh an toàn dữ liệu webhook nhờ vào cơ chế checksum.
  • Hỗ trợ hoàn toàn bất đồng bộ (async) với aiohttp giúp hiệu năng cao.
  • Hỗ trợ type hinting giúp dễ lập trình và kiểm tra lỗi.
  • Cơ chế xử lý lỗi riêng biệt cho các lỗi đặc thù từ API PayOS.

Cấu trúc dự án

root_project/
├── payos/
│   ├── __init__.py           # Biến 'payos' thành package và export các lớp chính
│   ├── constants.py          # Các hằng số API, thông báo lỗi, URL gốc
│   ├── custom_error.py       # Định nghĩa exception PayOSError
│   ├── index.py              # Lớp chính PayOS chứa logic gọi API
│   ├── type.py               # Định nghĩa các kiểu dữ liệu cho request/response
│   └── utils.py              # Các hàm tiện ích (ví dụ tạo chữ ký)
├── tests/
│   ├── __init__.py
│   ├── requirements-test.txt # Dependencies dùng cho test
│   ├── test_payos.py         # Unit test cho lớp PayOS
│   └── test_types.py         # Unit test cho các kiểu dữ liệu
├── pyproject.toml            # Thông tin build và cấu hình project
└── readme.md                 # File này

Yêu cầu hệ thống

  • Python 3.7 trở lên (do sử dụng async/await và type hinting).

  • Thư viện aiohttp để gửi request bất đồng bộ.

  • Tài khoản merchant tại PayOS với:

    • Client ID
    • API Key
    • Checksum Key

Cài đặt

Cài đặt từ PyPI (khuyên dùng)

Nếu package đã được publish lên PyPI, bạn có thể cài bằng pip:

pip install payos-sdk-async

Lệnh này sẽ tự động cài thêm aiohttp nếu chưa có.

Cài đặt từ mã nguồn

Sẽ có™

Cách sử dụng

Mọi hàm gọi API đều là bất đồng bộ, bạn cần dùng await.

Khởi tạo SDK

from payos import PayOS, PaymentData, ItemData
payos_client = PayOS(
    client_id="YOUR_CLIENT_ID",
    api_key="YOUR_API_KEY",
    checksum_key="YOUR_CHECKSUM_KEY"
)

Tạo liên kết thanh toán

payment_data = PaymentData(
    orderCode=123456,
    amount=1000000,
    description="Thanh toán đơn hàng #123456",
    cancelUrl="https://domain.com/cancel",
    returnUrl="https://domain.com/success",
    items=[
        ItemData(name="Áo thun", quantity=1, price=500000),
        ItemData(name="Quần jean", quantity=1, price=500000)
    ]
)
result = await payos_client.createPaymentLink(payment_data)
print(result.checkoutUrl)

Lấy thông tin thanh toán

info = await payos_client.getPaymentLinkInformation(order_id=123456)
print(info.status, info.amountPaid)

Huỷ thanh toán

await payos_client.cancelPaymentLink(order_id=123456, cancellationReason="Khách yêu cầu huỷ")

Xác nhận URL webhook

webhook_data = {
  # Data nhận được từ payOS khi bạn thiết đặt một webhook
}
await payos_client.confirmWebhook(webhook_data)

Xác minh dữ liệu webhook

verified = payos_client.verifyPaymentWebhookData(webhook_body_dict) 
# webhook_body_dict nhận được khi thiết đặt webhook trong kênh thanh toán
print(verified.orderCode, verified.amount)

Cấu trúc dữ liệu

Tham khảo file payos/type.py, gồm:

  • ItemData: Sản phẩm trong đơn hàng
  • PaymentData: Dữ liệu tạo đơn
  • CreatePaymentResult: Kết quả từ createPaymentLink
  • Transaction: Giao dịch liên quan
  • PaymentLinkInformation: Thông tin liên kết
  • WebhookData: Dữ liệu webhook đã xác minh

Xử lý lỗi

  • Nếu nhập sai dữ liệu đầu vào → raise TypeError, ValueError

  • Nếu API trả về lỗi → raise PayOSError

    • .code.message
  • Nếu chữ ký không khớp → raise Exception

Ví dụ:

try:
    await payos_client.createPaymentLink(payment_data)
except PayOSError as e:
    print(f"Lỗi PayOS: {e.code} - {e}")
except Exception as e:
    print(f"Lỗi khác: {e}")

Đóng góp

Rất hoan nghênh đóng góp!

  1. Fork repo
  2. Tạo branch mới
  3. Gửi pull request rõ ràng

Giấy phép

Mã nguồn được phát hành theo giấy phép MIT.

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

payos_async-0.1.0.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

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

payos_async-0.1.0-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file payos_async-0.1.0.tar.gz.

File metadata

  • Download URL: payos_async-0.1.0.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for payos_async-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0c4267f9928ebc93623290a98220a95198642e25dffb6f77faa0940038776e16
MD5 7fb39a33cb92021ea6f18d19231cbf8b
BLAKE2b-256 2eba4d8c2b026de35e05eec0878e826eaa301f885754334293943de71013e925

See more details on using hashes here.

File details

Details for the file payos_async-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: payos_async-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for payos_async-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 827f0a24d7e3b6ef52b42201e0ede57c54bdb485bf4f88214da74b170506002e
MD5 ac4128ceb11c1562e67acb90702214e5
BLAKE2b-256 a7814e5086a421b14e558ce3f49fc90fec0797cc0d6c7c1186ed50a2f92858f8

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