All checks were successful
Build and deploy / Validate source (push) Successful in 13s
Build and deploy / Integration suite on a real stack (push) Successful in 8m10s
Build and deploy / Secret scan and release gate (push) Successful in 5s
Build and deploy / Publish images (push) Successful in 1m17s
- A PIX paid in its last minutes after the quote's 24 h, or a card approved
after Mercado Pago's review, was refused ("quote expired before payment"):
money in, no order. A payment whose attempt began while the quote was
valid is now honoured; a card in review keeps its quote, files and the
reconciliation for 72 h.
- A second approved payment of the same cart (a PIX and a card) was taken
for a re-sent approval and logged only. It is now listed on the Kanban as
attention, with the order, for a refund.
- A coupon's uses were counted only when the cart was made, so carts made
before it ran out, or before it was paused or ended, still paid with the
discount. The intent checks it again and asks for a new cart without it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
74 lines
4.2 KiB
Python
74 lines
4.2 KiB
Python
"""Orders made on the Site and never paid (Dropstar's Fase 2, front 9, 05/11/2026).
|
||
|
||
An approved quote is an order the customer did not pay yet: Kanban's
|
||
Cotações › "Aguardando pagamento" lists it, with how long ago it came and
|
||
when it expires, so the seller can charge it by WhatsApp. After
|
||
QUOTE_VALID_HOURS (24 h) without payment it expires on its own: its files
|
||
are freed for deletion and the customer gets an e-mail ("seu pedido expirou,
|
||
suba de novo quando quiser"). Never while a payment attempt is still live
|
||
(a Pix not yet expired, a card being processed), so a late payment always
|
||
finds its files. Números counts orders made, paid and expired, and the
|
||
abandonment rate.
|
||
"""
|
||
from datetime import datetime, timedelta, timezone
|
||
|
||
from . import mail, payments
|
||
from .payments import CARD_REVIEW_HOURS, QUOTE_VALID_HOURS
|
||
|
||
# An intent in one of these no longer waits for money: approved (it became an
|
||
# order), rejected, cancelled, expired or refunded (app/payments.py).
|
||
FINAL_INTENT = ('approved', 'rejected', 'cancelled', 'expired', 'refunded')
|
||
|
||
|
||
def expire(c, origin):
|
||
"""Expire the approved, unpaid quotes past QUOTE_VALID_HOURS: set
|
||
quotes.expired_at, free their files (payments.release_files) and queue the
|
||
customer's e-mail (mail.quote_expired). Returns how many expired."""
|
||
rows = c.execute('''SELECT q.* FROM dtf_local.quotes q
|
||
WHERE q.approved IS NOT NULL AND q.discarded_at IS NULL AND q.expired_at IS NULL
|
||
AND q.approved_at <= now() - make_interval(hours => %s)
|
||
AND NOT EXISTS (SELECT 1 FROM dtf_local.orders o WHERE o.quote_id=q.id)
|
||
AND NOT EXISTS (
|
||
SELECT 1 FROM dtf_local.payment_intents i
|
||
WHERE i.quote_id=q.id
|
||
AND i.id = (SELECT i2.id FROM dtf_local.payment_intents i2
|
||
WHERE i2.quote_id=q.id ORDER BY i2.created_at DESC LIMIT 1)
|
||
AND i.status <> ALL(%s)
|
||
-- a card in review may be approved days later (payments.CARD_REVIEW_HOURS)
|
||
AND i.created_at > now() - make_interval(hours => CASE WHEN i.method='card' THEN %s ELSE %s END)
|
||
AND (i.response->>'expires_at' IS NULL
|
||
OR (i.response->>'expires_at')::timestamptz > now()))
|
||
ORDER BY q.approved_at LIMIT 50 FOR UPDATE OF q SKIP LOCKED''',
|
||
(QUOTE_VALID_HOURS, list(FINAL_INTENT), CARD_REVIEW_HOURS, QUOTE_VALID_HOURS + 1)).fetchall()
|
||
for row in rows:
|
||
c.execute('UPDATE dtf_local.quotes SET expired_at=now() WHERE id=%s', (row['id'],))
|
||
# expired_at is set first: release_files must no longer see this quote
|
||
# as open, or its own files would stay held.
|
||
payments.release_files(c, payments.quote_uploads(row['approved']))
|
||
# Only a quote that just ran out is news to the customer: the backlog
|
||
# from before this front existed expires silently on the first pass.
|
||
if row['approved_at'] > datetime.now(timezone.utc) - timedelta(hours=2 * QUOTE_VALID_HOURS):
|
||
mail.quote_expired(c, row, origin)
|
||
return len(rows)
|
||
|
||
|
||
def figures(c, start, end):
|
||
"""Números, for quotes approved between the local days `start` and `end`
|
||
(inclusive): {'made': n, 'paid': n, 'expired': n, 'waiting': n,
|
||
'abandon_pct': float | None}. Paid has an order; expired has expired_at;
|
||
waiting is neither yet. abandon_pct is expired / (paid + expired) * 100,
|
||
one decimal, None when both are 0."""
|
||
row = c.execute('''SELECT count(*) AS made,
|
||
count(*) FILTER (WHERE EXISTS (SELECT 1 FROM dtf_local.orders o WHERE o.quote_id=q.id)) AS paid,
|
||
count(*) FILTER (WHERE q.expired_at IS NOT NULL) AS expired,
|
||
count(*) FILTER (WHERE q.expired_at IS NULL
|
||
AND NOT EXISTS (SELECT 1 FROM dtf_local.orders o WHERE o.quote_id=q.id)) AS waiting
|
||
FROM dtf_local.quotes q
|
||
WHERE q.discarded_at IS NULL AND q.approved_at IS NOT NULL
|
||
AND (q.approved_at AT TIME ZONE 'America/Sao_Paulo')::date BETWEEN %s AND %s''',
|
||
(start, end)).fetchone()
|
||
paid, expired = row['paid'], row['expired']
|
||
decided = paid + expired
|
||
return {'made': row['made'], 'paid': paid, 'expired': expired, 'waiting': row['waiting'],
|
||
'abandon_pct': round(expired / decided * 100, 1) if decided else None}
|