route.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import prisma from '@/lib/prisma'
  2. import LemonSqueezy from '@lemonsqueezy/lemonsqueezy.js'
  3. const ls = new LemonSqueezy(process.env.LEMON_SQUEEZY_API_KEY as string)
  4. async function processEvent(event) {
  5. let processingError = ''
  6. const customData = event.body['meta']['custom_data'] || null
  7. if (!customData || !customData['user_id']) {
  8. processingError = 'No user ID, can\'t process'
  9. } else {
  10. const obj = event.body['data']
  11. if (event.eventName.startsWith('subscription_payment_')) {
  12. // Save subscription invoices; obj is a "Subscription invoice"
  13. /* Not implemented */
  14. } else if (event.eventName.startsWith('subscription_')) {
  15. // Save subscriptions; obj is a "Subscription"
  16. const data = obj['attributes']
  17. // We assume the Plan table is up to date
  18. const plan = await prisma.plan.findUnique({
  19. where: {
  20. variantId: data['variant_id']
  21. },
  22. })
  23. if (!plan) {
  24. processingError = 'Plan not found in DB. Could not process webhook event.'
  25. } else {
  26. // Update the subscription
  27. const lemonSqueezyId = parseInt(obj['id'])
  28. // Get subscription's Price object
  29. // We save the Price value to the subscription so we can display it in the UI
  30. let priceData = await ls.getPrice({ id: data['first_subscription_item']['price_id'] })
  31. const updateData = {
  32. orderId: data['order_id'],
  33. name: data['user_name'],
  34. email: data['user_email'],
  35. status: data['status'],
  36. renewsAt: data['renews_at'],
  37. endsAt: data['ends_at'],
  38. trialEndsAt: data['trial_ends_at'],
  39. planId: plan['id'],
  40. userId: customData['user_id'],
  41. price: priceData['data']['attributes']['unit_price'],
  42. subscriptionItemId: data['first_subscription_item']['id'],
  43. // Save this for usage-based billing reporting; no need to if you use quantity-based billing
  44. isUsageBased: data['first_subscription_item']['is_usage_based'],
  45. }
  46. const createData = { ...updateData, lemonSqueezyId}
  47. createData.price = plan.price
  48. try {
  49. // Create/update subscription
  50. await prisma.subscription.upsert({
  51. where: {
  52. lemonSqueezyId: lemonSqueezyId
  53. },
  54. update: updateData,
  55. create: createData,
  56. })
  57. } catch (error) {
  58. processingError = error
  59. console.log(error)
  60. }
  61. }
  62. } else if (event.eventName.startsWith('order_')) {
  63. // Save orders; obj is a "Order"
  64. /* Not implemented */
  65. } else if (event.eventName.startsWith('license_')) {
  66. // Save license keys; obj is a "License key"
  67. /* Not implemented */
  68. }
  69. try {
  70. // Mark event as processed
  71. await prisma.webhookEvent.update({
  72. where: {
  73. id: event.id
  74. },
  75. data: {
  76. processed: true,
  77. processingError
  78. }
  79. })
  80. } catch (error) {
  81. console.log(error)
  82. }
  83. }
  84. }
  85. export async function POST(request: Request) {
  86. // Make sure request is from Lemon Squeezy
  87. const crypto = require('crypto')
  88. const rawBody = await request.text()
  89. const secret = process.env.LEMONSQUEEZY_WEBHOOK_SECRET
  90. const hmac = crypto.createHmac('sha256', secret)
  91. const digest = Buffer.from(hmac.update(rawBody).digest('hex'), 'utf8')
  92. const signature = Buffer.from(request.headers.get('X-Signature') || '', 'utf8')
  93. if (!crypto.timingSafeEqual(digest, signature)) {
  94. throw new Error('Invalid signature.')
  95. }
  96. // Now save the event
  97. const data = JSON.parse(rawBody)
  98. const event = await prisma.webhookEvent.create({
  99. data: {
  100. eventName: data['meta']['event_name'],
  101. body: data
  102. },
  103. })
  104. // Process the event
  105. // This could be done out of the main thread
  106. processEvent(event)
  107. return new Response('Done');
  108. }