catcrafts.net/tools/dev-credit.sh

57 lines
2.3 KiB
Shell
Raw Normal View History

2026-08-20 20:15:47 +02:00
#!/bin/sh
# Pretend the bank reported an incoming transfer, so a dev order can be paid
# without a bank.
#
# The transfer rail settles an order when a credit quoting its reference shows
# up in the credits file. In production that file is filled from the real
bank: Nostro reads the bank's callback, and the shop's bunq code is gone The shadow run of 36a635f had Nostro's ReadCallback read a real callback beside our own parser on 2026-11-25, for order CC-BCD155: the same id, amount and reference, on the shop's own account. So the reader takes over, and what this repo carried of bunq goes: the callback parser, the payment-type table, the API client with its RSA signing, `--pull-credits`, the mode that read the bank from the server with an API key, and tools/register-bunq-webhook.sh. The server now knows no bank. Catcrafts.Server-BankCallback.cpp is the one unit that imports Nostro.Money: ReadBankCallback has a body read and says what it comes to, CreditMethodFor says how a payment arrived. Which bank it is is configuration: --banks= in project.cpp for what the build can read, BANK_CALLBACK_BANK on the server for what arrives. What changes for a running shop: - The endpoint is configured with BANK_CALLBACK_PATH and BANK_CALLBACK_BANK; BUNQ_WEBHOOK_PATH is no longer read. Without the bank's name, or without TRANSFER_IBAN, the endpoint stays off and the startup log says so: one that answered 200 and credited nothing would look like working. Production's payments.env got both names before this was pushed. - Only a payment on TRANSFER_IBAN is credited. A bank posts to whatever address any of its customers registered, so a callback about someone else's account can be the bank's own. Until now a payment in a callback was credited whichever account it was on. - `via` says `internal` for a payment from another account at the bank itself, where it said `bunq`, and `card` where it would have said `MASTERCARD`. A kind without a word is still passed on as the bank wrote it, now only where that is a plain word. - Every callback leaves one line in the log, `bank callback: credited ...` or `bank callback: nothing credited: <why>`, with nothing of the body in it beyond the credit. - A line of the credits file may carry the bank's kind and the bank (`"bank":"bunq","kind":"EBA_SCT/SCT"`) in place of `method`, put into the ledger's words when the file is read. The pull script ships those and takes the bank's name from nostrod, so it holds no table of a bank's words either. Also gone, with their last user: ParseAmountToMinor, PullCreditsInto and the explicit -lcrypto (Crafter.Network names what it needs). ShouldReadBankCallbacks replaces ShouldParseBunqPayments and ShouldCompareCallbackReaders: what a callback comes to for each shape, each method, the account and the sign, and that no note repeats anything a body carried. ShouldReceiveBankCallbacks posts to the real endpoint: an order settles from a callback, one from another account quoting the same order does not pay for it, and a server without the bank's name has no endpoint. Built from scratch and run in a copy of this tree with crafter-build 6.4.1: the server and the wasm bundle build, and every suite passes but ShouldServePostPages, which fails there for want of mirrored post media and passed in CI on the commit before this. A dry run of the pull script against nostrod gave 19 credits with their kinds. Not tried: a real callback through the new handler, and `nostro callbacks` against the registration that is in place, which was made with the script this removes. deploy/README.md has the new setup. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-25 15:08:42 +01:00
# account, by the bank's callback and by tools/pull-and-ship-credits.sh; here it is
# filled by hand. Same file, same format, same matching code — which is the point:
2026-08-20 20:15:47 +02:00
# this exercises the real settlement path rather than a test double.
#
# tools/dev-credit.sh <orders-file> <reference> <amount-in-cents> [method]
#
# Example, paying order CC-2B6457 the €570.43 it is waiting for:
# tools/dev-credit.sh /tmp/dev/orders.jsonl CC-2B6457 57043
#
# The reference is matched forgivingly (case, spacing and punctuation are
# ignored, and the RF… form works too), so it is worth deliberately mangling it
# to watch that hold:
# tools/dev-credit.sh /tmp/dev/orders.jsonl "betaling cc 2b6457 bedankt" 57043
set -eu
if [ "$#" -lt 3 ]; then
echo "usage: $0 <orders-file> <reference> <amount-in-cents> [method]" >&2
echo " method defaults to sepa; try 'ideal' or 'card' to see the ledger's" >&2
echo " via column change, which is what decides if an order is safe to ship." >&2
exit 2
fi
ORDERS="$1"
REFERENCE="$2"
CENTS="$3"
METHOD="${4:-sepa}"
CREDITS="$ORDERS.transfer-credits.jsonl"
case "$CENTS" in
''|*[!0-9]*) echo "$0: amount must be whole cents, got '$CENTS'" >&2; exit 2 ;;
esac
# A unique id per line, because the rail deduplicates on it: appending the same
# id twice is deliberately a no-op, which is what stops a repeated pull from
# double-crediting an order. Using a counter keeps each hand-made credit
# distinct without needing a clock.
n=1
if [ -f "$CREDITS" ]; then
n=$(( $(wc -l < "$CREDITS") + 1 ))
fi
# Escape the two characters that would break the line-per-record format. The
# reference is free text on purpose so it can be mangled realistically.
escaped=$(printf '%s' "$REFERENCE" | sed 's/\\/\\\\/g; s/"/\\"/g')
printf '{"id":"dev-%s","reference":"%s","amount_minor":%s,"method":"%s"}\n' \
"$n" "$escaped" "$CENTS" "$METHOD" >> "$CREDITS"
echo "credited $CENTS cents quoting '$REFERENCE' (method $METHOD)"
echo "wrote $CREDITS"
echo "the reconciler sweeps about once a minute; watch the dev log for 'paid'"