merchant

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

test_merchant_templates.sh (6670B)


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