Skip to content
Last updated

9. Exception Handling Best Practices

def handle_payment_with_retry(order_id, amount):
    try:
        payment = client.create_payment(order_id, "TTRON", "USDT")
    except requests.HTTPError as exc:
        error = exc.response.json()
        code = error.get("code")

        if code == 40906:
            # Order expired, create new order
            order = client.create_order(amount)
            payment = client.create_payment(order["order_id"], "TTRON", "USDT")
        elif code == 40904:
            # Temporary error, retry after delay
            time.sleep(30)
            payment = client.create_payment(order_id, "TTRON", "USDT")
        elif code == 46001:
            # Buyer must complete Infini login + approved KYC before payment creation
            order = client.request("GET", f"/v1/acquiring/order?order_id={order_id}")
            if order.get("kyc_required"):
                raise BuyerActionRequired("Redirect buyer to hosted checkout or authenticated KYC flow")
            raise
        elif code == 40007:
            # Unsupported currency
            currencies = client.get_currencies()
            raise UnsupportedCurrency(currencies)
        else:
            raise
    return payment

POST /v1/acquiring/payment now reuses a compatible active payment by default instead of failing with a duplicate-payment business code. Send force_create=true only when you explicitly need a fresh address rather than the reusable payment.