[javascript] email

Viewer

  1. import type { NextApiRequest, NextApiResponse } from 'next'
  2. import supabase from '../../../../lib/supabase'
  3. import axios from 'axios'
  4.  
  5. export default async function handler(
  6.   req: NextApiRequest,
  7.   res: NextApiResponse
  8. ) {
  9.   try {
  10.     var body = req.body
  11.     if (Object.prototype.toString.call(body) === '[object String]')
  12.       body = JSON.parse(body)
  13.     await supabase.from('logs').insert({ data: body, sender: "Sama alThawaf (Webhook)" })
  14.     var transactionsSnapshot = await supabase.from('transactions').select("*").eq("id", body.transactionId)
  15.     if (transactionsSnapshot.status >= 300 || (transactionsSnapshot.data && transactionsSnapshot.data!.length < 1))
  16.       throw 'Data transaksi tidak ditemukan'
  17.     var transaction = transactionsSnapshot.data![0]
  18.     if (body.status === transaction.status) throw `Data transaksi sudah ${body.status}`
  19.     transactionsSnapshot = await supabase.from('transactions').update({ status: body.status, serialnum: body.sn || "", updatedAt: new Date() }).eq("id", body.transactionId).select("*")
  20.     transaction = transactionsSnapshot.data![0]
  21.     if (body.status != "Success") throw 'Transaksi gagal atau sedang diproses'
  22.  
  23.     // Send WA 
  24.     // try {
  25.     //   if (!(transaction.formatnum)) throw 'Email tidak diset, batalkan pengiriman email'
  26.     //   const authorization = "ByJ2mp6pzAbmJ9d-wrX0elBsEp_qASEFD-DGIbZC1AE"
  27.     //   const url = 'https://service-chat.qontak.com/api/open/v1/broadcasts/whatsapp/direct'
  28.     //   const config = {
  29.     //     headers: {
  30.     //       "Authorization": authorization,
  31.     //       // "api-version": "2022-07-31",
  32.     //       "Content-Type": "application/json"
  33.     //     }
  34.     //   }
  35.     //   var bodyd = {
  36.     //     "to_number": transaction.formatnum,
  37.     //     "to_name": transaction.name,
  38.     //     "message_template_id": "1b1d72cb-a50b-4e19-93cd-aff1547cdbc9",
  39.     //     "channel_integration_id": "a71dd2d1-1089-445c-8344-5c6bf173c602",
  40.     //     "language": {
  41.     //       "code": "id"
  42.     //     },
  43.     //     "parameters": {
  44.     //       "body": [
  45.     //         {
  46.     //           "key": "1",
  47.     //           "value": "full_name",
  48.     //           "value_text": transaction.name,
  49.     //         },
  50.     //         {
  51.     //           "key": "2",
  52.     //           "value": "cust_number",
  53.     //           "value_text": transaction.customerId + "\n -Serial Number : " + transaction.serialnum,
  54.     //         },
  55.     //         {
  56.     //           "key": "3",
  57.     //           "value": "package_type",
  58.     //           "value_text": transaction.detail
  59.     //         },
  60.     //         {
  61.     //           "key": "4",
  62.     //           "value": "200",
  63.     //           "value_text": "Pembayaran Berhasil"
  64.     //         },
  65.     //         {
  66.     //           "key": "5",
  67.     //           "value": "invoice_link",
  68.     //           "value_text": "https://pelayananhaji.thawaf.id/"
  69.     //         },
  70.     //       ]
  71.     //     }
  72.     //   }
  73.     //   // res.send({ url: url, body: bodyd, config: config })
  74.     //   const response = await axios.post(url, bodyd, config)
  75.     //   await supabase.from('logs').insert({ data: response.data, sender: "Qontak (Callback)" })
  76.     // } catch (e) {
  77.     //   await supabase.from('consoles').insert({ summary: `${e}`, severity: 2 })
  78.     // }
  79.  
  80.   // Send Email using Lamun
  81.   try {
  82.     if (!(transaction.email)) throw 'Email tidak diset, batalkan pengiriman email'
  83.     await axios.post('http://api.lamun.my.id/api/api/mail/sent', {
  84.         to: transaction.email,
  85.         subject: 'TERIMAKASIH UNTUK PEMBELIAN ANDA!',
  86.         html: `
  87.         <html>
  88.         <body style="background-color:#e2e1e0;font-family: Open Sans, sans-serif;font-size:100%;font-weight:400;line-height:1.4;color:#000;">
  89.           <table style="max-width:670px;margin:50px auto 10px;background-color:#fff;padding:50px;-webkit-border-radius:3px;-moz-border-radius:3px;border-radius:3px;-webkit-box-shadow:0 1px 3px rgba(0,0,0,.12),0 1px 2px rgba(0,0,0,.24);-moz-box-shadow:0 1px 3px rgba(0,0,0,.12),0 1px 2px rgba(0,0,0,.24);box-shadow:0 1px 3px rgba(0,0,0,.12),0 1px 2px rgba(0,0,0,.24); border-top: solid 10px green;">
  90.             <tbody>
  91.               <tr>
  92.                 <td style="height:35px;"></td>
  93.               </tr>
  94.               <tr>
  95.                 <td colspan="2" style="border: solid 1px #ddd; padding:10px 20px;">
  96.                      <p><span style="font-family: Arial, Helvetica, sans-serif;">Assalamu'alaikum ${transaction.name}!<br /><br /><strong><span style="font-size: 20px;">Selamat Pembayaranmu Berhasil!</span></strong></span></p>
  97.                   <p style="font-size:14px;margin:0 0 6px 0;"><span style="font-weight:bold;display:inline-block;min-width:150px">Order status</span><b style="color:green;font-weight:normal;margin:0">${transaction.status}</b></p>
  98.                   <p style="font-size:14px;margin:0 0 6px 0;"><span style="font-weight:bold;display:inline-block;min-width:146px">Serial Number</span> ${transaction.serialnum}</p>
  99.                 </td>
  100.               </tr>
  101.               <tr>
  102.                 <td style="height:35px;"></td>
  103.               </tr>
  104.               <tr>
  105.                 <td style="width:50%;padding:20px;vertical-align:top">
  106.                   <p style="margin:0 0 10px 0;padding:0;font-size:14px;"><span style="display:block;font-weight:bold;font-size:13px">Name</span> ${transaction.name}</p>
  107.                   <p style="margin:0 0 10px 0;padding:0;font-size:14px;"><span style="display:block;font-weight:bold;font-size:13px;">Email</span> ${transaction.email}</p>
  108.                   <p style="margin:0 0 10px 0;padding:0;font-size:14px;"><span style="display:block;font-weight:bold;font-size:13px;">Phone</span> ${transaction.formatnum}</p>
  109.                 </td>
  110.                 <td style="width:50%;padding:20px;vertical-align:top">
  111.                   <p style="margin:0 0 10px 0;padding:0;font-size:14px;"><span style="display:block;font-weight:bold;font-size:13px;">Date</span> ${transaction.updatedAt}</p>
  112.                   <p style="margin:0 0 10px 0;padding:0;font-size:14px;"><span style="display:block;font-weight:bold;font-size:13px;">ID</span> TRANSACTION-${transaction.id}</p>
  113.                 </td>
  114.               </tr>
  115.               <tr>
  116.                 <td colspan="2" style="font-size:20px;padding:30px 15px 0 15px;">Paket Yang Dibeli</td>
  117.               </tr>
  118.               <tr>
  119.                 <td colspan="2" style="padding:15px;">
  120.                   <p style="font-size:14px;margin:0;padding:10px;border:solid 1px #ddd;font-weight:bold;">
  121.                     <span style="display:block;font-size:13px;font-weight:normal;">${transaction.productTitle}</span> Rp. ${transaction.productPrice} <b style="font-size:12px;font-weight:300;">${transaction.productDay}</b>
  122.           </table>
  123.           <p> </p>
  124.           <hr />
  125.           <p>E-mail ini dibuat otomatis, mohon tidak membalas. Jika butuh bantuan, silakan hubungi<a href="https://api.whatsapp.com/send/?phone=6281311100883&text&type=phone_number&app_absent=0"> <strong>Whatsapp</strong></a></p>
  126.           <p> </p>
  127.           <br>
  128.           <br>
  129.         </body>
  130.         </html>
  131.       `
  132.     }, {
  133.       headers: {
  134.         'Content-Type': 'application/json',
  135.         'username': 'noreply@payment-ir.thawaf.id', 
  136.         'apikey': 'cd7100dbac059e53da9f2525a933467b
  137.       }
  138.     });
  139.     await supabase.from('consoles').insert({ summary: `Email has been sent to ${transaction.email}`, severity: 1 })
  140.   } catch (e) {
  141.     await supabase.from('consoles').insert({ summary: `${e}`, severity: 1 })
  142.   }
  143. } catch (e) {
  144.   await supabase.from('consoles').insert({ summary: `${e}`, severity: 1 })
  145. }
  146.   res.status(200).send({ body: "Thanks", code: 200, status: "Success" })
  147. }
  148.  

Editor

You can edit this paste and save as new:


File Description
  • email
  • Paste Code
  • 02 May-2024
  • 7.66 Kb
You can Share it: