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 by
# `catcrafts-server --pull-credits` reading the real account; 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'"