"use client" import { isAdyen, isManual, isStripeLike } from "@lib/constants" import { placeOrder } from "@lib/data/cart" import { HttpTypes } from "@medusajs/types" import { Button } from "@modules/common/components/ui" import { useElements, useStripe } from "@stripe/react-stripe-js" import { useParams } from "next/navigation" import React, { useState } from "react" import ErrorMessage from "../error-message" import { getPaymentErrorMessage } from "@lib/util/payment-error" import AdyenPaymentButton from "./adyen-payment-button" type PaymentButtonProps = { cart: HttpTypes.StoreCart "data-testid": string } const PaymentButton: React.FC = ({ cart, "data-testid": dataTestId, }) => { const notReady = !cart || !cart.shipping_address || !cart.billing_address || !cart.email || (cart.shipping_methods?.length ?? 0) < 1 const paymentSession = cart.payment_collection?.payment_sessions?.[0] switch (true) { case isStripeLike(paymentSession?.provider_id): return ( ) case isAdyen(paymentSession?.provider_id): return ( ) case isManual(paymentSession?.provider_id): return ( ) default: return } } const StripePaymentButton = ({ cart, notReady, "data-testid": dataTestId, }: { cart: HttpTypes.StoreCart notReady: boolean "data-testid"?: string }) => { const [submitting, setSubmitting] = useState(false) const [errorMessage, setErrorMessage] = useState(null) const onPaymentCompleted = async () => { await placeOrder() .catch((err) => { setErrorMessage(err.message) }) .finally(() => { setSubmitting(false) }) } const stripe = useStripe() const elements = useElements() const { countryCode } = useParams() const disabled = !stripe || !elements ? true : false const handlePayment = async () => { if (!stripe || !elements || !cart) { return } setSubmitting(true) await stripe .confirmPayment({ elements, confirmParams: { return_url: `${window.location.origin}/api/payment-return?cart_id=${cart.id}&country_code=${countryCode}`, payment_method_data: { billing_details: { name: cart.billing_address?.first_name + " " + cart.billing_address?.last_name, address: { city: cart.billing_address?.city ?? undefined, country: cart.billing_address?.country_code ?? undefined, line1: cart.billing_address?.address_1 ?? undefined, line2: cart.billing_address?.address_2 ?? undefined, postal_code: cart.billing_address?.postal_code ?? undefined, state: cart.billing_address?.province ?? undefined, }, email: cart.email, phone: cart.billing_address?.phone ?? undefined, }, }, }, // Only leave the site when the selected method actually requires it, so // card payments still complete inline. redirect: "if_required", }) .then(({ error, paymentIntent }) => { if (error) { const pi = error.payment_intent if ( (pi && pi.status === "requires_capture") || (pi && pi.status === "succeeded") ) { onPaymentCompleted() return } setErrorMessage(getPaymentErrorMessage(error)) setSubmitting(false) return } if ( paymentIntent.status === "requires_capture" || paymentIntent.status === "succeeded" ) { onPaymentCompleted() return } setSubmitting(false) }) .catch((error) => { setErrorMessage(getPaymentErrorMessage(error)) setSubmitting(false) }) } return ( <> ) } const ManualTestPaymentButton = ({ notReady }: { notReady: boolean }) => { const [submitting, setSubmitting] = useState(false) const [errorMessage, setErrorMessage] = useState(null) const onPaymentCompleted = async () => { await placeOrder() .catch((err) => { setErrorMessage(err.message) }) .finally(() => { setSubmitting(false) }) } const handlePayment = () => { setSubmitting(true) onPaymentCompleted() } return ( <> ) } export default PaymentButton