Skip to content

Commit a3137b8

Browse files
committed
stripe checkout session status
1 parent cfd60f2 commit a3137b8

File tree

4 files changed

+113
-61
lines changed

4 files changed

+113
-61
lines changed

nix/tools.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,5 @@ in [
6868
styleTest
6969
prettier
7070
pkgs.alejandra
71+
pkgs.haskellPackages.cpphs
7172
]

pub/stripe-hs/src/Stripe/Client.hs

Lines changed: 95 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,104 @@
1-
{-# OPTIONS_GHC -cpp -pgmPcpphs -optP--cpp #-}
21
{-# LANGUAGE CPP #-}
2+
{-# OPTIONS_GHC -cpp -pgmPcpphs -optP--cpp #-}
3+
34
module Stripe.Client
45
( -- * Basics
5-
ApiKey, StripeClient, makeStripeClient, ClientError(..)
6+
ApiKey,
7+
StripeClient,
8+
makeStripeClient,
9+
ClientError (..),
10+
611
-- * Helper types
7-
, TimeStamp(..), StripeList(..)
12+
TimeStamp (..),
13+
StripeList (..),
14+
815
-- * Customers
9-
, createCustomer, retrieveCustomer, updateCustomer, listCustomers
10-
, CustomerId(..), Customer(..), CustomerCreate(..), CustomerUpdate(..)
16+
createCustomer,
17+
retrieveCustomer,
18+
updateCustomer,
19+
listCustomers,
20+
CustomerId (..),
21+
Customer (..),
22+
CustomerCreate (..),
23+
CustomerUpdate (..),
24+
1125
-- * Product catalog
12-
, ProductId(..), PriceId(..), Product(..), Price(..), PriceRecurring(..)
13-
, ProductCreate(..), PriceCreate(..), PriceCreateRecurring(..)
14-
, createProduct, retrieveProduct
15-
, createPrice, retrievePrice, listPrices
26+
ProductId (..),
27+
PriceId (..),
28+
Product (..),
29+
Price (..),
30+
PriceRecurring (..),
31+
ProductCreate (..),
32+
PriceCreate (..),
33+
PriceCreateRecurring (..),
34+
createProduct,
35+
retrieveProduct,
36+
createPrice,
37+
retrievePrice,
38+
listPrices,
39+
1640
-- * Subscriptions
17-
, SubscriptionId(..), SubscriptionItemId(..), Subscription(..), SubscriptionItem(..), SubscriptionCreate(..), SubscriptionCreateItem(..)
18-
, createSubscription, retrieveSubscription, listSubscriptions
41+
SubscriptionId (..),
42+
SubscriptionItemId (..),
43+
Subscription (..),
44+
SubscriptionItem (..),
45+
SubscriptionCreate (..),
46+
SubscriptionCreateItem (..),
47+
createSubscription,
48+
retrieveSubscription,
49+
listSubscriptions,
50+
1951
-- * Customer Portal
20-
, CustomerPortalId(..), CustomerPortal(..), CustomerPortalCreate(..)
21-
, createCustomerPortal
52+
CustomerPortalId (..),
53+
CustomerPortal (..),
54+
CustomerPortalCreate (..),
55+
createCustomerPortal,
56+
2257
-- * Checkout
23-
, CheckoutSessionId(..), CheckoutSession(..), CheckoutSessionCreate(..), CheckoutSessionCreateLineItem(..)
24-
, createCheckoutSession, retrieveCheckoutSession
58+
CheckoutSessionId (..),
59+
CheckoutSessionStatus (..),
60+
CheckoutSession (..),
61+
CheckoutSessionCreate (..),
62+
CheckoutSessionCreateLineItem (..),
63+
createCheckoutSession,
64+
retrieveCheckoutSession,
65+
2566
-- * Events
26-
, retrieveEvent, listEvents
27-
, EventId(..), Event(..), EventData(..)
67+
retrieveEvent,
68+
listEvents,
69+
EventId (..),
70+
Event (..),
71+
EventData (..),
2872
)
2973
where
3074

31-
import Stripe.Api
32-
import Stripe.Resources
33-
import Stripe.Client.Internal.Helpers
34-
3575
import Data.Proxy
36-
import Servant.API
37-
import Servant.Client
38-
import Network.HTTP.Client (Manager)
3976
import qualified Data.Text as T
4077
import qualified Data.Text.Encoding as T
78+
import Network.HTTP.Client (Manager)
79+
import Servant.API
80+
import Servant.Client
81+
import Stripe.Api
82+
import Stripe.Client.Internal.Helpers
83+
import Stripe.Resources
4184

4285
-- | Your Stripe API key. Can be obtained from the Stripe dashboard. Format: @sk_<mode>_<redacted>@
4386
type ApiKey = T.Text
4487

4588
-- | Holds a 'Manager' and your API key.
46-
data StripeClient
47-
= StripeClient
48-
{ scBasicAuthData :: BasicAuthData
49-
, scManager :: Manager
50-
, scMaxRetries :: Int
89+
data StripeClient = StripeClient
90+
{ scBasicAuthData :: BasicAuthData,
91+
scManager :: Manager,
92+
scMaxRetries :: Int
5193
}
5294

5395
-- | Construct a 'StripeClient'. Note that the passed 'Manager' must support https (e.g. via @http-client-tls@)
5496
makeStripeClient ::
55-
ApiKey
56-
-> Manager
57-
-> Int
58-
-- ^ Number of automatic retries the library should attempt. See also <https://stripe.com/docs/error-handling#safely-retrying-requests-with-idempotency Stripe Error Handling>
59-
-> StripeClient
97+
ApiKey ->
98+
Manager ->
99+
-- | Number of automatic retries the library should attempt. See also <https://stripe.com/docs/error-handling#safely-retrying-requests-with-idempotency Stripe Error Handling>
100+
Int ->
101+
StripeClient
60102
makeStripeClient k = StripeClient (BasicAuthData (T.encodeUtf8 k) "")
61103

62104
api :: Proxy StripeApi
@@ -80,35 +122,35 @@ stripeBaseUrl = BaseUrl Https "api.stripe.com" 443 ""
80122
N :: StripeClient -> ARG -> ARG2 -> ARG3 -> IO (Either ClientError R);\
81123
N sc a b c = runRequest (scMaxRetries sc) 0 $ runClientM (N##' (scBasicAuthData sc) a b c) (mkClientEnv (scManager sc) stripeBaseUrl)
82124

83-
EP(createCustomer, CustomerCreate, Customer)
84-
EP(retrieveCustomer, CustomerId, Customer)
85-
EP2(updateCustomer, CustomerId, CustomerUpdate, Customer)
86-
EP(listCustomers, Maybe CustomerId, (StripeList Customer))
125+
EP (createCustomer, CustomerCreate, Customer)
126+
EP (retrieveCustomer, CustomerId, Customer)
127+
EP2 (updateCustomer, CustomerId, CustomerUpdate, Customer)
128+
EP (listCustomers, Maybe CustomerId, (StripeList Customer))
87129

88-
EP(createProduct, ProductCreate, Product)
89-
EP(retrieveProduct, ProductId, Product)
130+
EP (createProduct, ProductCreate, Product)
131+
EP (retrieveProduct, ProductId, Product)
90132

91-
EP(createPrice, PriceCreate, Price)
92-
EP(retrievePrice, PriceId, Price)
93-
EP(listPrices, Maybe T.Text, (StripeList Price))
133+
EP (createPrice, PriceCreate, Price)
134+
EP (retrievePrice, PriceId, Price)
135+
EP (listPrices, Maybe T.Text, (StripeList Price))
94136

95-
EP(createSubscription, SubscriptionCreate, Subscription)
96-
EP(retrieveSubscription, SubscriptionId, Subscription)
97-
EP(listSubscriptions, Maybe CustomerId, (StripeList Subscription))
137+
EP (createSubscription, SubscriptionCreate, Subscription)
138+
EP (retrieveSubscription, SubscriptionId, Subscription)
139+
EP (listSubscriptions, Maybe CustomerId, (StripeList Subscription))
98140

99-
EP(createCheckoutSession, CheckoutSessionCreate, CheckoutSession)
100-
EP(retrieveCheckoutSession, CheckoutSessionId, CheckoutSession)
141+
EP (createCheckoutSession, CheckoutSessionCreate, CheckoutSession)
142+
EP (retrieveCheckoutSession, CheckoutSessionId, CheckoutSession)
101143

102-
EP(createCustomerPortal, CustomerPortalCreate, CustomerPortal)
144+
EP (createCustomerPortal, CustomerPortalCreate, CustomerPortal)
103145

104-
EP(retrieveEvent, EventId, Event)
105-
EP(listEvents, Maybe EventId, (StripeList Event))
146+
EP (retrieveEvent, EventId, Event)
147+
EP (listEvents, Maybe EventId, (StripeList Event))
106148

107149
(createCustomer' :<|> retrieveCustomer' :<|> updateCustomer' :<|> listCustomers')
108150
:<|> (createProduct' :<|> retrieveProduct')
109151
:<|> (createPrice' :<|> retrievePrice' :<|> listPrices')
110152
:<|> (createSubscription' :<|> retrieveSubscription' :<|> listSubscriptions')
111153
:<|> (createCheckoutSession' :<|> retrieveCheckoutSession')
112154
:<|> (createCustomerPortal')
113-
:<|> (retrieveEvent' :<|> listEvents')
114-
= client api
155+
:<|> (retrieveEvent' :<|> listEvents') =
156+
client api

pub/stripe-servant/src/Stripe/Resources.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{-# LANGUAGE DeriveDataTypeable #-}
2+
{-# LANGUAGE DerivingStrategies #-}
13
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
24
{-# LANGUAGE TemplateHaskell #-}
35

@@ -37,6 +39,7 @@ module Stripe.Resources
3739

3840
-- * Checkout
3941
CheckoutSessionId (..),
42+
CheckoutSessionStatus (..),
4043
CheckoutSession (..),
4144
CheckoutSessionCreate (..),
4245
CheckoutSessionCreateLineItem (..),
@@ -49,6 +52,7 @@ module Stripe.Resources
4952
where
5053

5154
import qualified Data.Aeson as A
55+
import Data.Data (Data)
5256
import qualified Data.HashMap.Strict as HM
5357
import Data.Maybe
5458
import qualified Data.Text as T
@@ -241,6 +245,12 @@ data SubscriptionCreate = SubscriptionCreate
241245
newtype CheckoutSessionId = CheckoutSessionId {unCheckoutSessionId :: T.Text}
242246
deriving (Show, Eq, ToJSON, FromJSON, ToHttpApiData)
243247

248+
data CheckoutSessionStatus
249+
= CssOpen
250+
| CssExpired
251+
| CssComplete
252+
deriving stock (Eq, Ord, Show, Read, Data, Generic, Enum, Bounded)
253+
244254
data CheckoutSession = CheckoutSession
245255
{ csId :: CheckoutSessionId,
246256
csLivemode :: Bool,
@@ -250,6 +260,7 @@ data CheckoutSession = CheckoutSession
250260
csPaymentMethodTypes :: V.Vector T.Text, -- TODO: make enum
251261
csSubscription :: Maybe SubscriptionId,
252262
csAllowPromotionCodes :: Maybe Bool,
263+
csStatus :: CheckoutSessionStatus,
253264
csUrl :: T.Text
254265
}
255266
deriving (Show, Eq)
@@ -295,6 +306,7 @@ $(deriveJSON (jsonOpts 2) ''StripeList)
295306
$(deriveJSON (jsonOpts 1) ''Customer)
296307
$(deriveJSON (jsonOpts 2) ''EventData)
297308
$(deriveJSON (jsonOpts 1) ''Event)
309+
$(deriveJSON (jsonOpts 3) ''CheckoutSessionStatus)
298310
$(deriveJSON (jsonOpts 2) ''CheckoutSession)
299311
$(deriveJSON (jsonOpts 2) ''PriceRecurring)
300312
$(deriveJSON (jsonOpts 1) ''Price)
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
-- |
2-
3-
module Stripe.Util.Aeson
4-
( jsonOpts, deriveJSON, ToJSON, FromJSON )
5-
where
1+
module Stripe.Util.Aeson (jsonOpts, deriveJSON, ToJSON, FromJSON) where
62

73
import Data.Aeson
84
import Data.Aeson.TH
@@ -11,6 +7,7 @@ import Text.Casing (quietSnake)
117
jsonOpts :: Int -> Options
128
jsonOpts x =
139
defaultOptions
14-
{ fieldLabelModifier = quietSnake . drop x
15-
, constructorTagModifier = quietSnake
16-
}
10+
{ fieldLabelModifier = quietSnake . drop x,
11+
constructorTagModifier = quietSnake . drop x,
12+
sumEncoding = UntaggedValue
13+
}

0 commit comments

Comments
 (0)