summaryrefslogtreecommitdiff
path: root/src/testing/test_merchant_transfer_tracking.sh
blob: 6ac079f33e4c957a993f3087dcb6ac756b844410 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/bin/bash
# This file is part of TALER
# Copyright (C) 2014-2023 Taler Systems SA
#
# TALER is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3, or
# (at your option) any later version.
#
# TALER is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with TALER; see the file COPYING.  If not, see
# <http://www.gnu.org/licenses/>
#
# Testcase for #6912 (failed to reproduce so far)

set -eu

echo "Re-initializing database (needed for current libeufin)" # FIXME-MS: enable idempotency AND db-reset!
dropdb talercheck || true
createdb talercheck

. setup.sh

# Launch system.
setup -c "test_template.conf" -enms -u "exchange-account-1" -d "iban"
LAST_RESPONSE=$(mktemp test_response.conf-XXXXXX)
WALLET_DB=$(mktemp test_wallet.json-XXXXXX)
CONF="test_template.conf.edited"
#FORTYTHREE=$(get_payto_uri merchant x)
BANK_URL="http://localhost:18082/demobanks/default/"
EXCHANGE_URL="http://localhost:8081/"

echo -n "First prepare wallet with coins..."
rm -f "$WALLET_DB"
taler-wallet-cli \
    --no-throttle \
    --wallet-db="$WALLET_DB" \
    api \
    --expect-success 'withdrawTestBalance' \
  "$(jq -n '
    {
        amount: "TESTKUDOS:99",
        bankAccessApiBaseUrl: $BANK_URL,
        exchangeBaseUrl: $EXCHANGE_URL
    }' \
    --arg BANK_URL "${BANK_URL}access-api/" \
    --arg EXCHANGE_URL "$EXCHANGE_URL"
  )" 2>wallet-withdraw-1.err >wallet-withdraw-1.out
echo -n "."
sleep 10
echo -n "."
# NOTE: once libeufin can do long-polling, we should
# be able to reduce the delay here and run wirewatch
# always in the background via setup
taler-exchange-wirewatch -L "INFO" -c "$CONF" -t &> taler-exchange-wirewatch.out
echo -n "."

taler-wallet-cli \
    --wallet-db="$WALLET_DB" \
    run-until-done \
    2>wallet-withdraw-finish-1.err >wallet-withdraw-finish-1.out
echo " OK"

#
# CREATE INSTANCE FOR TESTING
#

echo -n "Configuring merchant default instance ..."
TOR_PAYTO=$(get_payto_uri tor x)
GNUNET_PAYTO=$(get_payto_uri gnunet x)
# create with 2 address

STATUS=$(curl -H "Content-Type: application/json" -X POST \
    -H 'Authorization: Bearer secret-token:super_secret' \
    http://localhost:9966/management/instances \
    -d '{"auth":{"method":"external"},"accounts":[{"payto_uri":"'"$TOR_PAYTO"'"},{"payto_uri":"'"$GNUNET_PAYTO"'"}],"id":"default","name":"default","user_type":"business","address":{},"jurisdiction":{},"use_stefan":true,"default_wire_transfer_delay":{"d_us" : 50000000},"default_pay_delay":{"d_us": 60000000}}' \
    -w "%{http_code}" -s -o /dev/null)

if [ "$STATUS" != "204" ]
then
    exit_fail "Expected 204, instance created. got: $STATUS"
fi
echo "OK"

echo -n "Configuring merchant test instance ..."
SURVEY_PAYTO=$(get_payto_uri survey x)
TUTORIAL_PAYTO=$(get_payto_uri tutorial x)
# create with 2 address

STATUS=$(curl -H "Content-Type: application/json" -X POST \
    -H 'Authorization: Bearer secret-token:super_secret' \
    http://localhost:9966/management/instances \
    -d '{"auth":{"method":"external"},"accounts":[{"payto_uri":"'"$SURVEY_PAYTO"'"},{"payto_uri":"'"$TUTORIAL_PAYTO"'"}],"id":"test","name":"default","user_type":"business","address":{},"jurisdiction":{},"use_stefan":true,"default_wire_transfer_delay":{"d_us" : 50000000},"default_pay_delay":{"d_us": 60000000}}' \
    -w "%{http_code}" -s -o /dev/null)

if [ "$STATUS" != "204" ]
then
    exit_fail "Expected 204, instance created. got: $STATUS"
fi
echo "OK"

# CREATE ORDER AND SELL IT
echo -n "Creating order to be paid..."
STATUS=$(curl 'http://localhost:9966/instances/test/private/orders' \
    -d '{"order":{"amount":"TESTKUDOS:1","summary":"payme"}}' \
    -w "%{http_code}" -s -o "$LAST_RESPONSE")

if [ "$STATUS" != "200" ]
then
    cat "$LAST_RESPONSE"
    exit_fail "Expected 200 ok, order created. got: $STATUS"
fi

ORDER_ID=$(jq -e -r .order_id < "$LAST_RESPONSE")
#TOKEN=$(jq -e -r .token < "$LAST_RESPONSE")

STATUS=$(curl "http://localhost:9966/instances/test/private/orders/${ORDER_ID}" \
    -w "%{http_code}" -s -o "$LAST_RESPONSE")

if [ "$STATUS" != "200" ]
then
    cat "$LAST_RESPONSE"
    exit_fail "Expected 200 ok, getting order info before claming it. got: $STATUS"
fi
PAY_URL=$(jq -e -r .taler_pay_uri < "$LAST_RESPONSE")
echo "OK"

NOW=$(date +%s)
echo -n "Pay first order ..."
taler-wallet-cli \
    --no-throttle \
    --wallet-db="$WALLET_DB" \
    handle-uri "${PAY_URL}" \
    -y \
    2> wallet-pay1.err > wallet-pay1.log
NOW2=$(date +%s)
echo " OK (took $(( NOW2 - NOW )) secs)"

STATUS=$(curl "http://localhost:9966/instances/test/private/orders/${ORDER_ID}" \
    -w "%{http_code}" -s -o "$LAST_RESPONSE")

if [ "$STATUS" != "200" ]
then
    cat "$LAST_RESPONSE"
    exit_fail "Expected 200 ok, after pay. got: $STATUS"
fi

ORDER_STATUS=$(jq -r .order_status < "$LAST_RESPONSE")

if [ "$ORDER_STATUS" != "paid" ]
then
    cat "$LAST_RESPONSE"
    exit_fail "Expected 'paid'. got: $ORDER_STATUS"
fi

#
# WIRE TRANSFER TO MERCHANT AND NOTIFY BACKEND
#

#PAY_DEADLINE=$(jq -r .contract_terms.pay_deadline.t_s < "$LAST_RESPONSE")
WIRE_DEADLINE=$(jq -r .contract_terms.wire_transfer_deadline.t_s < "$LAST_RESPONSE")

NOW=$(date +%s)

TO_SLEEP=$(( WIRE_DEADLINE - NOW ))
echo "waiting $TO_SLEEP secs for wire transfer"

echo -n "Perform wire transfers ..."
taler-exchange-aggregator -y -c $CONF -T ${TO_SLEEP}000000 -t -L INFO &> aggregator.log
taler-exchange-transfer -c $CONF -t -L INFO &> transfer.log
echo " DONE"

echo -n "waiting for Nexus and Sandbox to settle the payment .."
sleep 3
echo " DONE"

echo -n "Obtaining wire transfer details from bank..."
# Emulating the previous pybank-based logic of getting
# the wire transfer information _via the exchange_ bank
# account.  NOTE: grabbing tx == 0, since the latest
# transaction appear first in the bank's history.
BANKDATA=$(get_bankaccount_transactions exchange x | jq '.transactions[0]')
SUBJECT=$(echo "$BANKDATA" | jq -r .subject)
WTID=$(echo "$SUBJECT" | awk '{print $1}')
WURL=$(echo "$SUBJECT" | awk '{print $2}')
CREDIT_AMOUNT=$(echo "$BANKDATA" | jq -r .currency):$(echo "$BANKDATA" | jq -r .amount)
TARGET=$(echo "$BANKDATA" | jq -r .creditorIban)
# Figure out which account got paid, in order to
# resort the right (and complete: including the receiver-name)
# TARGET_PAYTO
if echo "$SURVEY_PAYTO" | grep -q "$TARGET" > /dev/null; then
  TARGET_PAYTO="$SURVEY_PAYTO";
fi
if echo "$TUTORIAL_PAYTO" | grep -q "$TARGET" > /dev/null; then
  TARGET_PAYTO="$TUTORIAL_PAYTO";
fi
echo " DONE"
if [ "$EXCHANGE_URL" != "$WURL" ]
then
    exit_fail "Wrong exchange URL in subject '$SUBJECT', expected $EXCHANGE_URL"
fi

echo " OK"

set +e

export TARGET_PAYTO
export WURL
export WTID
export CREDIT_AMOUNT
export LAST_RESPONSE

echo -n "Notifying merchant of correct wire transfer, but on wrong instance..."

#issue 6912
#here we are notifying the transfer into a wrong instance (default) and the payto_uri of the default instance
STATUS=$(curl 'http://localhost:9966/instances/default/private/transfers' \
    -d "{\"credit_amount\":\"$CREDIT_AMOUNT\",\"wtid\":\"$WTID\",\"payto_uri\":\"$TOR_PAYTO\",\"exchange_url\":\"$WURL\"}" \
    -m 3 \
    -w "%{http_code}" -s -o "$LAST_RESPONSE")

if [ "$STATUS" != "204" ]
then
    jq . < "$LAST_RESPONSE"
    exit_fail "Expected response 204 no content, after providing transfer data. Got: $STATUS"
fi
echo " OK"


echo -n "Fetching wire transfers of DEFAULT instance ..."

STATUS=$(curl 'http://localhost:9966/instances/default/private/transfers' \
    -w "%{http_code}" -s -o "$LAST_RESPONSE")

if [ "$STATUS" != "200" ]
then
    jq . < "$LAST_RESPONSE"
    exit_fail "Expected response 200 Ok. got: $STATUS"
fi

TRANSFERS_LIST_SIZE=$(jq -r '.transfers | length' < "$LAST_RESPONSE")

if [ "$TRANSFERS_LIST_SIZE" != "1" ]
then
    jq . < "$LAST_RESPONSE"
    exit_fail "Expected one transfer. got: $TRANSFERS_LIST_SIZE"
fi

echo "OK"

echo -n "Fetching running taler-merchant-exchange on bogus transfer ..."
taler-merchant-exchange -c $CONF -L INFO -t &> taler-merchant-exchange-bad.log
echo "OK"

echo -n "Fetching wire transfers of 'test' instance ..."

STATUS=$(curl 'http://localhost:9966/instances/test/private/transfers' \
    -w "%{http_code}" -s -o "$LAST_RESPONSE")

if [ "$STATUS" != "200" ]
then
    jq . < "$LAST_RESPONSE"
    exit_fail "Expected response 200 Ok. got: $STATUS"
fi

TRANSFERS_LIST_SIZE=$(jq -r '.transfers | length' < "$LAST_RESPONSE")

if [ "$TRANSFERS_LIST_SIZE" != "0" ]
then
    jq . < "$LAST_RESPONSE"
    exit_fail "Expected non-empty transfer list size. got: $TRANSFERS_LIST_SIZE"
fi

echo "OK"

echo -n "Checking order status ..."
STATUS=$(curl "http://localhost:9966/instances/test/private/orders/${ORDER_ID}?transfer=YES" \
    -w "%{http_code}" -s -o "$LAST_RESPONSE")

if [ "$STATUS" != "200" ]
then
    jq . < "$LAST_RESPONSE"
    exit_fail "Expected 200 ok, after order inquiry. got: $STATUS"
fi

WAS_WIRED=$(jq -r .wired < "$LAST_RESPONSE")

if [ "$WAS_WIRED" == "true" ]
then
    jq . < "$LAST_RESPONSE"
    exit_fail ".wired is true, expected false"
fi

echo " OK"

echo -n "Notifying merchant of correct wire transfer in the correct instance..."
#this time in the correct instance so the order will be marked as wired...

STATUS=$(curl 'http://localhost:9966/instances/test/private/transfers' \
    -d '{"credit_amount":"'"$CREDIT_AMOUNT"'","wtid":"'"$WTID"'","payto_uri":"'"$TARGET_PAYTO"'","exchange_url":"'"$WURL"'"}' \
    -m 3 \
    -w "%{http_code}" -s -o "$LAST_RESPONSE")

if [ "$STATUS" != "204" ]
then
    jq . < "$LAST_RESPONSE"
    exit_fail "Expected response 204 no content, after providing transfer data. got: $STATUS"
fi
echo " OK"

echo -n "Fetching running taler-merchant-exchange on good transfer ..."
taler-merchant-exchange -c $CONF -L INFO -t &> taler-merchant-exchange-bad.log
echo "OK"

echo -n "Fetching wire transfers of TEST instance ..."

STATUS=$(curl 'http://localhost:9966/instances/test/private/transfers' \
    -w "%{http_code}" -s -o "$LAST_RESPONSE")

if [ "$STATUS" != "200" ]
then
    jq . < "$LAST_RESPONSE"
    exit_fail "Expected response 200 Ok. got: $STATUS"
fi

TRANSFERS_LIST_SIZE=$(jq -r '.transfers | length' < "$LAST_RESPONSE")

if [ "$TRANSFERS_LIST_SIZE" != "1" ]
then
    jq . < "$LAST_RESPONSE"
    exit_fail "Expected one transfer. got: $TRANSFERS_LIST_SIZE"
fi

echo "OK"

echo -n "Checking order status ..."
STATUS=$(curl "http://localhost:9966/instances/test/private/orders/${ORDER_ID}?transfer=YES" \
    -w "%{http_code}" -s -o "$LAST_RESPONSE")

if [ "$STATUS" != "200" ]
then
    jq . < "$LAST_RESPONSE"
    exit_fail "Expected 200 ok, after order inquiry. got: $STATUS"
fi

WAS_WIRED=$(jq -r .wired < "$LAST_RESPONSE")

if [ "$WAS_WIRED" != "true" ]
then
    jq . < "$LAST_RESPONSE"
    exit_fail ".wired false, expected true"
fi

echo " OK"

exit 0