@@ -62,7 +62,7 @@ import Routes.AvalaraUtils (createAvalaraTransaction, createAvalaraCustomer)
6262import Routes.Utils (getDisabledCheckoutDetails , getClientIP , generateUniqueToken )
6363import Routes.Carts (ValidateCartParameters (.. ))
6464import Routes.Customers (RegistrationParametersWith (.. ), registerNewCustomer )
65- import Validation (Validation (.. ))
65+ import Validation (Validation (.. ), singleFieldError )
6666import Workers (Task (.. ), AvalaraTask (.. ), enqueueTask )
6767
6868import qualified Avalara
@@ -1508,10 +1508,12 @@ getCheckoutTokenRoute token CheckoutTokenParameters {..} = withValidatedCookie t
15081508 (Entity cartId _) <- getBy (UniqueCustomerCart $ Just customerId)
15091509 >>= maybe (throwIO CartNotFound ) return
15101510 checkCartAndShippingAddress ctpCartInventoryNotifications cartId ctpShippingAddress ctpSkipShippingAddressVerification $
1511- lift $ getCheckoutToken (customerHelcimCustomerId customer)
1511+ lift $ getCheckoutToken $
1512+ maybe (Left $ customerEmail customer) Right (customerHelcimCustomerId customer)
15121513
15131514data AnonymousCheckoutTokenParameters = AnonymousCheckoutTokenParameters
15141515 { actpCartToken :: T. Text
1516+ , actpEmail :: T. Text
15151517 , actpCartInventoryNotifications :: CartInventoryNotifications
15161518 , actpShippingAddress :: AddressData
15171519 , actpSkipShippingAddressVerification :: Bool
@@ -1521,6 +1523,7 @@ instance FromJSON AnonymousCheckoutTokenParameters where
15211523 parseJSON = withObject " AnonymousCheckoutTokenParameters" $ \ v ->
15221524 AnonymousCheckoutTokenParameters
15231525 <$> v .: " sessionToken"
1526+ <*> v .: " email"
15241527 <*> v .: " cartInventoryNotifications"
15251528 <*> v .: " shippingAddress"
15261529 <*> v .: " skipAddressVerification"
@@ -1531,17 +1534,27 @@ type AnonymousCheckoutTokenRoute =
15311534
15321535anonymousGetCheckoutTokenRoute :: AnonymousCheckoutTokenParameters -> App (CheckoutResult CheckoutTokenData )
15331536anonymousGetCheckoutTokenRoute AnonymousCheckoutTokenParameters {.. } = runDB $ do
1537+ customerExists <- lift $ V. uniqueCustomer actpEmail
1538+ when customerExists $
1539+ lift $ singleFieldError " email" " Email is already associated with an existing account. Log in or use a different email."
15341540 (Entity cartId _) <- getBy (UniqueAnonymousCart $ Just actpCartToken)
15351541 >>= maybe (throwIO CartNotFound ) return
15361542 checkCartAndShippingAddress actpCartInventoryNotifications cartId (NewAddress actpShippingAddress) actpSkipShippingAddressVerification $
1537- lift $ getCheckoutToken Nothing
1543+ lift $ getCheckoutToken ( Left actpEmail)
15381544
1539- getCheckoutToken :: Maybe Helcim. CustomerId -> App CheckoutTokenData
1540- getCheckoutToken mbHelcimCustomerId = do
1541- mbCustomerCode <- case mbHelcimCustomerId of
1542- Just helcimCustomerId -> do
1545+ getCheckoutToken :: Either T. Text Helcim. CustomerId -> App CheckoutTokenData
1546+ getCheckoutToken emailOrHelcimCustomerId = do
1547+ mbCustomerCode <- case emailOrHelcimCustomerId of
1548+ Right helcimCustomerId -> do
15431549 getCustomer helcimCustomerId >>= either (throwIO . HelcimError ) (return . Just . creCustomerCode)
1544- Nothing -> return Nothing
1550+ Left email -> do
1551+ getCustomers GetCustomersRequest
1552+ { gcrSearch = Just email
1553+ , gcrCustomerCode = Nothing
1554+ , gcrLimit = Just 1
1555+ , gcrPage = Just 1
1556+ , gcrIncludeCards = Nothing
1557+ } >>= either (throwIO . HelcimError ) (return . fmap creCustomerCode . listToMaybe)
15451558 createVerifyCheckout mbCustomerCode >>= \ case
15461559 Left e -> throwIO $ HelcimError e
15471560 Right CheckoutCreateResponse {.. } ->
0 commit comments