"""Merchant API signing: Python 3. No dependencies. Server-side only.""" import hashlib def sign(params: dict, api_key: str) -> str: def scalar(value): if value is None or value is False: return "" if value is True: return "1" if isinstance(value, (dict, list, tuple)): raise ValueError("Only scalar values are supported") return str(value) pairs = [f"{key}={scalar(value)}" for key, value in sorted(params.items()) if key not in ("EncryptValue", "sign")] raw = "&".join(pairs) + "&HashKey=" + api_key # Match PHP strtolower: lowercase ASCII bytes only, preserving UTF-8 text. return hashlib.sha256(raw.encode("utf-8").lower()).hexdigest().upper() # Use strings for decimal amounts and opaque product codes, integer seconds for t. # params["EncryptValue"] = sign(params, os.environ["STARLINK_API_KEY"]) # Send with Content-Type: application/json and Accept: application/json.