#!/usr/bin/env python3
"""/mancgi/snapexpaypayment?elid=<payment>: BILLmanager sends the client here; create the SnapEX invoice and redirect."""

import os
import sys

sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "lib", "python"))

import snapexpay  # noqa: E402
from snapexpay import gateway, web  # noqa: E402

params = web.query()
payment_id = web.int_param(params, "elid")
site = web.site()
try:
    web.redirect(gateway.start(payment_id, site))
except gateway.UserError as err:
    web.page(422, str(err), site + "/billmgr")
except Exception as err:
    snapexpay.logger().exception("payment %d: %s", payment_id, err)
    web.page(500, "Could not start the crypto payment. Please try again later.", site + "/billmgr")
