All checks were successful
Build and deploy / Validate source (push) Successful in 5s
Build and deploy / Integration suite on a real stack (push) Successful in 3m14s
Build and deploy / Secret scan and release gate (push) Successful in 6s
Build and deploy / Publish images (push) Successful in 55s
Production approved a test card while every notification was refused by signature, and the order never appeared. payments.reconcile() asks the provider about the payments of open quotes and applies its answer through the same path as the webhook: the amount must match the reviewed quote, and the event id (payment id and status) makes a notice and a lookup one delivery, applied once. The payment page's polling checks an approved card at once; the worker checks every minute for the rest, such as a PIX paid after the page was closed. The webhook stays; it is no longer the only way in. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
72 lines
3.5 KiB
Python
72 lines
3.5 KiB
Python
"""An approved payment becomes the order even when its notification never arrives.
|
|
|
|
Run inside the API container: python -m tests.reconcile_test
|
|
"""
|
|
from uuid import uuid4
|
|
|
|
from psycopg.types.json import Jsonb
|
|
|
|
from app import payments
|
|
from app.core import db
|
|
|
|
|
|
class Provider:
|
|
"""Answers lookups as Mercado Pago would; counts them."""
|
|
name = 'mercadopago'
|
|
|
|
def __init__(self, payment):
|
|
self.payment, self.asked = payment, 0
|
|
|
|
def lookup(self, payment_id):
|
|
self.asked += 1
|
|
assert payment_id == str(self.payment['id'])
|
|
return self.payment
|
|
|
|
|
|
def run():
|
|
quote, payment_id = uuid4(), str(900_000_000 + uuid4().int % 99_999_999)
|
|
approved = {'customer': {'mail': 'reconcile@example.test'}, 'items': [], 'total_cents': 1490}
|
|
order_id = None
|
|
try:
|
|
with db.connect() as c:
|
|
c.execute('''INSERT INTO dtf_local.quotes(id,owner,request_key,request_hash,draft,approved,approved_at)
|
|
VALUES(%s,%s,%s,'reconcile-fixture',%s,%s,now())''',
|
|
(quote, uuid4(), uuid4(), Jsonb({'customer': approved['customer'], 'items': []}), Jsonb(approved)))
|
|
c.execute('''INSERT INTO dtf_local.payment_intents(id,quote_id,provider,provider_payment_id,method,status,amount_cents,response)
|
|
VALUES(%s,%s,'mercadopago',%s,'card','approved',1490,'{}')''', (uuid4(), quote, payment_id))
|
|
|
|
# Paid less than owed: no order, and nothing applied twice.
|
|
short = Provider({'id': payment_id, 'status': 'approved', 'currency_id': 'BRL',
|
|
'transaction_amount': 14.0, 'external_reference': str(quote)})
|
|
assert payments.reconcile(db.connect, short, quote_id=quote) == ['refused: paid 1400 but quote total is 1490']
|
|
assert payments.reconcile(db.connect, short, quote_id=quote) == []
|
|
|
|
# The provider is asked; its approval of the right amount is the order.
|
|
with db.connect() as c:
|
|
c.execute("DELETE FROM dtf_local.payment_events WHERE provider='mercadopago' AND event_id=%s",
|
|
(f'{payment_id}:approved',))
|
|
paid = Provider({'id': payment_id, 'status': 'approved', 'currency_id': 'BRL',
|
|
'transaction_amount': 14.9, 'external_reference': str(quote)})
|
|
outcomes = payments.reconcile(db.connect, paid, quote_id=quote)
|
|
assert len(outcomes) == 1 and outcomes[0].startswith('order '), outcomes
|
|
with db.connect() as c:
|
|
order_id = c.execute('SELECT id FROM dtf_local.orders WHERE quote_id=%s', (quote,)).fetchone()['id']
|
|
# With the order made, the quote is no longer asked about.
|
|
asked = paid.asked
|
|
assert payments.reconcile(db.connect, paid, quote_id=quote) == [] and paid.asked == asked
|
|
print('PASS: an approved payment is found by asking the provider; a short one is refused; each applies once')
|
|
finally:
|
|
with db.connect() as c:
|
|
if order_id:
|
|
c.execute("DELETE FROM dtf_local.outbox WHERE event_key LIKE %s", (f'{order_id}:%',))
|
|
c.execute('DELETE FROM dtf_local.print_files WHERE order_id=%s', (order_id,))
|
|
c.execute('DELETE FROM dtf_local.orders WHERE id=%s', (order_id,))
|
|
c.execute("DELETE FROM dtf_local.payment_events WHERE provider='mercadopago' AND event_id LIKE %s",
|
|
(f'{payment_id}:%',))
|
|
c.execute('DELETE FROM dtf_local.payment_intents WHERE quote_id=%s', (quote,))
|
|
c.execute('DELETE FROM dtf_local.quotes WHERE id=%s', (quote,))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
run()
|