merchant

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

test_merchant_transfer_tracking.sh (28758B)


      1 #!/usr/bin/env 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     http://localhost:9966/management/instances \
    123     -d '{"auth":{"method":"token","password":"super_secret"},"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}}' \
    124     -w "%{http_code}" -s -o /dev/null)
    125 
    126 if [ "$STATUS" != "204" ]
    127 then
    128     exit_fail "Expected 204, instance created. got: $STATUS"
    129 fi
    130 ADMIN_TOKEN=$(merchant_login admin super_secret)
    131 
    132 STATUS=$(curl -H "Content-Type: application/json" -X POST \
    133     -H "Authorization: Bearer $ADMIN_TOKEN" \
    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 $ADMIN_TOKEN" \
    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 $ADMIN_TOKEN" \
    160     http://localhost:9966/management/instances \
    161     -d '{"auth":{"method":"token","password":"super_secret"},"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 TOKEN_TEST=$(merchant_login test super_secret)
    169 STATUS=$(curl -H "Content-Type: application/json" -X POST \
    170     -H "Authorization: Bearer $TOKEN_TEST" \
    171     http://localhost:9966/instances/test/private/accounts \
    172     -d '{"payto_uri":"'"$SURVEY_PAYTO"'","credit_facade_url":"http://localhost:8082/accounts/survey/taler-revenue/","credit_facade_credentials":{"type":"basic","username":"survey","password":"x"}}' \
    173     -w "%{http_code}" -s -o /dev/null)
    174 
    175 if [ "$STATUS" != "200" ]
    176 then
    177     exit_fail "Expected 200 OK. Got: $STATUS"
    178 fi
    179 
    180 # CREATE ORDER AND SELL IT
    181 echo -n "Creating order to be paid..."
    182 STATUS=$(curl 'http://localhost:9966/instances/test/private/orders' \
    183     -H "Authorization: Bearer $TOKEN_TEST" \
    184     -d '{"refund_delay":{"d_us":0}, "order":{"amount":"TESTKUDOS:1","summary":"payme"}}' \
    185     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    186 
    187 if [ "$STATUS" != "200" ]
    188 then
    189     cat "$LAST_RESPONSE"
    190     exit_fail "Expected 200 ok, order created. got: $STATUS"
    191 fi
    192 
    193 echo " OK"
    194 
    195 ORDER_ID=$(jq -e -r .order_id < "$LAST_RESPONSE")
    196 #TOKEN=$(jq -e -r .token < "$LAST_RESPONSE")
    197 
    198 # Lists orders with the given extra query arguments and checks whether the
    199 # order created above is included ($2 is "some") or filtered out ($2 is
    200 # "none").  The age filters only apply to an ascending listing, hence the
    201 # hard-coded positive "limit".
    202 check_order_listing() {
    203     local args="$1"
    204     local expect="$2"
    205     local status
    206     local num
    207 
    208     status=$(curl "http://localhost:9966/instances/test/private/orders?limit=20&$args" \
    209               -H "Authorization: Bearer $TOKEN_TEST" \
    210               -w "%{http_code}" \
    211               -s \
    212               -o "$LAST_RESPONSE")
    213     if [ "$status" != "200" ]
    214     then
    215         cat "$LAST_RESPONSE"
    216         exit_fail "Expected 200 ok listing orders with '$args'. got: $status"
    217     fi
    218     num=$(jq -e -r '.orders | length' < "$LAST_RESPONSE")
    219     if [ "none" = "$expect" ] && [ "0" != "$num" ]
    220     then
    221         exit_fail "Expected no orders listed with '$args', got $num"
    222     fi
    223     if [ "some" = "$expect" ] && [ "0" = "$num" ]
    224     then
    225         exit_fail "Expected orders to be listed with '$args', got none"
    226     fi
    227 }
    228 
    229 echo -n "Checking order listing with max_age_s..."
    230 check_order_listing 'max_age_s=3600' some
    231 check_order_listing 'max_age_s=0' none
    232 echo " OK"
    233 
    234 echo -n "Checking unwired value in order listing..."
    235 STATUS=$(curl 'http://localhost:9966/instances/test/private/orders?limit=20' \
    236     -H "Authorization: Bearer $TOKEN_TEST" \
    237     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    238 if [ "$STATUS" != "200" ]
    239 then
    240     cat "$LAST_RESPONSE"
    241     exit_fail "Expected 200 ok listing unwired order. got: $STATUS"
    242 fi
    243 LIST_WIRED=$(jq -r --arg order_id "$ORDER_ID" \
    244     '.orders[] | select(.order_id == $order_id) | .wired' < "$LAST_RESPONSE")
    245 if [ "$LIST_WIRED" != "false" ]
    246 then
    247     jq . < "$LAST_RESPONSE"
    248     exit_fail "Order list .wired was not false before settlement"
    249 fi
    250 echo " OK"
    251 
    252 # "max_age" was deprecated in protocol v33, but MUST still work.
    253 echo -n "Checking order listing with deprecated max_age..."
    254 check_order_listing 'max_age=3600000' some
    255 check_order_listing 'max_age=0' none
    256 echo " OK"
    257 
    258 # If both are given, "max_age_s" wins (and the backend logs a warning).
    259 echo -n "Checking that max_age_s takes precedence over max_age..."
    260 check_order_listing 'max_age=0&max_age_s=3600' some
    261 check_order_listing 'max_age=3600000&max_age_s=0' none
    262 echo " OK"
    263 
    264 echo -n "Checking for Typst ..."
    265 if typst --version 2> /dev/null > /dev/null 
    266 then
    267     echo " OK"
    268 
    269 
    270     # Check max-age argument
    271     echo -n "Checking order list generation with max-age..."
    272     STATUS=$(curl 'http://localhost:9966/instances/test/private/orders?max_age=3600000' \
    273               -H "Authorization: Bearer $TOKEN_TEST" \
    274               -H 'Accept: application/pdf' \
    275               -w "%{http_code}" \
    276               -s \
    277               -o "$LAST_RESPONSE")
    278 
    279     if [ "$STATUS" != "200" ]
    280     then
    281         cat "$LAST_RESPONSE"
    282         exit_fail "Expected 200 ok, order created. got: $STATUS"
    283     fi
    284     # If we want to see the order list...
    285     # cat $LAST_RESPONSE > order-list.pdf
    286     echo " OK"
    287 else
    288     echo " Not found, skipping test"
    289 fi
    290 
    291 echo -n "Checking order status before claiming it..."
    292 STATUS=$(curl "http://localhost:9966/instances/test/private/orders/${ORDER_ID}" \
    293     -H "Authorization: Bearer $TOKEN_TEST" \
    294     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    295 
    296 if [ "$STATUS" != "200" ]
    297 then
    298     cat "$LAST_RESPONSE"
    299     exit_fail "Expected 200 ok, getting order info before claming it. got: $STATUS"
    300 fi
    301 PAY_URL=$(jq -e -r .taler_pay_uri < "$LAST_RESPONSE")
    302 echo "OK"
    303 
    304 NOW=$(date +%s)
    305 echo -n "Pay first order ..."
    306 taler-wallet-cli \
    307     --no-throttle \
    308     --wallet-db="$WALLET_DB" \
    309     handle-uri "${PAY_URL}" \
    310     -y \
    311     2> wallet-pay1.err > wallet-pay1.log
    312 NOW2=$(date +%s)
    313 echo " OK (took $(( NOW2 - NOW )) secs)"
    314 
    315 STATUS=$(curl "http://localhost:9966/instances/test/private/orders/${ORDER_ID}" \
    316     -H "Authorization: Bearer $TOKEN_TEST" \
    317     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    318 
    319 if [ "$STATUS" != "200" ]
    320 then
    321     cat "$LAST_RESPONSE"
    322     exit_fail "Expected 200 ok, after pay. got: $STATUS"
    323 fi
    324 
    325 ORDER_STATUS=$(jq -r .order_status < "$LAST_RESPONSE")
    326 
    327 if [ "$ORDER_STATUS" != "paid" ]
    328 then
    329     cat "$LAST_RESPONSE"
    330     exit_fail "Expected 'paid'. got: $ORDER_STATUS"
    331 fi
    332 
    333 #
    334 # WIRE TRANSFER TO MERCHANT AND NOTIFY BACKEND
    335 #
    336 
    337 #PAY_DEADLINE=$(jq -r .contract_terms.pay_deadline.t_s < "$LAST_RESPONSE")
    338 WIRE_DEADLINE=$(jq -r .contract_terms.wire_transfer_deadline.t_s < "$LAST_RESPONSE")
    339 
    340 NOW=$(date +%s)
    341 
    342 TO_SLEEP=$((3600 + WIRE_DEADLINE - NOW ))
    343 echo "waiting $TO_SLEEP secs for wire transfer"
    344 
    345 echo -n "Perform wire transfers ..."
    346 taler-exchange-aggregator -y -c $CONF -T ${TO_SLEEP}000000 -t -L INFO &> aggregator.log
    347 taler-exchange-transfer -c $CONF -t -L INFO &> transfer.log
    348 echo " DONE"
    349 
    350 echo -n "Obtaining wire transfer details from bank..."
    351 
    352 BANKDATA="$(curl 'http://localhost:8082/accounts/exchange/taler-wire-gateway/history/outgoing?delta=1' -s)"
    353 WTID=$(echo "$BANKDATA" | jq -r .outgoing_transactions[0].wtid)
    354 WURL=$(echo "$BANKDATA" | jq -r .outgoing_transactions[0].exchange_base_url)
    355 CREDIT_AMOUNT=$(echo "$BANKDATA" | jq -r .outgoing_transactions[0].amount)
    356 TARGET_PAYTO=$(echo "$BANKDATA" | jq -r .outgoing_transactions[0].credit_account)
    357 TARGET=$(echo "$TARGET_PAYTO" | awk -F = '{print $2}')
    358 
    359 # Figure out which account got paid, in order to
    360 # resort the right (and complete: including the receiver-name)
    361 # TARGET_PAYTO
    362 if echo "$SURVEY_PAYTO" | grep -q "$TARGET" > /dev/null; then
    363   TARGET_PAYTO="$SURVEY_PAYTO";
    364 fi
    365 if echo "$SURVEY_PAYTO" | grep -q "$TARGET" > /dev/null; then
    366   TARGET_PAYTO="$SURVEY_PAYTO";
    367 fi
    368 if [ "$EXCHANGE_URL" != "$WURL" ]
    369 then
    370     exit_fail "Wrong exchange URL in subject '$SUBJECT', expected $EXCHANGE_URL"
    371 fi
    372 echo " OK"
    373 
    374 set +e
    375 
    376 echo -n "Notifying merchant of correct wire transfer, but on wrong instance..."
    377 
    378 #issue 6912
    379 #here we are notifying the transfer into a wrong instance (admin) and the payto_uri of the admin instance
    380 STATUS=$(curl 'http://localhost:9966/private/transfers' \
    381     -H "Authorization: Bearer $ADMIN_TOKEN" \
    382     -d "{\"credit_amount\":\"$CREDIT_AMOUNT\",\"wtid\":\"$WTID\",\"payto_uri\":\"$TOR_PAYTO\",\"exchange_url\":\"$WURL\"}" \
    383     -m 3 \
    384     -w "%{http_code}" \
    385     -s \
    386     -o "$LAST_RESPONSE")
    387 
    388 if [ "$STATUS" != "204" ]
    389 then
    390     jq . < "$LAST_RESPONSE"
    391     exit_fail "Expected response 204 no content, after providing transfer data. Got: $STATUS"
    392 fi
    393 echo " OK"
    394 
    395 
    396 echo -n "Fetching wire transfers of ADMIN instance ..."
    397 
    398 STATUS=$(curl 'http://localhost:9966/private/transfers' \
    399               -H "Authorization: Bearer $ADMIN_TOKEN" \
    400               -w "%{http_code}" \
    401               -s \
    402               -o "$LAST_RESPONSE")
    403 
    404 if [ "$STATUS" != "200" ]
    405 then
    406     jq . < "$LAST_RESPONSE"
    407     exit_fail "Expected response 200 Ok. got: $STATUS"
    408 fi
    409 
    410 TRANSFERS_LIST_SIZE=$(jq -r '.transfers | length' < "$LAST_RESPONSE")
    411 
    412 if [ "$TRANSFERS_LIST_SIZE" != "1" ]
    413 then
    414     jq . < "$LAST_RESPONSE"
    415     exit_fail "Expected one transfer. got: $TRANSFERS_LIST_SIZE"
    416 fi
    417 
    418 echo "OK"
    419 
    420 echo -n "Fetching running taler-merchant-reconciliation on bogus transfer ..."
    421 taler-merchant-reconciliation \
    422     -c "$CONF" \
    423     -L INFO \
    424     -t &> taler-merchant-reconciliation0.log
    425 echo "OK"
    426 
    427 echo -n "Fetching wire transfers of 'test' instance ..."
    428 
    429 STATUS=$(curl 'http://localhost:9966/instances/test/private/transfers' \
    430               -H "Authorization: Bearer $TOKEN_TEST" \
    431               -w "%{http_code}" \
    432               -s \
    433               -o "$LAST_RESPONSE")
    434 
    435 if [ "$STATUS" != "200" ]
    436 then
    437     jq . < "$LAST_RESPONSE"
    438     exit_fail "Expected response 200 Ok. got: $STATUS"
    439 fi
    440 
    441 TRANSFERS_LIST_SIZE=$(jq -r '.transfers | length' < "$LAST_RESPONSE")
    442 
    443 if [ "$TRANSFERS_LIST_SIZE" != "0" ]
    444 then
    445     jq . < "$LAST_RESPONSE"
    446     exit_fail "Expected non-empty transfer list size. got: $TRANSFERS_LIST_SIZE"
    447 fi
    448 
    449 echo "OK"
    450 
    451 echo -n "Checking order status ..."
    452 STATUS=$(curl "http://localhost:9966/instances/test/private/orders/${ORDER_ID}?transfer=YES" \
    453     -H "Authorization: Bearer $TOKEN_TEST" \
    454     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    455 
    456 if [ "$STATUS" != "200" ]
    457 then
    458     jq . < "$LAST_RESPONSE"
    459     exit_fail "Expected 200 ok, after order inquiry. got: $STATUS"
    460 fi
    461 
    462 WAS_WIRED=$(jq -r .wired < "$LAST_RESPONSE")
    463 
    464 if [ "$WAS_WIRED" == "true" ]
    465 then
    466     jq . < "$LAST_RESPONSE"
    467     exit_fail ".wired is true, expected false"
    468 fi
    469 
    470 echo " OK"
    471 
    472 echo -n "Notifying merchant of correct wire transfer in the correct instance..."
    473 #this time in the correct instance so the order will be marked as wired...
    474 
    475 echo -n "Running taler-merchant-wirewatch to check transfer ..."
    476 taler-merchant-wirewatch \
    477     -c $CONF \
    478     -t \
    479     -L INFO &> taler-merchant-wirewatch1.log
    480 echo " DONE"
    481 
    482 echo -n "Post-check for exchange deposit ..."
    483 taler-merchant-depositcheck \
    484     -c $CONF \
    485     -t \
    486     -e "http://localhost:8081/" \
    487     -T ${TO_SLEEP}000000 \
    488     -L INFO &> depositcheck1a.log
    489 echo " DONE"
    490 
    491 echo -n "Fetching running taler-merchant-reconciliation on good transfer ..."
    492 taler-merchant-reconciliation \
    493     -c $CONF \
    494     -L INFO \
    495     -T ${TO_SLEEP}000000 \
    496     -t &> taler-merchant-reconciliation1.log
    497 echo "OK"
    498 
    499 echo -n "Fetching wire transfers of TEST instance ..."
    500 
    501 STATUS=$(curl 'http://localhost:9966/instances/test/private/transfers' \
    502               -H "Authorization: Bearer $TOKEN_TEST" \
    503               -w "%{http_code}" \
    504               -s \
    505               -o "$LAST_RESPONSE")
    506 
    507 if [ "$STATUS" != "200" ]
    508 then
    509     jq . < "$LAST_RESPONSE"
    510     exit_fail "Expected response 200 Ok. got: $STATUS"
    511 fi
    512 
    513 TRANSFERS_LIST_SIZE=$(jq -r '.transfers | length' < "$LAST_RESPONSE")
    514 
    515 if [ "$TRANSFERS_LIST_SIZE" != "1" ]
    516 then
    517     jq . < "$LAST_RESPONSE"
    518     exit_fail "Expected one transfer. got: $TRANSFERS_LIST_SIZE"
    519 fi
    520 
    521 echo "OK"
    522 
    523 echo -n "Checking order status ..."
    524 STATUS=$(curl "http://localhost:9966/instances/test/private/orders/${ORDER_ID}" \
    525     -H "Authorization: Bearer $TOKEN_TEST" \
    526     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    527 
    528 if [ "$STATUS" != "200" ]
    529 then
    530     jq . < "$LAST_RESPONSE"
    531     exit_fail "Expected 200 ok, after order inquiry. got: $STATUS"
    532 fi
    533 
    534 WAS_WIRED=$(jq -r .wired < "$LAST_RESPONSE")
    535 
    536 if [ "$WAS_WIRED" != "true" ]
    537 then
    538     jq . < "$LAST_RESPONSE"
    539     exit_fail ".wired false, expected true"
    540 fi
    541 
    542 echo " OK"
    543 
    544 echo -n "Checking wired value and filter in order listing..."
    545 STATUS=$(curl 'http://localhost:9966/instances/test/private/orders?limit=20&wired=YES' \
    546     -H "Authorization: Bearer $TOKEN_TEST" \
    547     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    548 if [ "$STATUS" != "200" ]
    549 then
    550     cat "$LAST_RESPONSE"
    551     exit_fail "Expected 200 ok listing wired order. got: $STATUS"
    552 fi
    553 LIST_WIRED=$(jq -e -r --arg order_id "$ORDER_ID" \
    554     '.orders[] | select(.order_id == $order_id) | .wired' < "$LAST_RESPONSE")
    555 if [ "$LIST_WIRED" != "true" ]
    556 then
    557     jq . < "$LAST_RESPONSE"
    558     exit_fail "Order list .wired was not true after settlement"
    559 fi
    560 echo " OK"
    561 
    562 
    563 echo "================== 2nd order ====================== "
    564 
    565 
    566 
    567 # CREATE ORDER AND SELL IT
    568 echo -n "Creating 2nd order to be paid..."
    569 STATUS=$(curl 'http://localhost:9966/instances/test/private/orders' \
    570     -H "Authorization: Bearer $TOKEN_TEST" \
    571     -d '{"refund_delay":{"d_us":0}, "order":{"amount":"TESTKUDOS:2","summary":"payme"}}' \
    572     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    573 
    574 if [ "$STATUS" != "200" ]
    575 then
    576     cat "$LAST_RESPONSE"
    577     exit_fail "Expected 200 ok, order created. got: $STATUS"
    578 fi
    579 
    580 ORDER_ID=$(jq -e -r .order_id < "$LAST_RESPONSE")
    581 #TOKEN=$(jq -e -r .token < "$LAST_RESPONSE")
    582 
    583 STATUS=$(curl "http://localhost:9966/instances/test/private/orders/${ORDER_ID}" \
    584     -H "Authorization: Bearer $TOKEN_TEST" \
    585     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    586 
    587 if [ "$STATUS" != "200" ]
    588 then
    589     cat "$LAST_RESPONSE"
    590     exit_fail "Expected 200 ok, getting order info before claming it. got: $STATUS"
    591 fi
    592 PAY_URL=$(jq -e -r .taler_pay_uri < "$LAST_RESPONSE")
    593 echo "OK"
    594 
    595 NOW=$(date +%s)
    596 echo -n "Pay second order ..."
    597 taler-wallet-cli \
    598     --no-throttle \
    599     --wallet-db="$WALLET_DB" \
    600     handle-uri "${PAY_URL}" \
    601     -y \
    602     2> wallet-pay2.err > wallet-pay2.log
    603 NOW2=$(date +%s)
    604 echo " OK (took $(( NOW2 - NOW )) secs)"
    605 
    606 STATUS=$(curl "http://localhost:9966/instances/test/private/orders/${ORDER_ID}" \
    607     -H "Authorization: Bearer $TOKEN_TEST" \
    608     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    609 
    610 if [ "$STATUS" != "200" ]
    611 then
    612     cat "$LAST_RESPONSE"
    613     exit_fail "Expected 200 ok, after pay. got: $STATUS"
    614 fi
    615 
    616 ORDER_STATUS=$(jq -r .order_status < "$LAST_RESPONSE")
    617 
    618 if [ "$ORDER_STATUS" != "paid" ]
    619 then
    620     cat "$LAST_RESPONSE"
    621     exit_fail "Expected 'paid'. got: $ORDER_STATUS"
    622 fi
    623 
    624 #
    625 # WIRE TRANSFER TO MERCHANT AND NOTIFY BACKEND
    626 #
    627 
    628 #PAY_DEADLINE=$(jq -r .contract_terms.pay_deadline.t_s < "$LAST_RESPONSE")
    629 WIRE_DEADLINE=$(jq -r .contract_terms.wire_transfer_deadline.t_s < "$LAST_RESPONSE")
    630 
    631 NOW=$(date +%s)
    632 
    633 TO_SLEEP=$((3600 + WIRE_DEADLINE - NOW ))
    634 echo "waiting $TO_SLEEP secs for wire transfer"
    635 
    636 echo -n "Pre-check for exchange deposit ..."
    637 taler-merchant-depositcheck \
    638     -c $CONF \
    639     -t \
    640     -e "http://localhost:8081/" \
    641     -L INFO &> depositcheck1b.log
    642 echo " DONE"
    643 
    644 echo -n "Perform wire transfers ..."
    645 taler-exchange-aggregator \
    646     -y \
    647     -c $CONF \
    648     -T ${TO_SLEEP}000000 \
    649     -t \
    650     -L INFO &> aggregator2.log
    651 taler-exchange-transfer \
    652     -c $CONF \
    653     -t \
    654     -L INFO &> transfer2.log
    655 echo " DONE"
    656 
    657 echo -n "Post-check for exchange deposit ..."
    658 taler-merchant-depositcheck \
    659     -c $CONF \
    660     -t \
    661     -e "http://localhost:8081/" \
    662     -T ${TO_SLEEP}000000 \
    663     -L INFO &> depositcheck1c.log
    664 echo " DONE"
    665 
    666 
    667 echo -n "Obtaining wire transfer details from bank..."
    668 
    669 BANKDATA="$(curl 'http://localhost:8082/accounts/exchange/taler-wire-gateway/history/outgoing?delta=2' -s)"
    670 
    671 WTID=$(echo "$BANKDATA" | jq -r .outgoing_transactions[1].wtid)
    672 WURL=$(echo "$BANKDATA" | jq -r .outgoing_transactions[1].exchange_base_url)
    673 CREDIT_AMOUNT=$(echo "$BANKDATA" | jq -r .outgoing_transactions[1].amount)
    674 TARGET_PAYTO=$(echo "$BANKDATA" | jq -r .outgoing_transactions[1].credit_account)
    675 TARGET=$(echo "$TARGET_PAYTO" | awk -F = '{print $2}')
    676 
    677 # Figure out which account got paid, in order to
    678 # resort the right (and complete: including the receiver-name)
    679 # TARGET_PAYTO
    680 if echo "$SURVEY_PAYTO" | grep -q "$TARGET" > /dev/null; then
    681   TARGET_PAYTO="$SURVEY_PAYTO";
    682 fi
    683 if echo "$SURVEY_PAYTO" | grep -q "$TARGET" > /dev/null; then
    684   TARGET_PAYTO="$SURVEY_PAYTO";
    685 fi
    686 if [ "$EXCHANGE_URL" != "$WURL" ]
    687 then
    688     exit_fail "Wrong exchange URL in subject '$SUBJECT', expected $EXCHANGE_URL"
    689 fi
    690 echo " OK"
    691 
    692 echo -n "Running taler-merchant-wirewatch to check transfer ..."
    693 taler-merchant-wirewatch \
    694     -c $CONF \
    695     -t \
    696     -L INFO &> taler-merchant-wirewatch2.log
    697 echo " DONE"
    698 
    699 echo -n "Fetching running taler-merchant-reconciliation on good transfer ..."
    700 taler-merchant-reconciliation \
    701     -c $CONF \
    702     -L INFO \
    703     -T ${TO_SLEEP}000000 \
    704     -t &> taler-merchant-reconciliation2.log
    705 echo "OK"
    706 
    707 echo -n "Fetching wire transfers of TEST instance ..."
    708 
    709 STATUS=$(curl 'http://localhost:9966/instances/test/private/transfers' \
    710               -H "Authorization: Bearer $TOKEN_TEST" \
    711               -w "%{http_code}" \
    712               -s \
    713               -o "$LAST_RESPONSE")
    714 
    715 if [ "$STATUS" != "200" ]
    716 then
    717     jq . < "$LAST_RESPONSE"
    718     exit_fail "Expected response 200 Ok. got: $STATUS"
    719 fi
    720 
    721 TRANSFERS_LIST_SIZE=$(jq -r '.transfers | length' < "$LAST_RESPONSE")
    722 
    723 if [ "$TRANSFERS_LIST_SIZE" != "2" ]
    724 then
    725     jq . < "$LAST_RESPONSE"
    726     exit_fail "Expected two transfers. got: $TRANSFERS_LIST_SIZE"
    727 fi
    728 
    729 echo "OK"
    730 
    731 echo -n "Checking order status ..."
    732 STATUS=$(curl "http://localhost:9966/instances/test/private/orders/${ORDER_ID}" \
    733     -H "Authorization: Bearer $TOKEN_TEST" \
    734     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    735 
    736 if [ "$STATUS" != "200" ]
    737 then
    738     jq . < "$LAST_RESPONSE"
    739     exit_fail "Expected 200 ok, after order inquiry. got: $STATUS"
    740 fi
    741 
    742 WAS_WIRED=$(jq -r .wired < "$LAST_RESPONSE")
    743 
    744 if [ "$WAS_WIRED" != "true" ]
    745 then
    746     jq . < "$LAST_RESPONSE"
    747     exit_fail ".wired false, expected true"
    748 fi
    749 
    750 echo " OK"
    751 
    752 echo "================== 3rd order ====================== "
    753 
    754 # CREATE ORDER AND SELL IT
    755 echo -n "Creating 3rd order to be paid..."
    756 STATUS=$(curl 'http://localhost:9966/instances/test/private/orders' \
    757     -H "Authorization: Bearer $TOKEN_TEST" \
    758     -d '{"refund_delay":{"d_us":0}, "order":{"amount":"TESTKUDOS:3","summary":"payme"}}' \
    759     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    760 
    761 if [ "$STATUS" != "200" ]
    762 then
    763     cat "$LAST_RESPONSE"
    764     exit_fail "Expected 200 ok, order created. got: $STATUS"
    765 fi
    766 
    767 ORDER_ID=$(jq -e -r .order_id < "$LAST_RESPONSE")
    768 #TOKEN=$(jq -e -r .token < "$LAST_RESPONSE")
    769 
    770 STATUS=$(curl "http://localhost:9966/instances/test/private/orders/${ORDER_ID}" \
    771     -H "Authorization: Bearer $TOKEN_TEST" \
    772     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    773 
    774 if [ "$STATUS" != "200" ]
    775 then
    776     cat "$LAST_RESPONSE"
    777     exit_fail "Expected 200 ok, getting order info before claming it. got: $STATUS"
    778 fi
    779 PAY_URL=$(jq -e -r .taler_pay_uri < "$LAST_RESPONSE")
    780 echo "OK"
    781 
    782 NOW=$(date +%s)
    783 echo -n "Pay third order ..."
    784 taler-wallet-cli \
    785     --no-throttle \
    786     --wallet-db="$WALLET_DB" \
    787     handle-uri "${PAY_URL}" \
    788     -y \
    789     2> wallet-pay2.err > wallet-pay2.log
    790 NOW2=$(date +%s)
    791 echo " OK (took $(( NOW2 - NOW )) secs)"
    792 
    793 STATUS=$(curl "http://localhost:9966/instances/test/private/orders/${ORDER_ID}" \
    794     -H "Authorization: Bearer $TOKEN_TEST" \
    795     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    796 
    797 if [ "$STATUS" != "200" ]
    798 then
    799     cat "$LAST_RESPONSE"
    800     exit_fail "Expected 200 ok, after pay. got: $STATUS"
    801 fi
    802 
    803 ORDER_STATUS=$(jq -r .order_status < "$LAST_RESPONSE")
    804 
    805 if [ "$ORDER_STATUS" != "paid" ]
    806 then
    807     cat "$LAST_RESPONSE"
    808     exit_fail "Expected 'paid'. got: $ORDER_STATUS"
    809 fi
    810 
    811 #
    812 # WIRE TRANSFER TO MERCHANT AND NOTIFY BACKEND
    813 #
    814 
    815 #PAY_DEADLINE=$(jq -r .contract_terms.pay_deadline.t_s < "$LAST_RESPONSE")
    816 WIRE_DEADLINE=$(jq -r .contract_terms.wire_transfer_deadline.t_s < "$LAST_RESPONSE")
    817 
    818 NOW=$(date +%s)
    819 
    820 TO_SLEEP=$((1200 + WIRE_DEADLINE - NOW ))
    821 echo "waiting $TO_SLEEP secs for wire transfer"
    822 
    823 echo -n "Perform wire transfers for 3rd order..."
    824 taler-exchange-aggregator \
    825     -y \
    826     -c $CONF \
    827     -T ${TO_SLEEP}000000 \
    828     -t \
    829     -L INFO &> aggregator3.log
    830 taler-exchange-transfer \
    831     -c $CONF \
    832     -t \
    833     -L INFO &> transfer3.log
    834 echo " DONE"
    835 
    836 echo -n "Running taler-merchant-wirewatch to check transfer ..."
    837 taler-merchant-wirewatch \
    838     -c $CONF \
    839     -t \
    840     -L INFO &> taler-merchant-wirewatch3.log
    841 echo " DONE"
    842 
    843 echo -n "Post-wirewatch check for exchange deposit ..."
    844 taler-merchant-depositcheck \
    845     -c $CONF \
    846     -t \
    847     -e "http://localhost:8081/" \
    848     -T ${TO_SLEEP}000000 \
    849     -L INFO &> depositcheck1d.log
    850 echo " DONE"
    851 
    852 echo -n "Fetching wire transfers of TEST instance ..."
    853 
    854 STATUS=$(curl 'http://localhost:9966/instances/test/private/transfers' \
    855               -H "Authorization: Bearer $TOKEN_TEST" \
    856               -w "%{http_code}" \
    857               -s \
    858               -o "$LAST_RESPONSE")
    859 
    860 if [ "$STATUS" != "200" ]
    861 then
    862     jq . < "$LAST_RESPONSE"
    863     exit_fail "Expected response 200 Ok. got: $STATUS"
    864 fi
    865 
    866 TRANSFERS_LIST_SIZE=$(jq -r '.transfers | length' < "$LAST_RESPONSE")
    867 
    868 if [ "$TRANSFERS_LIST_SIZE" != "3" ]
    869 then
    870     jq . < "$LAST_RESPONSE"
    871     exit_fail "Expected three transfers. got: $TRANSFERS_LIST_SIZE"
    872 fi
    873 
    874 echo "OK"
    875 
    876 echo -n "Fetching running taler-merchant-reconciliation on good transfer ..."
    877 taler-merchant-reconciliation \
    878     -c $CONF \
    879     -L INFO \
    880     -T ${TO_SLEEP}000000 \
    881     -t &> taler-merchant-reconciliation3.log
    882 echo "OK"
    883 
    884 echo -n "Checking order status ..."
    885 STATUS=$(curl "http://localhost:9966/instances/test/private/orders/${ORDER_ID}" \
    886     -H "Authorization: Bearer $TOKEN_TEST" \
    887     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    888 
    889 if [ "$STATUS" != "200" ]
    890 then
    891     jq . < "$LAST_RESPONSE"
    892     exit_fail "Expected 200 ok, after order inquiry. got: $STATUS"
    893 fi
    894 
    895 WAS_WIRED=$(jq -r .wired < "$LAST_RESPONSE")
    896 
    897 if [ "$WAS_WIRED" != "true" ]
    898 then
    899     jq . < "$LAST_RESPONSE"
    900     exit_fail ".wired false, expected true"
    901 fi
    902 
    903 EXPECTED_TRANSFER_SERIAL=$(jq -r .wire_details[0].expected_transfer_serial_id < "$LAST_RESPONSE")
    904 
    905 echo " OK"
    906 
    907 echo -n "Checking reconciliation details ..."
    908 STATUS=$(curl "http://localhost:9966/instances/test/private/incoming/${EXPECTED_TRANSFER_SERIAL}" \
    909     -H "Authorization: Bearer $TOKEN_TEST" \
    910     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    911 
    912 if [ "$STATUS" != "200" ]
    913 then
    914     jq . < "$LAST_RESPONSE"
    915     exit_fail "Expected 200 ok. got: $STATUS"
    916 fi
    917 
    918 SETTLED_ORDER_ID=$(jq -r .reconciliation_details[0].order_id < "$LAST_RESPONSE")
    919 
    920 if [ "$SETTLED_ORDER_ID" != "$ORDER_ID" ]
    921 then
    922     jq . < "$LAST_RESPONSE"
    923     exit_fail ".order_id false, expected $ORDER_ID, got $SETTLED_ORDER_ID"
    924 fi
    925 
    926 echo " OK"
    927 
    928 echo -n "Checking for Typst ..."
    929 if typst --version 2> /dev/null > /dev/null 
    930 then
    931     echo " OK"
    932 
    933     echo -n "Fetch order list as PDF..."
    934     STATUS=$(curl 'http://localhost:9966/instances/test/private/orders' \
    935               -H "Authorization: Bearer $TOKEN_TEST" \
    936               -H "Accept: application/pdf" \
    937               -w "%{http_code}" \
    938               -s \
    939               -o "$LAST_RESPONSE")
    940 
    941     if [ "$STATUS" != "200" ]
    942     then
    943         cat "$LAST_RESPONSE" >&2
    944         exit_fail "Expected 200, PDF created. got: $STATUS"
    945     fi
    946     # To keep PDF
    947     # mv $LAST_RESPONSE test.pdf
    948     echo "OK"
    949 else
    950     echo " Not found, skipping test"
    951 fi
    952 
    953 echo -n "Fetch order list as CSV..."
    954 STATUS=$(curl 'http://localhost:9966/instances/test/private/orders' \
    955               -H "Authorization: Bearer $TOKEN_TEST" \
    956               -H "Accept: text/csv" \
    957               -w "%{http_code}" \
    958               -s \
    959               -o "$LAST_RESPONSE")
    960 
    961 if [ "$STATUS" != "200" ]
    962 then
    963     cat "$LAST_RESPONSE" >&2
    964     exit_fail "Expected 200, CSV created. got: $STATUS"
    965 fi
    966 # To keep CSV
    967 # mv $LAST_RESPONSE test.csv
    968 echo "OK"
    969 
    970 echo -n "Fetch order list as XLS..."
    971 STATUS=$(curl 'http://localhost:9966/instances/test/private/orders' \
    972               -H "Authorization: Bearer $TOKEN_TEST" \
    973               -H "Accept: application/vnd.ms-excel" \
    974               -w "%{http_code}" \
    975               -s \
    976               -o "$LAST_RESPONSE")
    977 
    978 if [ "$STATUS" != "200" ]
    979 then
    980     cat "$LAST_RESPONSE" >&2
    981     exit_fail "Expected 200, XLS created. got: $STATUS"
    982 fi
    983 # To keep XLS
    984 # mv $LAST_RESPONSE test.xls
    985 echo "OK"
    986 
    987 
    988 echo "TEST PASSED"
    989 exit 0