problem: can't get zap data

This commit is contained in:
gsovereignty
2024-07-08 11:54:09 +08:00
parent b63746a672
commit a20df4901f

View File

@@ -87,7 +87,7 @@
productIDs.set(product[1].split(':')[0], product);
}
}
return productIDs
return productIDs;
}
//todo: check that this zap is not already included in the payment JSON for the product
@@ -95,39 +95,57 @@
//todo: make the page flash or something and show each time someone buys the product.
//todo: split this out so that we can consume it for the payment page too (so that we know if there are really products left or they're all sold)
function getZapData(zap: NDKEvent) {
let desc = zap.getMatchingTags('description');
if (desc && desc.length == 1) {
let request = new NDKEvent($ndk, JSON.parse(desc[0][1]));
let productPrice = 0;
let zapAmount = 0;
let productID: string | undefined = undefined;
let buyerPubkey: string | undefined = undefined;
let zapRequest: NDKEvent | undefined = undefined;
console.log(request.author.pubkey);
let eTags = request.getMatchingTags('e');
let zappedProductIsLegit = false;
let productPrice = 0;
if (eTags && eTags.length > 0) {
for (let etag of eTags) {
if (etag.length > 1) {
if ($latestRocketEvent && getMapOfProducts($latestRocketEvent)) {
let products = getMapOfProducts($latestRocketEvent)
let thisProduct = products.get(etag[1])
if (thisProduct) {
zappedProductIsLegit = true
productPrice = parseInt(thisProduct[1].split(":")[1], 10)
let desc = zap.getMatchingTags('description');
if (desc && desc.length == 1 && $latestRocketEvent) {
zapRequest = new NDKEvent($ndk, JSON.parse(desc[0][1]));
let zapRequestETags = zapRequest.getMatchingTags('e');
if (zapRequestETags && zapRequestETags.length > 0) {
for (let productIDfromZapRequest of zapRequestETags) {
if (productIDfromZapRequest.length > 1) {
let productsInRocket = getMapOfProducts($latestRocketEvent);
if (productsInRocket) {
productID = productIDfromZapRequest[1];
if (productID.length == 64) {
let productDataFromRocket = productsInRocket.get(productID);
if (productDataFromRocket) {
productPrice = parseInt(productDataFromRocket[1].split(':')[1], 10);
}
} else {
}
}
}
}
}
console.log(zappedProductIsLegit)
if (!zappedProductIsLegit) {
console.log(request.rawEvent())
}
let amount = request.getMatchingTags('amount');
let amount = zapRequest.getMatchingTags('amount');
if (amount && amount.length == 1) {
if (amount[0].length == 2) {
let amountInt = parseInt(amount[0][1], 10);
console.log(amountInt >= productPrice);
zapAmount = parseInt(amount[0][1], 10);
}
}
buyerPubkey = zapRequest.author.pubkey;
}
let success = false;
if (zapRequest && productID && buyerPubkey && productPrice && zapAmount) {
if (zapAmount >= productPrice && productID.length == 64 && buyerPubkey.length == 64) {
success = true;
return {
productPrice: productPrice,
zapAmount: zapAmount,
productID: productID,
buyerPubkey: buyerPubkey,
zapReceipt: zap.id
};
}
}
if (!success) {
console.log('invalid product payment zap found:', zapRequest?.rawEvent());
}
}
@@ -152,7 +170,10 @@
{#if zaps && $zaps}
{#each $zaps as z}<p
on:click={() => {
getZapData(z);
let zapdata = getZapData(z);
if (zapdata) {
console.log(zapdata);
}
}}
>
{z.id}