160 lines
5.8 KiB
JavaScript
160 lines
5.8 KiB
JavaScript
class CheckoutValidation {
|
|
static validateAddressForm(state, t = null) {
|
|
const {
|
|
invoiceAddress,
|
|
deliveryAddress,
|
|
useSameAddress,
|
|
deliveryMethod,
|
|
paymentMethod,
|
|
} = state;
|
|
const errors = {};
|
|
|
|
// Validate invoice address (skip if payment method is "cash")
|
|
if (paymentMethod !== "cash") {
|
|
if (!invoiceAddress.firstName)
|
|
errors.invoiceFirstName = t ? t('checkout.validationErrors.firstNameRequired') : "Vorname erforderlich";
|
|
if (!invoiceAddress.lastName)
|
|
errors.invoiceLastName = t ? t('checkout.validationErrors.lastNameRequired') : "Nachname erforderlich";
|
|
if (!invoiceAddress.street) errors.invoiceStreet = t ? t('checkout.validationErrors.streetRequired') : "Straße erforderlich";
|
|
if (!invoiceAddress.houseNumber)
|
|
errors.invoiceHouseNumber = t ? t('checkout.validationErrors.houseNumberRequired') : "Hausnummer erforderlich";
|
|
if (!invoiceAddress.postalCode)
|
|
errors.invoicePostalCode = t ? t('checkout.validationErrors.postalCodeRequired') : "PLZ erforderlich";
|
|
if (!invoiceAddress.city) errors.invoiceCity = t ? t('checkout.validationErrors.cityRequired') : "Stadt erforderlich";
|
|
}
|
|
|
|
// Validate delivery address for shipping methods that require it
|
|
if (
|
|
!useSameAddress &&
|
|
(deliveryMethod === "DHL" || deliveryMethod === "DPD")
|
|
) {
|
|
if (!deliveryAddress.firstName)
|
|
errors.deliveryFirstName = t ? t('checkout.validationErrors.firstNameRequired') : "Vorname erforderlich";
|
|
if (!deliveryAddress.lastName)
|
|
errors.deliveryLastName = t ? t('checkout.validationErrors.lastNameRequired') : "Nachname erforderlich";
|
|
if (!deliveryAddress.street)
|
|
errors.deliveryStreet = t ? t('checkout.validationErrors.streetRequired') : "Straße erforderlich";
|
|
if (!deliveryAddress.houseNumber)
|
|
errors.deliveryHouseNumber = t ? t('checkout.validationErrors.houseNumberRequired') : "Hausnummer erforderlich";
|
|
if (!deliveryAddress.postalCode)
|
|
errors.deliveryPostalCode = t ? t('checkout.validationErrors.postalCodeRequired') : "PLZ erforderlich";
|
|
if (!deliveryAddress.city) errors.deliveryCity = t ? t('checkout.validationErrors.cityRequired') : "Stadt erforderlich";
|
|
}
|
|
|
|
return errors;
|
|
}
|
|
|
|
static getValidationErrorMessage(state, isAddressOnly = false, t = null) {
|
|
const { termsAccepted } = state;
|
|
|
|
const addressErrors = this.validateAddressForm(state, t);
|
|
|
|
if (isAddressOnly) {
|
|
return addressErrors;
|
|
}
|
|
|
|
if (Object.keys(addressErrors).length > 0) {
|
|
return t ? t('checkout.addressValidationError') : "Bitte überprüfen Sie Ihre Eingaben in den Adressfeldern.";
|
|
}
|
|
|
|
// Validate terms acceptance
|
|
if (!termsAccepted) {
|
|
return t ? t('checkout.termsValidationError') : "Bitte akzeptieren Sie die AGBs, Datenschutzerklärung und Widerrufsrecht, um fortzufahren.";
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
static getOptimalPaymentMethod(deliveryMethod, cartItems = [], deliveryCost = 0) {
|
|
// Calculate total amount
|
|
const subtotal = cartItems.reduce((total, item) => total + item.price * item.quantity, 0);
|
|
const totalAmount = subtotal + deliveryCost;
|
|
|
|
// If total is 0, only cash is allowed
|
|
if (totalAmount === 0) {
|
|
return "cash";
|
|
}
|
|
|
|
// If total is less than 0.50€, stripe is not available
|
|
if (totalAmount < 0.50) {
|
|
return "wire";
|
|
}
|
|
|
|
// Prefer stripe when available and meets minimum amount
|
|
if (deliveryMethod === "DHL" || deliveryMethod === "DPD" || deliveryMethod === "Abholung") {
|
|
return "wire";/*stripe*/
|
|
}
|
|
|
|
// Fall back to wire transfer
|
|
return "wire";
|
|
}
|
|
|
|
static validatePaymentMethodForDelivery(deliveryMethod, paymentMethod, cartItems = [], deliveryCost = 0) {
|
|
let newPaymentMethod = paymentMethod;
|
|
|
|
// Calculate total amount for minimum validation
|
|
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
|
|
if (deliveryMethod !== "DHL" && paymentMethod === "onDelivery") {
|
|
newPaymentMethod = "wire";
|
|
}
|
|
|
|
// Allow stripe for DHL, DPD, and Abholung delivery methods, but check minimum amount
|
|
if (deliveryMethod !== "DHL" && deliveryMethod !== "DPD" && deliveryMethod !== "Abholung" && paymentMethod === "stripe") {
|
|
newPaymentMethod = "wire";
|
|
}
|
|
|
|
// Allow mollie for DHL, DPD, and Abholung delivery methods, but check minimum amount
|
|
if (deliveryMethod !== "DHL" && deliveryMethod !== "DPD" && deliveryMethod !== "Abholung" && paymentMethod === "mollie") {
|
|
newPaymentMethod = "wire";
|
|
}
|
|
|
|
// Check minimum amount for stripe payments
|
|
if (paymentMethod === "stripe" && totalAmount < 0.50) {
|
|
newPaymentMethod = "wire";
|
|
}
|
|
|
|
// Check minimum amount for mollie payments
|
|
if (paymentMethod === "mollie" && totalAmount < 0.50) {
|
|
newPaymentMethod = "wire";
|
|
}
|
|
|
|
if (deliveryMethod !== "Abholung" && paymentMethod === "cash") {
|
|
newPaymentMethod = "wire";
|
|
}
|
|
|
|
return newPaymentMethod;
|
|
}
|
|
|
|
static shouldForcePickupDelivery(cartItems) {
|
|
const isPickupOnly = cartItems.some(
|
|
(item) => item.versandklasse === "nur Abholung"
|
|
);
|
|
const hasStecklinge = cartItems.some(
|
|
(item) =>
|
|
item.id &&
|
|
typeof item.id === "string" &&
|
|
item.id.endsWith("steckling")
|
|
);
|
|
|
|
return isPickupOnly || hasStecklinge;
|
|
}
|
|
|
|
static getCartItemFlags(cartItems) {
|
|
const isPickupOnly = cartItems.some(
|
|
(item) => item.versandklasse === "nur Abholung"
|
|
);
|
|
const hasStecklinge = cartItems.some(
|
|
(item) =>
|
|
item.id &&
|
|
typeof item.id === "string" &&
|
|
item.id.endsWith("steckling")
|
|
);
|
|
|
|
return { isPickupOnly, hasStecklinge };
|
|
}
|
|
}
|
|
|
|
export default CheckoutValidation;
|