merchant

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

test_merchant_order_creation.sh (22094B)


      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 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 
     95 echo -n "Configuring merchant instance ..."
     96 
     97 STATUS=$(curl -H "Content-Type: application/json" -X POST \
     98     -H 'Authorization: Bearer secret-token:super_secret' \
     99     http://localhost:9966/management/instances \
    100     -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}}' \
    101     -w "%{http_code}" -s -o /dev/null)
    102 
    103 if [ "$STATUS" != "204" ]
    104 then
    105     exit_fail "Expected '204 No content' response. Got instead $STATUS"
    106 fi
    107 echo "Ok"
    108 
    109 echo -n "Configuring merchant account ..."
    110 
    111 if [ 1 = "$USE_FAKEBANK" ]
    112 then
    113     FORTYTHREE="payto://x-taler-bank/localhost/fortythree?receiver-name=fortythree"
    114 else
    115     FORTYTHREE=$(get_payto_uri fortythree x)
    116 fi
    117 # create with 2 bank account addresses
    118 STATUS=$(curl -H "Content-Type: application/json" -X POST \
    119     -H 'Authorization: Bearer secret-token:super_secret' \
    120     http://localhost:9966/private/accounts \
    121     -d '{"payto_uri":"'"$FORTYTHREE"'"}' \
    122     -w "%{http_code}" -s -o /dev/null)
    123 
    124 if [ "$STATUS" != "200" ]
    125 then
    126     exit_fail "Expected '200 OK' response. Got instead $STATUS"
    127 fi
    128 STATUS=$(curl -H "Content-Type: application/json" -X POST \
    129     -H 'Authorization: Bearer secret-token:super_secret' \
    130     http://localhost:9966/private/accounts \
    131     -d '{"payto_uri":"payto://iban/SANDBOXX/DE270744?receiver-name=Forty+Four"}' \
    132     -w "%{http_code}" -s -o /dev/null)
    133 
    134 if [ "$STATUS" != "200" ]
    135 then
    136     exit_fail "Expected '200 OK' response. Got instead $STATUS"
    137 fi
    138 
    139 echo "Ok"
    140 
    141 
    142 echo -n "Get accounts..."
    143 STATUS=$(curl http://localhost:9966/private/accounts \
    144     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    145 PAY_URI=$(jq -r .accounts[1].payto_uri < "$LAST_RESPONSE")
    146 H_WIRE=$(jq -r .accounts[1].h_wire < "$LAST_RESPONSE")
    147 if [ "$PAY_URI" != "payto://iban/SANDBOXX/DE270744?receiver-name=Forty+Four" ]
    148 then
    149     cat "$LAST_RESPONSE" >&2
    150     exit_fail "Expected second payto URI. Got $PAY_URI"
    151 fi
    152 echo "OK"
    153 
    154 # remove one account address
    155 echo -n "Deleting one account ..."
    156 STATUS=$(curl -H "Content-Type: application/json" -X PATCH \
    157     -H 'Authorization: Bearer secret-token:super_secret' \
    158     "http://localhost:9966/private/accounts/${H_WIRE}" \
    159     -X DELETE \
    160     -w "%{http_code}" -s -o /dev/null)
    161 
    162 if [ "$STATUS" != "204" ]
    163 then
    164     exit_fail "Expected '204 No content' for deletion of ${H_WIRE}. Got instead: $STATUS"
    165 fi
    166 echo "OK"
    167 
    168 RANDOM_IMG='data:image/png;base64,abcdefg'
    169 
    170 #
    171 # CREATE AN ORDER WITHOUT TOKEN
    172 #
    173 
    174 echo -n "Creating order without TOKEN..."
    175 STATUS=$(curl 'http://localhost:9966/private/orders' \
    176     -d '{"create_token":false,"refund_delay":{"d_us":0},"order":{"amount":"TESTKUDOS:7","summary":"3","products":[{"description":"desct","image":"'"$RANDOM_IMG"'","price":"TESTKUDOS:1","taxes":[],"unit":"u","quantity":1}]}}' \
    177     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    178 
    179 if [ "$STATUS" != "200" ]
    180 then
    181     cat "$LAST_RESPONSE" >&2
    182     exit_fail "Expected 200, order created. got: $STATUS"
    183 fi
    184 
    185 ORDER_ID=$(jq -r .order_id < "$LAST_RESPONSE")
    186 TOKEN=$(jq -r .token < "$LAST_RESPONSE")
    187 
    188 if [ "$TOKEN" != "null" ]
    189 then
    190     exit_fail "token should be null, got: $TOKEN"
    191 fi
    192 
    193 echo "OK"
    194 
    195 echo -n "Checking created order without TOKEN..."
    196 STATUS=$(curl http://localhost:9966/orders/"$ORDER_ID" \
    197               -w "%{http_code}" -s -o "$LAST_RESPONSE")
    198 PAY_URI=$(jq -r .taler_pay_uri < "$LAST_RESPONSE")
    199 if [ "$PAY_URI" == "null" ]
    200 then
    201     cat "$LAST_RESPONSE" >&2
    202     exit_fail "Expected non-NULL payuri. got $PAY_URI"
    203 fi
    204 echo "OK"
    205 
    206 #
    207 # CREATE AN ORDER WITHOUT TOKEN WITH FULLFILMENT URL
    208 #
    209 
    210 echo -n "Creating order without TOKEN and fullfilment URL..."
    211 STATUS=$(curl 'http://localhost:9966/private/orders' \
    212     -d '{"create_token":false,"refund_delay":{"d_us":0},"order":{"fulfillment_url":"go_here_please", "amount":"TESTKUDOS:7","summary":"3","products":[{"description":"desct","image":"'"$RANDOM_IMG"'","price":"TESTKUDOS:1","taxes":[],"unit":"u","quantity":1}]}}' \
    213     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    214 
    215 if [ "$STATUS" != "200" ]
    216 then
    217     cat "$LAST_RESPONSE" >&2
    218     exit_fail "Expected 200, order created. got: $STATUS"
    219 fi
    220 
    221 ORDER_ID=$(jq -r .order_id < "$LAST_RESPONSE")
    222 TOKEN=$(jq -r .token < "$LAST_RESPONSE")
    223 
    224 if [ "$TOKEN" != "null" ]
    225 then
    226     exit_fail "Token should be null, got: $TOKEN"
    227 fi
    228 
    229 STATUS=$(curl http://localhost:9966/orders/"$ORDER_ID" \
    230     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    231 
    232 PAY_URI=$(jq -r .taler_pay_uri < "$LAST_RESPONSE")
    233 FULLFILMENT_URL=$(jq -r .fulfillment_url < "$LAST_RESPONSE")
    234 
    235 if [ "$FULLFILMENT_URL" != "go_here_please" ]
    236 then
    237     cat "$LAST_RESPONSE" >&2
    238     exit_fail "Expected a pay URI. got: $PAY_URI"
    239 fi
    240 
    241 if [ "$PAY_URI" == "null" ]
    242 then
    243     cat "$LAST_RESPONSE" >&2
    244     exit_fail "Expected non-NULL pay URI. Got: $PAY_URI"
    245 fi
    246 echo "OK"
    247 
    248 #
    249 # CREATE A DISCOUNT TOKEN FAMILY
    250 #
    251 echo -n "Creating discount token family..."
    252 VALID_AFTER="{\"t_s\": $(date +%s)}" # now
    253 VALID_BEFORE="{\"t_s\": $(date +%s -d "+30 days")}" # 30 days from now
    254 DURATION="{\"d_us\": $(expr 30 \* 24 \* 60 \* 60 \* 1000000)}" # 30 days
    255 STATUS=$(curl 'http://localhost:9966/private/tokenfamilies' \
    256               -d "{\"kind\": \"discount\", \"slug\":\"test-discount\", \"name\": \"Test discount\", \"description\": \"Less money $$\", \"description_i18n\": {\"en\": \"Less money $$\", \"es\": \"Menos dinero $$\"}, \"valid_after\": $VALID_AFTER, \"valid_before\": $VALID_BEFORE, \"duration\": $DURATION, \"validity_granularity\": $DURATION}" \
    257               -w "%{http_code}" -s -o /dev/null)
    258 if [ "$STATUS" != "204" ]
    259 then
    260     exit_fail "Expected '204 OK' response. Got instead $STATUS"
    261 fi
    262 echo "Ok"
    263 
    264 #
    265 # CREATE A SUBSCRIPTION TOKEN FAMILY
    266 #
    267 echo -n "Creating subscription token family..."
    268 VALID_AFTER="{\"t_s\": $(date +%s)}" # now
    269 VALID_BEFORE="{\"t_s\": $(date +%s -d "+30 days")}" # 30 days from now
    270 DURATION="{\"d_us\": $(expr 30 \* 24 \* 60 \* 60 \* 1000000)}" # 30 days
    271 STATUS=$(curl 'http://localhost:9966/private/tokenfamilies' \
    272               -d "{\"kind\": \"subscription\", \"slug\":\"test-subscription\", \"name\": \"Test subscription\", \"description\": \"Money per month\", \"description_i18n\": {\"en\": \"Money $$$ per month\", \"es\": \"Dinero $$$ al mes\"}, \"valid_after\": $VALID_AFTER, \"valid_before\": $VALID_BEFORE, \"duration\": $DURATION, \"validity_granularity\": $DURATION}" \
    273               -w "%{http_code}" -s -o /dev/null)
    274 if [ "$STATUS" != "204" ]
    275 then
    276     exit_fail "Expected '204 OK' response. Got instead $STATUS"
    277 fi
    278 echo "Ok"
    279 
    280 #
    281 # CREATE AN DISCOUNTABLE ORDER WITHOUT TOKEN
    282 #
    283 RANDOM_IMG='data:image/png;base64,abcdefg'
    284 
    285 echo -n "Creating discountable order..."
    286 STATUS=$(curl 'http://localhost:9966/private/orders' \
    287     -d '{"create_token":true,"refund_delay":{"d_us":0},"order":{"version":1,"summary":"Expensive purchase","products":[{"description":"Expensive steak","quantity":2,"unit":"pieces","price":"TESTKUDOS:100"}],"choices":[{"amount":"TESTKUDOS:100"},{"amount":"TESTKUDOS:10","inputs":[{"type":"token","token_family_slug":"test-discount","count":1}],"outputs":[]}]}}' \
    288     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    289 
    290 if [ "$STATUS" != "200" ]
    291 then
    292     cat "$LAST_RESPONSE" >&2
    293     exit_fail "Expected 200, order created. got: $STATUS"
    294 fi
    295 echo "OK"
    296 
    297 ORDER_ID=$(jq -r .order_id < "$LAST_RESPONSE")
    298 TOKEN=$(jq -r .token < "$LAST_RESPONSE")
    299 
    300 echo -n "Checking created order..."
    301 STATUS=$(curl http://localhost:9966/orders/"$ORDER_ID?token=$TOKEN" \
    302               -w "%{http_code}" -s -o "$LAST_RESPONSE")
    303 PAY_URI=$(jq -r .taler_pay_uri < "$LAST_RESPONSE")
    304 if [ "$PAY_URI" == "null" ]
    305 then
    306     cat "$LAST_RESPONSE" >&2
    307     exit_fail "Expected non-NULL payuri. got $PAY_URI"
    308 fi
    309 echo "OK"
    310 
    311 echo -n "Claming order with token family ..."
    312 STATUS=$(curl http://localhost:9966/orders/"$ORDER_ID"/claim \
    313     -d '{"nonce":"","token":"'"$TOKEN"'"}' \
    314     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    315 
    316 if [ "$STATUS" != "200" ]
    317 then
    318     cat "$LAST_RESPONSE" >&2
    319     exit_fail "Expected 200, order claimed. got: $STATUS"
    320 fi
    321 
    322 echo " OK"
    323 
    324 # echo -n "Fetching pay URL for order ..."
    325 # STATUS=$(curl "http://localhost:9966/private/orders/${ORDER_ID}" \
    326 #     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    327 
    328 # if [ "$STATUS" != "200" ]
    329 # then
    330 #     jq . < "$LAST_RESPONSE"
    331 #     exit_fail "Expected 200, getting order info before claming it. got: $STATUS"
    332 # fi
    333 
    334 # PAY_URL=$(jq -e -r .taler_pay_uri < "$LAST_RESPONSE")
    335 
    336 # echo " OK"
    337 
    338 # NOW=$(date +%s)
    339 
    340 # echo -n "Pay for order ${PAY_URL} ..."
    341 # taler-wallet-cli --no-throttle --wallet-db="$WALLET_DB" handle-uri "${PAY_URL}" -y 2> wallet-pay1.err > wallet-pay1.log
    342 # taler-wallet-cli --no-throttle --wallet-db="$WALLET_DB" run-until-done 2> wallet-finish-pay1.err > wallet-finish-pay1.log
    343 # NOW2=$(date +%s)
    344 # echo " OK (took $(( NOW2 - NOW )) secs )"
    345 
    346 #
    347 # CREATE ORDER WITH NON-INVENTORY AND CHECK
    348 #
    349 
    350 echo -n "Creating order with non-inventory products..."
    351 STATUS=$(curl 'http://localhost:9966/private/orders' \
    352     -d '{"refund_delay":{"d_us":0},"order":{"amount":"TESTKUDOS:7","summary":"3","products":[{"description":"desct","image":"'"$RANDOM_IMG"'","price":"TESTKUDOS:1","taxes":[],"unit":"u","quantity":1}]}}' \
    353     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    354 
    355 if [ "$STATUS" != "200" ]
    356 then
    357     cat "$LAST_RESPONSE" >&2
    358     exit_fail "Expected 200, order created. got: $STATUS"
    359 fi
    360 
    361 ORDER_ID=$(jq -r .order_id < "$LAST_RESPONSE")
    362 TOKEN=$(jq -r .token < "$LAST_RESPONSE")
    363 
    364 STATUS=$(curl http://localhost:9966/orders/"$ORDER_ID"/claim \
    365     -d '{"nonce":"","token":"'"$TOKEN"'"}' \
    366     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    367 
    368 if [ "$STATUS" != "200" ]
    369 then
    370     cat "$LAST_RESPONSE" >&2
    371     exit_fail "Expected 200, order claimed. got: $STATUS"
    372 fi
    373 
    374 QUANTITY=$(jq -r .contract_terms.products[0].quantity < "$LAST_RESPONSE")
    375 if [ "$QUANTITY" != "1" ]
    376 then
    377     exit_fail "Expected quantity 1. got: $QUANTITY"
    378 fi
    379 
    380 IMAGE=$(jq -r .contract_terms.products[0].image < "$LAST_RESPONSE")
    381 if [ "$IMAGE" != "$RANDOM_IMG" ]
    382 then
    383     exit_fail "Expected $RANDOM_IMG but got something else: $IMAGE"
    384 fi
    385 echo "OK"
    386 
    387 
    388 #
    389 # CREATE INVENTORY PRODUCT AND CLAIM IT
    390 #
    391 
    392 echo -n "Creating product..."
    393 STATUS=$(curl 'http://localhost:9966/private/products' \
    394     -d '{"product_id":"2","description":"product with id 2 and price :15","unit_price":["TESTKUDOS:15"],"unit_total_stock":"2","description_i18n":{},"unit":"","image":"'$RANDOM_IMG'","taxes":[],"address":{},"next_restock":{"t_s":"never"}}' \
    395     -w "%{http_code}" -s -o /dev/null)
    396 
    397 if [ "$STATUS" != "204" ]
    398 then
    399     exit_fail "Expected 204, product created. got: $STATUS"
    400 fi
    401 echo "OK"
    402 
    403 echo -n "Creating order with inventory products..."
    404 STATUS=$(curl 'http://localhost:9966/private/orders' \
    405     -d '{"refund_delay":{"d_us":0},"order":{"amount":"TESTKUDOS:7","summary":"3"},"inventory_products":[{"product_id":"2","unit_quantity":"1"}]}' \
    406     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    407 
    408 
    409 if [ "$STATUS" != "200" ]
    410 then
    411     jq . < "$LAST_RESPONSE"
    412     exit_fail "Expected 200 OK, order created response. Got: $STATUS"
    413 fi
    414 
    415 ORDER_ID=$(jq -e -r .order_id < "$LAST_RESPONSE")
    416 TOKEN=$(jq -e -r .token < "$LAST_RESPONSE")
    417 
    418 STATUS=$(curl http://localhost:9966/orders/"$ORDER_ID"/claim \
    419     -d '{"nonce":"","token":"'"$TOKEN"'"}' \
    420     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    421 
    422 if [ "$STATUS" != "200" ]
    423 then
    424     jq . < "$LAST_RESPONSE"
    425     exit_fail "Expected 200, order claimed. got: $STATUS"
    426 fi
    427 
    428 QUANTITY=$(jq -r .contract_terms.products[0].quantity < "$LAST_RESPONSE")
    429 
    430 if [ "$QUANTITY" != "1" ]
    431 then
    432     exit_fail "Expected quantity 1. got: $QUANTITY"
    433 fi
    434 
    435 echo "OK"
    436 
    437 #
    438 # Create product in another currency
    439 #
    440 
    441 
    442 STATUS=$(curl 'http://localhost:9966/private/products' \
    443     -d '{"product_id":"1","description":"product with id 1 and price :15","unit_price":["USD:15"],"unit_total_stock":"1","description_i18n":{},"unit":"","image":"","taxes":[],"address":{},"next_restock":{"t_s":"never"}}' \
    444     -w "%{http_code}" -s -o /dev/null)
    445 
    446 if [ "$STATUS" != "204" ]
    447 then
    448     exit_fail "Expected 204 no content. got: $STATUS"
    449 fi
    450 
    451 #
    452 # CREATE ORDER AND SELL IT
    453 #
    454 
    455 echo -n "Creating order to be paid..."
    456 STATUS=$(curl 'http://localhost:9966/private/orders' \
    457     -d '{"refund_delay":{"d_us":0},"order":{"amount":"TESTKUDOS:1","summary":"payme"},"inventory_products":[{"product_id":"2","unit_quantity":"1"}]}' \
    458     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    459 
    460 if [ "$STATUS" != "200" ]
    461 then
    462     jq . < "$LAST_RESPONSE"
    463     exit_fail "Expected 200, order created. got: $STATUS"
    464 fi
    465 
    466 ORDER_ID=$(jq -e -r .order_id < "$LAST_RESPONSE")
    467 TOKEN=$(jq -e -r .token < "$LAST_RESPONSE")
    468 
    469 STATUS=$(curl "http://localhost:9966/private/orders/${ORDER_ID}" \
    470     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    471 
    472 if [ "$STATUS" != "200" ]
    473 then
    474     jq . < "$LAST_RESPONSE"
    475     exit_fail "Expected 200, getting order info before claming it. got: $STATUS"
    476 fi
    477 
    478 PAY_URL=$(jq -e -r .taler_pay_uri < "$LAST_RESPONSE")
    479 
    480 echo "OK"
    481 
    482 NOW=$(date +%s)
    483 
    484 echo -n "Pay first order ${PAY_URL} ..."
    485 taler-wallet-cli \
    486     --no-throttle \
    487     --wallet-db="$WALLET_DB" \
    488     handle-uri "${PAY_URL}" \
    489     -y 2> wallet-pay1.err > wallet-pay1.log
    490 taler-wallet-cli \
    491     --no-throttle \
    492     --wallet-db="$WALLET_DB" \
    493     run-until-done 2> wallet-finish-pay1.err > wallet-finish-pay1.log
    494 NOW2=$(date +%s)
    495 echo " OK (took $(( NOW2 - NOW )) secs )"
    496 
    497 STATUS=$(curl "http://localhost:9966/private/orders/${ORDER_ID}" \
    498     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    499 
    500 if [ "$STATUS" != "200" ]
    501 then
    502     jq . < "$LAST_RESPONSE"
    503     exit_fail "Expected 200, after pay. got: $STATUS"
    504 fi
    505 
    506 ORDER_STATUS=$(jq -r .order_status < "$LAST_RESPONSE")
    507 
    508 if [ "$ORDER_STATUS" != "paid" ]
    509 then
    510     jq . < "$LAST_RESPONSE"
    511     exit_fail "Order status should be 'paid'. got: $ORDER_STATUS"
    512 fi
    513 
    514 #
    515 # WIRE TRANSFER TO MERCHANT AND NOTIFY BACKEND
    516 #
    517 
    518 # PAY_DEADLINE=$(jq -r .contract_terms.pay_deadline.t_s < "$LAST_RESPONSE")
    519 WIRE_DEADLINE=$(jq -r .contract_terms.wire_transfer_deadline.t_s < "$LAST_RESPONSE")
    520 
    521 NOW=$(date +%s)
    522 
    523 TO_SLEEP=$((1200 + WIRE_DEADLINE - NOW ))
    524 echo "Waiting $TO_SLEEP secs for wire transfer"
    525 
    526 echo -n "Perform wire transfers ..."
    527 taler-exchange-aggregator \
    528     -y \
    529     -c "$CONF" \
    530     -T "${TO_SLEEP}"000000 \
    531     -t \
    532     -L INFO &> aggregator.log
    533 taler-exchange-transfer \
    534     -c "$CONF" \
    535     -t \
    536     -L INFO &> transfer.log
    537 echo " DONE"
    538 echo -n "Give time to Nexus to route the payment to Sandbox..."
    539 # FIXME-MS: trigger immediate update at nexus
    540 # NOTE: once libeufin can do long-polling, we should
    541 # be able to reduce the delay here and run aggregator/transfer
    542 # always in the background via setup
    543 sleep 3
    544 echo " DONE"
    545 
    546 echo -n "Obtaining wire transfer details from bank ($USE_FAKEBANK)..."
    547 
    548 BANKDATA="$(curl 'http://localhost:8082/accounts/exchange/taler-wire-gateway/history/outgoing?delta=1' -s)"
    549 WTID=$(echo "$BANKDATA" | jq -r .outgoing_transactions[0].wtid)
    550 WURL=$(echo "$BANKDATA" | jq -r .outgoing_transactions[0].exchange_base_url)
    551 CREDIT_AMOUNT=$(echo "$BANKDATA" | jq -r .outgoing_transactions[0].amount)
    552 TARGET_PAYTO=$(echo "$BANKDATA" | jq -r .outgoing_transactions[0].credit_account)
    553 
    554 if [ "$EXCHANGE_URL" != "$WURL" ]
    555 then
    556     exit_fail "Wrong exchange URL in '$BANKDATA' response, expected '$EXCHANGE_URL'"
    557 fi
    558 
    559 echo " OK"
    560 
    561 set +e
    562 
    563 echo -n "Notifying merchant of bogus wire transfer ..."
    564 
    565 STATUS=$(curl 'http://localhost:9966/private/transfers' \
    566     -d '{"credit_amount":"'"$CREDIT_AMOUNT"'1","wtid":"'"$WTID"'","payto_uri":"'"$TARGET_PAYTO"'","exchange_url":"'"$WURL"'"}' \
    567     -m 3 \
    568     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    569 
    570 if [ "$STATUS" != "204" ]
    571 then
    572     jq . < "$LAST_RESPONSE"
    573     exit_fail "Expected to fail since the amount is not valid. got: $STATUS"
    574 fi
    575 
    576 echo "OK"
    577 
    578 echo -n "Deleting bogus wire transfer ..."
    579 
    580 TID=$(curl -s http://localhost:9966/private/transfers | jq -r .transfers[0].transfer_serial_id)
    581 STATUS=$(curl -H "Content-Type: application/json" -X DELETE \
    582     "http://localhost:9966/private/transfers/$TID" \
    583     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    584 
    585 if [ "$STATUS" != "204" ]
    586 then
    587     jq . < "$LAST_RESPONSE"
    588     exit_fail "Expected response 204 No Content, after deleting valid TID. got: $STATUS"
    589 fi
    590 
    591 STATUS=$(curl -H "Content-Type: application/json" -X DELETE \
    592     "http://localhost:9966/private/transfers/$TID" \
    593     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    594 if [ "$STATUS" != "404" ]
    595 then
    596     jq . < "$LAST_RESPONSE"
    597     exit_fail "Expected response 404 Not found, after deleting TID again. got: $STATUS"
    598 fi
    599 
    600 echo " OK"
    601 
    602 echo -n "Notifying merchant of correct wire transfer..."
    603 
    604 STATUS=$(curl 'http://localhost:9966/private/transfers' \
    605     -d '{"credit_amount":"'"$CREDIT_AMOUNT"'","wtid":"'"$WTID"'","payto_uri":"'"$TARGET_PAYTO"'","exchange_url":"'"$WURL"'"}' \
    606     -m 3 \
    607     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    608 
    609 if [ "$STATUS" != "204" ]
    610 then
    611     jq . < "$LAST_RESPONSE"
    612     exit_fail "Expected response 204 No content, after providing transfer data. got: $STATUS"
    613 fi
    614 
    615 echo " OK"
    616 
    617 echo -n "Running taler-merchant-depositcheck ..."
    618 set -e
    619 taler-merchant-depositcheck \
    620     -L INFO \
    621     -c "$CONF" \
    622     -T "${TO_SLEEP}"000000 \
    623     -t &> taler-merchant-depositcheck.log
    624 echo " OK"
    625 
    626 echo -n "Running taler-merchant-reconciliation ..."
    627 set -e
    628 taler-merchant-reconciliation \
    629     -L INFO \
    630     -c "$CONF" \
    631     -T "${TO_SLEEP}"000000 \
    632     -t &> taler-merchant-reconciliation.log
    633 echo " OK"
    634 
    635 
    636 echo -n "Fetching wire transfers ..."
    637 
    638 STATUS=$(curl 'http://localhost:9966/private/transfers' \
    639     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    640 
    641 if [ "$STATUS" != "200" ]
    642 then
    643     jq . < "$LAST_RESPONSE"
    644     exit_fail "Expected response 200 Ok. got: $STATUS"
    645 fi
    646 
    647 TRANSFERS_LIST_SIZE=$(jq -r '.transfers | length' < "$LAST_RESPONSE")
    648 
    649 if [ "$TRANSFERS_LIST_SIZE" != "1" ]
    650 then
    651     jq . < "$LAST_RESPONSE"
    652     exit_fail "Expected 1 entry in transfer list. Got: $TRANSFERS_LIST_SIZE"
    653 fi
    654 
    655 echo "OK"
    656 
    657 echo -n "Checking order status ..."
    658 STATUS=$(curl "http://localhost:9966/private/orders/${ORDER_ID}?transfer=YES" \
    659     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    660 if [ "$STATUS" != "200" ]
    661 then
    662     jq . < "$LAST_RESPONSE"
    663     exit_fail "Expected 200, after order inquiry. got: $STATUS"
    664 fi
    665 DEPOSIT_TOTAL=$(jq -r .deposit_total < "$LAST_RESPONSE")
    666 if [ "$DEPOSIT_TOTAL" == "TESTKUDOS:0" ]
    667 then
    668     jq . < "$LAST_RESPONSE"
    669     exit_fail "Expected non-zero deposit total. got: $DEPOSIT_TOTAL"
    670 fi
    671 echo " OK"
    672 
    673 echo -n "Checking bank account status ..."
    674 if [ 1 = "$USE_FAKEBANK" ]
    675 then
    676     STATUS=$(curl "http://localhost:8082/accounts/fortythree" \
    677                   -w "%{http_code}" \
    678                   -s \
    679                   -o "$LAST_RESPONSE")
    680     if [ "$STATUS" != "200" ]
    681     then
    682         jq . < "$LAST_RESPONSE"
    683         exit_fail "Expected response 200 Ok, getting account status. Got: $STATUS"
    684     fi
    685     BALANCE=$(jq -r .balance.amount < "$LAST_RESPONSE")
    686     if [ "$BALANCE" == "TESTKUDOS:0" ]
    687     then
    688         jq . < "$LAST_RESPONSE"
    689         exit_fail "Wire transfer did not happen. Got: $BALANCE"
    690     fi
    691 else
    692     ACCOUNT_PASSWORD="fortythree:x"
    693     BANK_HOST="localhost:18082"
    694     STATUS=$(curl "http://$ACCOUNT_PASSWORD@$BANK_HOST/accounts/fortythree" \
    695                   -w "%{http_code}" -s -o "$LAST_RESPONSE")
    696     if [ "$STATUS" != "200" ]
    697     then
    698         jq . < "$LAST_RESPONSE"
    699         exit_fail "Expected response 200 Ok, getting account status. Got: $STATUS"
    700     fi
    701     BALANCE=$(jq -r .balance.amount < "$LAST_RESPONSE")
    702     if [ "$BALANCE" == "TESTKUDOS:0" ]
    703     then
    704         jq . < "$LAST_RESPONSE"
    705         exit_fail "Wire transfer did not happen. Got: $BALANCE"
    706     fi
    707 fi
    708 echo " OK"
    709 
    710 echo -n "Getting information about kyc ..."
    711 STATUS=$(curl -H "Content-Type: application/json" -X GET \
    712     http://localhost:9966/private/kyc \
    713     -w "%{http_code}" -s -o /dev/null)
    714 if [ "$STATUS" != "200" ]
    715 then
    716     exit_fail "Expected 200. Got: $STATUS"
    717 fi
    718 echo " OK"
    719 
    720 exit 0