|
| 1 | +{-# OPTIONS_GHC -cpp -pgmPcpphs -optP--cpp #-} |
| 2 | +{-# LANGUAGE CPP #-} |
| 3 | +module Stripe.Client |
| 4 | + ( -- * Basics |
| 5 | + ApiKey, StripeClient, makeStripeClient, ClientError(..) |
| 6 | + -- * Helper types |
| 7 | + , TimeStamp(..), StripeList(..) |
| 8 | + -- * Customers |
| 9 | + , createCustomer, retrieveCustomer, updateCustomer, listCustomers |
| 10 | + , CustomerId(..), Customer(..), CustomerCreate(..), CustomerUpdate(..) |
| 11 | + -- * Product catalog |
| 12 | + , ProductId(..), PriceId(..), Product(..), Price(..), PriceRecurring(..) |
| 13 | + , ProductCreate(..), PriceCreate(..), PriceCreateRecurring(..) |
| 14 | + , createProduct, retrieveProduct |
| 15 | + , createPrice, retrievePrice, listPrices |
| 16 | + -- * Subscriptions |
| 17 | + , SubscriptionId(..), SubscriptionItemId(..), Subscription(..), SubscriptionItem(..), SubscriptionCreate(..), SubscriptionCreateItem(..) |
| 18 | + , createSubscription, retrieveSubscription, listSubscriptions |
| 19 | + -- * Customer Portal |
| 20 | + , CustomerPortalId(..), CustomerPortal(..), CustomerPortalCreate(..) |
| 21 | + , createCustomerPortal |
| 22 | + -- * Checkout |
| 23 | + , CheckoutSessionId(..), CheckoutSession(..), CheckoutSessionCreate(..), CheckoutSessionCreateLineItem(..) |
| 24 | + , createCheckoutSession, retrieveCheckoutSession |
| 25 | + -- * Events |
| 26 | + , retrieveEvent, listEvents |
| 27 | + , EventId(..), Event(..), EventData(..) |
| 28 | + ) |
| 29 | +where |
| 30 | + |
| 31 | +import Stripe.Api |
| 32 | +import Stripe.Resources |
| 33 | +import Stripe.Client.Internal.Helpers |
| 34 | + |
| 35 | +import Data.Proxy |
| 36 | +import Servant.API |
| 37 | +import Servant.Client |
| 38 | +import Network.HTTP.Client (Manager) |
| 39 | +import qualified Data.Text as T |
| 40 | +import qualified Data.Text.Encoding as T |
| 41 | + |
| 42 | +-- | Your Stripe API key. Can be obtained from the Stripe dashboard. Format: @sk_<mode>_<redacted>@ |
| 43 | +type ApiKey = T.Text |
| 44 | + |
| 45 | +-- | Holds a 'Manager' and your API key. |
| 46 | +data StripeClient |
| 47 | + = StripeClient |
| 48 | + { scBasicAuthData :: BasicAuthData |
| 49 | + , scManager :: Manager |
| 50 | + , scMaxRetries :: Int |
| 51 | + } |
| 52 | + |
| 53 | +-- | Construct a 'StripeClient'. Note that the passed 'Manager' must support https (e.g. via @http-client-tls@) |
| 54 | +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 |
| 60 | +makeStripeClient k = StripeClient (BasicAuthData (T.encodeUtf8 k) "") |
| 61 | + |
| 62 | +api :: Proxy StripeApi |
| 63 | +api = Proxy |
| 64 | + |
| 65 | +stripeBaseUrl :: BaseUrl |
| 66 | +stripeBaseUrl = BaseUrl Https "api.stripe.com" 443 "" |
| 67 | + |
| 68 | +#define EP(N, ARG, R) \ |
| 69 | + N##' :: BasicAuthData -> ARG -> ClientM R;\ |
| 70 | + N :: StripeClient -> ARG -> IO (Either ClientError R);\ |
| 71 | + N sc a = runRequest (scMaxRetries sc) 0 $ runClientM (N##' (scBasicAuthData sc) a) (mkClientEnv (scManager sc) stripeBaseUrl) |
| 72 | + |
| 73 | +#define EP2(N, ARG, ARG2, R) \ |
| 74 | + N##' :: BasicAuthData -> ARG -> ARG2 -> ClientM R;\ |
| 75 | + N :: StripeClient -> ARG -> ARG2 -> IO (Either ClientError R);\ |
| 76 | + N sc a b = runRequest (scMaxRetries sc) 0 $ runClientM (N##' (scBasicAuthData sc) a b) (mkClientEnv (scManager sc) stripeBaseUrl) |
| 77 | + |
| 78 | +#define EP3(N, ARG, ARG2, ARG3, R) \ |
| 79 | + N##' :: BasicAuthData -> ARG -> ARG2 -> ARG3 -> ClientM R;\ |
| 80 | + N :: StripeClient -> ARG -> ARG2 -> ARG3 -> IO (Either ClientError R);\ |
| 81 | + N sc a b c = runRequest (scMaxRetries sc) 0 $ runClientM (N##' (scBasicAuthData sc) a b c) (mkClientEnv (scManager sc) stripeBaseUrl) |
| 82 | + |
| 83 | +EP(createCustomer, CustomerCreate, Customer) |
| 84 | +EP(retrieveCustomer, CustomerId, Customer) |
| 85 | +EP2(updateCustomer, CustomerId, CustomerUpdate, Customer) |
| 86 | +EP(listCustomers, Maybe CustomerId, (StripeList Customer)) |
| 87 | + |
| 88 | +EP(createProduct, ProductCreate, Product) |
| 89 | +EP(retrieveProduct, ProductId, Product) |
| 90 | + |
| 91 | +EP(createPrice, PriceCreate, Price) |
| 92 | +EP(retrievePrice, PriceId, Price) |
| 93 | +EP(listPrices, Maybe T.Text, (StripeList Price)) |
| 94 | + |
| 95 | +EP(createSubscription, SubscriptionCreate, Subscription) |
| 96 | +EP(retrieveSubscription, SubscriptionId, Subscription) |
| 97 | +EP(listSubscriptions, Maybe CustomerId, (StripeList Subscription)) |
| 98 | + |
| 99 | +EP(createCheckoutSession, CheckoutSessionCreate, CheckoutSession) |
| 100 | +EP(retrieveCheckoutSession, CheckoutSessionId, CheckoutSession) |
| 101 | + |
| 102 | +EP(createCustomerPortal, CustomerPortalCreate, CustomerPortal) |
| 103 | + |
| 104 | +EP(retrieveEvent, EventId, Event) |
| 105 | +EP(listEvents, Maybe EventId, (StripeList Event)) |
| 106 | + |
| 107 | +(createCustomer' :<|> retrieveCustomer' :<|> updateCustomer' :<|> listCustomers') |
| 108 | + :<|> (createProduct' :<|> retrieveProduct') |
| 109 | + :<|> (createPrice' :<|> retrievePrice' :<|> listPrices') |
| 110 | + :<|> (createSubscription' :<|> retrieveSubscription' :<|> listSubscriptions') |
| 111 | + :<|> (createCheckoutSession' :<|> retrieveCheckoutSession') |
| 112 | + :<|> (createCustomerPortal') |
| 113 | + :<|> (retrieveEvent' :<|> listEvents') |
| 114 | + = client api |
0 commit comments