-- | Look up information about a payment.
--
-- <https://developer.paypal.com/docs/classic/api/adaptive-payments/PaymentDetails_API_Operation/>

module PayPal.Adaptive.Lookup where

import           Data.ByteString.Lazy             (ByteString)

import           Import
import           PayPal.Adaptive.Core.Client
import           PayPal.Adaptive.Core.Error
import           PayPal.Adaptive.Core.Internal
import           PayPal.Adaptive.Core.PayResponse

data LookupPayment = LookupTransactionID TransactionId | LookupPayKey PayKey deriving (Eq, Show)

instance ToJSON LookupPayment where
  toJSON (LookupTransactionID (TransactionId p)) =
    object [ "transactionId"   .= p
           , "requestEnvelope" .= requestEnvelope
           ]
  toJSON (LookupPayKey (PayKey p)) =
    object [ "payKey"          .= p
           , "requestEnvelope" .= requestEnvelope
           ]

request :: Client -> LookupPayment -> IO (Either AdaptiveError (ByteString,PayResponse) )
request c l = do
  resp <- ppPost c "PaymentDetails" l
  case resp of
    Left e    -> return $ Left e
    Right bts -> return $ (,) bts <$> ppDecode bts