-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding boltcard for the POS behind a beta flag
- Loading branch information
Nicolas Burtey
committed
Feb 17, 2024
1 parent
0c62724
commit f06b68a
Showing
7 changed files
with
312 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
import React, { useState, useEffect } from "react" | ||
|
||
import { getParams } from "js-lnurl" | ||
import Image from "next/image" | ||
import styles from "./parse-payment.module.css" | ||
|
||
type Props = { | ||
paymentRequest?: string | undefined | ||
} | ||
|
||
function NFCComponent({ paymentRequest }: Props) { | ||
const [hasNFCPermission, setHasNFCPermission] = useState(false) | ||
const [nfcMessage, setNfcMessage] = useState("") | ||
|
||
const decodeNDEFRecord = (record) => { | ||
if (!record || !record.data) { | ||
console.log("No data found") | ||
return "" | ||
} | ||
|
||
// Ensure that the record's data is an instance of ArrayBuffer | ||
if (record.data instanceof ArrayBuffer) { | ||
console.log("ArrayBuffer") | ||
|
||
const decoder = new TextDecoder(record.encoding || "utf-8") | ||
return decoder.decode(record.data) | ||
} else { | ||
console.log("not ArrayBuffer") | ||
// If it's not an ArrayBuffer, it might be a DataView or another typed array. | ||
// In that case, we can create a new Uint8Array from the buffer of the DataView. | ||
const decoder = new TextDecoder(record.encoding || "utf-8") | ||
return decoder.decode(new Uint8Array(record.data.buffer)) | ||
} | ||
} | ||
|
||
const activateNfcScan = async () => { | ||
await handleNFCScan() | ||
alert( | ||
"Boltcard is now active. There will be no need to active it again. Please tap your card to redeem the payment", | ||
) | ||
} | ||
|
||
const handleNFCScan = async () => { | ||
if (!("NDEFReader" in window)) { | ||
console.error("NFC is not supported") | ||
return | ||
} | ||
|
||
console.log("NFC is supported, start reading") | ||
|
||
const ndef = new NDEFReader() | ||
|
||
try { | ||
await ndef.scan() | ||
|
||
console.log("NFC scan started successfully.") | ||
|
||
ndef.onreading = (event) => { | ||
console.log("NFC tag read.") | ||
console.log(event.message) | ||
|
||
const record = event.message.records[0] | ||
const text = decodeNDEFRecord(record) | ||
|
||
setNfcMessage(text) | ||
} | ||
|
||
ndef.onreadingerror = () => { | ||
console.error("Cannot read data from the NFC tag. Try another one?") | ||
} | ||
} catch (error) { | ||
console.error(`Error! Scan failed to start: ${error}.`) | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
;(async () => { | ||
if (!("permissions" in navigator)) { | ||
console.error("Permissions API not supported") | ||
return | ||
} | ||
|
||
let result: PermissionStatus | ||
try { | ||
result = await navigator.permissions.query({ name: "nfc" }) | ||
} catch (err) { | ||
console.error("Error querying NFC permission", err) | ||
return | ||
} | ||
|
||
console.log("result permission query", result) | ||
|
||
if (result.state === "granted") { | ||
setHasNFCPermission(true) | ||
} else { | ||
setHasNFCPermission(false) | ||
} | ||
|
||
result.onchange = () => { | ||
if (result.state === "granted") { | ||
setHasNFCPermission(true) | ||
} else { | ||
setHasNFCPermission(false) | ||
} | ||
} | ||
})() | ||
}, [setHasNFCPermission]) | ||
|
||
React.useEffect(() => { | ||
console.log("hasNFCPermission", hasNFCPermission) | ||
|
||
if (hasNFCPermission) { | ||
handleNFCScan() | ||
} | ||
}, [hasNFCPermission]) | ||
|
||
React.useEffect(() => { | ||
;(async () => { | ||
if (!nfcMessage) { | ||
return | ||
} | ||
|
||
if (!nfcMessage.toLowerCase().includes("lnurl")) { | ||
alert("Not a compatible boltcard") | ||
return | ||
} | ||
|
||
if (!paymentRequest) { | ||
alert("add an amount and create an invoice before scanning the card") | ||
return | ||
} | ||
|
||
const sound = new Audio("/payment-sound.mp3") | ||
sound | ||
.play() | ||
.then(() => { | ||
console.log("Playback started successfully") | ||
}) | ||
.catch((error) => { | ||
console.error("Playback failed", error) | ||
}) | ||
|
||
const lnurlParams = await getParams(nfcMessage) | ||
|
||
if (!("tag" in lnurlParams && lnurlParams.tag === "withdrawRequest")) { | ||
console.error("not a lnurl withdraw tag") | ||
return | ||
} | ||
|
||
const { callback, k1 } = lnurlParams | ||
|
||
const urlObject = new URL(callback) | ||
const searchParams = urlObject.searchParams | ||
searchParams.set("k1", k1) | ||
searchParams.set("pr", paymentRequest) | ||
|
||
const url = urlObject.toString() | ||
|
||
const result = await fetch(url) | ||
if (result.ok) { | ||
const lnurlResponse = await result.json() | ||
if (lnurlResponse?.status?.toLowerCase() !== "ok") { | ||
console.error(lnurlResponse, "error with redeeming") | ||
} | ||
|
||
console.log("payment succesful") | ||
} else { | ||
let errorMessage = "" | ||
try { | ||
const decoded = await result.json() | ||
if (decoded.reason) { | ||
errorMessage += decoded.reason | ||
} | ||
if (decoded.message) { | ||
errorMessage += decoded.message | ||
} | ||
} finally { | ||
let message = `Error processing payment.\n\nHTTP error code: ${result.status}` | ||
if (errorMessage) { | ||
message += `\n\nError message: ${errorMessage}` | ||
} | ||
alert(message) | ||
} | ||
} | ||
})() | ||
}, [nfcMessage, paymentRequest]) | ||
|
||
return ( | ||
<div> | ||
{!hasNFCPermission && ( | ||
<div className="d-flex justify-content-center" style={{ marginTop: "20px" }}> | ||
<button | ||
data-testid="pay-btn" | ||
className={styles.pay_new_btn} | ||
onClick={activateNfcScan} | ||
> | ||
<Image | ||
src={"/icons/lightning-icon.svg"} | ||
alt="lightning icon" | ||
width="20" | ||
height="20" | ||
/> | ||
Activate boltcard | ||
</button> | ||
</div> | ||
)} | ||
{/* {hasNFCPermission && <div>Boltcard scanning is active</div>} | ||
{nfcMessage && <div>LNURL: {nfcMessage}</div>} */} | ||
</div> | ||
) | ||
} | ||
|
||
export default NFCComponent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Oops, something went wrong.