[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 Mailgun from 'mailgun-js'
- interface PaymentData {
- sn?: string,
- transactionID?: string,
- status?: string,
- detail?: string,
- // add other fields as needed
- }
- 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(`Serial number with sn ${sn} and transaction 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];
- // Initialize Mailgun client
- const mg = Mailgun({
- apiKey: 'acd9a67295ba96a738a28f3ce060ffe0-054ba6b6-3a70cd77',
- domain: 'test-ir.thawaf.id'
- })
- const mailgunData = {
- to: email,
- subject: 'TERIMAKASIH UNTUK PEMBELIAN ANDA!',
- html: `
- <p><b>Selamat, Pembayaran telah berhasil di proses!<b></p>
- <p>Website: <a href="https://petugashaji.thawaf.id/">petugashaji.thawaf.id</a></p>
- <p>Untuk: ${name}, dari nomor ${formatnum}</p>
- <p>Paket : ${detail}</p>
- <p>Serial Number : ${sn}</p>
- `
- }
- mg.messages().send(mailgunData, (error, body) => {
- if (error) {
- console.log(error)
- throw new Error('Error sending email');
- } else {
- console.log(body);
- }
- });
- }
- res.status(200).json({ code, status, data })
- } catch (error) {
- console.error(error)
- res.status(500).json({ message: 'error' })
- }
- }
Editor
You can edit this paste and save as new: