ordering working

This commit is contained in:
Tiago Vasconcelos 2023-03-05 20:32:35 +00:00
parent 7ae14aa32f
commit ec1428cf6a
2 changed files with 89 additions and 51 deletions

View file

@ -24,9 +24,7 @@ async function customerStall(path) {
hasNip07: false, hasNip07: false,
checkoutDialog: { checkoutDialog: {
show: false, show: false,
data: { data: {}
pubkey: null
}
}, },
qrCodeDialog: { qrCodeDialog: {
data: { data: {
@ -130,56 +128,96 @@ async function customerStall(path) {
this.checkoutDialog.data.pubkey = pk this.checkoutDialog.data.pubkey = pk
this.checkoutDialog.data.privkey = sk this.checkoutDialog.data.privkey = sk
}, },
placeOrder() { async placeOrder() {
LNbits.utils // LNbits.utils
.confirmDialog( // .confirmDialog(
`Send the order to the merchant? You should receive a message with the payment details.` // `Send the order to the merchant? You should receive a message with the payment details.`
) // )
.onOk(async () => { // .onOk(async () => {
let orderData = this.checkoutDialog.data let orderData = this.checkoutDialog.data
let content = { let orderObj = {
name: orderData?.username, name: orderData?.username,
description: null, description: null,
address: orderData.address, address: orderData.address,
message: null, message: null,
contact: { contact: {
nostr: orderData.pubkey, nostr: orderData.pubkey,
phone: null, phone: null,
email: orderData?.email email: orderData?.email
}, },
items: Array.from(this.cart.products, p => { items: Array.from(this.cart.products, p => {
return {product_id: p[0], quantity: p[1].quantity} return {product_id: p[0], quantity: p[1].quantity}
})
}
let event = {
kind: 4,
created_at: Math.floor(Date.now() / 1000),
tags: [],
content: await window.nostr.nip04.encrypt(
orderData.pubkey,
content
),
pubkey: orderData.pubkey
}
event.id = NostrTools.getEventHash(event)
if (orderData.privkey) {
event.sig = NostrTools.signEvent(event, orderData.privkey)
} else if (this.hasNip07) {
await window.nostr.signEvent(event)
}
await this.sendOrder(event)
}) })
}
let event = {
...(await NostrTools.getBlankEvent()),
kind: 4,
created_at: Math.floor(Date.now() / 1000),
tags: [['p', this.stall.pubkey]],
pubkey: orderData.pubkey
}
if (orderData.privkey) {
event.content = await NostrTools.nip04.encrypt(
orderData.privkey,
this.stall.pubkey,
JSON.stringify(orderObj)
)
} else {
console.log('use extension')
event.content = await window.nostr.nip04.encrypt(
orderData.pubkey,
JSON.stringify(orderObj)
)
let userRelays = Object.keys(
(await window.nostr?.getRelays?.()) || []
)
if (userRelays.length != 0) {
userRelays.map(r => this.relays.add(r))
}
}
event.id = NostrTools.getEventHash(event)
if (orderData.privkey) {
event.sig = await NostrTools.signEvent(event, orderData.privkey)
} else if (this.hasNip07) {
event = await window.nostr.signEvent(event)
}
console.log(event, orderData)
await this.sendOrder(event)
// })
}, },
async sendOrder(order) { async sendOrder(order) {
const pool = new NostrTools.SimplePool() for (const url of Array.from(this.relays)) {
let relays = Array.from(this.relays) let relay = NostrTools.relayInit(url)
let pubs = await pool.publish(relays, order) relay.on('connect', () => {
pubs.on('ok', relay => { console.log(`connected to ${relay.url}`)
console.log(`${relay.url} has accepted our event`) })
}) relay.on('error', () => {
pubs.on('failed', reason => { console.log(`failed to connect to ${relay.url}`)
console.log(`failed to publish to ${reason}`) })
})
await relay.connect()
let pub = relay.publish(order)
pub.on('ok', () => {
console.log(`${relay.url} has accepted our event`)
})
pub.on('failed', reason => {
console.log(`failed to publish to ${relay.url}: ${reason}`)
})
}
this.checkoutDialog = {show: false, data: {}}
// const pool = new NostrTools.SimplePool()
// let relays = Array.from(this.relays)
// try {
// let pubs = await pool.publish(relays, order)
// pubs.on('ok', relay => {
// console.log(`${relay.url} has accepted our event`)
// })
// pubs.on('failed', (reason, err) => {
// console.log(`failed to publish to ${reason}: ${err}`)
// })
// } catch (err) {
// console.error(err)
// }
} }
}, },
created() { created() {

View file

@ -5,7 +5,6 @@ const market = async () => {
const defaultRelays = [ const defaultRelays = [
'wss://relay.damus.io', 'wss://relay.damus.io',
'wss://relay.snort.social', 'wss://relay.snort.social',
'wss://nostr.wine',
'wss://nostr-pub.wellorder.net', 'wss://nostr-pub.wellorder.net',
'wss://nostr.zebedee.cloud' 'wss://nostr.zebedee.cloud'
] ]
@ -175,6 +174,7 @@ const market = async () => {
return obj return obj
}) })
pool.close(relays) pool.close(relays)
return
}, },
async getRates() { async getRates() {
let noFiat = this.stalls.map(s => s.currency).every(c => c == 'sat') let noFiat = this.stalls.map(s => s.currency).every(c => c == 'sat')