Skip to content

Commit 4554978

Browse files
committed
get /donate endpoint compiling
1 parent 48b6b53 commit 4554978

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

worker/elm.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Executable worker
5252
Artifacts
5353
Cors
5454
Endpoint.Compile
55+
Endpoint.Donate
5556
Endpoint.Repl
5657

5758
AST.Canonical

worker/src/Endpoint/Donate.hs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE OverloadedStrings #-}
12
module Endpoint.Donate
23
( Manager
34
, endpoint
@@ -7,10 +8,16 @@ module Endpoint.Donate
78

89

910
import qualified Control.Exception as E
11+
import Control.Monad.Trans (liftIO)
12+
import Data.Aeson ((.:))
13+
import qualified Data.Aeson as Json
1014
import qualified Data.ByteString as BS
15+
import qualified Data.ByteString.Builder as B
16+
import qualified Data.ByteString.Char8 as BSC
1117
import qualified Data.ByteString.Lazy as LBS
18+
import qualified Data.Text as T
1219
import Snap.Core
13-
import qualified Network.HTTP.Client as Http (parseRequest)
20+
import qualified Network.HTTP.Client as Http
1421
import qualified Network.HTTP.Client.TLS as Http (tlsManagerSettings)
1522
import qualified Network.HTTP.Types.Header as Http (Header, hAccept, hAcceptEncoding, hUserAgent)
1623
import qualified Network.HTTP.Types.Method as Http (methodPost)
@@ -54,11 +61,16 @@ endpoint (Manager manager) =
5461
frequency <- requireParameter "frequency" toFrequency
5562
mabyeSession <- liftIO $ getStripeCheckoutSessionID manager amount
5663
case mabyeSession of
57-
Nothing ->
58-
5964
Just (StripeCheckoutSession id) ->
60-
do modifyResponse $ setContentType "text/plain"
61-
writeBS id
65+
do modifyResponse $ setContentType "text/plain; charset=utf-8"
66+
writeText id
67+
68+
Nothing ->
69+
do writeBuilder $ "Problem creating Stripe session ID for checkout."
70+
finishWith
71+
. setResponseStatus 500 "Internal Server Error"
72+
. setContentType "text/plain; charset=utf-8"
73+
=<< getResponse
6274

6375

6476
data Frequency
@@ -87,16 +99,16 @@ toAmount bytes =
8799

88100

89101
newtype StripeCheckoutSession =
90-
StripeCheckoutSession { _id :: BS.ByteString }
102+
StripeCheckoutSession { _id :: T.Text }
91103

92104

93105
getStripeCheckoutSessionID :: Http.Manager -> Int -> IO (Maybe StripeCheckoutSession)
94106
getStripeCheckoutSessionID manager amount =
95107
E.handle handleSomeException $
96108
do req0 <- Http.parseRequest "https://api.stripe.com/v1/checkout/sessions"
97-
req1 <- Multi.formDataBody (toOneTimeParts amount) $ req0 { method = Http.methodPost }
109+
req1 <- Multi.formDataBody (toOneTimeParts amount) $ req0 { Http.method = Http.methodPost }
98110
Http.withResponse req1 manager $ \response ->
99-
do chunks <- brConsume (responseBody response)
111+
do chunks <- Http.brConsume (Http.responseBody response)
100112
return $ Json.decode $ LBS.fromChunks chunks
101113

102114

@@ -106,7 +118,7 @@ toOneTimeParts amount =
106118
, Multi.partBS "line_items[][name]" "One-time donation"
107119
, Multi.partBS "line_items[][description]" "One-time donation to Elm Software Foundation"
108120
, Multi.partBS "line_items[][images][]" "https://foundation.elm-lang.org/donation.png"
109-
, Multi.partBS "line_items[][amount]" (BS.pack (show amount))
121+
, Multi.partBS "line_items[][amount]" (BSC.pack (show amount))
110122
, Multi.partBS "line_items[][currency]" "usd"
111123
, Multi.partBS "line_items[][quantity]" "1"
112124
, Multi.partBS "success_url" "https://foundation.elm-lang.org/thank_you?session_id={CHECKOUT_SESSION_ID}"
@@ -121,7 +133,7 @@ handleSomeException exception =
121133

122134
instance Json.FromJSON StripeCheckoutSession where
123135
parseJSON =
124-
withObject "StripeCheckoutSessionResponse" $ \obj ->
136+
Json.withObject "StripeCheckoutSessionResponse" $ \obj ->
125137
StripeCheckoutSession <$> obj .: "id"
126138

127139

@@ -144,7 +156,8 @@ requireParameter name toValue =
144156

145157
bailForMissingParam :: BS.ByteString -> Snap a
146158
bailForMissingParam name =
147-
do modifyResponse $ setResponseStatus 400 "Bad Request"
148-
modifyResponse $ setContentType "text/plain; charset=utf-8"
149-
writeBuilder $ "Missing parameter '" <> B.fromByteString name <> "' in requset."
150-
finishWith =<< getResponse
159+
do writeBuilder $ "Missing parameter '" <> B.byteString name <> "' in requset."
160+
finishWith
161+
. setResponseStatus 400 "Bad Request"
162+
. setContentType "text/plain; charset=utf-8"
163+
=<< getResponse

worker/src/Main.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Snap.Http.Server
1414
import qualified Artifacts
1515
import qualified Cors
1616
import qualified Endpoint.Compile as Compile
17+
import qualified Endpoint.Donate as Donate
1718
import qualified Endpoint.Repl as Repl
1819

1920

0 commit comments

Comments
 (0)