#!/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"