#!/usr/bin/env python3 """ This script makes sure that the merchant backend instances used by the test/demo environment are created. We assume that the merchant backend is running, and that the "~/activate" file has been sourced to provide the right environment variables. """ import requests from os import environ from sys import exit from urllib.parse import urljoin def expect_env(name): val = environ.get(name) if not val: print(f"{name} not defined. Please source the ~/activate file.") exit(1) return val MERCHANT_BACKEND_BASE_URL = expect_env("TALER_ENV_MERCHANT_BACKEND") TALER_ENV_NAME = expect_env("TALER_ENV_NAME") TALER_CONFIG_CURRENCY = expect_env("TALER_CONFIG_CURRENCY") TALER_ENV_FRONTENDS_APITOKEN = expect_env("TALER_ENV_FRONTENDS_APITOKEN") def ensure_instance(instance_id, name, payto_uris, auth): # FIXME: Use auth once the default instance also uses token auth instance_response = requests.get( urljoin(MERCHANT_BACKEND_BASE_URL, f"private/instances/${instance_id}") ) if instance_response.status_code == 200: return req = dict( id=instance_id, name=name, payto_uris=payto_uris, address=dict(), jurisdiction=dict(), default_max_wire_fee=f"{TALER_CONFIG_CURRENCY}:1", default_wire_fee_amortization=3, default_max_deposit_fee=f"{TALER_CONFIG_CURRENCY}:1", default_wire_transfer_delay=dict(d_ms="forever"), default_pay_delay=dict(d_ms="forever"), # FIXME: Eventually, this should be an actual secret token auth=auth, ) create_resp = requests.post( urljoin(MERCHANT_BACKEND_BASE_URL, "private/instances"), json=req ) if create_resp.status_code < 200 or create_resp.status_code >= 300: print(f"failed to create instance {instance_id}") print(create_resp.text) print("trying with PATCH") patch_resp = requests.patch( urljoin(MERCHANT_BACKEND_BASE_URL, f"private/instances/{instance_id}"), json=req ) if patch_resp.status_code < 200 or patch_resp.status_code >= 300: print(f"failed to patch instance {instance_id}") print(patch_resp.text) exit(1) ensure_instance( "blog", name="Blog", payto_uris=[f"payto://x-taler-bank/bank.{TALER_ENV_NAME}.taler.net/blog"], auth=dict(method="token", token=TALER_ENV_FRONTENDS_APITOKEN), ) ensure_instance( "donations", name="Donations", payto_uris=[f"payto://x-taler-bank/bank.{TALER_ENV_NAME}.taler.net/donations"], auth=dict(method="token", token=TALER_ENV_FRONTENDS_APITOKEN), ) ensure_instance( "survey", name="Survey", payto_uris=[f"payto://x-taler-bank/bank.{TALER_ENV_NAME}.taler.net/survey"], auth=dict(method="token", token=TALER_ENV_FRONTENDS_APITOKEN), ) ensure_instance( "pos", name="PoS", payto_uris=[f"payto://x-taler-bank/bank.{TALER_ENV_NAME}.taler.net/pos"], auth=dict(method="token", token=TALER_ENV_FRONTENDS_APITOKEN), ) ensure_instance( "GNUnet", name="GNUnet", payto_uris=[f"payto://x-taler-bank/bank.{TALER_ENV_NAME}.taler.net/GNUnet"], auth=dict(method="token", token=TALER_ENV_FRONTENDS_APITOKEN), ) ensure_instance( "Taler", name="Taler", payto_uris=[f"payto://x-taler-bank/bank.{TALER_ENV_NAME}.taler.net/Taler"], auth=dict(method="token", token=TALER_ENV_FRONTENDS_APITOKEN), ) ensure_instance( "Tor", name="Tor", payto_uris=[f"payto://x-taler-bank/bank.{TALER_ENV_NAME}.taler.net/Tor"], auth=dict(method="token", token=TALER_ENV_FRONTENDS_APITOKEN), ) # Note: this instance has a fixed secret-token, so as to allow anyone to easily # run their tutorial. ensure_instance( "Tutorial", name="Tutorial", payto_uris=[f"payto://x-taler-bank/bank.{TALER_ENV_NAME}.taler.net/Tutorial"], auth=dict(method="token", token="secret-token:sandbox"), )