[javascript] api

Viewer

  1. import type { NextApiRequest, NextApiResponse } from 'next'
  2. import supabase from '../../../../lib/supabase'
  3. import Mailgun from 'mailgun-js'
  4.  
  5. interface PaymentData {
  6.   sn?: string,
  7.   transactionID?: string,
  8.   status?: string,
  9.   detail?: string,
  10.   // add other fields as needed
  11. }
  12.  
  13. export default async function handler(
  14.   req: NextApiRequest,
  15.   res: NextApiResponse
  16. ) {
  17.   try {
  18.     const { code, status, data } = req.body 
  19.     if (!code) {
  20.       throw new Error('Missing Code')
  21.     }
  22.     if (!status) {
  23.       throw new Error('Missing status')
  24.     }
  25.  
  26.     if (!data) {
  27.       throw new Error('Missing the data')
  28.     }
  29.  
  30.     if (status === 'Success') {
  31.       const { sn, transactionID, status, detail } = data as PaymentData;
  32.       const { count } = await supabase
  33.         .from('transactions')
  34.         .update({ status: status, detail: detail, serialnum: sn, updatedAt: new Date()})
  35.         .eq('id', transactionID)
  36.       if (count === 0) {
  37.         throw new Error(`Serial number with sn ${sn} and transaction ID ${transactionID} not found`);
  38.       }
  39.  
  40.       const { data: paymentData, error } = await supabase
  41.       .from('transactions')
  42.       .select('formatnum, detail, name, email')
  43.       .eq('id', transactionID);
  44.       
  45.       if (error) {
  46.         throw new Error('Error getting payment data');
  47.       }
  48.  
  49.       if (!paymentData || paymentData.length === 0) {
  50.         throw new Error('Payment data not found');
  51.       }
  52.  
  53.       const { name, formatnum, email } = paymentData[0];
  54.  
  55.       // Initialize Mailgun client
  56.       const mg = Mailgun({
  57.         apiKey: 'acd9a67295ba96a738a28f3ce060ffe0-054ba6b6-3a70cd77',
  58.         domain: 'test-ir.thawaf.id'
  59.       })
  60.  
  61.       const mailgunData = {
  62.         to: email,
  63.         subject: 'TERIMAKASIH UNTUK PEMBELIAN ANDA!',
  64.         html: `
  65.           <p><b>Selamat, Pembayaran telah berhasil di proses!<b></p>
  66.           <p>Website: <a href="https://petugashaji.thawaf.id/">petugashaji.thawaf.id</a></p>
  67.           <p>Untuk: ${name}, dari nomor ${formatnum}</p>
  68.           <p>Paket : ${detail}</p>
  69.           <p>Serial Number : ${sn}</p>
  70.         `
  71.       }
  72.  
  73.       mg.messages().send(mailgunData, (error, body) => {
  74.         if (error) {
  75.           console.log(error)
  76.           throw new Error('Error sending email');
  77.         } else {
  78.           console.log(body);
  79.         }
  80.       });
  81.     }
  82.  
  83.     res.status(200).json({ code, status, data })
  84.     
  85.   } catch (error) {
  86.     console.error(error)
  87.     res.status(500).json({ message: 'error' })
  88.   }
  89. }
  90.  

Editor

You can edit this paste and save as new:


File Description
  • api
  • Paste Code
  • 26 Apr-2024
  • 2.49 Kb
You can Share it: