packages feed

iap-verifier (empty) → 0.1.0.0

raw patch · 4 files changed

+152/−0 lines, 4 filesdep +aesondep +basedep +base64-bytestringsetup-changed

Dependencies added: aeson, base, base64-bytestring, bytestring, conduit, http-conduit, monads-tf, text, transformers

Files

+ LICENSE view
@@ -0,0 +1,21 @@+The MIT License (MIT)++Copyright (c) 2015 tattsun.++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in+all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN+THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ iap-verifier.cabal view
@@ -0,0 +1,38 @@+-- Initial iap-verifier.cabal generated by cabal init.  For further+-- documentation, see http://haskell.org/cabal/users-guide/++name:                iap-verifier+version:             0.1.0.0+synopsis:            A simple wrapper of In-App-Purchase receipt validate APIs.+-- description:+homepage:            http://github.com/tattsun/iap-verifier+license:             MIT+license-file:        LICENSE+author:              tattsun+maintainer:          t.t.mc1192.sf@gmail.com+-- copyright:+category:            Network+build-type:          Simple+-- extra-source-files:+cabal-version:       >=1.10++source-repository head+  type:     git+  location: https://github.com/tattsun/iap-verifier.git++library+  exposed-modules:     Network.IAP.Verifier+  -- other-modules:+  -- other-extensions:+  build-depends:       aeson >= 0.8.0.2+                     , base >=4.7 && <4.8+                     , base64-bytestring >= 1.0.0.1+                     , bytestring >= 0.10.4.0+                     , conduit >= 1.2.4.2+                     , http-conduit >= 2.1.5+                     , monads-tf >= 0.1.0.2+                     , text >= 1.2.0.6+                     , transformers >= 0.3.0.0+  hs-source-dirs:      src+  default-language:    Haskell2010+  ghc-options:         -W
+ src/Network/IAP/Verifier.hs view
@@ -0,0 +1,91 @@+{-# LANGUAGE DeriveDataTypeable #-}+{-# LANGUAGE OverloadedStrings  #-}+{-# LANGUAGE TemplateHaskell    #-}+-- | This module is a simple wrapper of AppStore In-App-Purchase Receipt Validate APIs.+-- Example:+--+-- > import Network.IAP.Verifier+-- > main :: IO ()+-- > main = do+-- >   receipt <- readFile "./receipt"+-- >   result <- verify defaultIAPSettings receipt+-- >   case result of+-- >     0 -> putStrLn "OK"+-- >     _ -> putStrLn "Fail"+--+-- For more information, please see <https://developer.apple.com/library/ios/releasenotes/General/ValidateAppStoreReceipt/Introduction.html>.+module Network.IAP.Verifier+       ( -- * Settings+         IAPSettings(..)+       , defaultIAPSettings+       , sandboxIAPSettings+         -- * Result+       , Result(..)+         -- * Exception+       , IAPException(..)+         -- * Action+       , verify+       ) where++import           Control.Exception+import           Control.Monad.IO.Class+import           Data.Aeson                 hiding (Result)+import           Data.Aeson.TH+import qualified Data.ByteString.Char8      as BS+import qualified Data.ByteString.Lazy.Char8 as LBS+import qualified Data.Conduit               as C+import qualified Data.Text.Encoding         as T+import           Data.Typeable+import           Network.HTTP.Conduit++-- | In-App-Purchase Verify Settings.+data IAPSettings = IAPSettings { verifyUrl :: String+                               }+                   deriving (Show)++-- | 'IAPSettings' for production.+defaultIAPSettings :: IAPSettings+defaultIAPSettings =+  IAPSettings { verifyUrl = "https://buy.itunes.apple.com/verifyReceipt"+              }++-- | 'IAPSettings' for development.+sandboxIAPSettings :: IAPSettings+sandboxIAPSettings =+  defaultIAPSettings { verifyUrl = "https://sandbox.itunes.apple.com/verifyReceipt"+                     }++-- | A result of 'verify'.+data Result = Result { status :: Int+                     }+              deriving (Show)+$(deriveJSON defaultOptions ''Result)++-- | Exceptions thrown by 'verify'.+data IAPException = UnknownJSONException { unUnknownJSONException :: BS.ByteString }+                  | NoResponseException+                  deriving (Show, Eq, Typeable)+instance Exception IAPException++----------------------------------------------------------------------++-- | Verify your receipt.+-- Throw 'IAPException' when request is failed.+verify :: IAPSettings -> BS.ByteString -> IO Result+verify settings receipt = do+  requestRaw <- parseUrl (verifyUrl settings)+  let payload = encode $ object ["receipt-data" .= (T.decodeUtf8 $+                                                    receipt)]+      request = requestRaw { requestBody = RequestBodyLBS payload+                           , method = "POST"+                           }+  print request+  withManager $ \manager -> do+    response <- http request manager+    responseBody response C.$$+- do+      value <- C.await+      case value of+       Just x ->  case decode . LBS.fromStrict $ x of+                   Just result -> return result+                   Nothing -> liftIO . throwIO $ UnknownJSONException x+       Nothing -> liftIO . throwIO $ NoResponseException