wstkeys/app/routes.py

130 lines
3.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from flask import Blueprint, request, jsonify
import sqlite3
import json
# from services.rokky import ones
# from API.rok import get_order_content
from API.TG import send_telegram
import logging
main = Blueprint("main", __name__)
DB_PATH = "./files/rokky.db"
@main.route("/")
def index():
return "Hello wstkeys!!!"
@main.route("/orders/api_payments", methods=["POST", "GET"])
def index1():
# Получаем все GET-параметры
params = request.args.to_dict()
logging.warn(f"newOrder: {params}")
# Просто выводим на экран (в ответ клиенту)
# парсим данные
sku = params.get("id_d")
product_id = params.get("id_i")
email = params.get("email")
price = params.get("amount")
# всё остальное сохраняем как JSON
content = json.dumps(params, ensure_ascii=False)
send_telegram(content)
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
try:
cursor.execute("""
INSERT INTO orders (sku, product_id, email, price, content)
VALUES (?, ?, ?, ?, ?)
""", (sku, product_id, email, price, content))
conn.commit()
except Exception as e:
conn.close()
return jsonify({"error": str(e)}), 500
conn.close()
return jsonify({"success": True}), 200
# return "Hello GET"
@main.route("/sales", methods=["POST", "GET"])
def index2():
return "Hello GET!!!++!!ss"
@main.route("/newOrder", methods=["POST"])
def new_order():
if not request.is_json:
return jsonify({"error": "Invalid content type"}), 400
data = request.get_json()
print(data)
order_id = data.get("orderId")
if order_id is None:
return jsonify({"error": "orderId is required"}), 400
if not isinstance(order_id, int):
return jsonify({"error": "orderId must be int"}), 400
# TODO: обработка заказа
print(f"New order received: {order_id}")
send_telegram(data)
# logging.warn(get_order_content(order_id))
logging.warn(f"newOrder: {order_id}")
return jsonify({"status": "ok"}), 200
@main.route("/skuChanged", methods=["POST"])
def sku():
if not request.is_json:
return jsonify({"error": "Invalid content type"}), 400
data = request.get_json()
print(data)
order_id = data.get("orderId")
if order_id is None:
return jsonify({"error": "orderId is required"}), 400
if not isinstance(order_id, int):
return jsonify({"error": "orderId must be int"}), 400
# TODO: обработка заказа
print(f"New order received: {order_id}")
send_telegram(data)
# logging.warn(get_order_content(order_id))
logging.warn(f"NEWsku: {order_id}")
return jsonify({"status": "ok"}), 200
@main.route("/product")
def product_update():
# получаем параметры
sku = request.args.get("rokky")
ggsale = request.args.get("ggsale")
if not sku or not ggsale:
return jsonify({"error": "Missing parameters"}), 400
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
# проверяем есть ли продукт с таким SKU
cursor.execute("SELECT id FROM products WHERE sku=?", (sku,))
row = cursor.fetchone()
if not row:
conn.close()
return jsonify({"error": "SKU not found"}), 404
# обновляем поле gg
cursor.execute("UPDATE products SET gg=? WHERE sku=?", (ggsale, sku))
conn.commit()
conn.close()
return jsonify({"success": True, "sku": sku, "ggsale": ggsale})