Skip to content

Commit

Permalink
chore: add loops
Browse files Browse the repository at this point in the history
  • Loading branch information
Pagebakers committed Oct 28, 2023
1 parent d98d771 commit 7c382e4
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions apps/website/src/pages/api/lmsq.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { NextApiRequest, NextApiResponse } from 'next'

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
try {
if (
req.headers['x-signature'] !== process.env.LEMON_SQUEEZY_WEBHOOK_SECRET
) {
return res.status(401).json({ success: false, error: 'Unauthorized' })
}

const { meta, data } = req.body

if (meta.event_name === 'order_created') {
const response = await fetch(
`https://api.lemonsqueezy.com/v1/orders/${data.id}/customer`,
{
headers: {
Authorization: `Bearer ${process.env.LEMON_API_KEY}`,
Accept: 'application/json',
},
method: 'GET',
}
)

const customer = await response.json()
const attr = customer.data.attributes

const [firstName, ...lastName] = attr.name.split(' ')

const event = {
eventName: 'order_created',
email: attr.email,
firstName,
lastName: lastName.join(' '),
license: data.attributes.first_order_item.product_name,
}

fetch('https://app.loops.so/api/v1/events/send', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.LOOPS_API_KEY}`,
},
body: JSON.stringify(event),
})
}

res.status(200).json({ success: true })
} catch (err) {
console.log(err)

res.status(500).json({ success: false, error: 'Internal Server Error' })
}
}

export default handler

1 comment on commit 7c382e4

@vercel
Copy link

@vercel vercel bot commented on 7c382e4 Oct 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.