import requests
from config import PAYMENT_URL, VERIFY_URL, ZARINPAL_MERCHANT, CALLBACK_URL, GATEWAY_START

def create_payment(amount=150000, description="ثبت آگهی", mobile="", email=""):
    data = {
        "merchant_id": ZARINPAL_MERCHANT,
        "amount": amount,
        "callback_url": CALLBACK_URL,
        "description": description,
    }
    if mobile: data["mobile"] = mobile
    if email: data["email"] = email

    try:
        r = requests.post(PAYMENT_URL, json=data, timeout=10)
        result = r.json()
        if result.get("data") and result["data"]["code"] == 100:
            authority = result["data"]["authority"]
            return True, authority, GATEWAY_START + authority
        else:
            return False, result.get("errors", "خطا در اتصال به درگاه"), None
    except:
        return False, "خطا در ارتباط با درگاه", None