#!/usr/bin/env bash # Usage: bash order.sh [catalog|create|status]. Only use a sandbox key. set -euo pipefail case "${SPXRK_API_KEY:-}" in spk_test_*) ;; *) echo 'Sandbox SPXRK_API_KEY required' >&2; exit 2 ;; esac # Restrict credentials to printable characters and keep them out of the process argument list. if [[ ! "$SPXRK_API_KEY" =~ ^[a-zA-Z0-9_-]+$ ]]; then exit 2; fi auth_header() { printf 'Authorization: Bearer %s\n' "$SPXRK_API_KEY"; } options=(--silent --show-error --fail-with-body --max-time 15 --user-agent 'SPXRK-cURL-Example/0.1.0' -H @-) origin=https://spxrk.org case "${1:-catalog}" in catalog) auth_header | curl "${options[@]}" "$origin/v1/products?group=giftcards&limit=10" ;; create) : "${SPXRK_GIFT_CARD_SKU:?Select a sandbox gift card}" "${CHECKOUT_ID:?Use a saved checkout ID}" [[ "$SPXRK_GIFT_CARD_SKU" =~ ^sku_[a-f0-9]{32}$ && "$CHECKOUT_ID" =~ ^[A-Za-z0-9._-]{1,128}$ ]] || exit 2 body=$(printf '{"items":[{"sku":"%s","quantity":1}]}' "$SPXRK_GIFT_CARD_SKU") auth_header | curl "${options[@]}" -H "Idempotency-Key: $CHECKOUT_ID" \ -H 'Content-Type: application/json' --data-binary "$body" "$origin/v1/orders" ;; status) : "${SPXRK_ORDER_ID:?Set the returned UUID}" [[ "$SPXRK_ORDER_ID" =~ ^[a-fA-F0-9]{8}(-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$ ]] || exit 2 auth_header | curl "${options[@]}" "$origin/v1/orders/$SPXRK_ORDER_ID" ;; *) echo 'Usage: order.sh [catalog|create|status]' >&2; exit 2 ;; esac