Skip to content

Commit

Permalink
Update example
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-bach committed Mar 31, 2024
1 parent 016fcfa commit fcc9937
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 29 deletions.
4 changes: 2 additions & 2 deletions example/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import StripeHandling

main :: IO ()
main = do
result <- getCheckoutSessionEvents
either (putStrLn . T.unpack) (mapM_ (putStrLn . T.unpack)) result
result <- getCheckoutSessionId
putStrLn result
32 changes: 5 additions & 27 deletions example/src/StripeHandling.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import qualified Data.Maybe as Maybe
import qualified Data.Text as T
import qualified Network.HTTP.Simple as HS
import qualified StripeAPI as Stripe
import qualified StripeAPI.Types.NotificationEventData.Extra as Stripe

stripeAPIKey :: T.Text
stripeAPIKey = "sk_test_XXXXXXXXXX" -- Insert your API key here
Expand Down Expand Up @@ -41,11 +40,10 @@ checkoutLineItem2 =
}

checkoutSession =
( Stripe.mkPostCheckoutSessionsRequestBody
"https://localhost:8080/payments/index.html?success=false&sessionId={CHECKOUT_SESSION_ID}"
"https://localhost:8080/payments/index.html?success=true&sessionId={CHECKOUT_SESSION_ID}"
)
{ Stripe.postCheckoutSessionsRequestBodyLineItems = Just [checkoutLineItem, checkoutLineItem2],
Stripe.mkPostCheckoutSessionsRequestBody
{ Stripe.postCheckoutSessionsRequestBodySuccessUrl = Just "https://localhost:8080/payments/index.html?success=false&sessionId={CHECKOUT_SESSION_ID}",
Stripe.postCheckoutSessionsRequestBodyCancelUrl = Just "https://localhost:8080/payments/index.html?success=true&sessionId={CHECKOUT_SESSION_ID}",
Stripe.postCheckoutSessionsRequestBodyLineItems = Just [checkoutLineItem, checkoutLineItem2],
Stripe.postCheckoutSessionsRequestBodyPaymentMethodTypes = Just [Stripe.PostCheckoutSessionsRequestBodyPaymentMethodTypes'EnumCard]
}

Expand All @@ -68,7 +66,7 @@ conf = defaultConf {Stripe.configSecurityScheme = config}
getCheckoutSessionId :: IO String
getCheckoutSessionId = do
putStrLn "getCheckoutSessionId"
resp <- Stripe.runWithConfiguration conf $ Stripe.postCheckoutSessions checkoutSession
resp <- Stripe.runWithConfiguration conf $ Stripe.postCheckoutSessions $ Just checkoutSession
print resp
pure $
T.unpack $ case HS.getResponseBody resp of
Expand Down Expand Up @@ -149,23 +147,3 @@ getPaymentIntentSepaCallSecret =
MIO.liftIO $ print resp
pure $ trans resp
_ -> pure "response was not a success"

getCheckoutSessionEvents :: IO (Either T.Text [T.Text])
getCheckoutSessionEvents = Stripe.runWithConfiguration conf $ do
res <-
Stripe.getEvents
Stripe.mkGetEventsParameters
{ Stripe.getEventsParametersQueryType = Just "checkout.session.completed"
}
pure $ case HS.getResponseBody res of
Stripe.GetEventsResponse200 eventResponse ->
Right $
( \case
Stripe.CheckoutSessionCompletedEvent session -> Stripe.checkout'sessionId session
Stripe.UnknownEvent t -> "Unknown event type: " <> t
_ -> "Other event"
)
. Stripe.getEventData
<$> Stripe.getEventsResponseBody200Data eventResponse
Stripe.GetEventsResponseError err -> Left $ T.pack err
Stripe.GetEventsResponseDefault err -> Left $ Maybe.fromMaybe "" $ Stripe.apiErrorsMessage $ Stripe.errorError err

0 comments on commit fcc9937

Please sign in to comment.