Skip to content

Commit 77253a7

Browse files
committed
Add customer validation endpoint for stripe
Problem: there is no way to validate customer's email address, shipping address before we create stipe purchase Solution: introduce a new endpoint for validating customer details that would be called by the front end
1 parent 04cedf9 commit 77253a7

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

server/src/Routes/Checkout.hs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ type CheckoutAPI =
9191
:<|> "anonymous-success" :> AnonymousSuccessRoute
9292
-- :<|> "helcim-checkout-token" :> CheckoutTokenRoute
9393
-- :<|> "anonymous-helcim-checkout-token" :> AnonymousCheckoutTokenRoute
94+
:<|> "validate-anonymous-customer-details" :> AnonymousCustomerValidationRoute
95+
:<|> "validate-customer-details" :> CustomerValidationRoute
9496

9597
type CheckoutRoutes = ServerT CheckoutAPI App
9698

@@ -104,6 +106,8 @@ checkoutRoutes =
104106
:<|> anonymousSuccessRoute
105107
-- :<|> getCheckoutTokenRoute
106108
-- :<|> anonymousGetCheckoutTokenRoute
109+
:<|> anonymousCustomerValidationRoute
110+
:<|> customerValidationRoute
107111

108112

109113
-- COUPON ERRORS
@@ -1078,6 +1082,62 @@ getFirstStripeCard stripeCustomerId =
10781082
(return . fmap Stripe.cardId . listToMaybe . Stripe.list)
10791083
>>= maybe (throwIO CardChargeError) return
10801084

1085+
data AnonymousCustomerValidationParameters = AnonymousCustomerValidationParameters
1086+
{ acvpCartToken :: T.Text
1087+
, acvpEmail :: T.Text
1088+
, acvpCartInventoryNotifications :: CartInventoryNotifications
1089+
, acvpShippingAddress :: AddressData
1090+
, acvpSkipShippingAddressVerification :: Bool
1091+
}
1092+
1093+
instance FromJSON AnonymousCustomerValidationParameters where
1094+
parseJSON = withObject "AnonymousCustomerValidationParameters" $ \v ->
1095+
AnonymousCustomerValidationParameters
1096+
<$> v .: "sessionToken"
1097+
<*> v .: "email"
1098+
<*> v .: "cartInventoryNotifications"
1099+
<*> v .: "shippingAddress"
1100+
<*> v .: "skipAddressVerification"
1101+
1102+
type AnonymousCustomerValidationRoute =
1103+
ReqBody '[JSON] AnonymousCustomerValidationParameters :>
1104+
Post '[JSON] (CheckoutResult ())
1105+
1106+
anonymousCustomerValidationRoute :: AnonymousCustomerValidationParameters -> App (CheckoutResult ())
1107+
anonymousCustomerValidationRoute AnonymousCustomerValidationParameters {..} = runDB $ do
1108+
customerExists <- lift $ V.uniqueCustomer acvpEmail
1109+
when customerExists $
1110+
lift $ singleFieldError "email" "Email is already associated with an existing account. Log in or use a different email."
1111+
(Entity cartId _) <- getBy (UniqueAnonymousCart $ Just acvpCartToken)
1112+
>>= maybe (throwIO CartNotFound) return
1113+
checkCartAndShippingAddress acvpCartInventoryNotifications cartId (NewAddress acvpShippingAddress) acvpSkipShippingAddressVerification $
1114+
pure ()
1115+
1116+
data CustomerValidationParameters = CustomerValidationParameters
1117+
{ cvpCartInventoryNotifications :: CartInventoryNotifications
1118+
, cvpShippingAddress :: CustomerAddress
1119+
, cvpSkipShippingAddressVerification :: Bool
1120+
}
1121+
1122+
instance FromJSON CustomerValidationParameters where
1123+
parseJSON = withObject "CustomerValidationParameters" $ \v ->
1124+
CustomerValidationParameters
1125+
<$> v .: "cartInventoryNotifications"
1126+
<*> v .: "shippingAddress"
1127+
<*> v .: "skipAddressVerification"
1128+
1129+
type CustomerValidationRoute =
1130+
AuthProtect "cookie-auth" :>
1131+
ReqBody '[JSON] CustomerValidationParameters :>
1132+
Post '[JSON] (Cookied (CheckoutResult ()))
1133+
1134+
customerValidationRoute :: WrappedAuthToken -> CustomerValidationParameters -> App (Cookied (CheckoutResult ()))
1135+
customerValidationRoute token CustomerValidationParameters {..} = withValidatedCookie token $ \(Entity customerId _customer) -> runDB $ do
1136+
(Entity cartId _) <- getBy (UniqueCustomerCart $ Just customerId)
1137+
>>= maybe (throwIO CartNotFound) return
1138+
checkCartAndShippingAddress cvpCartInventoryNotifications cartId cvpShippingAddress cvpSkipShippingAddressVerification $
1139+
pure ()
1140+
10811141
-- -- | Get a Helcim Customer by their CustomerCode.
10821142
-- -- And update their billing & shipping addresses with the current order's values. Map the Helcim Customer id
10831143
-- -- with the Customer in the database.

0 commit comments

Comments
 (0)