|
|
@@ -1,4 +1,6 @@
|
|
|
"use client";
|
|
|
+
|
|
|
+import { useState } from "react";
|
|
|
import { useForm } from "react-hook-form";
|
|
|
import {
|
|
|
AddressDataTypes,
|
|
|
@@ -6,25 +8,129 @@ import {
|
|
|
import { isObject } from "@/utils/type-guards";
|
|
|
import { useCheckout } from "@utils/hooks/useCheckout";
|
|
|
import { ProceedToCheckout } from "../ProceedToCheckout";
|
|
|
+
|
|
|
+import {
|
|
|
+ usePayPal,
|
|
|
+ useEligibleMethods,
|
|
|
+ INSTANCE_LOADING_STATE,
|
|
|
+ PayPalOneTimePaymentButton,
|
|
|
+ VenmoOneTimePaymentButton,
|
|
|
+ PayLaterOneTimePaymentButton,
|
|
|
+ PayPalGuestPaymentButton,
|
|
|
+ PayPalCreditOneTimePaymentButton,
|
|
|
+ type OnApproveDataOneTimePayments,
|
|
|
+ type OnErrorData,
|
|
|
+ type OnCompleteData,
|
|
|
+ type OnCancelDataOneTimePayments,
|
|
|
+} from "@paypal/react-paypal-js/sdk-v6";
|
|
|
+
|
|
|
+import { useDispatch } from "react-redux";
|
|
|
+import {clearCart} from "@/store/slices/cart-slice";
|
|
|
+import { getCookie, setCookie } from "@/utils/cookie-tools";
|
|
|
+import { clientFetch } from "@/lib/restApiClient";
|
|
|
+import { ORDER_ID } from "@utils/constants";
|
|
|
+import { useRouter } from "next/navigation";
|
|
|
+
|
|
|
+type ModalType = "success" | "cancel" | "error" | null;
|
|
|
export default function OrderReview({
|
|
|
+ selectedPayment,
|
|
|
selectedPaymentTitle,
|
|
|
shippingAddress,
|
|
|
billingAddress,
|
|
|
selectedShipping : _selectedShipping,
|
|
|
selectedShippingRateTitle,
|
|
|
}: {
|
|
|
+ selectedPayment?: any;
|
|
|
selectedPaymentTitle?: string;
|
|
|
shippingAddress?: AddressDataTypes;
|
|
|
billingAddress?: AddressDataTypes;
|
|
|
selectedShipping?: string;
|
|
|
selectedShippingRateTitle?: string;
|
|
|
}) {
|
|
|
- const { isPlaceOrder, savePlaceOrder } = useCheckout();
|
|
|
+ const dispatch = useDispatch();
|
|
|
+ const router = useRouter();
|
|
|
+
|
|
|
+ const { isPlaceOrder, savePlaceOrder, createOrder } = useCheckout();
|
|
|
const { handleSubmit } = useForm();
|
|
|
const onSubmit = () => {
|
|
|
savePlaceOrder();
|
|
|
};
|
|
|
|
|
|
+ const [modalState, setModalState] = useState<ModalType>(null);
|
|
|
+ const { loadingStatus } = usePayPal();
|
|
|
+
|
|
|
+ // Fetch eligibility for one-time payment flow
|
|
|
+ const {
|
|
|
+ error: eligibilityError,
|
|
|
+ eligiblePaymentMethods,
|
|
|
+ isLoading: isEligibilityLoading,
|
|
|
+ } = useEligibleMethods({
|
|
|
+ payload: {
|
|
|
+ currencyCode: "USD",
|
|
|
+ paymentFlow: "ONE_TIME_PAYMENT",
|
|
|
+ },
|
|
|
+ });
|
|
|
+ const isLoading = loadingStatus === INSTANCE_LOADING_STATE.PENDING;
|
|
|
+
|
|
|
+ let orderId = '';
|
|
|
+ const handleCreateOrder = async () => {
|
|
|
+ let res = await createOrder();
|
|
|
+ console.log("createOrder res ---- ", res);
|
|
|
+ orderId = res?.orderId || '';
|
|
|
+ return res;
|
|
|
+ };
|
|
|
+
|
|
|
+ const handlePaymentCallbacks = {
|
|
|
+ onApprove: async (data: OnApproveDataOneTimePayments) => {
|
|
|
+ console.log("Payment approved:", data);
|
|
|
+ // api/shop/paypal/smart-button/capture-order
|
|
|
+ // const captureResult = await captureOrder({ orderId: data.orderId });
|
|
|
+ const captureResult = await clientFetch('/api/shop/paypal/smart-button/capture-order',{
|
|
|
+ method: "POST",
|
|
|
+ body: JSON.stringify({ orderId: data.orderId })
|
|
|
+ });
|
|
|
+ console.log("Payment capture result:", captureResult);
|
|
|
+
|
|
|
+ // setModalState("success");
|
|
|
+ // setCookie(ORDER_ID, orderId);
|
|
|
+ // dispatch(clearCart());
|
|
|
+ // router.replace("/success");
|
|
|
+ },
|
|
|
+
|
|
|
+ onCancel: (data: OnCancelDataOneTimePayments) => {
|
|
|
+ console.log("Payment cancelled:", data);
|
|
|
+ setModalState("cancel");
|
|
|
+ },
|
|
|
+
|
|
|
+ onError: (data: OnErrorData) => {
|
|
|
+ console.error("Payment error:", data);
|
|
|
+ setModalState("error");
|
|
|
+ },
|
|
|
+
|
|
|
+ onComplete: (data: OnCompleteData) => {
|
|
|
+ console.log("Payment session completed");
|
|
|
+ console.log("On Complete data:", data);
|
|
|
+ },
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
+ const paymentButtons = isLoading ? (
|
|
|
+ <div style={{ padding: "1rem", textAlign: "center" }}>
|
|
|
+ Loading payment methods...
|
|
|
+ </div>
|
|
|
+ ) : eligibilityError ? (
|
|
|
+ <div style={{ padding: "1rem", textAlign: "center", color: "red" }}>
|
|
|
+ Failed to load payment options. Please refresh the page.
|
|
|
+ </div>
|
|
|
+ ) : (
|
|
|
+ <PayPalOneTimePaymentButton
|
|
|
+ createOrder={handleCreateOrder}
|
|
|
+ presentationMode="auto"
|
|
|
+ {...handlePaymentCallbacks}
|
|
|
+ />
|
|
|
+ );
|
|
|
+
|
|
|
+
|
|
|
return (
|
|
|
<div className="mt-4 flex-col mb-20 sm:mb-0">
|
|
|
<div className="relative">
|
|
|
@@ -87,7 +193,10 @@ export default function OrderReview({
|
|
|
)}
|
|
|
</div>
|
|
|
<div className="flex flex-col gap-6">
|
|
|
- <form onSubmit={handleSubmit(onSubmit)}>
|
|
|
+ {selectedPayment === 'paypal_smart_button'
|
|
|
+
|
|
|
+ ? paymentButtons
|
|
|
+ : <form onSubmit={handleSubmit(onSubmit)}>
|
|
|
<div className="justify-self-end">
|
|
|
<ProceedToCheckout
|
|
|
buttonName="Place Order"
|
|
|
@@ -95,6 +204,10 @@ export default function OrderReview({
|
|
|
/>
|
|
|
</div>
|
|
|
</form>
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
</div>
|
|
|
</div>
|
|
|
);
|