Files
gramsrv/deploy/migrations/0215_bot_api_pre_checkout_shape.down.sql
SandyTAP e0fc067ff7 feat(botapi): gate bot invoice payments on pre_checkout_query
answerPreCheckoutQuery answered 501 and no pre_checkout_query was ever created,
so a bot could not refuse an order before the payer was charged. The whole gate
was missing, not just the method.

The payer is now held in sendStarsForm/sendPaymentForm for at most 10 seconds -
Telegram's window - while the bot answers. Approval charges the Stars, refusal
cancels them and shows the bot's error_message to the payer, and silence fails the
payment. Silence never degrades into charging anyway: a bot that does not answer
means a refusal, or the gate would verify nothing.

The gate runs before the invoice is marked settled. Settling first would leave a
declined invoice flagged as paid, and the retry would then hand back a receipt with
no money behind it. An already settled invoice skips the question, so a replayed
payment form neither re-asks the bot nor re-charges.

Only the bot that owns the order can answer, and a stale query id gets
QUERY_ID_INVALID rather than a false ok, since a bot told "ok" for an answer that
landed nowhere would assume it approved a payment.

answerShippingQuery stays blocked, now as METHOD_NOT_FOUND rather than the old
BLOCKED_DURABLE_QUERY_STATE_MISSING marker: a Stars payment has no shipping step,
so there is nothing to answer.

Also fixes an unrelated test defect that made the suite non-idempotent: the bot
invoice store test hardcoded a charge id while its users were unique per run, so
the unique charge index collided with every earlier run in a shared database.
2026-09-30 20:30:13 +03:00

52 lines
2.3 KiB
SQL

DROP INDEX IF EXISTS public.bot_api_updates_pre_checkout_unique;
ALTER TABLE public.bot_api_updates
DROP CONSTRAINT bot_api_updates_kind_check,
DROP CONSTRAINT bot_api_updates_peer_type_check,
DROP CONSTRAINT bot_api_updates_peer_id_check,
DROP CONSTRAINT bot_api_updates_message_id_check,
DROP CONSTRAINT bot_api_updates_callback_shape_check;
ALTER TABLE public.bot_api_updates
ADD CONSTRAINT bot_api_updates_kind_check CHECK (
update_kind IN ('message', 'edited_message', 'callback_query')
);
ALTER TABLE public.bot_api_updates
ADD CONSTRAINT bot_api_updates_peer_type_check CHECK (
peer_type IN ('user', 'channel')
OR (update_kind = 'callback_query' AND peer_type = '')
),
ADD CONSTRAINT bot_api_updates_peer_id_check CHECK (
peer_id > 0
OR (update_kind = 'callback_query' AND peer_id = 0)
),
ADD CONSTRAINT bot_api_updates_message_id_check CHECK (
message_id > 0
OR (update_kind = 'callback_query' AND message_id = 0)
),
ADD CONSTRAINT bot_api_updates_callback_shape_check CHECK (
(update_kind = 'callback_query'
AND callback_query_id <> 0
AND callback_user_id > 0
AND callback_chat_instance <> 0
AND COALESCE(octet_length(callback_data), 0) <= 64
AND source_pts = 0
AND (
(peer_type IN ('user', 'channel') AND peer_id > 0 AND message_id > 0
AND callback_inline_dc_id = 0 AND callback_inline_owner_id = 0
AND callback_inline_message_id = 0 AND callback_inline_access_hash = 0)
OR
(peer_type = '' AND peer_id = 0 AND message_id = 0
AND callback_inline_dc_id > 0 AND callback_inline_owner_id > 0
AND callback_inline_message_id > 0 AND callback_inline_access_hash <> 0)
))
OR
(update_kind IN ('message', 'edited_message')
AND peer_type IN ('user', 'channel') AND peer_id > 0 AND message_id > 0
AND callback_query_id = 0 AND callback_user_id = 0
AND callback_chat_instance = 0 AND callback_data IS NULL
AND callback_inline_dc_id = 0 AND callback_inline_owner_id = 0
AND callback_inline_message_id = 0 AND callback_inline_access_hash = 0)
);