Skip to content
Browse files

Add and update customer records via TokenIds.

  • Loading branch information...
1 parent 3d074da commit 3c51167335e34a3008a54c45dfe13b3d8762e221 @LukeHoersten committed Dec 22, 2012
Showing with 44 additions and 5 deletions.
  1. +44 −5 src/Web/Stripe/Customer.hs
View
49 src/Web/Stripe/Customer.hs
@@ -1,12 +1,15 @@
{-# LANGUAGE OverloadedStrings #-}
module Web.Stripe.Customer
- ( Customer(..)
+ ( Customer(..)
, CustomerId(..)
, Email(..)
, createCustomer
+ , createCustomerByTokenId
, updateCustomer
, updateCustomerById
+ , updateCustomerByTokenId
+ , updateCustomerByIdByTokenId
, getCustomer
, getCustomers
, delCustomer
@@ -25,20 +28,20 @@ module Web.Stripe.Customer
import Control.Applicative ((<$>), (<*>))
import Control.Monad (liftM, mzero)
import Control.Monad.Error (MonadIO)
+import Data.Aeson (FromJSON (..), Value (..), (.:), (.:?))
import Data.Maybe (fromMaybe)
+import qualified Data.Text as T
import Web.Stripe.Card (Card, RequestCard, rCardKV)
import Web.Stripe.Client (SConfig (..), StdMethod (..),
StripeRequest (..), StripeT (..),
baseSReq, query, queryData, runStripeT)
import Web.Stripe.Coupon (CpnId (..))
import Web.Stripe.Plan (PlanId (..))
+import Web.Stripe.Token (TokenId (..))
import Web.Stripe.Utils (Count (..), Description (..), Offset (..),
UTCTime (..), optionalArgs,
showByteString, textToByteString)
-import Data.Aeson (FromJSON (..), Value (..), (.:), (.:?))
-import qualified Data.Text as T
-
----------------
-- Data Types --
----------------
@@ -71,9 +74,25 @@ createCustomer mrc mcid me md mpid mtime =
, ("email", textToByteString . unEmail <$> me)
, ("description", textToByteString . unDescription <$> md)
, ("plan", textToByteString . unPlanId <$> mpid)
- , ("trial_end", showByteString <$> mtime)
+ , ("trial_end", showByteString <$> mtime)
+ ]
+
+-- | Create a new 'Customer' in the Stripe system using a TokenId.
+createCustomerByTokenId :: MonadIO m => Maybe TokenId -> Maybe CpnId -> Maybe Email
+ -> Maybe Description -> Maybe PlanId -> Maybe Int
+ -> StripeT m Customer
+createCustomerByTokenId mrt mcid me md mpid mtime =
+ snd `liftM` query (customerRq []) { sMethod = POST, sData = optionalArgs odata }
+ where
+ odata = [ ("card", textToByteString . unTokenId <$> mrt)
+ , ("coupon", textToByteString . unCpnId <$> mcid)
+ , ("email", textToByteString . unEmail <$> me)
+ , ("description", textToByteString . unDescription <$> md)
+ , ("plan", textToByteString . unPlanId <$> mpid)
+ , ("trial_end", showByteString <$> mtime)
]
+
-- | Update an existing 'Customer' in the Stripe system.
updateCustomer :: MonadIO m => Customer -> Maybe RequestCard -> Maybe CpnId
-> Maybe Email -> Maybe Description -> StripeT m Customer
@@ -93,6 +112,26 @@ updateCustomerById (CustomerId cid) mrc mcid me md =
, ("description", textToByteString . unDescription <$> md)
]
+-- | Update an existing 'Customer' in the Stripe system.
+updateCustomerByTokenId :: MonadIO m => Customer -> Maybe TokenId -> Maybe CpnId
+ -> Maybe Email -> Maybe Description -> StripeT m Customer
+updateCustomerByTokenId = updateCustomerByIdByTokenId . custId
+
+-- | Update an existing 'Customer', identified by 'CustomerId', in the Stripe
+-- system.
+updateCustomerByIdByTokenId :: MonadIO m => CustomerId -> Maybe TokenId
+ -> Maybe CpnId -> Maybe Email -> Maybe Description
+ -> StripeT m Customer
+updateCustomerByIdByTokenId (CustomerId cid) mrt mcid me md =
+ snd `liftM` query (customerRq [cid]) { sMethod = POST, sData = optionalArgs odata }
+ where
+ odata = [ ("card", textToByteString . unTokenId <$> mrt)
+ , ("coupon", textToByteString . unCpnId <$> mcid)
+ , ("email", textToByteString . unEmail <$> me)
+ , ("description", textToByteString . unDescription <$> md)
+ ]
+
+
-- | Retrieves a specific 'Customer' based on its 'CustomerId'.
getCustomer :: MonadIO m => CustomerId -> StripeT m Customer
getCustomer (CustomerId cid) =

0 comments on commit 3c51167

Please sign in to comment.
Something went wrong with that request. Please try again.