This repository has been archived by the owner on Nov 18, 2017. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Stripe.hs
63 lines (50 loc) · 1.91 KB
/
Stripe.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
module Stripe
( runStripe
, subscribeToPlan
, cancelSubscription
) where
import Import
import Control.Monad.Except (ExceptT, runExceptT, throwError)
import Web.Stripe ((-&-))
import qualified Web.Stripe as S
import qualified Web.Stripe.Customer as S
import qualified Web.Stripe.Error as S
import qualified Web.Stripe.Subscription as S
type Stripe = ReaderT S.StripeConfig (ExceptT S.StripeError IO)
runStripe :: Stripe a -> Handler (Maybe a)
runStripe f = do
stripeKey <- stripeKeysSecretKey . appStripeKeys <$> getYesod
result <- liftIO $ runExceptT $ runReaderT f $ S.StripeConfig $ S.StripeKey $ encodeUtf8 stripeKey
case result of
Left err -> do
$(logError) $ pack $ show err
return Nothing
Right x -> return $ Just x
subscribeToPlan :: S.TokenId
-> S.Email
-> S.PlanId
-> Maybe S.CustomerId
-> Stripe S.CustomerId
subscribeToPlan token email planId mstripeId = do
stripeId <- findOrCreateCustomer token email mstripeId
void $ request $ S.createSubscription stripeId planId
return stripeId
cancelSubscription :: Maybe S.CustomerId -> Stripe ()
cancelSubscription Nothing = return ()
cancelSubscription (Just stripeId) = do
subs <- S.list <$> (request $ S.getSubscriptions stripeId)
forM_ subs $ \sub ->
void $ request $ S.cancelSubscription stripeId (S.subscriptionId sub)
-&- S.AtPeriodEnd False
findOrCreateCustomer :: S.TokenId -> S.Email -> Maybe S.CustomerId -> Stripe S.CustomerId
findOrCreateCustomer _ _ (Just stripeId) = return stripeId
findOrCreateCustomer token email _ =
S.customerId <$> request (S.createCustomer -&- token -&- email)
request
:: FromJSON (S.StripeReturn a)
=> S.StripeRequest a
-> Stripe (S.StripeReturn a)
request f = do
config <- ask
result <- liftIO $ S.stripe config f
either throwError return result