main.py

 avatar
unknown
python
2 years ago
1.9 kB
5
Indexable
from flask import Flask, request, jsonify
import json
import hmac
import hashlib
import requests

app = Flask(__name__)
secret_key = b"woltpeopleareretarded"


def verify_hmac(data, received_hmac):
    calculated_hmac = hmac.new(
        secret_key, data.encode("utf-8"), hashlib.sha256
    ).hexdigest()
    return hmac.compare_digest(calculated_hmac, received_hmac)


def process_wolt_notification(order_id, order_api_key):
    wolt_url = "https://pos-integration-service.wolt.com/orders/{{order_id}}"
    # wolt_url = f"http://127.0.0.1/orders/{order_id}"

    headers = {"WOLT-API-KEY": order_api_key}
    response = requests.get(wolt_url, headers=headers)

    try:
        response_json = response.json()
        print(
            f"JSON response from Wolt API for order {order_id}:\n{jsonify(response_json)}"
        )
    except json.JSONDecodeError:
        print(f"Non-JSON response from Wolt API for order {order_id}:\n{response.text}")


@app.route("/webhook", methods=["POST"])
def receive_json():
    data = request.get_data(as_text=True)
    received_hmac = request.headers.get("Authorization")

    if verify_hmac(data, received_hmac):
        try:
            json_data = request.get_json()
            print(f"Received JSON: {json_data}")

            order_id = json_data["order"]["id"]
            status = json_data["order"]["status"]
            order_api_key = "SOMERITARTKEY"

            if status in ["PRODUCTION", "production"]:
                process_wolt_notification(order_id, order_api_key)
                return jsonify({"success": True})

            else:
                print(f"Order {order_id} is not in PRODUCTION status. NEXT.")
                return jsonify({"success": True})

        except Exception as e:
            return jsonify({"error": str(e)}), 400
    else:
        return jsonify({"error": "Invalid HMAC"}), 401


if __name__ == "__main__":
    app.run(port=5000)
Editor is loading...
Leave a Comment