payment/payment_backend/app.py

125 lines
4.5 KiB
Python
Raw Permalink Normal View History

2024-11-19 12:07:35 +00:00
import os
import subprocess
import sys
2024-11-21 09:28:14 +00:00
from flask import Flask, request, jsonify
2024-11-19 12:07:35 +00:00
from flask_cors import CORS
2024-11-21 09:28:14 +00:00
from loguru import logger
from config import get_config
from services.order import OrderService
2024-11-13 08:52:05 +00:00
from services.user import UserService
2024-11-19 12:07:35 +00:00
from utils.datetime import parse_time_string, to_milliseconds
2024-11-21 09:28:14 +00:00
from utils.proj import filter_empty
2024-11-19 12:07:35 +00:00
app = Flask('app')
CORS(app, resources={r"/*": {"origins": "http://localhost:8080"}})
config = get_config()
2024-11-19 12:07:35 +00:00
config.order.lifetime = to_milliseconds(**parse_time_string(config.order.lifetime))
order_service = OrderService(config)
user_service = UserService(config)
2024-11-19 12:07:35 +00:00
@app.after_request
def add_cors_headers(response):
response.headers['Access-Control-Allow-Origin'] = 'http://127.0.0.1:8080' # 具体的来源
# response.headers['Access-Control-Allow-Credentials'] = 'true' # 如果需要凭据支持
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
response.headers['Access-Control-Allow-Headers'] = 'Content-Type, Authorization'
return response
2024-11-13 08:52:05 +00:00
@app.route('/createOrder', methods=['POST'])
def create_order():
data = request.get_json()
2024-11-21 09:28:14 +00:00
name = filter_empty(data.get('name', None))
phone = filter_empty(data.get('phone', None))
email = filter_empty(data.get('email', None))
payment_method = filter_empty(data.get('paymentMethod', None))
if payment_method is None or payment_method not in config['PaymentAddresses']:
return jsonify({
2024-11-19 12:07:35 +00:00
"message": "wrong payment method"
}), 400
2024-11-20 01:49:25 +00:00
quant = int(data.get('amount', 0))
if quant < config.order.get_int("min_quant"):
2024-11-19 12:07:35 +00:00
return jsonify({
"message": "Amount below minimum limit."
}), 400
2024-11-13 09:23:59 +00:00
2024-11-21 09:28:14 +00:00
wallet_address = filter_empty(data.get('wallet_address', None))
2024-11-19 12:07:35 +00:00
if wallet_address is None:
2024-11-21 09:28:14 +00:00
logger.debug('wallet address not provided')
wallet_addresses = user_service.get_wallet_addresses(name, phone, email, payment_method)
2024-11-19 12:07:35 +00:00
if not wallet_addresses:
2024-11-21 09:28:14 +00:00
logger.debug('wallet address are not found')
2024-11-13 08:52:05 +00:00
return jsonify({
2024-11-19 12:07:35 +00:00
"message": "empty wallet address"
2024-11-13 08:52:05 +00:00
}), 400
2024-11-19 12:07:35 +00:00
if len(wallet_addresses) > 1:
2024-11-21 09:28:14 +00:00
logger.debug('multiple wallet addresses')
2024-11-13 08:52:05 +00:00
return jsonify({
"message": "Please select an address to place your order.",
2024-11-19 12:07:35 +00:00
"wallet_addresses": wallet_addresses
2024-11-13 08:52:05 +00:00
}), 200
# 单个地址
2024-11-19 12:07:35 +00:00
wallet_address = wallet_addresses[0]
else:
user_service.update_or_create(name, phone, email, wallet_address, payment_method)
2024-11-21 09:28:14 +00:00
logger.debug(f'wallet address: {wallet_address}')
2024-11-20 17:09:07 +00:00
order_id, create_timestamp = order_service.create_order(name=name, phone=phone, email=email,
quant=quant, payment_method=payment_method,
wallet_address=wallet_address)
2024-11-13 08:52:05 +00:00
2024-11-19 12:07:35 +00:00
return jsonify({"orderID": order_id,
"amount": quant,
"orderCreateTimestamp": create_timestamp,
"orderExpirationTime": config.order.lifetime}), 200
2024-11-20 17:09:07 +00:00
@app.route('/queryOrder', methods=['POST'])
def query_order():
data = request.get_json()
2024-11-21 09:28:14 +00:00
name = filter_empty(data.get('name', None))
phone = filter_empty(data.get('phone', None))
email = filter_empty(data.get('email', None))
wallet_address = filter_empty(data.get('wallet_address', None))
2024-11-20 17:09:07 +00:00
page = data.get('page', 1) # 获取页码,默认为第 1 页
page_size = data.get('page_size', 10)
offset = (page - 1) * page_size
limit = page_size
2024-11-22 03:54:56 +00:00
if name or phone or email or wallet_address:
orders, order_count = order_service.query_order(name, phone, email, wallet_address, offset, limit)
else:
orders, order_count = [], 0
return jsonify({"orders": orders, "totalOrders": order_count}), 200
2024-11-20 17:09:07 +00:00
2024-11-13 09:44:45 +00:00
order_status = config.order.order_status
2024-11-19 12:07:35 +00:00
2024-11-13 09:44:45 +00:00
@app.route('/finishOrder', methods=['GET'])
def finish_order():
2024-11-20 17:09:07 +00:00
order_id = request.args.get('orderID', None)
logger.debug(f"complete oreder: {order_id}")
2024-11-13 09:44:45 +00:00
status = order_service.finish_order(order_id)
return jsonify({"order_id": order_id,
"status": status,
"msg": order_status[status]}), 200
2024-11-19 12:07:35 +00:00
def run_gunicorn():
# Gunicorn 的命令和参数
command = [sys.executable, '-m', 'gunicorn', 'app:app']
# 启动 Gunicorn
subprocess.run(command)
if __name__ == '__main__':
2024-11-19 12:07:35 +00:00
if os.name == 'nt':
app.run(debug=True, port=5000, host='0.0.0.0')
elif os.name == 'posix':
run_gunicorn()