Handling Withdrawals

Phase 1: Receiving Withdrawal Request from a User

Step 1

The user should send a withdrawal request to the server. The withdrawal request should have the currency and amount being transferred.
1
// Express post endpoint to receive deposit requests
2
app.post(
3
'/plugins/bank/withdrawal',
4
toolsLib.security.verifyBearerTokenExpressMiddleware(['user']),
5
(req, res) => {
6
...
7
}
8
);
Copied!

Step 2

The server creates a pending burn transaction for this specific withdrawal. This will lock the amount being withdrawn in the user's balance. This ensures that this amount will not be used during this withdrawal process. The transaction_id of the pending burn is required for phase 3 of this process.
1
const amount, currency, BANK_ACCOUNT = // withdrawal data from request
2
const user_id = // ID of user making request
3
4
// Create a pending burn and save response
5
const pendingBurn = await toolsLib.wallet.burnAssetByKitId(
6
user_id, // User kit id
7
currency, // currency
8
amount, // amount
9
{
10
status: false, // status false will make this burn pending
11
description: 'Bank Deposit', // description
12
transactionId: transaction_id // transaction id
13
}
14
);
Copied!

Phase 2: Payment service attempts withdrawal to user's bank account

Step 1

The server makes a withdrawal request using the payment service.
This step will differ based on which payment service is used.
1
const library = PAYMENT_SERVICE_SDK_LIBRARY; // Payment service sdk
2
const amount, currency = // withdrawal data from request
3
BANK_ACCOUNT; // user's bank account to withdrawal to
4
5
// Withdrawal method for payment service sdk
6
library.withdrawal(amount, currency, BANK_ACCOUNT);
Copied!

Step 2

Payment service attempts to make the withdrawal to the user's bank account.

Step 3

Payment service responds to server with the withdrawal's status.
1
// Withdrawal method for payment service sdk
2
library.withdrawal(amount, currency, USER_BANK_ACCOUNT)
3
.then((data) => {
4
// data contains status of the withdrawal
5
})
6
.catch((err) => {
7
// withdrawal request failed
8
});
Copied!

Phase 3: Server updates status of pending burn

Step 1

The server updates the status of the pending burn created in phase 2 based on response from payment service.
1
// Withdrawal method for payment service sdk
2
library.withdrawal(amount, currency, USER_BANK_ACCOUNT)
3
.then((data) => {
4
if (data.successful) {
5
// If the withdrawal was successful
6
toolsLib.wallet.updatePendingBurn(
7
pendingBurn.transaction_id, // transaction_id saved from phase 1
8
{
9
status: true,
10
// Optional: Update burn's transaction id to match
11
// payment service withdrawal's transaction id
12
updatedTransactionId: data.transaction_id
13
}
14
);
15
} else if (data.failed) {
16
// If the withdrawal failed
17
toolsLib.wallet.updatePendingBurn(
18
pendingBurn.transaction_id, // transaction_id saved from phase 1
19
{
20
rejected: true
21
}
22
);
23
}
24
})
25
.catch((err) => {
26
// withdrawal request failed
27
toolsLib.wallet.updatePendingBurn(
28
pendingBurn.transaction_id, // transaction_id saved from phase 1
29
{
30
rejected: true
31
}
32
);
33
});
Copied!

Step 2

Once the pending burn's status is updated, the locked amount on the user's balance will be unlocked. If the withdrawal was successful, the user's locked amount will be removed entirely from the total balance. If the withdrawal was unsuccessful, the locked amount will return to the user's total balance.

Code Example

For this example, we are using a placeholder payment service sdk called PAYMENT_SERVICE_SDK_LIBRARY. The withdrawal step will differ based on the payment service used. Regardless, the overall flow of this process should be relatively the same.
1
app.post(
2
'/plugins/bank/withdrawal',
3
toolsLib.security.verifyBearerTokenExpressMiddleware(['user']),
4
async (req, res) => {
5
const { currency, amount, bank_account } = req.body;
6
const user_id = req.auth.sub.id;
7
8
const pendingBurn = await toolsLib.wallet.burnAssetByKitId(
9
user_id, // User kit id
10
currency, // currency
11
amount, // amount
12
{
13
status: false, // status false will make this burn pending
14
description: 'Bank Withdrawal', // description
15
transactionId: transaction_id // transaction id
16
}
17
);
18
19
const library = PAYMENT_SERVICE_SDK_LIBRARY; // Payment service sdk
20
const amount, currency = // withdrawal data from request
21
22
// Withdrawal method for payment service sdk
23
await library.withdrawal(amount, currency, bank_account)
24
.then((data) => {
25
if (data.successful) {
26
// If the withdrawal was successful
27
return toolsLib.wallet.updatePendingBurn(
28
pendingBurn.transaction_id, // transaction_id saved from phase 1
29
{
30
status: true,
31
// Optional: Update burn's transaction id to match
32
// payment service withdrawal's transaction id
33
updatedTransactionId: data.transaction_id
34
}
35
);
36
} else if (data.failed) {
37
// If the withdrawal failed
38
return toolsLib.wallet.updatePendingBurn(
39
pendingBurn.transaction_id, // transaction_id saved from phase 1
40
{ rejected: true }
41
);
42
}
43
})
44
.catch((err) => {
45
// withdrawal request failed
46
return toolsLib.wallet.updatePendingBurn(
47
pendingBurn.transaction_id, // transaction_id saved from phase 1
48
{ rejected: true }
49
);
50
});
51
});
Copied!

Advanced

Although bank withdrawals can be handled one by one like above, we generally recommend withdrawals to be batched in intervals. You can do this by using a cron job that runs every five minutes or so. Every time the job runs, it will query all pending burns in the database, batch them by currency, and make a single withdrawal per currency to the payment service. This will lower the amount of calls being made.