feat: Implement default payment method for pickup and enhance payment method handling in CartTab and PaymentMethodSelector components
This commit is contained in:
@@ -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(
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"],
|
||||
},
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user