[javascript] code

Viewer

  1. import type { NextApiRequest, NextApiResponse } from 'next'
  2. import supabase from '../../../../lib/supabase'
  3. import axios from 'axios'
  4. const formData = require('form-data');
  5. // const Mailgun = require('mailgun.js');
  6. // const mailgun = new Mailgun(formData);
  7. // const mg = mailgun.client({ username: 'api', key: 'acd9a67295ba96a738a28f3ce060ffe0-054ba6b6-3a70cd77' });
  8.  
  9. export default async function handler(
  10.   req: NextApiRequest,
  11.   res: NextApiResponse
  12. ) {
  13.   try {
  14.     var body = req.body
  15.     if (Object.prototype.toString.call(body) === '[object String]')
  16.       body = JSON.parse(body)
  17.     await supabase.from('logs').insert({ data: body, sender: "Sama alThawaf (Webhook)" })
  18.     var transactionsSnapshot = await supabase.from('transactions').select("*").eq("id", body.transactionId)
  19.     if (transactionsSnapshot.status >= 300 || (transactionsSnapshot.data && transactionsSnapshot.data!.length < 1))
  20.       throw 'Data transaksi tidak ditemukan'
  21.     var transaction = transactionsSnapshot.data![0]
  22.     if (body.status === transaction.status) throw `Data transaksi sudah ${body.status}`
  23.     transactionsSnapshot = await supabase.from('transactions').update({ status: body.status, serialnum: body.sn || "", updatedAt: new Date() }).eq("id", body.transactionId).select("*")
  24.     transaction = transactionsSnapshot.data![0]
  25.     if (body.status != "Success") throw 'Transaksi gagal atau sedang diproses'
  26.  
  27.     // Send WA 
  28.     try {
  29.       if (!(transaction.formatnum)) throw 'Email tidak diset, batalkan pengiriman email'
  30.       const authorization = "ByJ2mp6pzAbmJ9d-wrX0elBsEp_qASEFD-DGIbZC1AE"
  31.       const url = 'https://service-chat.qontak.com/api/open/v1/broadcasts/whatsapp/direct'
  32.       const config = {
  33.         headers: {
  34.           "Authorization": authorization,
  35.           // "api-version": "2022-07-31",
  36.           "Content-Type": "application/json"
  37.         }
  38.       }
  39.       var bodyd = {
  40.         "to_number": transaction.formatnum,
  41.         "to_name": transaction.name,
  42.         "message_template_id": "1b1d72cb-a50b-4e19-93cd-aff1547cdbc9",
  43.         "channel_integration_id": "a71dd2d1-1089-445c-8344-5c6bf173c602",
  44.         "language": {
  45.           "code": "id"
  46.         },
  47.         "parameters": {
  48.           "body": [
  49.             {
  50.               "key": "1",
  51.               "value": "full_name",
  52.               "value_text": transaction.name,
  53.             },
  54.             {
  55.               "key": "2",
  56.               "value": "cust_number",
  57.               "value_text": transaction.customerId + "\n -Serial Number : " + transaction.serialnum,
  58.             },
  59.             {
  60.               "key": "3",
  61.               "value": "package_type",
  62.               "value_text": transaction.detail
  63.             },
  64.             {
  65.               "key": "4",
  66.               "value": "200",
  67.               "value_text": "Pembayaran Berhasil"
  68.             },
  69.             {
  70.               "key": "5",
  71.               "value": "invoice_link",
  72.               "value_text": "https://petugashaji.thawaf.id/"
  73.             },
  74.           ]
  75.         }
  76.       }
  77.       // res.send({ url: url, body: bodyd, config: config })
  78.       const response = await axios.post(url, bodyd, config)
  79.       await supabase.from('logs').insert({ data: response.data, sender: "Qontak (Callback)" })
  80.     } catch (e) {
  81.       await supabase.from('consoles').insert({ summary: `${e}`, severity: 2 })
  82.     }
  83.  
  84.     // Send Email
  85.   //   try {
  86.   //     if (!(transaction.email)) throw 'Email tidak diset, batalkan pengiriman email'
  87.   //     mg.messages.create('payment-ir.thawaf.id', {
  88.   //       from: 'Kuwais Group <[email protected]>',
  89.   //       to: [transaction.email],
  90.   //       subject: 'TERIMAKASIH UNTUK PEMBELIAN ANDA!',
  91.   //       html: `
  92.   //       <html>
  93.   //       <body style="background-color:#e2e1e0;font-family: Open Sans, sans-serif;font-size:100%;font-weight:400;line-height:1.4;color:#000;">
  94.   //         <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;">
  95.   //           <tbody>
  96.   //             <tr>
  97.   //               <td style="height:35px;"></td>
  98.   //             </tr>
  99.   //             <tr>
  100.   //               <td colspan="2" style="border: solid 1px #ddd; padding:10px 20px;">
  101.   //                    <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>
  102.   //                 <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>
  103.   //                 <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>
  104.   //               </td>
  105.   //             </tr>
  106.   //             <tr>
  107.   //               <td style="height:35px;"></td>
  108.   //             </tr>
  109.   //             <tr>
  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">Name</span> ${transaction.name}</p>
  112.   //                 <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>
  113.   //                 <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>
  114.   //               </td>
  115.   //               <td style="width:50%;padding:20px;vertical-align:top">
  116.   //                 <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>
  117.   //                 <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>
  118.   //               </td>
  119.   //             </tr>
  120.   //             <tr>
  121.   //               <td colspan="2" style="font-size:20px;padding:30px 15px 0 15px;">Paket Yang Dibeli</td>
  122.   //             </tr>
  123.   //             <tr>
  124.   //               <td colspan="2" style="padding:15px;">
  125.   //                 <p style="font-size:14px;margin:0;padding:10px;border:solid 1px #ddd;font-weight:bold;">
  126.   //                   <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>
  127.   //         </table>
  128.   //         <p> </p>
  129.   //         <hr />
  130.   //         <p>E-mail ini dibuat otomatis, mohon tidak membalas. Jika butuh bantuan, silakan hubungi<a href="https://api.whatsapp.com/send/?phone=6281224881962&text&type=phone_number&app_absent=0"> <strong>Whatsapp</strong></a></p>
  131.   //         <p> </p>
  132.   //         <br>
  133.   //         <br>
  134.   //       </body>
  135.   //       </html>
  136.   //     `
  137.   //     })
  138.   //     await supabase.from('consoles').insert({ summary: `Email has sended to ${transaction.email}`, severity: 1 })
  139.   //   } catch (e) {
  140.   //     await supabase.from('consoles').insert({ summary: `${e}`, severity: 1 })
  141.   //   }
  142.   // } catch (e) {
  143.   //   await supabase.from('consoles').insert({ summary: `${e}`, severity: 1 })
  144.   // }
  145.  
  146.  
  147.   // Send Email using Lamun
  148.   try {
  149.     if (!(transaction.email)) throw 'Email tidak diset, batalkan pengiriman email'
  150.     await axios.post('http://api.lamun.my.id/api/mail/send', {
  151.         to: [transaction.email],
  152.         subject: 'TERIMAKASIH UNTUK PEMBELIAN ANDA!',
  153.         html: `
  154.         <html>
  155.         <body style="background-color:#e2e1e0;font-family: Open Sans, sans-serif;font-size:100%;font-weight:400;line-height:1.4;color:#000;">
  156.           <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;">
  157.             <tbody>
  158.               <tr>
  159.                 <td style="height:35px;"></td>
  160.               </tr>
  161.               <tr>
  162.                 <td colspan="2" style="border: solid 1px #ddd; padding:10px 20px;">
  163.                      <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>
  164.                   <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>
  165.                   <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>
  166.                 </td>
  167.               </tr>
  168.               <tr>
  169.                 <td style="height:35px;"></td>
  170.               </tr>
  171.               <tr>
  172.                 <td style="width:50%;padding:20px;vertical-align:top">
  173.                   <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>
  174.                   <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>
  175.                   <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>
  176.                 </td>
  177.                 <td style="width:50%;padding:20px;vertical-align:top">
  178.                   <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>
  179.                   <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>
  180.                 </td>
  181.               </tr>
  182.               <tr>
  183.                 <td colspan="2" style="font-size:20px;padding:30px 15px 0 15px;">Paket Yang Dibeli</td>
  184.               </tr>
  185.               <tr>
  186.                 <td colspan="2" style="padding:15px;">
  187.                   <p style="font-size:14px;margin:0;padding:10px;border:solid 1px #ddd;font-weight:bold;">
  188.                     <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>
  189.           </table>
  190.           <p> </p>
  191.           <hr />
  192.           <p>E-mail ini dibuat otomatis, mohon tidak membalas. Jika butuh bantuan, silakan hubungi<a href="https://api.whatsapp.com/send/?phone=6281224881962&text&type=phone_number&app_absent=0"> <strong>Whatsapp</strong></a></p>
  193.           <p> </p>
  194.           <br>
  195.           <br>
  196.         </body>
  197.         </html>
  198.       ` // Insert HTML content here
  199.     }, {
  200.       headers: {
  201.         'Content-Type': 'application/json',
  202.         'username': 'noreply@payment-ir.thawaf.id', 
  203.         'apikey': 'cd7100dbac059e53da9f2525a933467b
  204.       }
  205.     });
  206.     await supabase.from('consoles').insert({ summary: `Email has been sent to ${transaction.email}`, severity: 1 })
  207.   } catch (e) {
  208.     await supabase.from('consoles').insert({ summary: `${e}`, severity: 1 })
  209.   }
  210. } catch (e) {
  211.   await supabase.from('consoles').insert({ summary: `${e}`, severity: 1 })
  212. }
  213.   res.status(200).send({ body: "Thanks", code: 200, status: "Success" })
  214. }
  215.  

Editor

You can edit this paste and save as new:


File Description
  • code
  • Paste Code
  • 26 Apr-2024
  • 12.11 Kb
You can Share it: