The Select for Withdrawal use case allows users to select an external account to off-board their funds. Users can choose previously linked external accounts or add new ones to withdraw to.
The example code below is for web applications. For native applications using a WebView, you’ll still need to implement communication through a bridge to send events to the native side, as highlighted in the Using the SDK page.
Once you have the session, initialize the Payment Widget for the Select for Withdrawal flow:
Copy
Ask AI
import { PaymentWidget } from '@uphold/enterprise-payment-widget-web-sdk';const initializeWithdrawalWidget = async () => { // Create the session const sessionData = await createWithdrawalSession(); // Initialize the Widget with the session const widget = new PaymentWidget<'select-for-withdrawal'>(sessionData.session, { debug: true }); // Set up event handlers (see sections below) setupEventHandlers(widget); // Mount the Widget widget.mountIframe(document.getElementById('payment-container'));};
The complete event is fired when the user successfully selects a withdrawal method.
Copy
Ask AI
widget.on('complete', (event) => { const result = event.detail.value; console.log('Withdrawal destination selected:', result); // result.via indicates the type of selection const { via, selection } = result; // Process the selected external account handleWithdrawalMethodSelected(via, selection); // Clean up the Widget widget.unmount();});
External Account Selection (via: "external-account")
When an external account is selected, the selection property contains an external account object with the saved payment method details.
Copy
Ask AI
const handleWithdrawalMethodSelected = (via, selection) => { if (via === 'external-account') { // selection is an ExternalAccount console.log('Selected external account:', { id: selection.id, type: selection.type, // 'card' or 'bank' label: selection.label, // Additional properties depend on external account type }); // Proceed with existing external account proceedWithExternalAccount(selection); }};
The cancel event is fired when the user closes the Widget without selecting an external account.
Copy
Ask AI
widget.on('cancel', () => { console.log('User cancelled withdrawal destination selection'); // Handle cancellation handleWithdrawalCancelled(); // Clean up the Widget widget.unmount();});const handleWithdrawalCancelled = () => { // Redirect back to the previous page or main withdrawal page};
The error event is fired when an error occurs during the external account selection process.
Copy
Ask AI
widget.on('error', (event) => { const error = event.detail.error; console.error('Withdrawal Widget error:', error); // Handle the error handleWithdrawalError(error); // Clean up the Widget widget.unmount();});const handleWithdrawalError = (error) => { console.error('Error details:', { code: error.code, message: error.message }); // Show user-friendly error message like 'An error occurred. Please try again later.'};
The Payment Widget does not offer user-facing error handling. It’s the responsibility of the host application to present an error message to the user and unmount the Widget.
After the user selects an external account to withdraw to, you’ll typically want to:
Collect withdrawal details - Collect remaining instructions from the user (e.g. amount, origin account)
Create withdrawal transaction - Use the selected external account to create the actual withdrawal. If the selected payment method is a card, you’ll need to use the Authorize flow for additional authentication