diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-03-03 16:44:12 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-03-03 16:44:12 +0700 |
| commit | 76961c8312312609dbef0646274f6dd1f6c2bf19 (patch) | |
| tree | 111c9ff63449f4e188a72435a850ac8efc2a9d28 /src/pages/api/shop/midtrans-payment.js | |
| parent | 069f9fa637cd24e9b92c7a1e4de56fa9e263508f (diff) | |
add midtrans payment email notification
Diffstat (limited to 'src/pages/api/shop/midtrans-payment.js')
| -rw-r--r-- | src/pages/api/shop/midtrans-payment.js | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/src/pages/api/shop/midtrans-payment.js b/src/pages/api/shop/midtrans-payment.js index a9bf16ac..be676d38 100644 --- a/src/pages/api/shop/midtrans-payment.js +++ b/src/pages/api/shop/midtrans-payment.js @@ -1,17 +1,18 @@ import odooApi from '@/core/api/odooApi' +import camelcaseObjectDeep from 'camelcase-object-deep' 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' }) + return res.status(422).json({ error: 'parameter missing' }) } let { auth } = req.cookies if (!auth) { - res.status(401).json({ error: 'Unauthorized' }) + return res.status(401).json({ error: 'Unauthorized' }) } auth = JSON.parse(auth) @@ -22,7 +23,7 @@ export default async function handler(req, res) { { Token: auth.token } ) if (!transaction?.id) { - res.status(400).json({ error: 'No Data' }) + return res.status(400).json({ error: 'No Data' }) } const snap = new midtransClient.Snap({ @@ -32,20 +33,36 @@ export default async function handler(req, res) { const parameter = { transaction_details: { - order_id: transaction.name, + order_id: transaction.name?.replaceAll('/', '-'), gross_amount: transaction.amountTotal }, credit_card: { secure: true }, + item_details: transaction.products.map((product) => ({ + id: product.code, + price: Math.round(product.price.priceDiscount), + quantity: product.quantity, + name: product.name?.substring(0, 50) + })), customer_details: { - first_name: transaction.address.invoice.name, - email: transaction.address.invoice.email, - phone: transaction.address.invoice.phone + first_name: transaction.address.customer.name, + email: transaction.address.customer.email || '', + phone: transaction.address.customer.phone || '', + billing_address: { + first_name: transaction.address.invoice.name, + email: transaction.address.invoice.email || '', + phone: transaction.address.invoice.phone || '' + }, + shipping_address: { + first_name: transaction.address.shipping.name, + email: transaction.address.shipping.email || '', + phone: transaction.address.shipping.phone || '' + } } } const midtransTransaction = await snap.createTransaction(parameter) - res.status(200).json(midtransTransaction) + return res.status(200).json(camelcaseObjectDeep(midtransTransaction)) } |
