142 lines
4.0 KiB
Python
142 lines
4.0 KiB
Python
# from flask import Blueprint, request, jsonify
|
||
from flask import Blueprint, request, jsonify, send_from_directory
|
||
import os
|
||
import sqlite3
|
||
import json
|
||
# from services.rokky import ones
|
||
from API.rok import create_order
|
||
from API.TG import send_telegram
|
||
from services.orders import get_sku
|
||
import logging
|
||
|
||
|
||
main = Blueprint("main", __name__)
|
||
DB_PATH = "./files/rokky.db"
|
||
|
||
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||
IMAGE_FOLDER = os.path.join(BASE_DIR, 'images')
|
||
|
||
@main.route("/")
|
||
def index():
|
||
return "Hello wstkeys!!!"
|
||
|
||
@main.route("/images/<filename>")
|
||
def get_image(filename):
|
||
return send_from_directory(IMAGE_FOLDER, filename)
|
||
|
||
@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)
|
||
db_sku, db_price = get_sku(sku)
|
||
create_order(db_sku, price, db_price, f"my-test-order-{product_id}")
|
||
# 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}) |