merchant

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

test_merchant_order_refund.sh (6773B)


      1 #!/bin/bash
      2 #!/bin/bash
      3 # This file is in the public domain.
      4 
      5 set -eu
      6 
      7 function clean_wallet() {
      8     echo rm -f "${WALLET_DB}"
      9     exit_cleanup
     10 }
     11 
     12 
     13 # Replace with 0 for nexus...
     14 USE_FAKEBANK=1
     15 if [ 1 = "$USE_FAKEBANK" ]
     16 then
     17     ACCOUNT="exchange-account-2"
     18     BANK_FLAGS="-f -d x-taler-bank -u $ACCOUNT"
     19     BANK_URL="http://localhost:8082/"
     20 else
     21     ACCOUNT="exchange-account-1"
     22     BANK_FLAGS="-ns -d iban -u $ACCOUNT"
     23     BANK_URL="http://localhost:18082/"
     24     echo -n "Testing for libeufin-bank"
     25     libeufin-bank --help >/dev/null </dev/null || exit_skip " MISSING"
     26     echo " FOUND"
     27 
     28 fi
     29 
     30 . setup.sh
     31 
     32 echo -n "Testing for taler-harness"
     33 taler-harness --help >/dev/null </dev/null || exit_skip " MISSING"
     34 echo " FOUND"
     35 
     36 # Launch exchange, merchant and bank.
     37 setup -c "test_template.conf" \
     38       -em \
     39       -r "merchant-exchange-default" \
     40       $BANK_FLAGS
     41 LAST_RESPONSE=$(mktemp -p "${TMPDIR:-/tmp}" test_response.conf-XXXXXX)
     42 CONF="test_template.conf.edited"
     43 WALLET_DB=$(mktemp -p "${TMPDIR:-/tmp}" test_wallet.json-XXXXXX)
     44 EXCHANGE_URL="http://localhost:8081/"
     45 
     46 # Install cleanup handler (except for kill -9)
     47 trap clean_wallet EXIT
     48 
     49 echo -n "First prepare wallet with coins ..."
     50 rm -f "$WALLET_DB"
     51 taler-wallet-cli \
     52     --no-throttle \
     53     --wallet-db="$WALLET_DB" \
     54     api \
     55     --expect-success 'withdrawTestBalance' \
     56   "$(jq -n '
     57     {
     58         amount: "TESTKUDOS:99",
     59         corebankApiBaseUrl: $BANK_URL,
     60         exchangeBaseUrl: $EXCHANGE_URL
     61     }' \
     62     --arg BANK_URL "${BANK_URL}" \
     63     --arg EXCHANGE_URL "$EXCHANGE_URL"
     64   )" 2>wallet-withdraw-1.err >wallet-withdraw-1.out
     65 echo -n "."
     66 # FIXME-MS: add logic to have nexus check immediately here.
     67 # sleep 10
     68 echo -n "."
     69 # NOTE: once libeufin can do long-polling, we should
     70 # be able to reduce the delay here and run wirewatch
     71 # always in the background via setup
     72 taler-exchange-wirewatch \
     73     -a "$ACCOUNT" \
     74     -L "INFO" \
     75     -c "$CONF" \
     76     -t &> taler-exchange-wirewatch.out
     77 echo -n "."
     78 taler-wallet-cli \
     79     --wallet-db="$WALLET_DB" \
     80     run-until-done \
     81     2>wallet-withdraw-finish-1.err \
     82     >wallet-withdraw-finish-1.out
     83 echo " OK"
     84 
     85 CURRENCY_COUNT=$(taler-wallet-cli --wallet-db="$WALLET_DB" balance | jq '.balances|length')
     86 if [ "$CURRENCY_COUNT" = "0" ]
     87 then
     88     exit_fail "Expected least one currency, withdrawal failed. check log."
     89 fi
     90 
     91 #
     92 # CREATE INSTANCE FOR TESTING
     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 
    129 echo "Ok"
    130 
    131 
    132 #
    133 # CREATE ORDER AND SELL IT
    134 #
    135 
    136 echo -n "Creating order to be paid..."
    137 STATUS=$(curl 'http://localhost:9966/private/orders' \
    138     -d '{"order":{"amount":"TESTKUDOS:5","summary":"payme","auto_refund":{"d_us":180000000}}}' \
    139     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    140 
    141 if [ "$STATUS" != "200" ]
    142 then
    143     jq . < "$LAST_RESPONSE"
    144     exit_fail "Expected 200, order created. got: $STATUS"
    145 fi
    146 
    147 ORDER_ID=$(jq -e -r .order_id < "$LAST_RESPONSE")
    148 TOKEN=$(jq -e -r .token < "$LAST_RESPONSE")
    149 
    150 STATUS=$(curl "http://localhost:9966/private/orders/${ORDER_ID}" \
    151     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    152 
    153 if [ "$STATUS" != "200" ]
    154 then
    155     jq . < "$LAST_RESPONSE"
    156     exit_fail "Expected 200, getting order info before claming it. got: $STATUS"
    157 fi
    158 
    159 PAY_URL=$(jq -e -r .taler_pay_uri < "$LAST_RESPONSE")
    160 
    161 echo "OK"
    162 
    163 #
    164 # PAY THE ORDER
    165 #
    166 # set -x
    167 PAYMENT_START=$(date +%s)
    168 echo "Pay first order ${PAY_URL} ..."
    169 taler-wallet-cli --no-throttle -V --wallet-db="$WALLET_DB" handle-uri "${PAY_URL}" -y 2> wallet-pay1.err > wallet-pay1.log
    170 PAYMENT_END=$(date +%s)
    171 echo " OK (payment took $(( PAYMENT_END - PAYMENT_START )) secs )"
    172 
    173 STATUS=$(curl "http://localhost:9966/private/orders/${ORDER_ID}" \
    174     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    175 
    176 if [ "$STATUS" != "200" ]
    177 then
    178     jq . < "$LAST_RESPONSE"
    179     exit_fail "Expected 200, after pay. got: $STATUS"
    180 fi
    181 
    182 ORDER_STATUS=$(jq -r .order_status < "$LAST_RESPONSE")
    183 
    184 if [ "$ORDER_STATUS" != "paid" ]
    185 then
    186     jq . < "$LAST_RESPONSE"
    187     exit_fail "Order status should be 'paid'. got: $ORDER_STATUS"
    188 fi
    189 
    190 
    191 echo Sending refund for TESTKUDOS:1
    192 
    193 STATUS=$(curl "http://localhost:9966/private/orders/${ORDER_ID}/refund" \
    194     -d '{"refund":"TESTKUDOS:1","reason":"duplicated"}' \
    195     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    196 
    197 if [ "$STATUS" != "200" ]
    198 then
    199     jq . < "$LAST_RESPONSE"
    200     exit_fail "Expected 200, after refund. got: $STATUS"
    201 fi
    202 
    203 REFUND_URI=$(jq -e -r .taler_refund_uri < "$LAST_RESPONSE")
    204 
    205 REFUND_START=$(date +%s)
    206 echo "First refund order ${REFUND_URI} ..."
    207 set -x
    208 taler-wallet-cli --no-throttle --wallet-db="$WALLET_DB" handle-uri "${REFUND_URI}" -y 2> wallet-refund1.err > wallet-refund1.log
    209 taler-wallet-cli --no-throttle --wallet-db="$WALLET_DB" run-pending -y 2> wallet-pending-refund1.err > wallet-pending-refund1.log
    210 REFUND_END=$(date +%s)
    211 echo " OK (refund1 took $(( REFUND_END - REFUND_START )) secs )"
    212 
    213 echo Increasing refund for TESTKUDOS:3
    214 
    215 STATUS=$(curl "http://localhost:9966/private/orders/${ORDER_ID}/refund" \
    216     -d '{"refund":"TESTKUDOS:5","reason":"duplicated"}' \
    217     -w "%{http_code}" -s -o "$LAST_RESPONSE")
    218 
    219 if [ "$STATUS" != "200" ]
    220 then
    221     jq . < "$LAST_RESPONSE"
    222     exit_fail "Expected 200, after refund. got: $STATUS"
    223 fi
    224 
    225 REFUND_URI=$(jq -e -r .taler_refund_uri < "$LAST_RESPONSE")
    226 
    227 REFUND2_START=$(date +%s)
    228 echo "Second refund order ${REFUND_URI} ..."
    229 taler-wallet-cli --no-throttle --wallet-db="$WALLET_DB" handle-uri "${REFUND_URI}" -y 2> wallet-refund2.err > wallet-refund2.log
    230 taler-wallet-cli --no-throttle --wallet-db="$WALLET_DB" run-pending -y 2> wallet-pending-refund2.err > wallet-pending-refund2.log
    231 REFUND2_END=$(date +%s)
    232 echo " OK (refund2 took $(( REFUND2_END - REFUND_START )) secs )"
    233 
    234 exit 0