module Web.Stripe.Token
( Token(..)
, TokenId(..)
, createToken
, getToken
, tokRq
, UTCTime(..)
, Amount(..)
, Card(..)
, Currency(..)
, StripeConfig(..)
, StripeT(..)
, runStripeT
) where
import Control.Applicative ((<$>), (<*>))
import Control.Monad (liftM, mzero)
import Control.Monad.Error (MonadIO)
import Data.Aeson (FromJSON (..), Value (..), (.:))
import qualified Data.Text as T
import Network.HTTP.Types (StdMethod (..))
import Web.Stripe.Card (Card (..), RequestCard (..), rCardKV)
import Web.Stripe.Client (StripeConfig (..), StripeRequest (..),
StripeT (..), baseSReq, query, runStripeT)
import Web.Stripe.Utils (Amount (..), Currency (..), UTCTime (..),
fromSeconds)
data Token = Token
{ tokId :: TokenId
, tokLive :: Bool
, tokUsed :: Bool
, tokCreated :: UTCTime
, tokCard :: Card
} deriving Show
newtype TokenId = TokenId { unTokenId :: T.Text } deriving (Show, Eq)
createToken :: MonadIO m => RequestCard -> StripeT m Token
createToken rc =
snd `liftM` query (tokRq []) { sMethod = POST, sData = rCardKV rc }
getToken :: MonadIO m => TokenId -> StripeT m Token
getToken (TokenId tid) = return . snd =<< query (tokRq [tid])
tokRq :: [T.Text] -> StripeRequest
tokRq pcs = baseSReq { sDestination = "tokens":pcs }
instance FromJSON Token where
parseJSON (Object o) = Token
<$> (TokenId <$> o .: "id")
<*> o .: "livemode"
<*> o .: "used"
<*> (fromSeconds <$> o .: "created")
<*> o .: "card"
parseJSON _ = mzero