merchant

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

test_merchant_transfer_tracking.sh (24598B)


      1 #!/bin/bash
      2 # This file is part of TALER
      3 # Copyright (C) 2014-2026 Taler Systems SA
      4 #
      5 # TALER is free software; you can redistribute it and/or modify
      6 # it under the terms of the GNU General Public License as
      7 # published by the Free Software Foundation; either version 3, or
      8 # (at your option) any later version.
      9 #
     10 # TALER is distributed in the hope that it will be useful, but
     11 # WITHOUT ANY WARRANTY; without even the implied warranty of
     12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 # GNU General Public License for more details.
     14 #
     15 # You should have received a copy of the GNU General Public
     16 # License along with TALER; see the file COPYING.  If not, see
     17 # <http://www.gnu.org/licenses/>
     18 #
     19 # Testcase for #6912 and #8061
     20 
     21 set -eu
     22 
     23 . setup.sh
     24 
     25 # Replace with 0 for nexus...
     26 USE_FAKEBANK=1
     27 if [ 1 = "$USE_FAKEBANK" ]
     28 then
     29     ACCOUNT="exchange-account-2"
     30     WIRE_METHOD="x-taler-bank"
     31     BANK_FLAGS="-f -d $WIRE_METHOD -u $ACCOUNT"
     32     BANK_URL="http://localhost:8082/"
     33 else
     34     echo -n "Testing for libeufin-bank"
     35     libeufin-bank --help >/dev/null </dev/null || exit_skip " MISSING"
     36     echo " FOUND"
     37     ACCOUNT="exchange-account-1"
     38     WIRE_METHOD="iban"
     39     BANK_FLAGS="-ns -d $WIRE_METHOD -u $ACCOUNT"
     40     BANK_URL="http://localhost:18082/"
     41 fi
     42 
     43 
     44 echo -n "Testing for taler-harness"
     45 taler-harness --help >/dev/null </dev/null || exit_skip " MISSING"
     46 echo " FOUND"
     47 
     48 # Launch system.
     49 setup -c "test_template.conf" \
     50       -r "merchant-exchange-default" \
     51       -em \
     52       $BANK_FLAGS
     53 LAST_RESPONSE=$(mktemp -p "${TMPDIR:-/tmp}" test_response.conf-XXXXXX)
     54 WALLET_DB=$(mktemp -p "${TMPDIR:-/tmp}" test_wallet.json-XXXXXX)
     55 CONF="test_template.conf.edited"
     56 EXCHANGE_URL="http://localhost:8081/"
     57 
     58 echo -n "First prepare wallet with coins..."
     59 rm -f "$WALLET_DB"
     60 taler-wallet-cli \
     61     --no-throttle \
     62     --wallet-db="$WALLET_DB" \
     63     api \
     64     --expect-success 'withdrawTestBalance' \
     65   "$(jq -n '
     66     {
     67         amount: "TESTKUDOS:99",
     68         corebankApiBaseUrl: $BANK_URL,
     69         exchangeBaseUrl: $EXCHANGE_URL
     70     }' \
     71     --arg BANK_URL "${BANK_URL}" \
     72     --arg EXCHANGE_URL "$EXCHANGE_URL"
     73   )" 2>wallet-withdraw-1.err >wallet-withdraw-1.out
     74 echo -n "."
     75 if [ 1 = "$USE_FAKEBANK" ]
     76 then
     77     # Fakebank is instant...
     78     sleep 0
     79 else
     80     sleep 10
     81     # NOTE: once libeufin can do long-polling, we should
     82     # be able to reduce the delay here and run wirewatch
     83     # always in the background via setup
     84 fi
     85 echo -n "."
     86 # NOTE: once libeufin can do long-polling, we should
     87 # be able to reduce the delay here and run wirewatch
     88 # always in the background via setup
     89 taler-exchange-wirewatch \
     90     -L "INFO" \
     91     -c "$CONF" \
     92     -t &> taler-exchange-wirewatch0.out
     93 echo -n "."
     94 
     95 timeout 60 taler-wallet-cli \
     96     --wallet-db="$WALLET_DB" \
     97     run-until-done \
     98     2>wallet-withdraw-finish-1.err \
     99     >wallet-withdraw-finish-1.out
    100 echo " OK"
    101 
    102 #
    103 # CREATE INSTANCE FOR TESTING
    104 #
    105 
    106 echo -n "Configuring merchant admin instance ..."
    107 if [ 1 = "$USE_FAKEBANK" ]
    108 then
    109     TOR_PAYTO="payto://x-taler-bank/localhost/tor?receiver-name=tor"
    110     GNUNET_PAYTO="payto://x-taler-bank/localhost/gnunet?receiver-name=gnunet"
    111     SURVEY_PAYTO="payto://x-taler-bank/localhost/survey?receiver-name=survey"
    112     TUTORIAL_PAYTO="payto://x-taler-bank/localhost/tutorial?receiver-name=tutorial"
    113 else
    114     TOR_PAYTO=$(get_payto_uri tor x)
    115     GNUNET_PAYTO=$(get_payto_uri gnunet x)
    116     SURVEY_PAYTO=$(get_payto_uri survey x)
    117     TUTORIAL_PAYTO=$(get_payto_uri tutorial x)
    118 fi
    119 # create with 2 address
    120 
    121 STATUS=$(curl -H "Content-Type: application/json" -X POST \
    122     -H 'Authorization: Bearer secret-token:super_secret' \
    123     http://localhost:9966/management/instances \
    124     -d '{"auth":{"method":"external"},"id":"admin","name":"default","user_type":"business","address":{},"jurisdiction":{},"use_stefan":true,"default_wire_transfer_delay":{"d_us" : 50000000},"default_pay_delay":{"d_us": 60000000}}' \
    125     -w "%{http_code}" -s -o /dev/null)
    126 
    127 if [ "$STATUS" != "204" ]
    128 then
    129     exit_fail "Expected 204, instance created. got: $STATUS"
    130 fi
    131 
    132 STATUS=$(curl -H "Content-Type: application/json" -X POST \
    133     -H 'Authorization: Bearer secret-token:super_secret' \
    134     http://localhost:9966/private/accounts \
    135     -d '{"payto_uri":"'"$TOR_PAYTO"'"}' \
    136     -w "%{http_code}" -s -o /dev/null)
    137 
    138 if [ "$STATUS" != "200" ]
    139 then
    140     exit_fail "Expected 200 OK. Got: $STATUS"
    141 fi
    142 STATUS=$(curl -H "Content-Type: application/json" -X POST \
    143     -H 'Authorization: Bearer secret-token:super_secret' \
    144     http://localhost:9966/private/accounts \
    145     -d '{"payto_uri":"'"$GNUNET_PAYTO"'"}' \
    146     -w "%{http_code}" -s -o /dev/null)
    147 
    148 if [ "$STATUS" != "200" ]
    149 then
    150     exit_fail "Expected 200 OK. Got: $STATUS"
    151 fi
    152 
    153 echo "OK"
    154 
    155 echo -n "Configuring merchant test instance ..."
    156 # create with 2 address
    157 
    158 STATUS=$(curl -H "Content-Type: application/json" -X POST \
    159     -H 'Authorization: Bearer secret-token:super_secret' \
    160     http://localhost:9966/management/instances \
    161     -d '{"auth":{"method":"external"},"id":"test","name":"test","user_type":"business","address":{},"jurisdiction":{},"use_stefan":true,"default_wire_transfer_delay":{"d_us" : 50000000},"default_pay_delay":{"d_us": 60000000}}' \
    162     -w "%{http_code}" -s -o /dev/null)
    163 
    164 if [ "$STATUS" != "204" ]
    165 then
    166     exit_fail "Expected 204, instance created. got: $STATUS"
    167 fi
    168 STATUS=$(curl -H "Content-Type: application/json" -X POST \
    169     -H 'Authorization: Bearer secret-token:super_secret' \
    170     http://localhost:9966/instances/test/private/accounts \
    171     -d '{"payto_uri":"'"$SURVEY_PAYTO"'","credit_facade_url":"http://localhost:8082/accounts/survey/taler-revenue/","credit_facade_credentials":{"type":"basic","username":"survey","password":"x"}}' \
    172     -w "%{http_code}" -s -o /dev/null)
    173 
    174 if [ "$STATUS" != "200" ]
    175 then
    176     exit_fail "Expected 200 OK. Got: $STATUS"
    177 fi
    178 
    179 # CREATE ORDER AND SELL IT
    180 echo -n "Creating order to be paid..."
    181 STATUS=$(curl 'http://localhost:9966/instances/test/private/orders' \
    182     -d '{"refund_delay":{"d_us":0}, "order":{"amount":"TESTKUDOS:1","summary":"payme"}}' \
    183     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    184 
    185 if [ "$STATUS" != "200" ]
    186 then
    187     cat "$LAST_RESPONSE"
    188     exit_fail "Expected 200 ok, order created. got: $STATUS"
    189 fi
    190 
    191 echo " OK"
    192 
    193 ORDER_ID=$(jq -e -r .order_id < "$LAST_RESPONSE")
    194 #TOKEN=$(jq -e -r .token < "$LAST_RESPONSE")
    195 
    196 echo -n "Checking for Typst ..."
    197 if typst --version 2> /dev/null > /dev/null 
    198 then
    199     echo " OK"
    200 
    201 
    202     # Check max-age argument
    203     echo -n "Checking order list generation with max-age..."
    204     STATUS=$(curl 'http://localhost:9966/instances/test/private/orders?max_age=3600000' \
    205               -H 'Accept: application/pdf' \
    206               -w "%{http_code}" \
    207               -s \
    208               -o "$LAST_RESPONSE")
    209 
    210     if [ "$STATUS" != "200" ]
    211     then
    212         cat "$LAST_RESPONSE"
    213         exit_fail "Expected 200 ok, order created. got: $STATUS"
    214     fi
    215     # If we want to see the order list...
    216     # cat $LAST_RESPONSE > order-list.pdf
    217     echo " OK"
    218 else
    219     echo " Not found, skipping test"
    220 fi
    221 
    222 echo -n "Checking order status before claiming it..."
    223 STATUS=$(curl "http://localhost:9966/instances/test/private/orders/${ORDER_ID}" \
    224     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    225 
    226 if [ "$STATUS" != "200" ]
    227 then
    228     cat "$LAST_RESPONSE"
    229     exit_fail "Expected 200 ok, getting order info before claming it. got: $STATUS"
    230 fi
    231 PAY_URL=$(jq -e -r .taler_pay_uri < "$LAST_RESPONSE")
    232 echo "OK"
    233 
    234 NOW=$(date +%s)
    235 echo -n "Pay first order ..."
    236 taler-wallet-cli \
    237     --no-throttle \
    238     --wallet-db="$WALLET_DB" \
    239     handle-uri "${PAY_URL}" \
    240     -y \
    241     2> wallet-pay1.err > wallet-pay1.log
    242 NOW2=$(date +%s)
    243 echo " OK (took $(( NOW2 - NOW )) secs)"
    244 
    245 STATUS=$(curl "http://localhost:9966/instances/test/private/orders/${ORDER_ID}" \
    246     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    247 
    248 if [ "$STATUS" != "200" ]
    249 then
    250     cat "$LAST_RESPONSE"
    251     exit_fail "Expected 200 ok, after pay. got: $STATUS"
    252 fi
    253 
    254 ORDER_STATUS=$(jq -r .order_status < "$LAST_RESPONSE")
    255 
    256 if [ "$ORDER_STATUS" != "paid" ]
    257 then
    258     cat "$LAST_RESPONSE"
    259     exit_fail "Expected 'paid'. got: $ORDER_STATUS"
    260 fi
    261 
    262 #
    263 # WIRE TRANSFER TO MERCHANT AND NOTIFY BACKEND
    264 #
    265 
    266 #PAY_DEADLINE=$(jq -r .contract_terms.pay_deadline.t_s < "$LAST_RESPONSE")
    267 WIRE_DEADLINE=$(jq -r .contract_terms.wire_transfer_deadline.t_s < "$LAST_RESPONSE")
    268 
    269 NOW=$(date +%s)
    270 
    271 TO_SLEEP=$((3600 + WIRE_DEADLINE - NOW ))
    272 echo "waiting $TO_SLEEP secs for wire transfer"
    273 
    274 echo -n "Perform wire transfers ..."
    275 taler-exchange-aggregator -y -c $CONF -T ${TO_SLEEP}000000 -t -L INFO &> aggregator.log
    276 taler-exchange-transfer -c $CONF -t -L INFO &> transfer.log
    277 echo " DONE"
    278 
    279 echo -n "Obtaining wire transfer details from bank..."
    280 
    281 BANKDATA="$(curl 'http://localhost:8082/accounts/exchange/taler-wire-gateway/history/outgoing?delta=1' -s)"
    282 WTID=$(echo "$BANKDATA" | jq -r .outgoing_transactions[0].wtid)
    283 WURL=$(echo "$BANKDATA" | jq -r .outgoing_transactions[0].exchange_base_url)
    284 CREDIT_AMOUNT=$(echo "$BANKDATA" | jq -r .outgoing_transactions[0].amount)
    285 TARGET_PAYTO=$(echo "$BANKDATA" | jq -r .outgoing_transactions[0].credit_account)
    286 TARGET=$(echo "$TARGET_PAYTO" | awk -F = '{print $2}')
    287 
    288 # Figure out which account got paid, in order to
    289 # resort the right (and complete: including the receiver-name)
    290 # TARGET_PAYTO
    291 if echo "$SURVEY_PAYTO" | grep -q "$TARGET" > /dev/null; then
    292   TARGET_PAYTO="$SURVEY_PAYTO";
    293 fi
    294 if echo "$SURVEY_PAYTO" | grep -q "$TARGET" > /dev/null; then
    295   TARGET_PAYTO="$SURVEY_PAYTO";
    296 fi
    297 if [ "$EXCHANGE_URL" != "$WURL" ]
    298 then
    299     exit_fail "Wrong exchange URL in subject '$SUBJECT', expected $EXCHANGE_URL"
    300 fi
    301 echo " OK"
    302 
    303 set +e
    304 
    305 echo -n "Notifying merchant of correct wire transfer, but on wrong instance..."
    306 
    307 #issue 6912
    308 #here we are notifying the transfer into a wrong instance (admin) and the payto_uri of the admin instance
    309 STATUS=$(curl 'http://localhost:9966/private/transfers' \
    310     -d "{\"credit_amount\":\"$CREDIT_AMOUNT\",\"wtid\":\"$WTID\",\"payto_uri\":\"$TOR_PAYTO\",\"exchange_url\":\"$WURL\"}" \
    311     -m 3 \
    312     -w "%{http_code}" \
    313     -s \
    314     -o "$LAST_RESPONSE")
    315 
    316 if [ "$STATUS" != "204" ]
    317 then
    318     jq . < "$LAST_RESPONSE"
    319     exit_fail "Expected response 204 no content, after providing transfer data. Got: $STATUS"
    320 fi
    321 echo " OK"
    322 
    323 
    324 echo -n "Fetching wire transfers of ADMIN instance ..."
    325 
    326 STATUS=$(curl 'http://localhost:9966/private/transfers' \
    327               -w "%{http_code}" \
    328               -s \
    329               -o "$LAST_RESPONSE")
    330 
    331 if [ "$STATUS" != "200" ]
    332 then
    333     jq . < "$LAST_RESPONSE"
    334     exit_fail "Expected response 200 Ok. got: $STATUS"
    335 fi
    336 
    337 TRANSFERS_LIST_SIZE=$(jq -r '.transfers | length' < "$LAST_RESPONSE")
    338 
    339 if [ "$TRANSFERS_LIST_SIZE" != "1" ]
    340 then
    341     jq . < "$LAST_RESPONSE"
    342     exit_fail "Expected one transfer. got: $TRANSFERS_LIST_SIZE"
    343 fi
    344 
    345 echo "OK"
    346 
    347 echo -n "Fetching running taler-merchant-reconciliation on bogus transfer ..."
    348 taler-merchant-reconciliation \
    349     -c "$CONF" \
    350     -L INFO \
    351     -t &> taler-merchant-reconciliation0.log
    352 echo "OK"
    353 
    354 echo -n "Fetching wire transfers of 'test' instance ..."
    355 
    356 STATUS=$(curl 'http://localhost:9966/instances/test/private/transfers' \
    357               -w "%{http_code}" \
    358               -s \
    359               -o "$LAST_RESPONSE")
    360 
    361 if [ "$STATUS" != "200" ]
    362 then
    363     jq . < "$LAST_RESPONSE"
    364     exit_fail "Expected response 200 Ok. got: $STATUS"
    365 fi
    366 
    367 TRANSFERS_LIST_SIZE=$(jq -r '.transfers | length' < "$LAST_RESPONSE")
    368 
    369 if [ "$TRANSFERS_LIST_SIZE" != "0" ]
    370 then
    371     jq . < "$LAST_RESPONSE"
    372     exit_fail "Expected non-empty transfer list size. got: $TRANSFERS_LIST_SIZE"
    373 fi
    374 
    375 echo "OK"
    376 
    377 echo -n "Checking order status ..."
    378 STATUS=$(curl "http://localhost:9966/instances/test/private/orders/${ORDER_ID}?transfer=YES" \
    379     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    380 
    381 if [ "$STATUS" != "200" ]
    382 then
    383     jq . < "$LAST_RESPONSE"
    384     exit_fail "Expected 200 ok, after order inquiry. got: $STATUS"
    385 fi
    386 
    387 WAS_WIRED=$(jq -r .wired < "$LAST_RESPONSE")
    388 
    389 if [ "$WAS_WIRED" == "true" ]
    390 then
    391     jq . < "$LAST_RESPONSE"
    392     exit_fail ".wired is true, expected false"
    393 fi
    394 
    395 echo " OK"
    396 
    397 echo -n "Notifying merchant of correct wire transfer in the correct instance..."
    398 #this time in the correct instance so the order will be marked as wired...
    399 
    400 echo -n "Running taler-merchant-wirewatch to check transfer ..."
    401 taler-merchant-wirewatch \
    402     -c $CONF \
    403     -t \
    404     -L INFO &> taler-merchant-wirewatch1.log
    405 echo " DONE"
    406 
    407 echo -n "Post-check for exchange deposit ..."
    408 taler-merchant-depositcheck \
    409     -c $CONF \
    410     -t \
    411     -e "http://localhost:8081/" \
    412     -T ${TO_SLEEP}000000 \
    413     -L INFO &> depositcheck1a.log
    414 echo " DONE"
    415 
    416 echo -n "Fetching running taler-merchant-reconciliation on good transfer ..."
    417 taler-merchant-reconciliation \
    418     -c $CONF \
    419     -L INFO \
    420     -T ${TO_SLEEP}000000 \
    421     -t &> taler-merchant-reconciliation1.log
    422 echo "OK"
    423 
    424 echo -n "Fetching wire transfers of TEST instance ..."
    425 
    426 STATUS=$(curl 'http://localhost:9966/instances/test/private/transfers' \
    427               -w "%{http_code}" \
    428               -s \
    429               -o "$LAST_RESPONSE")
    430 
    431 if [ "$STATUS" != "200" ]
    432 then
    433     jq . < "$LAST_RESPONSE"
    434     exit_fail "Expected response 200 Ok. got: $STATUS"
    435 fi
    436 
    437 TRANSFERS_LIST_SIZE=$(jq -r '.transfers | length' < "$LAST_RESPONSE")
    438 
    439 if [ "$TRANSFERS_LIST_SIZE" != "1" ]
    440 then
    441     jq . < "$LAST_RESPONSE"
    442     exit_fail "Expected one transfer. got: $TRANSFERS_LIST_SIZE"
    443 fi
    444 
    445 echo "OK"
    446 
    447 echo -n "Checking order status ..."
    448 STATUS=$(curl "http://localhost:9966/instances/test/private/orders/${ORDER_ID}" \
    449     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    450 
    451 if [ "$STATUS" != "200" ]
    452 then
    453     jq . < "$LAST_RESPONSE"
    454     exit_fail "Expected 200 ok, after order inquiry. got: $STATUS"
    455 fi
    456 
    457 WAS_WIRED=$(jq -r .wired < "$LAST_RESPONSE")
    458 
    459 if [ "$WAS_WIRED" != "true" ]
    460 then
    461     jq . < "$LAST_RESPONSE"
    462     exit_fail ".wired false, expected true"
    463 fi
    464 
    465 echo " OK"
    466 
    467 
    468 echo "================== 2nd order ====================== "
    469 
    470 
    471 
    472 # CREATE ORDER AND SELL IT
    473 echo -n "Creating 2nd order to be paid..."
    474 STATUS=$(curl 'http://localhost:9966/instances/test/private/orders' \
    475     -d '{"refund_delay":{"d_us":0}, "order":{"amount":"TESTKUDOS:2","summary":"payme"}}' \
    476     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    477 
    478 if [ "$STATUS" != "200" ]
    479 then
    480     cat "$LAST_RESPONSE"
    481     exit_fail "Expected 200 ok, order created. got: $STATUS"
    482 fi
    483 
    484 ORDER_ID=$(jq -e -r .order_id < "$LAST_RESPONSE")
    485 #TOKEN=$(jq -e -r .token < "$LAST_RESPONSE")
    486 
    487 STATUS=$(curl "http://localhost:9966/instances/test/private/orders/${ORDER_ID}" \
    488     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    489 
    490 if [ "$STATUS" != "200" ]
    491 then
    492     cat "$LAST_RESPONSE"
    493     exit_fail "Expected 200 ok, getting order info before claming it. got: $STATUS"
    494 fi
    495 PAY_URL=$(jq -e -r .taler_pay_uri < "$LAST_RESPONSE")
    496 echo "OK"
    497 
    498 NOW=$(date +%s)
    499 echo -n "Pay second order ..."
    500 taler-wallet-cli \
    501     --no-throttle \
    502     --wallet-db="$WALLET_DB" \
    503     handle-uri "${PAY_URL}" \
    504     -y \
    505     2> wallet-pay2.err > wallet-pay2.log
    506 NOW2=$(date +%s)
    507 echo " OK (took $(( NOW2 - NOW )) secs)"
    508 
    509 STATUS=$(curl "http://localhost:9966/instances/test/private/orders/${ORDER_ID}" \
    510     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    511 
    512 if [ "$STATUS" != "200" ]
    513 then
    514     cat "$LAST_RESPONSE"
    515     exit_fail "Expected 200 ok, after pay. got: $STATUS"
    516 fi
    517 
    518 ORDER_STATUS=$(jq -r .order_status < "$LAST_RESPONSE")
    519 
    520 if [ "$ORDER_STATUS" != "paid" ]
    521 then
    522     cat "$LAST_RESPONSE"
    523     exit_fail "Expected 'paid'. got: $ORDER_STATUS"
    524 fi
    525 
    526 #
    527 # WIRE TRANSFER TO MERCHANT AND NOTIFY BACKEND
    528 #
    529 
    530 #PAY_DEADLINE=$(jq -r .contract_terms.pay_deadline.t_s < "$LAST_RESPONSE")
    531 WIRE_DEADLINE=$(jq -r .contract_terms.wire_transfer_deadline.t_s < "$LAST_RESPONSE")
    532 
    533 NOW=$(date +%s)
    534 
    535 TO_SLEEP=$((3600 + WIRE_DEADLINE - NOW ))
    536 echo "waiting $TO_SLEEP secs for wire transfer"
    537 
    538 echo -n "Pre-check for exchange deposit ..."
    539 taler-merchant-depositcheck \
    540     -c $CONF \
    541     -t \
    542     -e "http://localhost:8081/" \
    543     -L INFO &> depositcheck1b.log
    544 echo " DONE"
    545 
    546 echo -n "Perform wire transfers ..."
    547 taler-exchange-aggregator \
    548     -y \
    549     -c $CONF \
    550     -T ${TO_SLEEP}000000 \
    551     -t \
    552     -L INFO &> aggregator2.log
    553 taler-exchange-transfer \
    554     -c $CONF \
    555     -t \
    556     -L INFO &> transfer2.log
    557 echo " DONE"
    558 
    559 echo -n "Post-check for exchange deposit ..."
    560 taler-merchant-depositcheck \
    561     -c $CONF \
    562     -t \
    563     -e "http://localhost:8081/" \
    564     -T ${TO_SLEEP}000000 \
    565     -L INFO &> depositcheck1c.log
    566 echo " DONE"
    567 
    568 
    569 echo -n "Obtaining wire transfer details from bank..."
    570 
    571 BANKDATA="$(curl 'http://localhost:8082/accounts/exchange/taler-wire-gateway/history/outgoing?delta=2' -s)"
    572 
    573 WTID=$(echo "$BANKDATA" | jq -r .outgoing_transactions[1].wtid)
    574 WURL=$(echo "$BANKDATA" | jq -r .outgoing_transactions[1].exchange_base_url)
    575 CREDIT_AMOUNT=$(echo "$BANKDATA" | jq -r .outgoing_transactions[1].amount)
    576 TARGET_PAYTO=$(echo "$BANKDATA" | jq -r .outgoing_transactions[1].credit_account)
    577 TARGET=$(echo "$TARGET_PAYTO" | awk -F = '{print $2}')
    578 
    579 # Figure out which account got paid, in order to
    580 # resort the right (and complete: including the receiver-name)
    581 # TARGET_PAYTO
    582 if echo "$SURVEY_PAYTO" | grep -q "$TARGET" > /dev/null; then
    583   TARGET_PAYTO="$SURVEY_PAYTO";
    584 fi
    585 if echo "$SURVEY_PAYTO" | grep -q "$TARGET" > /dev/null; then
    586   TARGET_PAYTO="$SURVEY_PAYTO";
    587 fi
    588 if [ "$EXCHANGE_URL" != "$WURL" ]
    589 then
    590     exit_fail "Wrong exchange URL in subject '$SUBJECT', expected $EXCHANGE_URL"
    591 fi
    592 echo " OK"
    593 
    594 echo -n "Running taler-merchant-wirewatch to check transfer ..."
    595 taler-merchant-wirewatch \
    596     -c $CONF \
    597     -t \
    598     -L INFO &> taler-merchant-wirewatch2.log
    599 echo " DONE"
    600 
    601 echo -n "Fetching running taler-merchant-reconciliation on good transfer ..."
    602 taler-merchant-reconciliation \
    603     -c $CONF \
    604     -L INFO \
    605     -T ${TO_SLEEP}000000 \
    606     -t &> taler-merchant-reconciliation2.log
    607 echo "OK"
    608 
    609 echo -n "Fetching wire transfers of TEST instance ..."
    610 
    611 STATUS=$(curl 'http://localhost:9966/instances/test/private/transfers' \
    612               -w "%{http_code}" \
    613               -s \
    614               -o "$LAST_RESPONSE")
    615 
    616 if [ "$STATUS" != "200" ]
    617 then
    618     jq . < "$LAST_RESPONSE"
    619     exit_fail "Expected response 200 Ok. got: $STATUS"
    620 fi
    621 
    622 TRANSFERS_LIST_SIZE=$(jq -r '.transfers | length' < "$LAST_RESPONSE")
    623 
    624 if [ "$TRANSFERS_LIST_SIZE" != "2" ]
    625 then
    626     jq . < "$LAST_RESPONSE"
    627     exit_fail "Expected two transfers. got: $TRANSFERS_LIST_SIZE"
    628 fi
    629 
    630 echo "OK"
    631 
    632 echo -n "Checking order status ..."
    633 STATUS=$(curl "http://localhost:9966/instances/test/private/orders/${ORDER_ID}" \
    634     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    635 
    636 if [ "$STATUS" != "200" ]
    637 then
    638     jq . < "$LAST_RESPONSE"
    639     exit_fail "Expected 200 ok, after order inquiry. got: $STATUS"
    640 fi
    641 
    642 WAS_WIRED=$(jq -r .wired < "$LAST_RESPONSE")
    643 
    644 if [ "$WAS_WIRED" != "true" ]
    645 then
    646     jq . < "$LAST_RESPONSE"
    647     exit_fail ".wired false, expected true"
    648 fi
    649 
    650 echo " OK"
    651 
    652 echo "================== 3rd order ====================== "
    653 
    654 # CREATE ORDER AND SELL IT
    655 echo -n "Creating 3rd order to be paid..."
    656 STATUS=$(curl 'http://localhost:9966/instances/test/private/orders' \
    657     -d '{"refund_delay":{"d_us":0}, "order":{"amount":"TESTKUDOS:3","summary":"payme"}}' \
    658     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    659 
    660 if [ "$STATUS" != "200" ]
    661 then
    662     cat "$LAST_RESPONSE"
    663     exit_fail "Expected 200 ok, order created. got: $STATUS"
    664 fi
    665 
    666 ORDER_ID=$(jq -e -r .order_id < "$LAST_RESPONSE")
    667 #TOKEN=$(jq -e -r .token < "$LAST_RESPONSE")
    668 
    669 STATUS=$(curl "http://localhost:9966/instances/test/private/orders/${ORDER_ID}" \
    670     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    671 
    672 if [ "$STATUS" != "200" ]
    673 then
    674     cat "$LAST_RESPONSE"
    675     exit_fail "Expected 200 ok, getting order info before claming it. got: $STATUS"
    676 fi
    677 PAY_URL=$(jq -e -r .taler_pay_uri < "$LAST_RESPONSE")
    678 echo "OK"
    679 
    680 NOW=$(date +%s)
    681 echo -n "Pay third order ..."
    682 taler-wallet-cli \
    683     --no-throttle \
    684     --wallet-db="$WALLET_DB" \
    685     handle-uri "${PAY_URL}" \
    686     -y \
    687     2> wallet-pay2.err > wallet-pay2.log
    688 NOW2=$(date +%s)
    689 echo " OK (took $(( NOW2 - NOW )) secs)"
    690 
    691 STATUS=$(curl "http://localhost:9966/instances/test/private/orders/${ORDER_ID}" \
    692     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    693 
    694 if [ "$STATUS" != "200" ]
    695 then
    696     cat "$LAST_RESPONSE"
    697     exit_fail "Expected 200 ok, after pay. got: $STATUS"
    698 fi
    699 
    700 ORDER_STATUS=$(jq -r .order_status < "$LAST_RESPONSE")
    701 
    702 if [ "$ORDER_STATUS" != "paid" ]
    703 then
    704     cat "$LAST_RESPONSE"
    705     exit_fail "Expected 'paid'. got: $ORDER_STATUS"
    706 fi
    707 
    708 #
    709 # WIRE TRANSFER TO MERCHANT AND NOTIFY BACKEND
    710 #
    711 
    712 #PAY_DEADLINE=$(jq -r .contract_terms.pay_deadline.t_s < "$LAST_RESPONSE")
    713 WIRE_DEADLINE=$(jq -r .contract_terms.wire_transfer_deadline.t_s < "$LAST_RESPONSE")
    714 
    715 NOW=$(date +%s)
    716 
    717 TO_SLEEP=$((1200 + WIRE_DEADLINE - NOW ))
    718 echo "waiting $TO_SLEEP secs for wire transfer"
    719 
    720 echo -n "Perform wire transfers for 3rd order..."
    721 taler-exchange-aggregator \
    722     -y \
    723     -c $CONF \
    724     -T ${TO_SLEEP}000000 \
    725     -t \
    726     -L INFO &> aggregator3.log
    727 taler-exchange-transfer \
    728     -c $CONF \
    729     -t \
    730     -L INFO &> transfer3.log
    731 echo " DONE"
    732 
    733 echo -n "Running taler-merchant-wirewatch to check transfer ..."
    734 taler-merchant-wirewatch \
    735     -c $CONF \
    736     -t \
    737     -L INFO &> taler-merchant-wirewatch3.log
    738 echo " DONE"
    739 
    740 echo -n "Post-wirewatch check for exchange deposit ..."
    741 taler-merchant-depositcheck \
    742     -c $CONF \
    743     -t \
    744     -e "http://localhost:8081/" \
    745     -T ${TO_SLEEP}000000 \
    746     -L INFO &> depositcheck1d.log
    747 echo " DONE"
    748 
    749 echo -n "Fetching wire transfers of TEST instance ..."
    750 
    751 STATUS=$(curl 'http://localhost:9966/instances/test/private/transfers' \
    752               -w "%{http_code}" \
    753               -s \
    754               -o "$LAST_RESPONSE")
    755 
    756 if [ "$STATUS" != "200" ]
    757 then
    758     jq . < "$LAST_RESPONSE"
    759     exit_fail "Expected response 200 Ok. got: $STATUS"
    760 fi
    761 
    762 TRANSFERS_LIST_SIZE=$(jq -r '.transfers | length' < "$LAST_RESPONSE")
    763 
    764 if [ "$TRANSFERS_LIST_SIZE" != "3" ]
    765 then
    766     jq . < "$LAST_RESPONSE"
    767     exit_fail "Expected three transfers. got: $TRANSFERS_LIST_SIZE"
    768 fi
    769 
    770 echo "OK"
    771 
    772 echo -n "Fetching running taler-merchant-reconciliation on good transfer ..."
    773 taler-merchant-reconciliation \
    774     -c $CONF \
    775     -L INFO \
    776     -T ${TO_SLEEP}000000 \
    777     -t &> taler-merchant-reconciliation3.log
    778 echo "OK"
    779 
    780 echo -n "Checking order status ..."
    781 STATUS=$(curl "http://localhost:9966/instances/test/private/orders/${ORDER_ID}" \
    782     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    783 
    784 if [ "$STATUS" != "200" ]
    785 then
    786     jq . < "$LAST_RESPONSE"
    787     exit_fail "Expected 200 ok, after order inquiry. got: $STATUS"
    788 fi
    789 
    790 WAS_WIRED=$(jq -r .wired < "$LAST_RESPONSE")
    791 
    792 if [ "$WAS_WIRED" != "true" ]
    793 then
    794     jq . < "$LAST_RESPONSE"
    795     exit_fail ".wired false, expected true"
    796 fi
    797 
    798 EXPECTED_TRANSFER_SERIAL=$(jq -r .wire_details[0].expected_transfer_serial_id < "$LAST_RESPONSE")
    799 
    800 echo " OK"
    801 
    802 echo -n "Checking reconciliation details ..."
    803 STATUS=$(curl "http://localhost:9966/instances/test/private/incoming/${EXPECTED_TRANSFER_SERIAL}" \
    804     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    805 
    806 if [ "$STATUS" != "200" ]
    807 then
    808     jq . < "$LAST_RESPONSE"
    809     exit_fail "Expected 200 ok. got: $STATUS"
    810 fi
    811 
    812 SETTLED_ORDER_ID=$(jq -r .reconciliation_details[0].order_id < "$LAST_RESPONSE")
    813 
    814 if [ "$SETTLED_ORDER_ID" != "$ORDER_ID" ]
    815 then
    816     jq . < "$LAST_RESPONSE"
    817     exit_fail ".order_id false, expected $ORDER_ID, got $SETTLED_ORDER_ID"
    818 fi
    819 
    820 echo " OK"
    821 
    822 echo -n "Checking for Typst ..."
    823 if typst --version 2> /dev/null > /dev/null 
    824 then
    825     echo " OK"
    826 
    827     echo -n "Fetch order list as PDF..."
    828     STATUS=$(curl 'http://localhost:9966/instances/test/private/orders' \
    829               -H "Accept: application/pdf" \
    830               -w "%{http_code}" \
    831               -s \
    832               -o "$LAST_RESPONSE")
    833 
    834     if [ "$STATUS" != "200" ]
    835     then
    836         cat "$LAST_RESPONSE" >&2
    837         exit_fail "Expected 200, PDF created. got: $STATUS"
    838     fi
    839     # To keep PDF
    840     # mv $LAST_RESPONSE test.pdf
    841     echo "OK"
    842 else
    843     echo " Not found, skipping test"
    844 fi
    845 
    846 echo -n "Fetch order list as CSV..."
    847 STATUS=$(curl 'http://localhost:9966/instances/test/private/orders' \
    848               -H "Accept: text/csv" \
    849               -w "%{http_code}" \
    850               -s \
    851               -o "$LAST_RESPONSE")
    852 
    853 if [ "$STATUS" != "200" ]
    854 then
    855     cat "$LAST_RESPONSE" >&2
    856     exit_fail "Expected 200, CSV created. got: $STATUS"
    857 fi
    858 # To keep CSV
    859 # mv $LAST_RESPONSE test.csv
    860 echo "OK"
    861 
    862 echo -n "Fetch order list as XLS..."
    863 STATUS=$(curl 'http://localhost:9966/instances/test/private/orders' \
    864               -H "Accept: application/vnd.ms-excel" \
    865               -w "%{http_code}" \
    866               -s \
    867               -o "$LAST_RESPONSE")
    868 
    869 if [ "$STATUS" != "200" ]
    870 then
    871     cat "$LAST_RESPONSE" >&2
    872     exit_fail "Expected 200, XLS created. got: $STATUS"
    873 fi
    874 # To keep XLS
    875 # mv $LAST_RESPONSE test.xls
    876 echo "OK"
    877 
    878 
    879 echo "TEST PASSED"
    880 exit 0