merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

test_merchant_templates.sh (6713B)


      1 #!/bin/bash
      2 # This file is in the public domain.
      3 
      4 set -eu
      5 
      6 function clean_wallet() {
      7     rm -f "${WALLET_DB}"
      8     exit_cleanup
      9 }
     10 
     11 
     12 # Replace with 0 for nexus...
     13 USE_FAKEBANK=1
     14 if [ 1 = "$USE_FAKEBANK" ]
     15 then
     16     ACCOUNT="exchange-account-2"
     17     BANK_FLAGS="-f -d x-taler-bank -u $ACCOUNT"
     18     BANK_URL="http://localhost:8082/"
     19 else
     20     ACCOUNT="exchange-account-1"
     21     BANK_FLAGS="-ns -d iban -u $ACCOUNT"
     22     BANK_URL="http://localhost:18082/"
     23     echo -n "Testing for libeufin-bank"
     24     libeufin-bank --help >/dev/null </dev/null || exit_skip " MISSING"
     25     echo " FOUND"
     26 
     27 fi
     28 
     29 . setup.sh
     30 
     31 echo -n "Testing for taler-harness"
     32 taler-harness --help >/dev/null </dev/null || exit_skip " MISSING"
     33 echo " FOUND"
     34 
     35 # Launch exchange, merchant and bank.
     36 setup -c "test_template.conf" \
     37       -r "merchant-exchange-default" \
     38       -em \
     39       $BANK_FLAGS
     40 LAST_RESPONSE=$(mktemp -p "${TMPDIR:-/tmp}" test_response.conf-XXXXXX)
     41 CONF="test_template.conf.edited"
     42 WALLET_DB=$(mktemp -p "${TMPDIR:-/tmp}" test_wallet.json-XXXXXX)
     43 EXCHANGE_URL="http://localhost:8081/"
     44 
     45 # Install cleanup handler (except for kill -9)
     46 trap clean_wallet EXIT
     47 
     48 echo -n "First prepare wallet with coins ..."
     49 rm -f "$WALLET_DB"
     50 taler-wallet-cli \
     51     --no-throttle \
     52     --wallet-db="$WALLET_DB" \
     53     api \
     54     --expect-success 'withdrawTestBalance' \
     55   "$(jq -n '
     56     {
     57         amount: "TESTKUDOS:99",
     58         corebankApiBaseUrl: $BANK_URL,
     59         exchangeBaseUrl: $EXCHANGE_URL
     60     }' \
     61     --arg BANK_URL "${BANK_URL}" \
     62     --arg EXCHANGE_URL "$EXCHANGE_URL"
     63   )" 2>wallet-withdraw-1.err >wallet-withdraw-1.out
     64 echo -n "."
     65 # FIXME-MS: add logic to have nexus check immediately here.
     66 # sleep 10
     67 echo -n "."
     68 # NOTE: once libeufin can do long-polling, we should
     69 # be able to reduce the delay here and run wirewatch
     70 # always in the background via setup
     71 taler-exchange-wirewatch \
     72     -a "$ACCOUNT" \
     73     -L "INFO" \
     74     -c "$CONF" \
     75     -t &> taler-exchange-wirewatch.out
     76 echo -n "."
     77 timeout 60 taler-wallet-cli \
     78     --wallet-db="$WALLET_DB" \
     79     run-until-done \
     80     2>wallet-withdraw-finish-1.err \
     81     >wallet-withdraw-finish-1.out
     82 echo " OK"
     83 
     84 CURRENCY_COUNT=$(taler-wallet-cli --wallet-db="$WALLET_DB" balance | jq '.balances|length')
     85 if [ "$CURRENCY_COUNT" = "0" ]
     86 then
     87     exit_fail "Expected least one currency, withdrawal failed. check log."
     88 fi
     89 
     90 #
     91 # CREATE INSTANCE FOR TESTING
     92 #
     93 
     94 echo -n "Configuring merchant instance ..."
     95 
     96 STATUS=$(curl -H "Content-Type: application/json" -X POST \
     97     -H 'Authorization: Bearer secret-token:super_secret' \
     98     http://localhost:9966/management/instances \
     99     -d '{"auth":{"method":"external"},"id":"admin","name":"default","user_type":"business","address":{},"jurisdiction":{},"use_stefan":true,"default_wire_transfer_delay":{"d_us" : 50000000000},"default_pay_delay":{"d_us": 60000000000}}' \
    100     -w "%{http_code}" -s -o /dev/null)
    101 
    102 if [ "$STATUS" != "204" ]
    103 then
    104     exit_fail "Expected '204 No content' response. Got instead $STATUS"
    105 fi
    106 echo "Ok"
    107 
    108 echo -n "Configuring merchant bank account ..."
    109 
    110 if [ 1 = "$USE_FAKEBANK" ]
    111 then
    112     FORTYTHREE="payto://x-taler-bank/localhost/fortythree?receiver-name=fortythree"
    113 else
    114     FORTYTHREE=$(get_payto_uri fortythree x)
    115 fi
    116 # add bank account address
    117 STATUS=$(curl -H "Content-Type: application/json" -X POST \
    118     -H 'Authorization: Bearer secret-token:super_secret' \
    119     http://localhost:9966/private/accounts \
    120     -d '{"payto_uri":"'"$FORTYTHREE"'"}' \
    121     -w "%{http_code}" -s -o /dev/null)
    122 
    123 if [ "$STATUS" != "200" ]
    124 then
    125     exit_fail "Expected '200 OK' response. Got instead $STATUS"
    126 fi
    127 echo "Ok"
    128 
    129 
    130 echo -n "Creating Paivana template..."
    131 TID="paivana"
    132 STATUS=$(curl 'http://localhost:9966/private/templates' \
    133     -d '{"template_id":"paivana","template_description":"A Paivana template","template_contract":{"template_type":"paivana","summary":"The summary","choices":[{"amount":"TESTKUDOS:1"}]}}' \
    134     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    135 
    136 if [ "$STATUS" != "204" ]
    137 then
    138     cat "$LAST_RESPONSE" >&2
    139     exit_fail "Expected 204, template created. got: $STATUS"
    140 fi
    141 
    142 echo "Checking template data ..."
    143 STATUS=$(curl http://localhost:9966/templates/"$TID" \
    144     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    145 
    146 AMOUNT=$(jq -r .template_contract.choices[0].amount < "$LAST_RESPONSE")
    147 if [ "$AMOUNT" != "TESTKUDOS:1" ]
    148 then
    149     cat "$LAST_RESPONSE" >&2
    150     exit_fail "Expected TESTKUDOS:1. Got: $AMOUNT"
    151 fi
    152 echo " OK"
    153 
    154 echo -n "Creating order using template..."
    155 PAIVANA_ID="4321-4ZAXW7M5WW09QNZDF4MRAXBKS1321QQEBFVSWD68FCXHXS1G04A4ZAXW7M5WW09QNZDF4MRAXBKS1321QQEBFVSWD68FCXHXS1G04A0"
    156 STATUS=$(curl 'http://localhost:9966/templates/'"$TID" \
    157               -d '{"template_type":"paivana","tip":"TESTKUDOS:0.1","website":"https://example.com/","paivana_id":"'"${PAIVANA_ID}"'"}' \
    158     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    159 
    160 if [ "$STATUS" != "200" ]
    161 then
    162     cat "$LAST_RESPONSE" >&2
    163     exit_fail "Expected 200, order created. got: $STATUS"
    164 fi
    165 
    166 ORDER_ID=$(jq -r .order_id < "$LAST_RESPONSE")
    167 TOKEN=$(jq -r .token < "$LAST_RESPONSE")
    168 
    169 if [ "$TOKEN" == "null" ]
    170 then
    171     cat "$LAST_RESPONSE" >&2
    172     exit_fail "token should not be null, got: $TOKEN"
    173 fi
    174 
    175 echo "OK"
    176 
    177 echo -n "Fetching order details ..."
    178 STATUS=$(curl "http://localhost:9966/private/orders/${ORDER_ID}?session_id=${PAIVANA_ID}" \
    179     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    180 
    181 if [ "$STATUS" != "200" ]
    182 then
    183     cat "$LAST_RESPONSE" >&2
    184     exit_fail "Expected 200, getting order info. got: $STATUS"
    185 fi
    186 
    187 PRICE=$(jq -r .total_amount < "$LAST_RESPONSE")
    188 if [ "$PRICE" != "TESTKUDOS:1.1" ]
    189 then
    190     cat "$LAST_RESPONSE" >&2
    191     exit_fail "Expected TESTKUDOS:1.1 (with tip) but got: $PRICE"
    192 fi
    193 
    194 PAY_URL=$(jq -e -r .taler_pay_uri < "$LAST_RESPONSE")
    195 echo "OK: $PAY_URL"
    196 
    197 
    198 NOW=$(date +%s)
    199 
    200 echo -n "Pay first order ${PAY_URL} ..."
    201 # echo "0" to tell wallet to use choice #0
    202 echo "0" | \
    203   taler-wallet-cli \
    204       --no-throttle \
    205       --wallet-db="$WALLET_DB" \
    206       handle-uri "${PAY_URL}" \
    207       -y 2> wallet-pay1.err > wallet-pay1.log
    208 
    209 timeout 60 taler-wallet-cli \
    210     --no-throttle \
    211     --wallet-db="$WALLET_DB" \
    212     run-until-done \
    213     2> wallet-finish-pay1.err \
    214     > wallet-finish-pay1.log
    215 NOW2=$(date +%s)
    216 echo " OK (took $(( NOW2 - NOW )) secs )"
    217 
    218 # Check payment status AND binding to session ID.
    219 STATUS=$(curl "http://localhost:9966/private/orders/${ORDER_ID}?session_id=${PAIVANA_ID}" \
    220     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    221 
    222 if [ "$STATUS" != "200" ]
    223 then
    224     cat "$LAST_RESPONSE" >&2
    225     exit_fail "Expected 200, after pay. got: $STATUS"
    226 fi
    227 
    228 ORDER_STATUS=$(jq -r .order_status < "$LAST_RESPONSE")
    229 
    230 if [ "$ORDER_STATUS" != "paid" ]
    231 then
    232     cat "$LAST_RESPONSE" >&2
    233     exit_fail "Order status should be 'paid'. got: $ORDER_STATUS"
    234 fi
    235 
    236 echo "TEST PASSED"
    237 exit 0