diff --git a/react/lib/components/Widget/Widget.tsx b/react/lib/components/Widget/Widget.tsx index a6cdc156..076336c6 100644 --- a/react/lib/components/Widget/Widget.tsx +++ b/react/lib/components/Widget/Widget.tsx @@ -126,6 +126,32 @@ interface StyleProps { copied: boolean } +function convertBip21ToDeeplink(bip21Url: string, apiBaseUrl?: string): string | undefined { + if (!apiBaseUrl) { + return undefined + } + + // Parse BIP21 URL: ecash:address?amount=100&opreturnraw=xxx + const [addressPart, queryPart] = bip21Url.split('?') + const params = new URLSearchParams() + params.set('address', addressPart) + + if (queryPart) { + const queryParams = new URLSearchParams(queryPart) + // Add all query parameters from the BIP21 string + queryParams.forEach((value, key) => { + params.set(key, value) + }) + } + + // Add b=1 to the deeplink URL to indicate that the payment app should move + // back to the browser after the payment is complete. + params.set('b', '1') + + // Return absolute URL for the deeplink + return `${apiBaseUrl}/app?${params.toString()}` +} + export const Widget: React.FunctionComponent = props => { const { to, @@ -948,7 +974,8 @@ export const Widget: React.FunctionComponent = props => { const handleButtonClick = async () => { if (thisAddressType === 'XEC') { - await openCashtabPayment(url) + const deeplinkUrl = convertBip21ToDeeplink(url, apiBaseUrl) + await openCashtabPayment(url, deeplinkUrl) } else { window.location.href = url } diff --git a/react/lib/components/Widget/WidgetContainer.tsx b/react/lib/components/Widget/WidgetContainer.tsx index e0836d23..dba0fb8d 100644 --- a/react/lib/components/Widget/WidgetContainer.tsx +++ b/react/lib/components/Widget/WidgetContainer.tsx @@ -2,7 +2,7 @@ import { OptionsObject, SnackbarProvider, useSnackbar } from 'notistack'; import React, { useCallback, useEffect, useMemo, useState } from 'react'; import { getAltpaymentClient } from '../../altpayment'; import { GlobalStyles } from '@mui/material' - +import config from '../../paybutton-config.json' import successSound from '../../assets/success.mp3.json'; @@ -121,7 +121,7 @@ export const WidgetContainer: React.FunctionComponent = disabled, editable, wsBaseUrl, - apiBaseUrl, + apiBaseUrl = config.apiBaseUrl, successText, hoverText, disableAltpayment,