From 405dc33dba63f008f45b76061a8f1be5031a1d5d Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Thu, 2 Mar 2023 17:02:12 +0700 Subject: midtrans integration --- src/lib/auth/components/Activate.jsx | 2 +- src/lib/auth/components/Register.jsx | 2 +- src/lib/checkout/components/Checkout.jsx | 58 +++++++++---------------------- src/lib/home/api/popularProductApi.js | 2 +- src/lib/home/hooks/useCategoryHome.js | 4 ++- src/lib/invoice/utils/invoices.js | 4 +-- src/lib/product/api/productSearchApi.js | 2 +- src/lib/product/api/productSimilarApi.js | 2 +- src/lib/transaction/utils/transactions.js | 4 +-- 9 files changed, 28 insertions(+), 52 deletions(-) (limited to 'src/lib') 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 ( @@ -222,32 +221,6 @@ const Checkout = () => { -
-
- Metode Pembayaran (Wajib dipilih) -
-
- {payments.map((payment, index) => ( - - ))} -
-
- - -
Purchase Order
@@ -284,15 +257,16 @@ const Checkout = () => { {isLoading ? 'Loading...' : 'Bayar'}
+ +