259 lines
8.1 KiB
TypeScript
259 lines
8.1 KiB
TypeScript
"use client"
|
|
import { RadioGroup } from "@headlessui/react"
|
|
import { getPaymentInfo, isStripeLike } from "@lib/constants"
|
|
import { initiatePaymentSession } from "@lib/data/cart"
|
|
import { CheckCircle2 as CheckCircleSolid, CreditCard } from "lucide-react"
|
|
import ErrorMessage from "@modules/checkout/components/error-message"
|
|
import PaymentContainer, {
|
|
StripePaymentContainer,
|
|
} from "@modules/checkout/components/payment-container"
|
|
import Divider from "@modules/common/components/divider"
|
|
import {
|
|
Button,
|
|
Container,
|
|
Heading,
|
|
Text,
|
|
clx,
|
|
} from "@modules/common/components/ui"
|
|
import { HttpTypes } from "@medusajs/types"
|
|
import { usePathname, useRouter, useSearchParams } from "next/navigation"
|
|
import { useCallback, useEffect, useState } from "react"
|
|
|
|
const Payment = ({
|
|
cart,
|
|
availablePaymentMethods,
|
|
}: {
|
|
cart: HttpTypes.StoreCart
|
|
availablePaymentMethods: { id: string }[]
|
|
}) => {
|
|
const activeSession = cart.payment_collection?.payment_sessions?.find(
|
|
(paymentSession) => paymentSession.status === "pending"
|
|
)
|
|
|
|
const [isLoading, setIsLoading] = useState(false)
|
|
const [error, setError] = useState<string | null>(null)
|
|
const [paymentComplete, setPaymentComplete] = useState(false)
|
|
const [selectedPaymentMethod, setSelectedPaymentMethod] = useState(
|
|
activeSession?.provider_id ?? ""
|
|
)
|
|
|
|
const searchParams = useSearchParams()
|
|
const router = useRouter()
|
|
const pathname = usePathname()
|
|
|
|
const isOpen = searchParams.get("step") === "payment"
|
|
|
|
const setPaymentMethod = async (method: string) => {
|
|
setError(null)
|
|
setSelectedPaymentMethod(method)
|
|
if (isStripeLike(method)) {
|
|
await initiatePaymentSession(cart, {
|
|
provider_id: method,
|
|
})
|
|
}
|
|
}
|
|
|
|
const paidByGiftcard = !!(
|
|
(cart as unknown as Record<string, unknown>)?.gift_cards && ((cart as unknown as Record<string, unknown>)?.gift_cards as unknown[])?.length > 0 && cart?.total === 0
|
|
)
|
|
|
|
const paymentReady =
|
|
(activeSession && (cart?.shipping_methods?.length ?? 0) !== 0) || paidByGiftcard
|
|
|
|
const createQueryString = useCallback(
|
|
(name: string, value: string) => {
|
|
const params = new URLSearchParams(searchParams)
|
|
params.set(name, value)
|
|
|
|
return params.toString()
|
|
},
|
|
[searchParams]
|
|
)
|
|
|
|
const handleEdit = () => {
|
|
router.push(pathname + "?" + createQueryString("step", "payment"), {
|
|
scroll: false,
|
|
})
|
|
}
|
|
|
|
const handleSubmit = async () => {
|
|
setIsLoading(true)
|
|
try {
|
|
const shouldInputPaymentDetails =
|
|
isStripeLike(selectedPaymentMethod) && !activeSession
|
|
|
|
const checkActiveSession =
|
|
activeSession?.provider_id === selectedPaymentMethod
|
|
|
|
if (!checkActiveSession) {
|
|
await initiatePaymentSession(cart, {
|
|
provider_id: selectedPaymentMethod,
|
|
})
|
|
}
|
|
|
|
if (!shouldInputPaymentDetails) {
|
|
return router.push(
|
|
pathname + "?" + createQueryString("step", "review"),
|
|
{
|
|
scroll: false,
|
|
}
|
|
)
|
|
}
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : String(err))
|
|
} finally {
|
|
setIsLoading(false)
|
|
}
|
|
}
|
|
|
|
useEffect(() => {
|
|
setError(null)
|
|
}, [isOpen])
|
|
|
|
return (
|
|
<div className="bg-white">
|
|
<div className="flex flex-row items-center justify-between mb-6">
|
|
<Heading
|
|
level="h2"
|
|
className={clx(
|
|
"flex flex-row font-display text-3xl gap-x-2 items-baseline",
|
|
{
|
|
"opacity-50 pointer-events-none select-none":
|
|
!isOpen && !paymentReady,
|
|
}
|
|
)}
|
|
>
|
|
Plaćanje
|
|
{!isOpen && paymentReady && <CheckCircleSolid />}
|
|
</Heading>
|
|
{!isOpen && paymentReady && (
|
|
<Text>
|
|
<button
|
|
onClick={handleEdit}
|
|
className="text-primary hover:text-primary/80"
|
|
data-testid="edit-payment-button"
|
|
>
|
|
Izmijeni
|
|
</button>
|
|
</Text>
|
|
)}
|
|
</div>
|
|
<div>
|
|
<div className={isOpen ? "block" : "hidden"}>
|
|
{!paidByGiftcard && availablePaymentMethods?.length && (
|
|
<>
|
|
<RadioGroup
|
|
value={selectedPaymentMethod}
|
|
onChange={(value: string) => setPaymentMethod(value)}
|
|
>
|
|
{availablePaymentMethods.map((paymentMethod) => (
|
|
<div key={paymentMethod.id}>
|
|
{isStripeLike(paymentMethod.id) ? (
|
|
<StripePaymentContainer
|
|
paymentProviderId={paymentMethod.id}
|
|
selectedPaymentOptionId={selectedPaymentMethod}
|
|
setError={setError}
|
|
setPaymentComplete={setPaymentComplete}
|
|
/>
|
|
) : (
|
|
<PaymentContainer
|
|
paymentProviderId={paymentMethod.id}
|
|
selectedPaymentOptionId={selectedPaymentMethod}
|
|
/>
|
|
)}
|
|
</div>
|
|
))}
|
|
</RadioGroup>
|
|
</>
|
|
)}
|
|
|
|
{paidByGiftcard && (
|
|
<div className="flex flex-col w-1/3">
|
|
<Text className="text-sm font-medium text-foreground mb-1">
|
|
Način plaćanja
|
|
</Text>
|
|
<Text
|
|
className="text-sm text-muted-foreground"
|
|
data-testid="payment-method-summary"
|
|
>
|
|
Poklon kartica
|
|
</Text>
|
|
</div>
|
|
)}
|
|
|
|
<ErrorMessage
|
|
error={error}
|
|
data-testid="payment-method-error-message"
|
|
/>
|
|
|
|
<Button
|
|
size="large"
|
|
className="mt-6"
|
|
onClick={handleSubmit}
|
|
isLoading={isLoading}
|
|
disabled={
|
|
(isStripeLike(selectedPaymentMethod) && !paymentComplete) ||
|
|
(!selectedPaymentMethod && !paidByGiftcard)
|
|
}
|
|
data-testid="submit-payment-button"
|
|
>
|
|
{!activeSession && isStripeLike(selectedPaymentMethod)
|
|
? "Unesite podatke za plaćanje"
|
|
: "Nastavite na pregled"}
|
|
</Button>
|
|
</div>
|
|
|
|
<div className={isOpen ? "hidden" : "block"}>
|
|
{cart && paymentReady && activeSession ? (
|
|
<div className="flex items-start gap-x-1 w-full">
|
|
<div className="flex flex-col w-1/3">
|
|
<Text className="text-sm font-medium text-foreground mb-1">
|
|
Način plaćanja
|
|
</Text>
|
|
<Text
|
|
className="text-sm text-muted-foreground"
|
|
data-testid="payment-method-summary"
|
|
>
|
|
{getPaymentInfo(activeSession?.provider_id)?.title ||
|
|
activeSession?.provider_id}
|
|
</Text>
|
|
</div>
|
|
<div className="flex flex-col w-1/3">
|
|
<Text className="text-sm font-medium text-foreground mb-1">
|
|
Podaci o plaćanju
|
|
</Text>
|
|
<div
|
|
className="flex gap-2 text-sm text-muted-foreground items-center"
|
|
data-testid="payment-details-summary"
|
|
>
|
|
<Container className="flex items-center h-7 w-fit p-2 bg-muted">
|
|
{getPaymentInfo(selectedPaymentMethod)?.icon || (
|
|
<CreditCard />
|
|
)}
|
|
</Container>
|
|
<Text>Another step will appear</Text>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
) : paidByGiftcard ? (
|
|
<div className="flex flex-col w-1/3">
|
|
<Text className="text-sm font-medium text-foreground mb-1">
|
|
Način plaćanja
|
|
</Text>
|
|
<Text
|
|
className="text-sm text-muted-foreground"
|
|
data-testid="payment-method-summary"
|
|
>
|
|
Poklon kartica
|
|
</Text>
|
|
</div>
|
|
) : null}
|
|
</div>
|
|
</div>
|
|
<Divider className="mt-8" />
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default Payment
|