feat: Implement default payment method for pickup and enhance payment method handling in CartTab and PaymentMethodSelector components

This commit is contained in:
sebseb7
2026-03-24 06:51:47 +01:00
parent e6f3fb7c18
commit 1840512aac
3 changed files with 43 additions and 8 deletions

View File

@@ -223,6 +223,15 @@ class CartTab extends Component {
handleDeliveryMethodChange = (event) => { handleDeliveryMethodChange = (event) => {
const newDeliveryMethod = this.normalizeDeliveryMethod(event.target.value); const newDeliveryMethod = this.normalizeDeliveryMethod(event.target.value);
const deliveryCost = this.orderService.getDeliveryCost(); 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 // Get optimal payment method for the new delivery method
const optimalPaymentMethod = CheckoutValidation.getOptimalPaymentMethod( const optimalPaymentMethod = CheckoutValidation.getOptimalPaymentMethod(

View File

@@ -80,8 +80,13 @@ class CheckoutValidation {
return "wire"; return "wire";
} }
// Prefer stripe when available and meets minimum amount // Pickup defaults to in-store payment
if (deliveryMethod === "DHL" || deliveryMethod === "DPD" || deliveryMethod === "Abholung") { if (deliveryMethod === "Abholung") {
return "cash";
}
// Prefer wire for shippable delivery methods
if (deliveryMethod === "DHL" || deliveryMethod === "DPD") {
return "wire";/*stripe*/ return "wire";/*stripe*/
} }
@@ -96,9 +101,10 @@ class CheckoutValidation {
const subtotal = cartItems.reduce((total, item) => total + item.price * item.quantity, 0); const subtotal = cartItems.reduce((total, item) => total + item.price * item.quantity, 0);
const totalAmount = subtotal + deliveryCost; 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") { 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 // Allow stripe for DHL, DPD, and Abholung delivery methods, but check minimum amount

View File

@@ -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 { Box, Typography, Radio } from "@mui/material";
import { withI18n } from "../../i18n/withTranslation.js"; import { withI18n } from "../../i18n/withTranslation.js";
@@ -7,6 +7,7 @@ const PaymentMethodSelector = ({ paymentMethod, onChange, deliveryMethod, onDeli
// Calculate total amount // Calculate total amount
const subtotal = cartItems.reduce((total, item) => total + item.price * item.quantity, 0); const subtotal = cartItems.reduce((total, item) => total + item.price * item.quantity, 0);
const totalAmount = subtotal + deliveryCost; const totalAmount = subtotal + deliveryCost;
const previousDeliveryMethodRef = useRef(deliveryMethod);
// Handle payment method changes with automatic delivery method adjustment // Handle payment method changes with automatic delivery method adjustment
const handlePaymentMethodChange = useCallback((event) => { const handlePaymentMethodChange = useCallback((event) => {
@@ -18,15 +19,34 @@ const PaymentMethodSelector = ({ paymentMethod, onChange, deliveryMethod, onDeli
onDeliveryMethodChange({ target: { value: "Abholung" } }); 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); onChange(event);
}, [deliveryMethod, onDeliveryMethodChange, onChange]); }, [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(() => { 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*/ } }); 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]); }, [deliveryMethod, paymentMethod, handlePaymentMethodChange]);
// Auto-switch to cash when total amount is 0 // Auto-switch to cash when total amount is 0
@@ -75,7 +95,7 @@ const PaymentMethodSelector = ({ paymentMethod, onChange, deliveryMethod, onDeli
id: "onDelivery", id: "onDelivery",
name: t ? t('payment.methods.cashOnDelivery') : "Nachnahme", name: t ? t('payment.methods.cashOnDelivery') : "Nachnahme",
description: t ? t('payment.methods.cashOnDeliveryDescription') : "Bezahlen Sie bei Lieferung (8,99 € Aufschlag)", 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"], icons: ["/assets/images/cash.png"],
}, },
{ {