summaryrefslogtreecommitdiff
path: root/src/pages/api/shop/midtrans-payment.js
blob: a9bf16ac52fea469a1881a732f0bfe706cdfc0be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import odooApi from '@/core/api/odooApi'
import midtransClient from 'midtrans-client'

export default async function handler(req, res) {
  const { transactionId = null } = req.query

  if (!transactionId) {
    res.status(422).json({ error: 'parameter missing' })
  }

  let { auth } = req.cookies

  if (!auth) {
    res.status(401).json({ error: 'Unauthorized' })
  }

  auth = JSON.parse(auth)
  const transaction = await odooApi(
    'GET',
    `/api/v1/partner/${auth.partnerId}/sale_order/${transactionId}`,
    {},
    { Token: auth.token }
  )
  if (!transaction?.id) {
    res.status(400).json({ error: 'No Data' })
  }

  const snap = new midtransClient.Snap({
    isProduction: process.env.MIDTRANS_ENV == 'production',
    serverKey: process.env.MIDTRANS_SERVER_KEY
  })

  const parameter = {
    transaction_details: {
      order_id: transaction.name,
      gross_amount: transaction.amountTotal
    },
    credit_card: {
      secure: true
    },
    customer_details: {
      first_name: transaction.address.invoice.name,
      email: transaction.address.invoice.email,
      phone: transaction.address.invoice.phone
    }
  }

  const midtransTransaction = await snap.createTransaction(parameter)

  res.status(200).json(midtransTransaction)
}