merchant

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

test_merchant_order_refund.sh (7855B)


      1 #!/bin/bash
      2 # This file is in the public domain.
      3 
      4 set -eu
      5 
      6 function clean_wallet() {
      7     echo 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       -em \
     38       -r "merchant-exchange-default" \
     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 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 # create with 2 bank account addresses
    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 
    128 echo "Ok"
    129 
    130 
    131 #
    132 # CREATE ORDER AND SELL IT
    133 #
    134 
    135 echo -n "Creating order to be paid..."
    136 STATUS=$(curl 'http://localhost:9966/private/orders' \
    137     -d '{"order":{"amount":"TESTKUDOS:5","summary":"payme","auto_refund":{"d_us":180000000}}}' \
    138     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    139 
    140 if [ "$STATUS" != "200" ]
    141 then
    142     jq . < "$LAST_RESPONSE"
    143     exit_fail "Expected 200, order created. got: $STATUS"
    144 fi
    145 
    146 ORDER_ID=$(jq -e -r .order_id < "$LAST_RESPONSE")
    147 TOKEN=$(jq -e -r .token < "$LAST_RESPONSE")
    148 
    149 RESPONSE_HEADERS=$(curl "http://localhost:9966/private/orders/${ORDER_ID}" \
    150                         -w "%{header_json}" \
    151                         -s \
    152                         -o "$LAST_RESPONSE")
    153 
    154 ETAG=$(echo "$RESPONSE_HEADERS" | jq -r .etag[0])
    155 ETAG_NQ=$(echo "$ETAG" | tr -d '"')
    156 PAY_URL=$(jq -e -r .taler_pay_uri < "$LAST_RESPONSE")
    157 
    158 echo "OK"
    159 
    160 LP_START=$(date +%s)
    161 
    162 echo -n "Long-polling for order status change paid..."
    163 
    164 curl "http://localhost:9966/private/orders/${ORDER_ID}?timeout_ms=30000&lp_not_etag=${ETAG_NQ}" \
    165      -H "If-none-match: $ETAG" \
    166      -w "%{header_json}" \
    167      -s \
    168      -o "${LAST_RESPONSE}-long-poll" &
    169 LP_PID="$!"
    170 
    171 #
    172 # PAY THE ORDER
    173 #
    174 
    175 # FIXME: use claim-only first:
    176 # $ taler-wallet-cli api 'preparePayForUri' '{"talerPayUri":"taler://pay/backend.demo.taler.net/instances/blog/2026.035-0184YVPMMWKKE/b4f09f9c-f863-4805-9caf-4f849e5eb34b?c=0P603WJ9T6TYTC7Z6QRDFXEJQ0"}'
    177 
    178 
    179 # set -x
    180 PAYMENT_START=$(date +%s)
    181 echo "Pay first order ${PAY_URL} ..."
    182 taler-wallet-cli \
    183     --no-throttle \
    184     -V \
    185     --wallet-db="$WALLET_DB" \
    186     handle-uri "${PAY_URL}" \
    187     -y \
    188     2> wallet-pay1.err \
    189     > wallet-pay1.log
    190 PAYMENT_END=$(date +%s)
    191 echo " OK (payment took $(( PAYMENT_END - PAYMENT_START )) secs )"
    192 
    193 STATUS=$(curl "http://localhost:9966/private/orders/${ORDER_ID}" \
    194     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    195 
    196 if [ "$STATUS" != "200" ]
    197 then
    198     jq . < "$LAST_RESPONSE"
    199     exit_fail "Expected 200, after pay. got: $STATUS"
    200 fi
    201 
    202 ORDER_STATUS=$(jq -r .order_status < "$LAST_RESPONSE")
    203 
    204 if [ "$ORDER_STATUS" != "paid" ]
    205 then
    206     jq . < "$LAST_RESPONSE"
    207     exit_fail "Order status should be 'paid'. got: $ORDER_STATUS"
    208 fi
    209 
    210 # Wait for long-poller
    211 echo -e "Wait for long-poller ..."
    212 wait $LP_PID &> /dev/null
    213 LP_END=$(date +%s)
    214 
    215 if (( "$LP_END" - "$LP_START" > 10 ))
    216 then
    217     exit_fail "Expected long-poller to return quickly. Instead took from $LP_START to $LP_END"
    218 fi
    219 echo " OK"
    220 
    221 echo -n "Sending refund for TESTKUDOS:1 ..."
    222 
    223 STATUS=$(curl "http://localhost:9966/private/orders/${ORDER_ID}/refund" \
    224     -d '{"refund":"TESTKUDOS:1","reason":"duplicated"}' \
    225     -w "%{http_code}" \
    226     -s \
    227     -o "$LAST_RESPONSE")
    228 
    229 if [ "$STATUS" != "200" ]
    230 then
    231     jq . < "$LAST_RESPONSE"
    232     exit_fail "Expected 200, after refund. got: $STATUS"
    233 fi
    234 REFUND_URI=$(jq -e -r .taler_refund_uri < "$LAST_RESPONSE")
    235 echo " OK"
    236 
    237 REFUND_START=$(date +%s)
    238 echo "First refund order ${REFUND_URI} ..."
    239 set -x
    240 taler-wallet-cli \
    241     --no-throttle \
    242     --wallet-db="$WALLET_DB" \
    243     handle-uri "${REFUND_URI}" \
    244     -y \
    245     2> wallet-refund1.err \
    246     > wallet-refund1.log
    247 timeout 60 taler-wallet-cli \
    248     --no-throttle \
    249     --wallet-db="$WALLET_DB" \
    250     run-until-done \
    251     2> wallet-pending-refund1.err \
    252     > wallet-pending-refund1.log
    253 REFUND_END=$(date +%s)
    254 echo " OK (refund1 took $(( REFUND_END - REFUND_START )) secs)"
    255 
    256 echo -n "Increasing refund for TESTKUDOS:3 ..."
    257 
    258 STATUS=$(curl "http://localhost:9966/private/orders/${ORDER_ID}/refund" \
    259     -d '{"refund":"TESTKUDOS:5","reason":"duplicated"}' \
    260     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    261 
    262 if [ "$STATUS" != "200" ]
    263 then
    264     jq . < "$LAST_RESPONSE"
    265     exit_fail "Expected 200, after refund. got: $STATUS"
    266 fi
    267 REFUND_URI=$(jq -e -r .taler_refund_uri < "$LAST_RESPONSE")
    268 echo " OK"
    269 
    270 REFUND2_START=$(date +%s)
    271 echo -n "Second refund order ${REFUND_URI} ..."
    272 taler-wallet-cli \
    273     --no-throttle \
    274     --wallet-db="$WALLET_DB" \
    275     handle-uri "${REFUND_URI}" \
    276     -y \
    277     2> wallet-refund2.err \
    278     > wallet-refund2.log
    279 timeout 60 taler-wallet-cli \
    280     --no-throttle \
    281     --wallet-db="$WALLET_DB" \
    282     run-until-done \
    283     2> wallet-pending-refund2.err \
    284     > wallet-pending-refund2.log
    285 REFUND2_END=$(date +%s)
    286 echo " OK (refund2 took $(( REFUND2_END - REFUND_START )) secs)"
    287 
    288 echo "TEST PASSED"
    289 exit 0