diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-03-02 17:02:12 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-03-02 17:02:12 +0700 |
| commit | 405dc33dba63f008f45b76061a8f1be5031a1d5d (patch) | |
| tree | 7d60a44153dfadd11f0ff0a81144720b961b5823 /src | |
| parent | d336735a91133cc3f1cf6f67ba2ac29f0985fd2e (diff) | |
midtrans integration
Diffstat (limited to 'src')
| -rw-r--r-- | src/core/api/odooApi.js | 4 | ||||
| -rw-r--r-- | src/core/api/searchSuggestApi.js | 2 | ||||
| -rw-r--r-- | src/lib/auth/components/Activate.jsx | 2 | ||||
| -rw-r--r-- | src/lib/auth/components/Register.jsx | 2 | ||||
| -rw-r--r-- | src/lib/checkout/components/Checkout.jsx | 58 | ||||
| -rw-r--r-- | src/lib/home/api/popularProductApi.js | 2 | ||||
| -rw-r--r-- | src/lib/home/hooks/useCategoryHome.js | 4 | ||||
| -rw-r--r-- | src/lib/invoice/utils/invoices.js | 4 | ||||
| -rw-r--r-- | src/lib/product/api/productSearchApi.js | 2 | ||||
| -rw-r--r-- | src/lib/product/api/productSimilarApi.js | 2 | ||||
| -rw-r--r-- | src/lib/transaction/utils/transactions.js | 4 | ||||
| -rw-r--r-- | src/pages/_document.jsx | 13 | ||||
| -rw-r--r-- | src/pages/api/activation-request.js | 2 | ||||
| -rw-r--r-- | src/pages/api/shop/midtrans-payment.js | 51 |
14 files changed, 96 insertions, 56 deletions
diff --git a/src/core/api/odooApi.js b/src/core/api/odooApi.js index 202c355e..41460fda 100644 --- a/src/core/api/odooApi.js +++ b/src/core/api/odooApi.js @@ -4,7 +4,7 @@ import { getCookie, setCookie } from 'cookies-next' import { getAuth } from '../utils/auth' const renewToken = async () => { - let token = await axios.get(process.env.ODOO_HOST + '/api/token') + let token = await axios.get(process.env.NEXT_PUBLIC_ODOO_HOST + '/api/token') setCookie('token', token.data.result) return token.data.result } @@ -26,7 +26,7 @@ const odooApi = async (method, url, data = {}, headers = {}) => { let axiosParameter = { method, - url: process.env.ODOO_HOST + url, + url: process.env.NEXT_PUBLIC_ODOO_HOST + url, headers: { Authorization: token, ...headers } } if (auth) axiosParameter.headers['Token'] = auth.token diff --git a/src/core/api/searchSuggestApi.js b/src/core/api/searchSuggestApi.js index e4445c9a..3dabfbd6 100644 --- a/src/core/api/searchSuggestApi.js +++ b/src/core/api/searchSuggestApi.js @@ -2,7 +2,7 @@ import axios from 'axios' const searchSuggestApi = async ({ query }) => { const dataSearchSuggest = await axios( - `${process.env.SELF_HOST}/api/shop/suggest?q=${query.trim()}` + `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/suggest?q=${query.trim()}` ) return dataSearchSuggest } diff --git a/src/lib/auth/components/Activate.jsx b/src/lib/auth/components/Activate.jsx index 7970524c..f67cc168 100644 --- a/src/lib/auth/components/Activate.jsx +++ b/src/lib/auth/components/Activate.jsx @@ -67,7 +67,7 @@ const Activate = () => { const activationRequest = async (e) => { e.preventDefault() setIsLoading(true) - let activationRequest = await axios.post(`${process.env.SELF_HOST}/api/activation-request`, { + let activationRequest = await axios.post(`${process.env.NEXT_PUBLIC_SELF_HOST}/api/activation-request`, { email }) if (activationRequest.data.activationRequest) { diff --git a/src/lib/auth/components/Register.jsx b/src/lib/auth/components/Register.jsx index d02081ce..82b81b52 100644 --- a/src/lib/auth/components/Register.jsx +++ b/src/lib/auth/components/Register.jsx @@ -27,7 +27,7 @@ const Register = () => { const isRegistered = await registerApi({ data }) setIsLoading(false) if (isRegistered.register) { - await axios.post(`${process.env.SELF_HOST}/api/activation-request`, { email }) + await axios.post(`${process.env.NEXT_PUBLIC_SELF_HOST}/api/activation-request`, { email }) setAlert({ children: 'Berhasil mendaftarkan akun anda, cek email untuk melakukan aktivasi akun', type: 'success' diff --git a/src/lib/checkout/components/Checkout.jsx b/src/lib/checkout/components/Checkout.jsx index f6170b13..42608cef 100644 --- a/src/lib/checkout/components/Checkout.jsx +++ b/src/lib/checkout/components/Checkout.jsx @@ -5,7 +5,6 @@ import useAuth from '@/core/hooks/useAuth' import { getItemAddress } from '@/core/utils/address' import addressesApi from '@/lib/address/api/addressesApi' import CartApi from '@/lib/cart/api/CartApi' -import VariantCard from '@/lib/variant/components/VariantCard' import { ExclamationCircleIcon } from '@heroicons/react/24/outline' import { useEffect, useRef, useState } from 'react' import _ from 'lodash' @@ -16,6 +15,8 @@ import getFileBase64 from '@/core/utils/getFileBase64' import checkoutApi from '../api/checkoutApi' import { useRouter } from 'next/router' import VariantGroupCard from '@/lib/variant/components/VariantGroupCard' +import axios from 'axios' +import Script from 'next/script' const Checkout = () => { const router = useRouter() @@ -89,18 +90,12 @@ const Checkout = () => { } }, [products]) - const [selectedPayment, setSelectedPayment] = useState(null) - const poNumber = useRef('') const poFile = useRef('') const [isLoading, setIsLoading] = useState(false) const checkout = async () => { - if (!selectedPayment) { - toast.error('Pilih metode pembayaran', { position: 'bottom-center' }) - return - } const file = poFile.current.files[0] if (typeof file !== 'undefined' && file.size > 5000000) { toast.error('Maksimal ukuran file adalah 5MB', { position: 'bottom-center' }) @@ -122,12 +117,16 @@ const Checkout = () => { const isCheckouted = await checkoutApi({ data }) setIsLoading(false) - if (isCheckouted?.id) { - for (const product of products) deleteItemCart({ productId: product.id }) - router.push(`/shop/checkout/finish?id=${isCheckouted.id}`) + if (!isCheckouted?.id) { + toast.error('Gagal melakukan transaksi, terjadi kesalahan internal') return } - toast.error('Gagal melakukan transaksi, terjadi kesalahan internal') + + const payment = await axios.post( + `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/midtrans-payment?transactionId=${isCheckouted.id}` + ) + for (const product of products) deleteItemCart({ productId: product.id }) + window.snap.pay(payment.data.token) } return ( @@ -223,32 +222,6 @@ const Checkout = () => { <Divider /> <div className='p-4'> - <div className='font-medium'> - Metode Pembayaran <span className='font-normal text-gray_r-11'>(Wajib dipilih)</span> - </div> - <div className='grid gap-y-3 mt-4'> - {payments.map((payment, index) => ( - <button - type='button' - className={ - 'text-left border border-gray_r-6 rounded-md p-3 ' + - (selectedPayment == payment.name && 'border-yellow_r-10 bg-yellow_r-3') - } - onClick={() => setSelectedPayment(payment.name)} - key={index} - > - <p> - {payment.name} - {payment.number} - </p> - <p className='mt-1 text-gray_r-11'>PT. Indoteknik Dotcom Gemilang</p> - </button> - ))} - </div> - </div> - - <Divider /> - - <div className='p-4'> <div className='font-medium'>Purchase Order</div> <div className='mt-4 flex gap-x-3'> @@ -284,15 +257,16 @@ const Checkout = () => { {isLoading ? 'Loading...' : 'Bayar'} </button> </div> + + <Script + async + src='https://app.sandbox.midtrans.com/snap/snap.js' + data-client-key='' + /> </> ) } -const payments = [ - { name: 'BCA', number: '8870-4000-81' }, - { name: 'MANDIRI', number: '155-0067-6869-75' } -] - const SectionAddress = ({ address, label, url }) => ( <div className='p-4'> <div className='flex justify-between items-center'> diff --git a/src/lib/home/api/popularProductApi.js b/src/lib/home/api/popularProductApi.js index 5a6d3212..37e4390e 100644 --- a/src/lib/home/api/popularProductApi.js +++ b/src/lib/home/api/popularProductApi.js @@ -2,7 +2,7 @@ import axios from 'axios' const popularProductApi = async () => { const dataPopularProducts = await axios( - `${process.env.SELF_HOST}/api/shop/search?q=*&page=1&orderBy=popular` + `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/search?q=*&page=1&orderBy=popular` ) return dataPopularProducts.data.response } diff --git a/src/lib/home/hooks/useCategoryHome.js b/src/lib/home/hooks/useCategoryHome.js index cfaa3d9c..ea9f8d6a 100644 --- a/src/lib/home/hooks/useCategoryHome.js +++ b/src/lib/home/hooks/useCategoryHome.js @@ -3,7 +3,9 @@ import { useQuery } from 'react-query' const useCategoryHome = ({ id }) => { const fetchCategoryHome = async () => await categoryHomeApi({ id }) - const { isLoading, data } = useQuery(`categoryHome-${id}`, fetchCategoryHome) + const { isLoading, data } = useQuery(`categoryHome-${id}`, fetchCategoryHome, { + refetchOnWindowFocus: false + }) return { categoryHome: { data, isLoading } diff --git a/src/lib/invoice/utils/invoices.js b/src/lib/invoice/utils/invoices.js index 221e53cf..63fe91f6 100644 --- a/src/lib/invoice/utils/invoices.js +++ b/src/lib/invoice/utils/invoices.js @@ -1,10 +1,10 @@ const downloadInvoice = (invoice) => { - const url = `${process.env.ODOO_HOST}/api/v1/download/invoice/${invoice.id}/${invoice.token}` + const url = `${process.env.NEXT_PUBLIC_ODOO_HOST}/api/v1/download/invoice/${invoice.id}/${invoice.token}` window.open(url, 'download') } const downloadTaxInvoice = (invoice) => { - const url = `${process.env.ODOO_HOST}/api/v1/download/tax-invoice/${invoice.id}/${invoice.token}` + const url = `${process.env.NEXT_PUBLIC_ODOO_HOST}/api/v1/download/tax-invoice/${invoice.id}/${invoice.token}` window.open(url, 'download') } diff --git a/src/lib/product/api/productSearchApi.js b/src/lib/product/api/productSearchApi.js index b9acd94b..e7ad49a6 100644 --- a/src/lib/product/api/productSearchApi.js +++ b/src/lib/product/api/productSearchApi.js @@ -2,7 +2,7 @@ import _ from 'lodash-contrib' import axios from 'axios' const productSearchApi = async ({ query }) => { - const dataProductSearch = await axios(`${process.env.SELF_HOST}/api/shop/search?${query}`) + const dataProductSearch = await axios(`${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/search?${query}`) return dataProductSearch.data } diff --git a/src/lib/product/api/productSimilarApi.js b/src/lib/product/api/productSimilarApi.js index 7142fab4..8fd17ab9 100644 --- a/src/lib/product/api/productSimilarApi.js +++ b/src/lib/product/api/productSimilarApi.js @@ -2,7 +2,7 @@ import axios from 'axios' const productSimilarApi = async ({ query }) => { const dataProductSimilar = await axios( - `${process.env.SELF_HOST}/api/shop/search?q=${query}&page=1&orderBy=popular` + `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/search?q=${query}&page=1&orderBy=popular` ) return dataProductSimilar.data.response } diff --git a/src/lib/transaction/utils/transactions.js b/src/lib/transaction/utils/transactions.js index 4c7522be..ef2f8d97 100644 --- a/src/lib/transaction/utils/transactions.js +++ b/src/lib/transaction/utils/transactions.js @@ -2,13 +2,13 @@ import { getAuth } from '@/core/utils/auth' const downloadPurchaseOrder = (transaction) => { const auth = getAuth() - const url = `${process.env.ODOO_HOST}/api/v1/partner/${auth.partnerId}/sale_order/${transaction.id}/download_po/${transaction.token}` + const url = `${process.env.NEXT_PUBLIC_ODOO_HOST}/api/v1/partner/${auth.partnerId}/sale_order/${transaction.id}/download_po/${transaction.token}` window.open(url, 'download') } const downloadQuotation = (transaction) => { const auth = getAuth() - const url = `${process.env.ODOO_HOST}/api/v1/partner/${auth.partnerId}/sale_order/${transaction.id}/download/${transaction.token}` + const url = `${process.env.NEXT_PUBLIC_ODOO_HOST}/api/v1/partner/${auth.partnerId}/sale_order/${transaction.id}/download/${transaction.token}` window.open(url, 'download') } diff --git a/src/pages/_document.jsx b/src/pages/_document.jsx new file mode 100644 index 00000000..bff2b1b2 --- /dev/null +++ b/src/pages/_document.jsx @@ -0,0 +1,13 @@ +import { Html, Head, Main, NextScript } from 'next/document' + +export default function Document() { + return ( + <Html> + <Head /> + <body> + <Main /> + <NextScript /> + </body> + </Html> + ) +} diff --git a/src/pages/api/activation-request.js b/src/pages/api/activation-request.js index 7fae2fd1..61dbb597 100644 --- a/src/pages/api/activation-request.js +++ b/src/pages/api/activation-request.js @@ -13,7 +13,7 @@ export default async function handler(req, res) { html: ` <h1>Permintaan Aktivasi Akun Indoteknik</h1> <br> - <p>Aktivasi akun anda melalui link berikut: <a href="${process.env.SELF_HOST}/activate?token=${result.token}">Aktivasi Akun</a></p> + <p>Aktivasi akun anda melalui link berikut: <a href="${process.env.NEXT_PUBLIC_SELF_HOST}/activate?token=${result.token}">Aktivasi Akun</a></p> ` }) } diff --git a/src/pages/api/shop/midtrans-payment.js b/src/pages/api/shop/midtrans-payment.js new file mode 100644 index 00000000..a9bf16ac --- /dev/null +++ b/src/pages/api/shop/midtrans-payment.js @@ -0,0 +1,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) +} |
