[javascript] api
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- import type { NextApiRequest, NextApiResponse } from 'next'
- import supabase from '../../../../lib/supabase'
- import fetch from 'node-fetch';
- interface PaymentData {
- sn?: string,
- transactionID?: string,
- status?: string,
- detail?: string,
- }
- interface EmailApiResponse {
- status: string;
- detail: string;
- }
- export default async function handler(
- req: NextApiRequest,
- res: NextApiResponse
- ) {
- try {
- const { code, status, data } = req.body;
- if (!code) {
- throw new Error('Missing Code');
- }
- if (!status) {
- throw new Error('Missing status');
- }
- if (!data) {
- throw new Error('Missing the data');
- }
- if (status === 'Success') {
- const { sn, transactionID, status, detail } = data as PaymentData;
- const { count } = await supabase
- .from('transactions')
- .update({ status: status, detail: detail, serialnum: sn, updatedAt: new Date()})
- .eq('id', transactionID);
- if (count === 0) {
- throw new Error(`Transaction with ID ${transactionID} not found`);
- }
- const { data: paymentData, error } = await supabase
- .from('transactions')
- .select('formatnum, detail, name, email')
- .eq('id', transactionID);
- if (error) {
- throw new Error('Error getting payment data');
- }
- if (!paymentData || paymentData.length === 0) {
- throw new Error('Payment data not found');
- }
- const { name, formatnum, email } = paymentData[0];
- const emailResponse = await fetch(process.env.LAMUN_PUBLIC_API_URL as string, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- 'username': process.env.NEXT_PUBLIC_API_USERNAME as string,
- 'apikey': process.env.NEXT_PUBLIC_API_APIKEY as string,
- },
- body: JSON.stringify({
- to: email,
- subject: 'TERIMAKASIH UNTUK PEMBELIAN ANDA!',
- content: `
- <p><b>Selamat, Pembayaran telah berhasil di proses!<b></p>
- <p>Website: <a href="https://pelayananhaji.thawaf.id/">pelayananhaji.thawaf.id</a></p>
- <p>Untuk: ${name}, dari nomor ${formatnum}</p>
- <p>Paket : ${detail}</p>
- <p>Serial Number : ${sn}</p>
- `,
- })
- });
- const emailResult = await emailResponse.json() as EmailApiResponse;
- if (!emailResponse.ok) {
- throw new Error('Error sending email: ' + emailResult.detail);
- }
- console.log('Email sent:', emailResult);
- }
- res.status(200).json({ code, status, data });
- } catch (error) {
- console.error(error);
- res.status(500).json({ message: 'Internal Server Error' });
- }
- }
Editor
You can edit this paste and save as new: