Python SDK
Trang thanh toán payOS
Khám phá mã nguồn mẫu hoàn chỉnh được phát triển bằng Framework Django. Mã nguồn server-side chuyển hướng đến trang thanh toán do payOS phát triển. Trước khi bắt đầu, hãy tạo một kênh thanh toán trên trang payOS.
Tài liệu
Xem tài liệu API payOS để biết thêm thông tin.
Cài đặt thư viện payOS cho dự án Python
Cài đặt thư viện với pip:
# Windows
pip install payos
# Linux/macOS
pip3 install payos
Khởi tạo đối tượng PayOS
Bạn cần khởi tạo đối tượng PayOS
bằng client_id
, api_key
và checksum_key
của cổng thanh toán mà bạn đã tạo.
Xem hướng dẫn
from payos import PayOS
import os
client_id = os.environ.get('PAYOS_CLIENT_ID')
api_key = os.environ.get('PAYOS_API_KEY')
checksum_key = os.environ.get('PAYOS_CHECKSUM_KEY')
payOS = PayOS(client_id=client_id, api_key=api_key, checksum_key=checksum_key)
Các phương thức có trong đối tượng PayOS
Phương thức | Kiểu tham số đầu vào | Mô tả | Kiểu trả về |
---|---|---|---|
createPaymentLink | PaymentData | Tạo link thanh toán cho dữ liệu đơn hàng | CreatePaymentResult |
getPaymentLinkInformation | Union[str, int] | Lấy thông tin thanh toán của đơn hàng đã tạo link thanh toán. | PaymentLinkInformation |
cancelPaymentLink | Union[str, int] , str | Hủy link thanh toán của đơn hàng. | PaymentLinkInformation |
confirmWebhook | str | Xác thực URL Webhook của kênh thanh toán và thêm hoặc cập nhật URL Webhook cho Kênh thanh toán đó nếu thành công | str |
verifyPaymentWebhookData | json | Xác minh dữ liệu nhận được qua webhook sau khi thanh toán | WebhookData |
Phương thức createPaymentLink
Tạo link thanh toán cho dữ liệu đơn hàng với đầu vào là paymentData
có kiểu PaymentData
và trả về CreatePaymentResult
.
Xem hướng dẫn
from payos import PayOS, ItemData, PaymentData
payOS = PayOS(client_id=client_id, api_key=api_key, checksum_key=checksum_key)
item = ItemData(name="Mì tôm hảo hảo ly", quantity=1, price=1000)
paymentData = PaymentData(orderCode=11, amount=1000, description="Thanh toan don hang",
items=[item], cancelUrl="http://localhost:8000", returnUrl="http://localhost:8000")
paymentLinkData = payOS.createPaymentLink(paymentData = paymentData)
Phương thức getPaymentLinkInformation
Lấy thông tin thanh toán của đơn hàng đã tạo link thanh toán với đầu vào là orderId
có kiểu Union[str, int]
và trả về PaymentLinkInformation
Xem hướng dẫn
from payos import PayOS
payOS = PayOS(client_id=client_id, api_key=api_key, checksum_key=checksum_key)
orderId = 1
paymentLinkInfo = payOS.getPaymentLinkInformation(orderId = orderId)
Phương thức cancelPaymentLink
Hủy link thanh toán của đơn hàng với đầu vào gồm 2 trường là orderId
có kiểu Union[str, int]
và cancellationReason
,
ta có thể bỏ qua trường cancellationReason
có kiểu str
. Phương thức này sẽ trả về PaymentLinkInformation
.
Xem hướng dẫn
from payos import PayOS
payOS = PayOS(client_id=client_id, api_key=api_key, checksum_key=checksum_key)
orderId = 1
paymentLinkInfo = payOS.cancelPaymentLink(orderId = orderId)
Phương thức confirmWebhook
Xác thực URL Webhook của kênh thanh toán và thêm hoặc cập nhật URL Webhook cho Kênh thanh toán. Có đầu vào là webhookUrl
có kiểu là str
và trả về kiểu str
.
Xem hướng dẫn
from payos import PayOS
payOS = PayOS(client_id=client_id, api_key=api_key, checksum_key=checksum_key)
payOS.confirmWebhook("https://your-webhook-url/")