[javascript] api

Viewer

  1. import type { NextApiRequest, NextApiResponse } from 'next'
  2. import supabase from '../../../../lib/supabase'
  3. import fetch from 'node-fetch';
  4.  
  5. interface PaymentData {
  6.   sn?: string,
  7.   transactionID?: string,
  8.   status?: string,
  9.   detail?: string,
  10. }
  11.  
  12. interface EmailApiResponse {
  13.   status: string;
  14.   detail: string;
  15. }
  16.  
  17. export default async function handler(
  18.   req: NextApiRequest,
  19.   res: NextApiResponse
  20. ) {
  21.   try {
  22.     const { code, status, data } = req.body;
  23.     if (!code) {
  24.       throw new Error('Missing Code');
  25.     }
  26.     if (!status) {
  27.       throw new Error('Missing status');
  28.     }
  29.     if (!data) {
  30.       throw new Error('Missing the data');
  31.     }
  32.  
  33.     if (status === 'Success') {
  34.       const { sn, transactionID, status, detail } = data as PaymentData;
  35.       const { count } = await supabase
  36.         .from('transactions')
  37.         .update({ status: status, detail: detail, serialnum: sn, updatedAt: new Date()})
  38.         .eq('id', transactionID);
  39.  
  40.       if (count === 0) {
  41.         throw new Error(`Transaction with ID ${transactionID} not found`);
  42.       }
  43.  
  44.       const { data: paymentData, error } = await supabase
  45.         .from('transactions')
  46.         .select('formatnum, detail, name, email')
  47.         .eq('id', transactionID);
  48.  
  49.       if (error) {
  50.         throw new Error('Error getting payment data');
  51.       }
  52.  
  53.       if (!paymentData || paymentData.length === 0) {
  54.         throw new Error('Payment data not found');
  55.       }
  56.  
  57.       const { name, formatnum, email } = paymentData[0];
  58.  
  59.       const emailResponse = await fetch(process.env.LAMUN_PUBLIC_API_URL as string, {
  60.         method: 'POST',
  61.         headers: {
  62.           'Content-Type': 'application/json',
  63.           'username': process.env.NEXT_PUBLIC_API_USERNAME as string,
  64.           'apikey': process.env.NEXT_PUBLIC_API_APIKEY as string,
  65.         },
  66.         body: JSON.stringify({
  67.           to: email,
  68.           bcc: "[email protected]",
  69.           subject: 'TERIMAKASIH UNTUK PEMBELIAN ANDA!',
  70.           content: `
  71.             <p><b>Selamat, Pembayaran telah berhasil di proses!<b></p>
  72.             <p>Website: <a href="https://pelayananhaji.thawaf.id/">pelayananhaji.thawaf.id</a></p>
  73.             <p>Untuk: ${name}, dari nomor ${formatnum}</p>
  74.             <p>Paket : ${detail}</p>
  75.             <p>Serial Number : ${sn}</p>
  76.           `,
  77.         })
  78.       });
  79.  
  80.       const emailResult = await emailResponse.json() as EmailApiResponse;
  81.  
  82.       if (!emailResponse.ok) {
  83.         throw new Error('Error sending email: ' + emailResult.detail);
  84.       }
  85.  
  86.       console.log('Email sent:', emailResult);
  87.     }
  88.  
  89.     res.status(200).json({ code, status, data });
  90.     
  91.   } catch (error) {
  92.     console.error(error);
  93.     res.status(500).json({ message: 'Internal Server Error' });
  94.   }
  95. }
  96.  

Editor

You can edit this paste and save as new:


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