This project integrates VNPAY with FastAPI to handle online payments.
- Install Python 3.9+.
- Create a virtual environment:
python -m venv venv source venv/bin/activate # On macOS/Linux venv\Scripts\activate # On Windows
- Install required dependencies:
pip install -r requirements.txt
- Set up the
.env
file:VNPAY_URL=<VNPAY URL> VNPAY_TMN_CODE=<TMN Code> VNPAY_HASH_SECRET=<Secret Key> VNPAY_RETURN_URL=<Callback URL>
- Run the application with FastAPI:
uvicorn main:app --reload
Access the VNPAY Sandbox Page
- Register an account by providing your email and necessary details.
- After successful registration, you will receive the required credentials:
- vnp_TmnCode: Test merchant site code.
- vnp_HashSecret: Secret key for data encryption.
- API Endpoint: Test payment API endpoint.
GET /create_payment?amount=100000&order_desc=Order Payment
GET /vnpay_return
- Receive transaction results from VNPAY
sequenceDiagram
participant User
participant Website
participant VNPAY
User->>Website: 1. Request payment
Website->>VNPAY: 2. Send transaction request
VNPAY-->>Website: 3. Return payment URL
Website->>User: 4. Redirect to VNPAY
User->>VNPAY: 5. Enter payment details
VNPAY-->>Website: 6. Send transaction result
Website->>User: 7. Display transaction result
Request Payment
Server redirects to VNPAY's payment URL
Enter Card Information
Payment Result
Modify the logic to match your application needs
If you encounter any issues, feel free to reach out for support!