merchant

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

test_merchant_order_creation.sh (25053B)


      1 #!/usr/bin/env bash
      2 # This file is in the public domain.
      3 
      4 set -eu
      5 
      6 function clean_wallet() {
      7     rm -f "${WALLET_DB}"
      8     rm -f "${HTML_RESPONSE}"
      9     exit_cleanup
     10 }
     11 
     12 function assert_html_contains() {
     13     if ! grep -Fq -- "$2" "$1"
     14     then
     15         cat "$1" >&2
     16         exit_fail "Expected rendered HTML to contain: $2"
     17     fi
     18 }
     19 
     20 function assert_html_rendered() {
     21     if grep -Fq -- '{{' "$1"
     22     then
     23         cat "$1" >&2
     24         exit_fail "Rendered HTML contains unresolved Mustache markup"
     25     fi
     26 }
     27 
     28 
     29 # Replace with 0 for nexus...
     30 USE_FAKEBANK=1
     31 if [ 1 = "$USE_FAKEBANK" ]
     32 then
     33     ACCOUNT="exchange-account-2"
     34     BANK_FLAGS="-f -d x-taler-bank -u $ACCOUNT"
     35     BANK_URL="http://localhost:8082/"
     36 else
     37     ACCOUNT="exchange-account-1"
     38     BANK_FLAGS="-ns -d iban -u $ACCOUNT"
     39     BANK_URL="http://localhost:8082/"
     40     echo -n "Testing for libeufin-bank"
     41     libeufin-bank --help >/dev/null </dev/null || exit_skip " MISSING"
     42     echo " FOUND"
     43 
     44 fi
     45 
     46 . setup.sh
     47 
     48 echo -n "Testing for taler-harness"
     49 taler-harness --help >/dev/null </dev/null || exit_skip " MISSING"
     50 echo " FOUND"
     51 
     52 # Launch exchange, merchant and bank.
     53 setup -c "test_template.conf" \
     54       -r "merchant-exchange-default" \
     55       -em \
     56       $BANK_FLAGS
     57 LAST_RESPONSE=$(mktemp -p "${TMPDIR:-/tmp}" test_response.conf-XXXXXX)
     58 HTML_RESPONSE="${LAST_RESPONSE}.html"
     59 CONF="test_template.conf.edited"
     60 WALLET_DB=$(mktemp -p "${TMPDIR:-/tmp}" test_wallet.json-XXXXXX)
     61 EXCHANGE_URL="http://localhost:8081/"
     62 
     63 # Install cleanup handler (except for kill -9)
     64 trap clean_wallet EXIT
     65 
     66 echo -n "First prepare wallet with coins ..."
     67 rm -f "$WALLET_DB"
     68 taler-wallet-cli \
     69     --no-throttle \
     70     --wallet-db="$WALLET_DB" \
     71     api \
     72     --expect-success 'withdrawTestBalance' \
     73   "$(jq -n '
     74     {
     75         amount: "TESTKUDOS:99",
     76         corebankApiBaseUrl: $BANK_URL,
     77         exchangeBaseUrl: $EXCHANGE_URL
     78     }' \
     79     --arg BANK_URL "${BANK_URL}" \
     80     --arg EXCHANGE_URL "$EXCHANGE_URL"
     81   )" 2>wallet-withdraw-1.err >wallet-withdraw-1.out
     82 echo -n "."
     83 # FIXME-MS: add logic to have nexus check immediately here.
     84 # sleep 10
     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     -a "$ACCOUNT" \
     91     -L "INFO" \
     92     -c "$CONF" \
     93     -t &> taler-exchange-wirewatch.out
     94 echo -n "."
     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 CURRENCY_COUNT=$(taler-wallet-cli --wallet-db="$WALLET_DB" balance | jq '.balances|length')
    103 if [ "$CURRENCY_COUNT" = "0" ]
    104 then
    105     exit_fail "Expected least one currency, withdrawal failed. check log."
    106 fi
    107 
    108 #
    109 # CREATE INSTANCE FOR TESTING
    110 #
    111 
    112 
    113 echo -n "Configuring merchant instance ..."
    114 
    115 STATUS=$(curl -H "Content-Type: application/json" -X POST \
    116     -H 'Authorization: Bearer secret-token:super_secret' \
    117     http://localhost:9966/management/instances \
    118     -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}}' \
    119     -w "%{http_code}" -s -o /dev/null)
    120 
    121 if [ "$STATUS" != "204" ]
    122 then
    123     exit_fail "Expected '204 No content' response. Got instead $STATUS"
    124 fi
    125 echo "Ok"
    126 
    127 echo -n "Configuring merchant account ..."
    128 
    129 if [ 1 = "$USE_FAKEBANK" ]
    130 then
    131     FORTYTHREE="payto://x-taler-bank/localhost/fortythree?receiver-name=fortythree"
    132 else
    133     FORTYTHREE=$(get_payto_uri fortythree x)
    134 fi
    135 # create with 2 bank account addresses
    136 STATUS=$(curl -H "Content-Type: application/json" -X POST \
    137     -H 'Authorization: Bearer secret-token:super_secret' \
    138     http://localhost:9966/private/accounts \
    139     -d '{"payto_uri":"'"$FORTYTHREE"'"}' \
    140     -w "%{http_code}" -s -o /dev/null)
    141 
    142 if [ "$STATUS" != "200" ]
    143 then
    144     exit_fail "Expected '200 OK' response. Got instead $STATUS"
    145 fi
    146 STATUS=$(curl -H "Content-Type: application/json" -X POST \
    147     -H 'Authorization: Bearer secret-token:super_secret' \
    148     http://localhost:9966/private/accounts \
    149     -d '{"payto_uri":"payto://iban/SANDBOXX/DE270744?receiver-name=Forty+Four"}' \
    150     -w "%{http_code}" -s -o /dev/null)
    151 
    152 if [ "$STATUS" != "200" ]
    153 then
    154     exit_fail "Expected '200 OK' response. Got instead $STATUS"
    155 fi
    156 
    157 echo "Ok"
    158 
    159 
    160 echo -n "Get accounts..."
    161 STATUS=$(curl http://localhost:9966/private/accounts \
    162     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    163 PAY_URI=$(jq -r .accounts[1].payto_uri < "$LAST_RESPONSE")
    164 H_WIRE=$(jq -r .accounts[1].h_wire < "$LAST_RESPONSE")
    165 if [ "$PAY_URI" != "payto://iban/SANDBOXX/DE270744?receiver-name=Forty+Four" ]
    166 then
    167     cat "$LAST_RESPONSE" >&2
    168     exit_fail "Expected second payto URI. Got $PAY_URI"
    169 fi
    170 echo "OK"
    171 
    172 # remove one account address
    173 echo -n "Deleting one account ..."
    174 STATUS=$(curl -H "Content-Type: application/json" -X PATCH \
    175     -H 'Authorization: Bearer secret-token:super_secret' \
    176     "http://localhost:9966/private/accounts/${H_WIRE}" \
    177     -X DELETE \
    178     -w "%{http_code}" -s -o /dev/null)
    179 
    180 if [ "$STATUS" != "204" ]
    181 then
    182     exit_fail "Expected '204 No content' for deletion of ${H_WIRE}. Got instead: $STATUS"
    183 fi
    184 echo "OK"
    185 
    186 RANDOM_IMG='data:image/png;base64,abcdefg'
    187 
    188 #
    189 # CREATE AN ORDER WITHOUT TOKEN
    190 #
    191 
    192 echo -n "Creating order without TOKEN..."
    193 STATUS=$(curl 'http://localhost:9966/private/orders' \
    194     -d '{"create_token":false,"refund_delay":{"d_us":0},"order":{"amount":"TESTKUDOS:7","summary":"3 <unsafe> & tea","products":[{"description":"desct","image":"'"$RANDOM_IMG"'","price":"TESTKUDOS:1","taxes":[],"unit":"u","quantity":1}]}}' \
    195     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    196 
    197 if [ "$STATUS" != "200" ]
    198 then
    199     cat "$LAST_RESPONSE" >&2
    200     exit_fail "Expected 200, order created. got: $STATUS"
    201 fi
    202 
    203 ORDER_ID=$(jq -r .order_id < "$LAST_RESPONSE")
    204 TOKEN=$(jq -r .token < "$LAST_RESPONSE")
    205 
    206 if [ "$TOKEN" != "null" ]
    207 then
    208     exit_fail "token should be null, got: $TOKEN"
    209 fi
    210 
    211 echo "OK"
    212 
    213 echo -n "Checking created order without TOKEN..."
    214 STATUS=$(curl http://localhost:9966/orders/"$ORDER_ID" \
    215               -w "%{http_code}" -s -o "$LAST_RESPONSE")
    216 PAY_URI=$(jq -r .taler_pay_uri < "$LAST_RESPONSE")
    217 if [ "$PAY_URI" == "null" ]
    218 then
    219     cat "$LAST_RESPONSE" >&2
    220     exit_fail "Expected non-NULL payuri. got $PAY_URI"
    221 fi
    222 echo "OK"
    223 
    224 echo -n "Checking unclaimed order HTML..."
    225 STATUS=$(curl -H 'Accept: text/html' \
    226     "http://localhost:9966/orders/${ORDER_ID}" \
    227     -w "%{http_code}" -s -o "$HTML_RESPONSE")
    228 if [ "$STATUS" != "402" ]
    229 then
    230     cat "$HTML_RESPONSE" >&2
    231     exit_fail "Expected 402 payment HTML. Got: $STATUS"
    232 fi
    233 assert_html_contains "$HTML_RESPONSE" 'Pay with GNU Taler'
    234 assert_html_contains "$HTML_RESPONSE" 'TESTKUDOS:7'
    235 assert_html_contains "$HTML_RESPONSE" '3 &lt;unsafe&gt; &amp; tea'
    236 assert_html_contains "$HTML_RESPONSE" "$ORDER_ID"
    237 assert_html_contains "$HTML_RESPONSE" 'class="qr-frame"'
    238 assert_html_rendered "$HTML_RESPONSE"
    239 echo "OK"
    240 
    241 #
    242 # CREATE AN ORDER WITHOUT TOKEN WITH FULLFILMENT URL
    243 #
    244 
    245 echo -n "Creating order without TOKEN and fullfilment URL..."
    246 STATUS=$(curl 'http://localhost:9966/private/orders' \
    247     -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}]}}' \
    248     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    249 
    250 if [ "$STATUS" != "200" ]
    251 then
    252     cat "$LAST_RESPONSE" >&2
    253     exit_fail "Expected 200, order created. got: $STATUS"
    254 fi
    255 
    256 ORDER_ID=$(jq -r .order_id < "$LAST_RESPONSE")
    257 TOKEN=$(jq -r .token < "$LAST_RESPONSE")
    258 
    259 if [ "$TOKEN" != "null" ]
    260 then
    261     exit_fail "Token should be null, got: $TOKEN"
    262 fi
    263 
    264 STATUS=$(curl http://localhost:9966/orders/"$ORDER_ID" \
    265     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    266 
    267 PAY_URI=$(jq -r .taler_pay_uri < "$LAST_RESPONSE")
    268 FULLFILMENT_URL=$(jq -r .fulfillment_url < "$LAST_RESPONSE")
    269 
    270 if [ "$FULLFILMENT_URL" != "go_here_please" ]
    271 then
    272     cat "$LAST_RESPONSE" >&2
    273     exit_fail "Expected a pay URI. got: $PAY_URI"
    274 fi
    275 
    276 if [ "$PAY_URI" == "null" ]
    277 then
    278     cat "$LAST_RESPONSE" >&2
    279     exit_fail "Expected non-NULL pay URI. Got: $PAY_URI"
    280 fi
    281 echo "OK"
    282 
    283 #
    284 # CREATE A DISCOUNT TOKEN FAMILY
    285 #
    286 echo -n "Creating discount token family..."
    287 VALID_AFTER="{\"t_s\": $(date +%s)}" # now
    288 VALID_BEFORE="{\"t_s\": $(date +%s -d "+30 days")}" # 30 days from now
    289 DURATION="{\"d_us\": $(expr 30 \* 24 \* 60 \* 60 \* 1000000)}" # 30 days
    290 STATUS=$(curl 'http://localhost:9966/private/tokenfamilies' \
    291               -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}" \
    292               -w "%{http_code}" -s -o "$LAST_RESPONSE")
    293 if [ "$STATUS" != "204" ]
    294 then
    295     cat "$LAST_RESPONSE" >&2
    296     exit_fail "Expected '204 OK' response. Got instead $STATUS"
    297 fi
    298 echo "Ok"
    299 
    300 #
    301 # CREATE A SUBSCRIPTION TOKEN FAMILY
    302 #
    303 echo -n "Creating subscription token family..."
    304 VALID_AFTER="{\"t_s\": $(date +%s)}" # now
    305 VALID_BEFORE="{\"t_s\": $(date +%s -d "+30 days")}" # 30 days from now
    306 DURATION="{\"d_us\": $(expr 30 \* 24 \* 60 \* 60 \* 1000000)}" # 30 days
    307 STATUS=$(curl 'http://localhost:9966/private/tokenfamilies' \
    308               -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}" \
    309               -w "%{http_code}" -s -o "$LAST_RESPONSE")
    310 if [ "$STATUS" != "204" ]
    311 then
    312    cat "$LAST_RESPONSE" >&2
    313    exit_fail "Expected '204 OK' response. Got instead $STATUS"
    314 fi
    315 echo "Ok"
    316 
    317 #
    318 # CREATE AN DISCOUNTABLE ORDER WITHOUT TOKEN
    319 #
    320 RANDOM_IMG='data:image/png;base64,abcdefg'
    321 
    322 echo -n "Creating discountable order..."
    323 STATUS=$(curl 'http://localhost:9966/private/orders' \
    324     -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":[]}]}}' \
    325     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    326 
    327 if [ "$STATUS" != "200" ]
    328 then
    329     cat "$LAST_RESPONSE" >&2
    330     exit_fail "Expected 200, order created. got: $STATUS"
    331 fi
    332 echo "OK"
    333 
    334 ORDER_ID=$(jq -r .order_id < "$LAST_RESPONSE")
    335 TOKEN=$(jq -r .token < "$LAST_RESPONSE")
    336 
    337 echo -n "Checking created order..."
    338 STATUS=$(curl http://localhost:9966/orders/"$ORDER_ID?token=$TOKEN" \
    339               -w "%{http_code}" -s -o "$LAST_RESPONSE")
    340 PAY_URI=$(jq -r .taler_pay_uri < "$LAST_RESPONSE")
    341 if [ "$PAY_URI" == "null" ]
    342 then
    343     cat "$LAST_RESPONSE" >&2
    344     exit_fail "Expected non-NULL payuri. got $PAY_URI"
    345 fi
    346 echo "OK"
    347 
    348 echo -n "Checking multi-option order HTML..."
    349 STATUS=$(curl -H 'Accept: text/html' \
    350     "http://localhost:9966/orders/${ORDER_ID}?token=${TOKEN}" \
    351     -w "%{http_code}" -s -o "$HTML_RESPONSE")
    352 if [ "$STATUS" != "402" ]
    353 then
    354     cat "$HTML_RESPONSE" >&2
    355     exit_fail "Expected 402 payment HTML. Got: $STATUS"
    356 fi
    357 assert_html_contains "$HTML_RESPONSE" 'Expensive purchase'
    358 assert_html_contains "$HTML_RESPONSE" 'select the amount and payment option in your wallet'
    359 assert_html_rendered "$HTML_RESPONSE"
    360 echo "OK"
    361 
    362 echo -n "Claming order with token family ..."
    363 STATUS=$(curl http://localhost:9966/orders/"$ORDER_ID"/claim \
    364     -d '{"nonce":"0FTTZ475NBECCZN97VFTN6DEKWVKJ8AQY9PWR6VS36JZQFS66YG0","token":"'"$TOKEN"'"}' \
    365     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    366 
    367 if [ "$STATUS" != "200" ]
    368 then
    369     cat "$LAST_RESPONSE" >&2
    370     exit_fail "Expected 200, order claimed. got: $STATUS"
    371 fi
    372 
    373 echo " OK"
    374 
    375 echo -n "Checking claimed order HTML..."
    376 STATUS=$(curl -H 'Accept: text/html' \
    377     "http://localhost:9966/orders/${ORDER_ID}?token=${TOKEN}" \
    378     -w "%{http_code}" -s -o "$HTML_RESPONSE")
    379 if [ "$STATUS" != "402" ]
    380 then
    381     cat "$HTML_RESPONSE" >&2
    382     exit_fail "Expected 402 claimed payment HTML. Got: $STATUS"
    383 fi
    384 assert_html_contains "$HTML_RESPONSE" 'Order claimed by wallet'
    385 assert_html_contains "$HTML_RESPONSE" 'data-claimed="true"'
    386 assert_html_rendered "$HTML_RESPONSE"
    387 echo "OK"
    388 
    389 # echo -n "Fetching pay URL for order ..."
    390 # STATUS=$(curl "http://localhost:9966/private/orders/${ORDER_ID}" \
    391 #     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    392 
    393 # if [ "$STATUS" != "200" ]
    394 # then
    395 #     jq . < "$LAST_RESPONSE"
    396 #     exit_fail "Expected 200, getting order info before claming it. got: $STATUS"
    397 # fi
    398 
    399 # PAY_URL=$(jq -e -r .taler_pay_uri < "$LAST_RESPONSE")
    400 
    401 # echo " OK"
    402 
    403 # NOW=$(date +%s)
    404 
    405 # echo -n "Pay for order ${PAY_URL} ..."
    406 # taler-wallet-cli --no-throttle --wallet-db="$WALLET_DB" handle-uri "${PAY_URL}" -y 2> wallet-pay1.err > wallet-pay1.log
    407 # taler-wallet-cli --no-throttle --wallet-db="$WALLET_DB" run-until-done 2> wallet-finish-pay1.err > wallet-finish-pay1.log
    408 # NOW2=$(date +%s)
    409 # echo " OK (took $(( NOW2 - NOW )) secs )"
    410 
    411 #
    412 # CREATE ORDER WITH NON-INVENTORY AND CHECK
    413 #
    414 
    415 echo -n "Creating order with non-inventory products..."
    416 STATUS=$(curl 'http://localhost:9966/private/orders' \
    417     -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}]}}' \
    418     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    419 
    420 if [ "$STATUS" != "200" ]
    421 then
    422     cat "$LAST_RESPONSE" >&2
    423     exit_fail "Expected 200, order created. got: $STATUS"
    424 fi
    425 
    426 ORDER_ID=$(jq -r .order_id < "$LAST_RESPONSE")
    427 TOKEN=$(jq -r .token < "$LAST_RESPONSE")
    428 
    429 STATUS=$(curl http://localhost:9966/orders/"$ORDER_ID"/claim \
    430     -d '{"nonce":"0FTTZ475NBECCZN97VFTN6DEKWVKJ8AQY9PWR6VS36JZQFS66YG0","token":"'"$TOKEN"'"}' \
    431     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    432 
    433 if [ "$STATUS" != "200" ]
    434 then
    435     cat "$LAST_RESPONSE" >&2
    436     exit_fail "Expected 200, order claimed. got: $STATUS"
    437 fi
    438 
    439 QUANTITY=$(jq -r .contract_terms.products[0].quantity < "$LAST_RESPONSE")
    440 if [ "$QUANTITY" != "1" ]
    441 then
    442     cat < "$LAST_RESPONSE" >&2
    443     exit_fail "Expected quantity 1. got: $QUANTITY"
    444 fi
    445 
    446 IMAGE=$(jq -r .contract_terms.products[0].image < "$LAST_RESPONSE")
    447 if [ "$IMAGE" != "$RANDOM_IMG" ]
    448 then
    449     cat "$LAST_RESPONSE" >&2
    450     exit_fail "Expected $RANDOM_IMG but got something else: $IMAGE"
    451 fi
    452 echo "OK"
    453 
    454 
    455 #
    456 # CREATE INVENTORY PRODUCT AND CLAIM IT
    457 #
    458 
    459 echo -n "Creating product..."
    460 STATUS=$(curl 'http://localhost:9966/private/products' \
    461     -d '{"product_id":"2","description":"product with id 2 and price :15","unit_price":["TESTKUDOS:15"],"unit_total_stock":"2","description_i18n":{},"unit":"u","image":"'$RANDOM_IMG'","taxes":[],"address":{},"next_restock":{"t_s":"never"}}' \
    462     -w "%{http_code}" -s -o /dev/null)
    463 
    464 if [ "$STATUS" != "204" ]
    465 then
    466     exit_fail "Expected 204, product created. got: $STATUS"
    467 fi
    468 echo "OK"
    469 
    470 echo -n "Creating order with inventory products..."
    471 STATUS=$(curl 'http://localhost:9966/private/orders' \
    472     -d '{"refund_delay":{"d_us":0},"order":{"amount":"TESTKUDOS:7","summary":"3"},"inventory_products":[{"product_id":"2","unit_quantity":"1"}]}' \
    473     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    474 
    475 
    476 if [ "$STATUS" != "200" ]
    477 then
    478     jq . < "$LAST_RESPONSE"
    479     exit_fail "Expected 200 OK, order created response. Got: $STATUS"
    480 fi
    481 
    482 ORDER_ID=$(jq -e -r .order_id < "$LAST_RESPONSE")
    483 TOKEN=$(jq -e -r .token < "$LAST_RESPONSE")
    484 
    485 STATUS=$(curl http://localhost:9966/orders/"$ORDER_ID"/claim \
    486     -d '{"nonce":"1ZRJCEAGM5N98P6ATH0NSR9SP3RKQJQQ05MQJAJA57T8YNPCPWEG","token":"'"$TOKEN"'"}' \
    487     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    488 
    489 if [ "$STATUS" != "200" ]
    490 then
    491     jq . < "$LAST_RESPONSE"
    492     exit_fail "Expected 200, order claimed. got: $STATUS"
    493 fi
    494 
    495 QUANTITY=$(jq -r .contract_terms.products[0].quantity < "$LAST_RESPONSE")
    496 
    497 if [ "$QUANTITY" != "1" ]
    498 then
    499     exit_fail "Expected quantity 1. got: $QUANTITY"
    500 fi
    501 
    502 echo "OK"
    503 
    504 #
    505 # Create product in another currency
    506 #
    507 
    508 
    509 STATUS=$(curl 'http://localhost:9966/private/products' \
    510     -d '{"product_id":"1","description":"product with id 1 and price :15","unit_price":["USD:15"],"unit_total_stock":"1","description_i18n":{},"unit":"u","image":"","taxes":[],"address":{},"next_restock":{"t_s":"never"}}' \
    511     -w "%{http_code}" -s -o /dev/null)
    512 
    513 if [ "$STATUS" != "204" ]
    514 then
    515     exit_fail "Expected 204 no content. got: $STATUS"
    516 fi
    517 
    518 #
    519 # CREATE ORDER AND SELL IT
    520 #
    521 
    522 echo -n "Creating order to be paid..."
    523 STATUS=$(curl 'http://localhost:9966/private/orders' \
    524     -d '{"refund_delay":{"d_us":0},"order":{"amount":"TESTKUDOS:1","summary":"payme"},"inventory_products":[{"product_id":"2","unit_quantity":"1"}]}' \
    525     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    526 
    527 if [ "$STATUS" != "200" ]
    528 then
    529     jq . < "$LAST_RESPONSE"
    530     exit_fail "Expected 200, order created. got: $STATUS"
    531 fi
    532 
    533 ORDER_ID=$(jq -e -r .order_id < "$LAST_RESPONSE")
    534 TOKEN=$(jq -e -r .token < "$LAST_RESPONSE")
    535 
    536 STATUS=$(curl "http://localhost:9966/private/orders/${ORDER_ID}" \
    537     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    538 
    539 if [ "$STATUS" != "200" ]
    540 then
    541     jq . < "$LAST_RESPONSE"
    542     exit_fail "Expected 200, getting order info. got: $STATUS"
    543 fi
    544 
    545 PAY_URL=$(jq -e -r .taler_pay_uri < "$LAST_RESPONSE")
    546 
    547 echo "OK"
    548 
    549 NOW=$(date +%s)
    550 
    551 echo -n "Pay first order ${PAY_URL} ..."
    552 timeout 60 taler-wallet-cli \
    553     --no-throttle \
    554     --wallet-db="$WALLET_DB" \
    555     handle-uri "${PAY_URL}" \
    556     -y 2> wallet-pay1.err > wallet-pay1.log
    557 timeout 60 taler-wallet-cli \
    558     --no-throttle \
    559     --wallet-db="$WALLET_DB" \
    560     run-until-done 2> wallet-finish-pay1.err > wallet-finish-pay1.log
    561 NOW2=$(date +%s)
    562 echo " OK (took $(( NOW2 - NOW )) secs )"
    563 
    564 STATUS=$(curl "http://localhost:9966/private/orders/${ORDER_ID}" \
    565     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    566 
    567 if [ "$STATUS" != "200" ]
    568 then
    569     jq . < "$LAST_RESPONSE"
    570     exit_fail "Expected 200, after pay. got: $STATUS"
    571 fi
    572 
    573 ORDER_STATUS=$(jq -r .order_status < "$LAST_RESPONSE")
    574 
    575 if [ "$ORDER_STATUS" != "paid" ]
    576 then
    577     jq . < "$LAST_RESPONSE"
    578     exit_fail "Order status should be 'paid'. got: $ORDER_STATUS"
    579 fi
    580 
    581 echo -n "Checking paid order HTML..."
    582 STATUS=$(curl -H 'Accept: text/html' \
    583     "http://localhost:9966/orders/${ORDER_ID}?token=${TOKEN}" \
    584     -w "%{http_code}" -s -o "$HTML_RESPONSE")
    585 if [ "$STATUS" != "200" ]
    586 then
    587     cat "$HTML_RESPONSE" >&2
    588     exit_fail "Expected 200 paid order HTML. Got: $STATUS"
    589 fi
    590 assert_html_contains "$HTML_RESPONSE" 'Payment complete'
    591 assert_html_contains "$HTML_RESPONSE" 'TESTKUDOS:1'
    592 assert_html_contains "$HTML_RESPONSE" "$ORDER_ID"
    593 assert_html_rendered "$HTML_RESPONSE"
    594 echo "OK"
    595 
    596 #
    597 # WIRE TRANSFER TO MERCHANT AND NOTIFY BACKEND
    598 #
    599 
    600 # PAY_DEADLINE=$(jq -r .contract_terms.pay_deadline.t_s < "$LAST_RESPONSE")
    601 WIRE_DEADLINE=$(jq -r .contract_terms.wire_transfer_deadline.t_s < "$LAST_RESPONSE")
    602 
    603 NOW=$(date +%s)
    604 
    605 TO_SLEEP=$((1200 + WIRE_DEADLINE - NOW ))
    606 echo "Waiting $TO_SLEEP secs for wire transfer"
    607 
    608 echo -n "Call taler-exchange-aggregator ..."
    609 taler-exchange-aggregator \
    610     -y \
    611     -c "$CONF" \
    612     -T "${TO_SLEEP}"000000 \
    613     -t \
    614     -L INFO &> aggregator.log
    615 echo " DONE"
    616 echo -n "Call taler-exchange-transfer ..."
    617 taler-exchange-transfer \
    618     -c "$CONF" \
    619     -t \
    620     -L INFO &> transfer.log
    621 echo " DONE"
    622 echo -n "Give time to Nexus to route the payment to Sandbox..."
    623 # FIXME: trigger immediate update at nexus
    624 # NOTE: once libeufin can do long-polling, we should
    625 # be able to reduce the delay here and run aggregator/transfer
    626 # always in the background via setup
    627 sleep 3
    628 echo " DONE"
    629 
    630 echo -n "Obtaining wire transfer details from bank ($USE_FAKEBANK)..."
    631 
    632 BANKDATA="$(curl 'http://localhost:8082/accounts/exchange/taler-wire-gateway/history/outgoing?delta=1' -s)"
    633 WTID=$(echo "$BANKDATA" | jq -r .outgoing_transactions[0].wtid)
    634 WURL=$(echo "$BANKDATA" | jq -r .outgoing_transactions[0].exchange_base_url)
    635 CREDIT_AMOUNT=$(echo "$BANKDATA" | jq -r .outgoing_transactions[0].amount)
    636 TARGET_PAYTO=$(echo "$BANKDATA" | jq -r .outgoing_transactions[0].credit_account)
    637 
    638 if [ "$EXCHANGE_URL" != "$WURL" ]
    639 then
    640     exit_fail "Wrong exchange URL in '$BANKDATA' response, expected '$EXCHANGE_URL'"
    641 fi
    642 
    643 echo " OK"
    644 
    645 set +e
    646 
    647 echo -n "Notifying merchant of bogus wire transfer ..."
    648 
    649 STATUS=$(curl 'http://localhost:9966/private/transfers' \
    650     -d '{"credit_amount":"'"$CREDIT_AMOUNT"'1","wtid":"'"$WTID"'","payto_uri":"'"$TARGET_PAYTO"'","exchange_url":"'"$WURL"'"}' \
    651     -m 3 \
    652     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    653 
    654 if [ "$STATUS" != "204" ]
    655 then
    656     jq . < "$LAST_RESPONSE"
    657     exit_fail "Expected to fail since the amount is not valid. got: $STATUS"
    658 fi
    659 
    660 echo "OK"
    661 
    662 echo -n "Deleting bogus wire transfer ..."
    663 
    664 TID=$(curl -s http://localhost:9966/private/transfers | jq -r .transfers[0].transfer_serial_id)
    665 STATUS=$(curl -H "Content-Type: application/json" -X DELETE \
    666     "http://localhost:9966/private/transfers/$TID" \
    667     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    668 
    669 if [ "$STATUS" != "204" ]
    670 then
    671     jq . < "$LAST_RESPONSE"
    672     exit_fail "Expected response 204 No Content, after deleting valid TID. got: $STATUS"
    673 fi
    674 
    675 STATUS=$(curl -H "Content-Type: application/json" -X DELETE \
    676     "http://localhost:9966/private/transfers/$TID" \
    677     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    678 if [ "$STATUS" != "404" ]
    679 then
    680     jq . < "$LAST_RESPONSE"
    681     exit_fail "Expected response 404 Not found, after deleting TID again. got: $STATUS"
    682 fi
    683 
    684 echo " OK"
    685 
    686 echo -n "Notifying merchant of correct wire transfer..."
    687 
    688 STATUS=$(curl 'http://localhost:9966/private/transfers' \
    689     -d '{"credit_amount":"'"$CREDIT_AMOUNT"'","wtid":"'"$WTID"'","payto_uri":"'"$TARGET_PAYTO"'","exchange_url":"'"$WURL"'"}' \
    690     -m 3 \
    691     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    692 
    693 if [ "$STATUS" != "204" ]
    694 then
    695     jq . < "$LAST_RESPONSE"
    696     exit_fail "Expected response 204 No content, after providing transfer data. got: $STATUS"
    697 fi
    698 
    699 echo " OK"
    700 
    701 echo -n "Running taler-merchant-depositcheck ..."
    702 set -e
    703 taler-merchant-depositcheck \
    704     -L INFO \
    705     -c "$CONF" \
    706     -T "${TO_SLEEP}"000000 \
    707     -t &> taler-merchant-depositcheck.log
    708 echo " OK"
    709 
    710 echo -n "Running taler-merchant-reconciliation ..."
    711 set -e
    712 taler-merchant-reconciliation \
    713     -L INFO \
    714     -c "$CONF" \
    715     -T "${TO_SLEEP}"000000 \
    716     -t &> taler-merchant-reconciliation.log
    717 echo " OK"
    718 
    719 
    720 echo -n "Fetching wire transfers ..."
    721 
    722 STATUS=$(curl 'http://localhost:9966/private/transfers' \
    723     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    724 
    725 if [ "$STATUS" != "200" ]
    726 then
    727     jq . < "$LAST_RESPONSE"
    728     exit_fail "Expected response 200 Ok. got: $STATUS"
    729 fi
    730 
    731 TRANSFERS_LIST_SIZE=$(jq -r '.transfers | length' < "$LAST_RESPONSE")
    732 
    733 if [ "$TRANSFERS_LIST_SIZE" != "1" ]
    734 then
    735     jq . < "$LAST_RESPONSE"
    736     exit_fail "Expected 1 entry in transfer list. Got: $TRANSFERS_LIST_SIZE"
    737 fi
    738 
    739 echo "OK"
    740 
    741 echo -n "Checking order status ..."
    742 STATUS=$(curl "http://localhost:9966/private/orders/${ORDER_ID}?transfer=YES" \
    743     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    744 if [ "$STATUS" != "200" ]
    745 then
    746     jq . < "$LAST_RESPONSE"
    747     exit_fail "Expected 200, after order inquiry. got: $STATUS"
    748 fi
    749 DEPOSIT_TOTAL=$(jq -r .deposit_total < "$LAST_RESPONSE")
    750 if [ "$DEPOSIT_TOTAL" == "TESTKUDOS:0" ]
    751 then
    752     jq . < "$LAST_RESPONSE"
    753     exit_fail "Expected non-zero deposit total. got: $DEPOSIT_TOTAL"
    754 fi
    755 echo " OK"
    756 
    757 echo -n "Checking bank account status ..."
    758 if [ 1 = "$USE_FAKEBANK" ]
    759 then
    760     STATUS=$(curl "http://localhost:8082/accounts/fortythree" \
    761                   -w "%{http_code}" \
    762                   -s \
    763                   -o "$LAST_RESPONSE")
    764     if [ "$STATUS" != "200" ]
    765     then
    766         jq . < "$LAST_RESPONSE"
    767         exit_fail "Expected response 200 Ok, getting account status. Got: $STATUS"
    768     fi
    769     BALANCE=$(jq -r .balance.amount < "$LAST_RESPONSE")
    770     if [ "$BALANCE" == "TESTKUDOS:0" ]
    771     then
    772         jq . < "$LAST_RESPONSE"
    773         exit_fail "Wire transfer did not happen. Got: $BALANCE"
    774     fi
    775 else
    776     ACCOUNT_PASSWORD="fortythree:x"
    777     BANK_HOST="localhost:18082"
    778     STATUS=$(curl "http://$ACCOUNT_PASSWORD@$BANK_HOST/accounts/fortythree" \
    779                   -w "%{http_code}" -s -o "$LAST_RESPONSE")
    780     if [ "$STATUS" != "200" ]
    781     then
    782         jq . < "$LAST_RESPONSE"
    783         exit_fail "Expected response 200 Ok, getting account status. Got: $STATUS"
    784     fi
    785     BALANCE=$(jq -r .balance.amount < "$LAST_RESPONSE")
    786     if [ "$BALANCE" == "TESTKUDOS:0" ]
    787     then
    788         jq . < "$LAST_RESPONSE"
    789         exit_fail "Wire transfer did not happen. Got: $BALANCE"
    790     fi
    791 fi
    792 echo " OK"
    793 
    794 echo -n "Getting information about kyc ..."
    795 STATUS=$(curl -H "Content-Type: application/json" -X GET \
    796     http://localhost:9966/private/kyc \
    797     -w "%{http_code}" -s -o /dev/null)
    798 if [ "$STATUS" != "200" ]
    799 then
    800     exit_fail "Expected 200. Got: $STATUS"
    801 fi
    802 echo " OK"
    803 
    804 echo "Test PASSED"
    805 exit 0