From 1840512aac0d70e510bc6c1610e17fa7236b8882 Mon Sep 17 00:00:00 2001 From: sebseb7 Date: Tue, 24 Mar 2026 06:51:47 +0100 Subject: [PATCH] feat: Implement default payment method for pickup and enhance payment method handling in CartTab and PaymentMethodSelector components --- src/components/profile/CartTab.js | 9 ++++++ src/components/profile/CheckoutValidation.js | 14 +++++++--- .../profile/PaymentMethodSelector.js | 28 ++++++++++++++++--- 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/components/profile/CartTab.js b/src/components/profile/CartTab.js index 3d65616..5f26942 100644 --- a/src/components/profile/CartTab.js +++ b/src/components/profile/CartTab.js @@ -223,6 +223,15 @@ class CartTab extends Component { handleDeliveryMethodChange = (event) => { const newDeliveryMethod = this.normalizeDeliveryMethod(event.target.value); const deliveryCost = this.orderService.getDeliveryCost(); + + // Pickup should default to in-store payment. + if (newDeliveryMethod === "Abholung") { + this.setState({ + deliveryMethod: newDeliveryMethod, + paymentMethod: "cash", + }); + return; + } // Get optimal payment method for the new delivery method const optimalPaymentMethod = CheckoutValidation.getOptimalPaymentMethod( diff --git a/src/components/profile/CheckoutValidation.js b/src/components/profile/CheckoutValidation.js index f7b5d7b..3051a98 100644 --- a/src/components/profile/CheckoutValidation.js +++ b/src/components/profile/CheckoutValidation.js @@ -80,8 +80,13 @@ class CheckoutValidation { return "wire"; } - // Prefer stripe when available and meets minimum amount - if (deliveryMethod === "DHL" || deliveryMethod === "DPD" || deliveryMethod === "Abholung") { + // Pickup defaults to in-store payment + if (deliveryMethod === "Abholung") { + return "cash"; + } + + // Prefer wire for shippable delivery methods + if (deliveryMethod === "DHL" || deliveryMethod === "DPD") { return "wire";/*stripe*/ } @@ -96,9 +101,10 @@ class CheckoutValidation { const subtotal = cartItems.reduce((total, item) => total + item.price * item.quantity, 0); const totalAmount = subtotal + deliveryCost; - // Reset payment method if it's no longer valid + // Reset "Nachnahme" when delivery is not DHL. + // For pickup, default to in-store payment. if (deliveryMethod !== "DHL" && paymentMethod === "onDelivery") { - newPaymentMethod = "wire"; + newPaymentMethod = deliveryMethod === "Abholung" ? "cash" : "wire"; } // Allow stripe for DHL, DPD, and Abholung delivery methods, but check minimum amount diff --git a/src/components/profile/PaymentMethodSelector.js b/src/components/profile/PaymentMethodSelector.js index 464b698..6aa3051 100644 --- a/src/components/profile/PaymentMethodSelector.js +++ b/src/components/profile/PaymentMethodSelector.js @@ -1,4 +1,4 @@ -import React, { useEffect, useCallback } from "react"; +import React, { useEffect, useCallback, useRef } from "react"; import { Box, Typography, Radio } from "@mui/material"; import { withI18n } from "../../i18n/withTranslation.js"; @@ -7,6 +7,7 @@ const PaymentMethodSelector = ({ paymentMethod, onChange, deliveryMethod, onDeli // Calculate total amount const subtotal = cartItems.reduce((total, item) => total + item.price * item.quantity, 0); const totalAmount = subtotal + deliveryCost; + const previousDeliveryMethodRef = useRef(deliveryMethod); // Handle payment method changes with automatic delivery method adjustment const handlePaymentMethodChange = useCallback((event) => { @@ -18,15 +19,34 @@ const PaymentMethodSelector = ({ paymentMethod, onChange, deliveryMethod, onDeli onDeliveryMethodChange({ target: { value: "Abholung" } }); } } + + // If "Nachnahme" is selected, force delivery method to "DHL" + if (selectedPaymentMethod === "onDelivery" && deliveryMethod !== "DHL") { + if (onDeliveryMethodChange) { + onDeliveryMethodChange({ target: { value: "DHL" } }); + } + } onChange(event); }, [deliveryMethod, onDeliveryMethodChange, onChange]); - // Handle delivery method changes - auto-switch to stripe when DHL/DPD is selected + // Handle delivery method changes - auto-switch to wire when DHL/DPD is selected useEffect(() => { - if ((deliveryMethod === "DHL" || deliveryMethod === "DPD") && paymentMethod === "cash") { + const previousDeliveryMethod = previousDeliveryMethodRef.current; + const deliveryMethodChanged = previousDeliveryMethod !== deliveryMethod; + + if (deliveryMethodChanged && (deliveryMethod === "DHL" || deliveryMethod === "DPD") && paymentMethod === "cash") { handlePaymentMethodChange({ target: { value: "wire" /*stripe*/ } }); } + + previousDeliveryMethodRef.current = deliveryMethod; + }, [deliveryMethod, paymentMethod, handlePaymentMethodChange]); + + // If switching to pickup while "Nachnahme" is selected, default to in-store payment + useEffect(() => { + if (deliveryMethod === "Abholung" && paymentMethod === "onDelivery") { + handlePaymentMethodChange({ target: { value: "cash" } }); + } }, [deliveryMethod, paymentMethod, handlePaymentMethodChange]); // Auto-switch to cash when total amount is 0 @@ -75,7 +95,7 @@ const PaymentMethodSelector = ({ paymentMethod, onChange, deliveryMethod, onDeli id: "onDelivery", name: t ? t('payment.methods.cashOnDelivery') : "Nachnahme", description: t ? t('payment.methods.cashOnDeliveryDescription') : "Bezahlen Sie bei Lieferung (8,99 € Aufschlag)", - disabled: totalAmount === 0 || deliveryMethod !== "DHL", + disabled: totalAmount === 0, icons: ["/assets/images/cash.png"], }, {