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 | |
| parent | 069f9fa637cd24e9b92c7a1e4de56fa9e263508f (diff) | |
add midtrans payment email notification
Diffstat (limited to 'src/pages/api')
| -rw-r--r-- | src/pages/api/shop/finish-checkout.js | 91 | ||||
| -rw-r--r-- | src/pages/api/shop/midtrans-payment.js | 33 |
2 files changed, 116 insertions, 8 deletions
diff --git a/src/pages/api/shop/finish-checkout.js b/src/pages/api/shop/finish-checkout.js new file mode 100644 index 00000000..66874549 --- /dev/null +++ b/src/pages/api/shop/finish-checkout.js @@ -0,0 +1,91 @@ +import odooApi from '@/core/api/odooApi' +import mailer from '@/core/utils/mailer' +import FinishCheckoutEmail from '@/lib/checkout/email/FinishCheckoutEmail' +import { render } from '@react-email/render' +import axios from 'axios' +import camelcaseObjectDeep from 'camelcase-object-deep' + +export default async function handler(req, res) { + const { orderName = null } = req.query + + if (!orderName) { + return res.status(422).json({ error: 'parameter missing' }) + } + + let { auth } = req.cookies + + if (!auth) { + return res.status(401).json({ error: 'Unauthorized' }) + } + auth = JSON.parse(auth) + + const midtransAuthKey = btoa(process.env.MIDTRANS_SERVER_KEY + ':') + const midtransHeaders = { + Accept: 'application/json', + 'Content-Type': 'application/json', + Authorization: `Basic ${midtransAuthKey}` + } + let midtransStatus = await axios.get(`${process.env.MIDTRANS_HOST}/v2/${orderName}/status`, { + headers: midtransHeaders + }) + midtransStatus = camelcaseObjectDeep(midtransStatus.data) + if (!midtransStatus?.orderId) { + return res.status(400).json({ error: 'Payment Not Found' }) + } + + const query = `name=${orderName.replaceAll('-', '/')}&limit=1` + const searchTransaction = await odooApi( + 'GET', + `/api/v1/partner/${auth.partnerId}/sale_order?${query}`, + {}, + { Token: auth.token } + ) + if (searchTransaction.saleOrderTotal == 0) { + return res.status(400).json({ error: 'Transaction Not Found' }) + } + + let transaction = await odooApi( + 'GET', + `/api/v1/partner/${auth.partnerId}/sale_order/${searchTransaction.saleOrders[0].id}`, + {}, + { Token: auth.token } + ) + if (!transaction?.id) { + return res.status(400).json({ error: 'Transaction Detail Not Found' }) + } + + transaction.subtotal = 0 + transaction.discountTotal = 0 + for (const product of transaction.products) { + transaction.subtotal += product.price.price * product.quantity + transaction.discountTotal -= + (product.price.price - product.price.priceDiscount) * product.quantity + } + + let statusPayment = '' + const transactionStatus = midtransStatus.transactionStatus + if (['capture', 'settlement'].includes(transactionStatus)) { + statusPayment = 'success' + } else if (transactionStatus == 'pending') { + statusPayment = 'pending' + } else { + statusPayment = 'failed' + } + + const emailMessage = render( + <FinishCheckoutEmail + transaction={transaction} + payment={midtransStatus} + statusPayment={statusPayment} + /> + ) + + mailer.sendMail({ + from: 'sales@indoteknik.com', + to: transaction.address.customer.email, + subject: 'Pembelian di Indoteknik.com', + html: emailMessage + }) + + return res.status(200).json({ description: 'success' }) +} 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)) } |
