Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

cachix/fastspring

Repository files navigation

FastSpring API/Webhooks for Haskell

Hackage

Getting started with Webhooks

Implements FastSpring Webhooks using Servant Server.

import Data.Aeson (eitherDecode)
import Data.ByteString (ByteString)
import Data.ByteString.Lazy (fromStrict)
import Control.Monad (unless, forM_)
import Control.Monad.Catch (throwM)
import Control.Monad.IO.Class (liftIO)
import Network.HTTP.Client.TLS ( newTlsManager )
import Servant

import FastSpring


secret :: SignatureSecret
secret = SignatureSecret "foobar"

webhook :: ByteString -> Signature -> Handler NoContent
webhook body signature = do
  -- verify that fastspring signature matches
  unless (FastSpring.create secret body == signature) $ throwM err400

  liftIO $ case eitherDecode (fromStrict body) of
    Left err ->
      print err
    Right (Events { events=events}) ->
      forM_ events $ \event ->
        case FastSpring.parse event of
          Right (SubscriptionActivated payload) -> print payload
          Left (FailedToParse err) -> print err
          Left (UnknownEvent unknownevent) -> print unknownevent
  return NoContent

And hook up your servant server to webhook endpoint using FastSpring.Webhook.API.api.

Getting started with the API

Implements FastSpring API using Servant Client.

main :: IO ()
main = do
  manager <- newTlsManager
  orderOrErr <- runClient manager $ orderGet client (BasicAuthData "user" "password") "order-id"
  case orderOrErr of
    Right order -> print order
    Left err -> print err