catcrafts.net/tools/dev-credit.sh
Jorijn van der Graaf e0d3181840
bank: the hourly pull asks nostrod, and holds no bunq key
tools/pull-and-ship-credits.sh ran this repo's own bunq client
(`catcrafts-server --pull-credits`) with the API key from .env: a key
that can initiate payments, where every program run in the checkout
could read it. It now asks nostrod (Nostro) for the account's
transactions over the system bus and turns the answer into the same
credit lines. nostrod lets a program look without a password and lets
nothing pay without one, so the pull holds no credential at all.

- Each run ships the last 90 days (CREDITS_DAYS), the longest window an
  order can wait in, in place of an accumulating local copy fed by a
  reader that only ever saw the newest 50 payments.
- --dry-run prints what would be shipped and ships nothing.
- deploy/README.md: how the pull works now, the polkit rule a timer
  needs to look without a login session, and what the old reader is
  still kept for (tools/register-bunq-webhook.sh, until `nostro
  callbacks` of Nostro 1.1.0 has been seen to list the shop's webhook).

Checked on 2026-11-21 against the production account, through nostrod:
a dry run gave, for each of the 24 incoming payments the old client had
written, a line byte-identical to the old client's. A real run, with
the ship to the server, has not been made, and the timer is installed
nowhere yet.

No C++ changes: the two edits to sources are to comments.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-25 14:21:48 +01:00

57 lines
2.3 KiB
Shell
Executable file

#!/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
# account, by the bunq webhook 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:
# 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'"