douyinpay
抖音支付 Python SDK
Python library for integrating Douyin Pay (Chinese TikTok's payment system) into your backend. Handles API calls, signing, encryption, certificates, and payment notifications automatically.
This is a Python library for backend developers who need to integrate with Douyin Pay, the payment system of Douyin, which is the Chinese version of TikTok. The README is written in Chinese. The library handles the API calls, signing, encryption, and certificate management that the Douyin Pay platform requires.
Installing the library and creating a client requires a merchant ID, an API certificate serial number, a private key, and an encryption key, all of which are obtained from the Douyin merchant platform. Once configured, the client provides methods for creating payment orders across several payment types: app payments, H5 browser payments, JSAPI payments for mini-programs, and native QR code payments. It also supports querying and closing orders, processing refunds, downloading billing records, splitting revenue among multiple parties (profit sharing), and transferring funds directly to a user's Douyin wallet.
The library supports both synchronous and asynchronous usage. Both clients share the same method names and parameters, so switching between them is straightforward. Async calls require only an added await keyword. A context manager interface is available for both modes to ensure connections are closed properly.
For security, the library supports two signing algorithms: RSA and the Chinese national cryptography standard SM2. Choosing between them is a matter of passing the appropriate value in the config. The library also manages platform certificates automatically, downloading, decrypting, caching, and refreshing them without any manual steps.
When Douyin sends a payment notification to your server, the included parser verifies the signature and decrypts the payload, then raises specific exception types for signature failures or decryption errors so that you can return the correct HTTP status codes as required by the platform. Error handling also covers API business errors and network failures as distinct exception classes.
Where it fits
- Add Douyin Pay checkout to a Chinese e-commerce app or mini-program so customers can pay via QR code, H5, or in-app.
- Handle refunds and query order status automatically in a backend service that sells to Douyin users.
- Split revenue between multiple parties after a sale, such as a marketplace paying out sellers.
- Receive and securely verify payment notifications from Douyin, returning the correct status codes the platform requires.