diff --git a/.env.example b/.env.example index f307066..1cc0147 100644 --- a/.env.example +++ b/.env.example @@ -63,3 +63,5 @@ RUNNER_REGISTRATION_TOKEN= # installed from the [uhhm] Arch registry. gdo hands the vel's mail to # the host's mail server; on kasse that is Klingenberg Bygg's postfix. GDO_RELEASE=v0.2.0 +# kron, the money service: a bendik/kron tag on Docker Hub. +KRON_RELEASE=0.1.0 diff --git a/.gitignore b/.gitignore index 27f4f02..0b15703 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ nats/nats.conf certs/ .kanidm-recovered gitea-oauth.secret +kron.env diff --git a/README.md b/README.md index ce555dd..8d14d1b 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,35 @@ whose `runs-on` matches that runner's host label, running `nats --server nats://portal:$NATS_PASSWORD@127.0.0.1:4222 pub portal.content.reload ""`. Until then, `podman compose restart portal` picks up new content. +## Money: kron and Vipps MobilePay + +Portal decides what is owed - a `payment:` on a state in the content - +and kron, the money service in this stack, asks Vipps MobilePay for +it, pushes it to the person's app, and reports back over NATS. Portal +then moves the record on (`then:`) by itself, as if the treasurer had +pressed the button. The treasurer's desk still works: money that came +to the account some other way is registered there as before. + +To bring it up, once per environment: + +1. `cp kron.env.example kron.env`, `chmod 600 kron.env`, and fill in + the four keys from the Vipps MobilePay portal (API-nøkler). Test + keys first: `VIPPS_ENV=test`, MSN 563326. +2. Host Caddy: copy `host-caddy/kron.vel.klingenbergbygg.no` into + `/etc/caddy/conf.d/` and `systemctl reload caddy`. Wildcard DNS + already points the name here. +3. `sh register-vipps-webhook.sh` - registers the webhook with Vipps + and writes `WEBHOOK_SECRET` into `kron.env`. Needs `jq`. +4. `podman compose up -d kron`, then `curl 127.0.0.1:3080/healthz`. +5. The content says what is owed: a `payment:` on the paying state in + `aggregates.yaml` in tomtervel/questions, without `by_hand`. The + content repo's lint needs `PAY_URL` set to allow that, the same as + the `portal` service here. + +Production is the same five steps with the production keys and +`VIPPS_ENV=prod`. `KRON_RELEASE` in `.env` is a bendik/kron tag on +Docker Hub; bumping it is the upgrade. + ## Upgrading portal Bump `PORTAL_RELEASE` in `.env` (a tag of uhhm/portal) and diff --git a/compose.yml b/compose.yml index a5ec1c3..30512e2 100644 --- a/compose.yml +++ b/compose.yml @@ -95,6 +95,11 @@ services: # The namespace of everything this site keeps on NATS from portal 0.9. # Must not change: a new name is a new, empty site. SITE_NAME: tomtervel + # The money service is there: the lint then allows `payment:` without + # `by_hand: true`. Nothing at run time reads it - a request is + # published whenever the content did not say by_hand - so the same + # variable has to be set where the content repo's lint runs. + PAY_URL: http://kron:3000 volumes: - learn_cache:/app/learn-cache ports: @@ -135,6 +140,28 @@ services: nats: condition: service_healthy + # The money service. Portal decides what is owed - a `payment:` on a + # state in the content - and publishes the request on this NATS; kron + # is what asks Vipps MobilePay for it, pushes it to the person's app, + # and says on the same NATS what became of it. One kron per site, its + # keys in kron.env and nowhere else. Host-published for kasse's host + # Caddy (kron.vel.klingenbergbygg.no -> here): Vipps has to reach + # /webhook over public HTTPS, and a person who paid by link lands on + # /returned/. + kron: + image: docker.io/bendik/kron:${KRON_RELEASE} + restart: unless-stopped + env_file: kron.env + environment: + NATS_URL: nats://portal:${NATS_PASSWORD}@nats:4222 + SITE_NAME: tomtervel + PUBLIC_URL: https://kron.${PORTAL_HOST} + ports: + - "127.0.0.1:3080:3000" + depends_on: + nats: + condition: service_healthy + # The Gitea Actions runner is a host service on kasse (pacman gitea-runner), # registered against prosjekt.klingenbergbygg.no. It reaches this NATS via the # published 127.0.0.1:4222 port above, so no in-compose runner — and no diff --git a/host-caddy/kron.vel.klingenbergbygg.no b/host-caddy/kron.vel.klingenbergbygg.no new file mode 100644 index 0000000..0aea9da --- /dev/null +++ b/host-caddy/kron.vel.klingenbergbygg.no @@ -0,0 +1,6 @@ +# kasse's host Caddy: /etc/caddy/conf.d/kron.vel.klingenbergbygg.no. +# Vipps MobilePay delivers webhooks here, and a person who paid by link +# comes back here; both go to the vel stack's kron (127.0.0.1:3080). +kron.vel.klingenbergbygg.no { + reverse_proxy 127.0.0.1:3080 +} diff --git a/kron.env.example b/kron.env.example new file mode 100644 index 0000000..24fcbbc --- /dev/null +++ b/kron.env.example @@ -0,0 +1,19 @@ +# kron, the money service beside portal: Vipps MobilePay for Tomter Vel. +# Copy to kron.env (root, mode 600, like portal.env) and fill in the +# values from the Vipps MobilePay portal, API-nøkler. Never commit kron.env. +# +# Test first: VIPPS_ENV=test with the keys from "API-nøkler – test" +# (MSN 563326); production is the same five lines with the production +# keys and VIPPS_ENV=prod. +PROVIDER=vipps +VIPPS_ENV=test +VIPPS_MSN=563326 +VIPPS_CLIENT_ID= +VIPPS_CLIENT_SECRET= +# Ocp-Apim-Subscription-Key (primær). The secondary exists so this one +# can be rotated without downtime; kron takes one at a time. +VIPPS_SUBSCRIPTION_KEY= +# From registering /webhook with Vipps: register-vipps-webhook.sh +# fills this in. Until it is set every webhook is refused and payments +# settle by the sweep alone, which works but is slower. +WEBHOOK_SECRET= diff --git a/register-vipps-webhook.sh b/register-vipps-webhook.sh new file mode 100755 index 0000000..db6658b --- /dev/null +++ b/register-vipps-webhook.sh @@ -0,0 +1,44 @@ +#!/bin/sh +# Registers /webhook with Vipps MobilePay's Webhooks API and +# puts the secret it answers with into kron.env as WEBHOOK_SECRET, +# without showing it. Run once per environment, as root, in this +# directory, after kron.env has the four keys and before kron starts. +# +# sh register-vipps-webhook.sh +# +# The events are the ones kron reads (src/provider/vipps.rs, +# seen_of_event): authorized, captured, expired, cancelled, aborted, +# terminated and refunded. kron does not register itself, by design: +# registering is a one-time act with a secret in the answer, and a +# service that could do it could also do it twice. +set -eu +[ -f kron.env ] || { echo "no kron.env here" >&2; exit 1; } +. ./kron.env +: "${VIPPS_CLIENT_ID:?}" "${VIPPS_CLIENT_SECRET:?}" "${VIPPS_SUBSCRIPTION_KEY:?}" "${VIPPS_MSN:?}" +public_url=$(grep -E '^PUBLIC_URL=' kron.env | cut -d= -f2- || true) +[ -n "$public_url" ] || public_url="https://kron.$(sed -n 's/^PORTAL_HOST=//p' .env)" +base=https://api.vipps.no +[ "${VIPPS_ENV:-test}" = test ] && base=https://apitest.vipps.no + +token=$(curl -sf -X POST "$base/accesstoken/get" \ + -H "client_id: $VIPPS_CLIENT_ID" -H "client_secret: $VIPPS_CLIENT_SECRET" \ + -H "Ocp-Apim-Subscription-Key: $VIPPS_SUBSCRIPTION_KEY" -H "Merchant-Serial-Number: $VIPPS_MSN" \ + | jq -r .access_token) +[ -n "$token" ] && [ "$token" != null ] || { echo "no access token: check the four keys" >&2; exit 1; } + +answer=$(curl -sf -X POST "$base/webhooks/v1/webhooks" \ + -H "Authorization: Bearer $token" \ + -H "Ocp-Apim-Subscription-Key: $VIPPS_SUBSCRIPTION_KEY" -H "Merchant-Serial-Number: $VIPPS_MSN" \ + -H 'Content-Type: application/json' \ + -d "$(jq -n --arg url "$public_url/webhook" '{url: $url, events: [ + "epayments.payment.authorized.v1", "epayments.payment.captured.v1", + "epayments.payment.expired.v1", "epayments.payment.cancelled.v1", + "epayments.payment.aborted.v1", "epayments.payment.terminated.v1", + "epayments.payment.refunded.v1"]}')") +id=$(printf '%s' "$answer" | jq -r .id) +secret=$(printf '%s' "$answer" | jq -r .secret) +[ -n "$secret" ] && [ "$secret" != null ] || { echo "registered nothing: $(printf '%s' "$answer" | jq -c 'del(.secret)')" >&2; exit 1; } + +tmp=$(mktemp) && grep -v '^WEBHOOK_SECRET=' kron.env > "$tmp" && printf 'WEBHOOK_SECRET=%s\n' "$secret" >> "$tmp" \ + && cat "$tmp" > kron.env && rm -f "$tmp" +echo "webhook $id registered at $public_url/webhook; WEBHOOK_SECRET written to kron.env"