From 63443ecb2e16851f9c7f00114dc55e98a59e2f2e Mon Sep 17 00:00:00 2001 From: siddharth Date: Tue, 4 Jun 2024 04:27:28 +0530 Subject: [PATCH 1/2] feat(voucher): Adding ability to print from Bitcoinze POS machine. --- apps/voucher/app/manifest.ts | 24 +++++++++++++++++ .../withdraw/[voucher-secret]/lnurl/lnurl.tsx | 19 +++++++++++++ .../hooks/get-installed-related-apps.d.ts | 7 +++++ .../hooks/use-check-installed-apps.tsx | 27 +++++++++++++++++++ apps/voucher/lib/print-companion.ts | 13 +++++++++ 5 files changed, 90 insertions(+) create mode 100644 apps/voucher/app/manifest.ts create mode 100644 apps/voucher/hooks/get-installed-related-apps.d.ts create mode 100644 apps/voucher/hooks/use-check-installed-apps.tsx create mode 100644 apps/voucher/lib/print-companion.ts diff --git a/apps/voucher/app/manifest.ts b/apps/voucher/app/manifest.ts new file mode 100644 index 0000000000..e514ecbd05 --- /dev/null +++ b/apps/voucher/app/manifest.ts @@ -0,0 +1,24 @@ +import { MetadataRoute } from "next" + +export default function manifest(): MetadataRoute.Manifest { + return { + name: "Blink Voucher", + short_name: "Voucher", + start_url: "/", + display: "standalone", + related_applications: [ + { + platform: "play", + id: "com.blink.pos.companion", + url: "https://github.com/GaloyMoney/pos-print-companion", + }, + ], + icons: [ + { + src: "/blink-logo.svg", + sizes: "any", + type: "image/svg", + }, + ], + } +} diff --git a/apps/voucher/app/withdraw/[voucher-secret]/lnurl/lnurl.tsx b/apps/voucher/app/withdraw/[voucher-secret]/lnurl/lnurl.tsx index 8bb5a92769..67f73358d6 100644 --- a/apps/voucher/app/withdraw/[voucher-secret]/lnurl/lnurl.tsx +++ b/apps/voucher/app/withdraw/[voucher-secret]/lnurl/lnurl.tsx @@ -14,6 +14,9 @@ import { } from "@/lib/graphql/generated" import Button from "@/components/button" import FundsPaid from "@/components/funds-paid" +import useCheckInstalledApps from "@/hooks/use-check-installed-apps" +import { startPrintCompanion } from "@/lib/print-companion" +import { formatCurrency } from "@/lib/utils" gql` query GetWithdrawLink($voucherSecret: String) { @@ -52,6 +55,9 @@ type VoucherDetailsProps = { export default function LnurlPage({ voucherSecret, voucherUrl }: Props) { const [revealLNURL, setRevealLNURL] = useState(false) + const posCompanionInstalled = useCheckInstalledApps({ + appId: "com.blink.pos.companion", + }) const { loading, error, data } = useGetWithdrawLinkQuery({ variables: { voucherSecret }, @@ -87,6 +93,19 @@ export default function LnurlPage({ voucherSecret, voucherUrl }: Props) { } const handlePrint = () => { + if (posCompanionInstalled) { + startPrintCompanion({ + lnurl, + voucherPrice: withdrawLink.displayVoucherPrice, + voucherAmount: formatCurrency({ + amount: withdrawLink.voucherAmountInCents / 100, + currency: "USD", + }), + voucherSecret: withdrawLink.voucherSecret, + }) + + return + } setRevealLNURL(true) setTimeout(() => { window.print() diff --git a/apps/voucher/hooks/get-installed-related-apps.d.ts b/apps/voucher/hooks/get-installed-related-apps.d.ts new file mode 100644 index 0000000000..a39ef93930 --- /dev/null +++ b/apps/voucher/hooks/get-installed-related-apps.d.ts @@ -0,0 +1,7 @@ +interface InstalledAppInfo { + id: string +} + +interface Navigator { + getInstalledRelatedApps: () => Promise +} diff --git a/apps/voucher/hooks/use-check-installed-apps.tsx b/apps/voucher/hooks/use-check-installed-apps.tsx new file mode 100644 index 0000000000..5de2580c81 --- /dev/null +++ b/apps/voucher/hooks/use-check-installed-apps.tsx @@ -0,0 +1,27 @@ +import { useState, useEffect } from "react" + +function useCheckInstalledApps({ appId }: { appId: string }): boolean { + const [isAppInstalled, setIsAppInstalled] = useState(false) + + useEffect(() => { + const checkInstalledApps = async () => { + try { + if ("getInstalledRelatedApps" in navigator) { + const apps = await navigator.getInstalledRelatedApps() + const foundApp = apps.some((app) => app.id === appId) + setIsAppInstalled(foundApp) + } else { + console.error("getInstalledRelatedApps is not supported in this browser.") + } + } catch (error) { + console.error("Error checking installed related apps:", error) + } + } + + checkInstalledApps() + }, [appId]) + + return isAppInstalled +} + +export default useCheckInstalledApps diff --git a/apps/voucher/lib/print-companion.ts b/apps/voucher/lib/print-companion.ts new file mode 100644 index 0000000000..05f2fbaca1 --- /dev/null +++ b/apps/voucher/lib/print-companion.ts @@ -0,0 +1,13 @@ +export function startPrintCompanion(params: { + lnurl: string + voucherPrice: string + voucherAmount: string + voucherSecret: string +}): void { + const { lnurl, voucherPrice, voucherAmount, voucherSecret } = params + window.location.href = `blink-pos-companion://print?app=voucher&lnurl=${encodeURIComponent( + lnurl, + )}&voucherPrice=${encodeURIComponent(voucherPrice)}&voucherAmount=${encodeURIComponent( + voucherAmount, + )}&voucherSecret=${encodeURIComponent(voucherSecret)}` +} From 104b23d29c6291c645e65f51295133cbbe280514 Mon Sep 17 00:00:00 2001 From: siddharth Date: Tue, 4 Jun 2024 23:40:24 +0530 Subject: [PATCH 2/2] chore: added commissionPercentage --- .../voucher/app/withdraw/[voucher-secret]/lnurl/lnurl.tsx | 1 + apps/voucher/lib/print-companion.ts | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/apps/voucher/app/withdraw/[voucher-secret]/lnurl/lnurl.tsx b/apps/voucher/app/withdraw/[voucher-secret]/lnurl/lnurl.tsx index 67f73358d6..8bb3dad145 100644 --- a/apps/voucher/app/withdraw/[voucher-secret]/lnurl/lnurl.tsx +++ b/apps/voucher/app/withdraw/[voucher-secret]/lnurl/lnurl.tsx @@ -102,6 +102,7 @@ export default function LnurlPage({ voucherSecret, voucherUrl }: Props) { currency: "USD", }), voucherSecret: withdrawLink.voucherSecret, + commissionPercentage: withdrawLink.commissionPercentage, }) return diff --git a/apps/voucher/lib/print-companion.ts b/apps/voucher/lib/print-companion.ts index 05f2fbaca1..5a0fc08877 100644 --- a/apps/voucher/lib/print-companion.ts +++ b/apps/voucher/lib/print-companion.ts @@ -3,11 +3,15 @@ export function startPrintCompanion(params: { voucherPrice: string voucherAmount: string voucherSecret: string + commissionPercentage: number }): void { - const { lnurl, voucherPrice, voucherAmount, voucherSecret } = params + const { lnurl, voucherPrice, voucherAmount, voucherSecret, commissionPercentage } = + params window.location.href = `blink-pos-companion://print?app=voucher&lnurl=${encodeURIComponent( lnurl, )}&voucherPrice=${encodeURIComponent(voucherPrice)}&voucherAmount=${encodeURIComponent( voucherAmount, - )}&voucherSecret=${encodeURIComponent(voucherSecret)}` + )}&voucherSecret=${encodeURIComponent( + voucherSecret, + )}&commissionPercentage=${encodeURIComponent(commissionPercentage)}` }