The user should send a withdrawal request to the server. The withdrawal request should have the currency and amount being transferred.
// Express post endpoint to receive deposit requestsapp.post('/plugins/bank/withdrawal',toolsLib.security.verifyBearerTokenExpressMiddleware(['user']), (req, res) => {... });
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.
constamount,currency,BANK_ACCOUNT=// withdrawal data from requestconstuser_id=// ID of user making request// Create a pending burn and save responseconstpendingBurn=awaittoolsLib.wallet.burnAssetByKitId( user_id,// User kit id currency,// currency amount,// amount { status:false,// status false will make this burn pending description:'Bank Deposit',// description transactionId: transaction_id // transaction id });
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.
constlibrary=PAYMENT_SERVICE_SDK_LIBRARY; // Payment service sdkconstamount,currency=// withdrawal data from requestBANK_ACCOUNT; // user's bank account to withdrawal to// Withdrawal method for payment service sdklibrary.withdrawal(amount, currency,BANK_ACCOUNT);
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.
// Withdrawal method for payment service sdklibrary.withdrawal(amount, currency,USER_BANK_ACCOUNT).then((data) => {// data contains status of the withdrawal }).catch((err) => {// withdrawal request failed });
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.
// Withdrawal method for payment service sdklibrary.withdrawal(amount, currency,USER_BANK_ACCOUNT).then((data) => {if (data.successful) {// If the withdrawal was successfultoolsLib.wallet.updatePendingBurn(pendingBurn.transaction_id,// transaction_id saved from phase 1 { status:true,// Optional: Update burn's transaction id to match // payment service withdrawal's transaction id updatedTransactionId:data.transaction_id } ); } elseif (data.failed) {// If the withdrawal failedtoolsLib.wallet.updatePendingBurn(pendingBurn.transaction_id,// transaction_id saved from phase 1 { rejected:true } ); } }).catch((err) => {// withdrawal request failedtoolsLib.wallet.updatePendingBurn(pendingBurn.transaction_id,// transaction_id saved from phase 1 { rejected:true } ); });
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.
app.post('/plugins/bank/withdrawal',toolsLib.security.verifyBearerTokenExpressMiddleware(['user']),async (req, res) => {const { currency,amount,bank_account } =req.body;constuser_id=req.auth.sub.id;constpendingBurn=awaittoolsLib.wallet.burnAssetByKitId( user_id,// User kit id currency,// currency amount,// amount { status:false,// status false will make this burn pending description:'Bank Withdrawal',// description transactionId: transaction_id // transaction id } );constlibrary=PAYMENT_SERVICE_SDK_LIBRARY; // Payment service sdkconstamount,currency=// withdrawal data from request// Withdrawal method for payment service sdkawaitlibrary.withdrawal(amount, currency, bank_account).then((data) => {if (data.successful) {// If the withdrawal was successfulreturntoolsLib.wallet.updatePendingBurn(pendingBurn.transaction_id,// transaction_id saved from phase 1 { status:true,// Optional: Update burn's transaction id to match // payment service withdrawal's transaction id updatedTransactionId:data.transaction_id } ); } elseif (data.failed) {// If the withdrawal failedreturntoolsLib.wallet.updatePendingBurn(pendingBurn.transaction_id,// transaction_id saved from phase 1 { rejected:true } ); } }).catch((err) => {// withdrawal request failedreturntoolsLib.wallet.updatePendingBurn(pendingBurn.transaction_id,// transaction_id saved from phase 1 { rejected:true } ); });});
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.