module TD.Data.PaymentResult
  (PaymentResult(..)) where

import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I
import qualified Data.Text as T

data PaymentResult
  = PaymentResult -- ^ Contains the result of a payment request
    { PaymentResult -> Maybe Bool
success          :: Maybe Bool   -- ^ True, if the payment request was successful; otherwise, the verification_url will be non-empty
    , PaymentResult -> Maybe Text
verification_url :: Maybe T.Text -- ^ URL for additional payment credentials verification
    }
  deriving (PaymentResult -> PaymentResult -> Bool
(PaymentResult -> PaymentResult -> Bool)
-> (PaymentResult -> PaymentResult -> Bool) -> Eq PaymentResult
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PaymentResult -> PaymentResult -> Bool
== :: PaymentResult -> PaymentResult -> Bool
$c/= :: PaymentResult -> PaymentResult -> Bool
/= :: PaymentResult -> PaymentResult -> Bool
Eq, Int -> PaymentResult -> ShowS
[PaymentResult] -> ShowS
PaymentResult -> String
(Int -> PaymentResult -> ShowS)
-> (PaymentResult -> String)
-> ([PaymentResult] -> ShowS)
-> Show PaymentResult
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PaymentResult -> ShowS
showsPrec :: Int -> PaymentResult -> ShowS
$cshow :: PaymentResult -> String
show :: PaymentResult -> String
$cshowList :: [PaymentResult] -> ShowS
showList :: [PaymentResult] -> ShowS
Show)

instance I.ShortShow PaymentResult where
  shortShow :: PaymentResult -> String
shortShow PaymentResult
    { success :: PaymentResult -> Maybe Bool
success          = Maybe Bool
success_
    , verification_url :: PaymentResult -> Maybe Text
verification_url = Maybe Text
verification_url_
    }
      = String
"PaymentResult"
        String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
        [ String
"success"          String -> Maybe Bool -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Bool
success_
        , String
"verification_url" String -> Maybe Text -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Text
verification_url_
        ]

instance AT.FromJSON PaymentResult where
  parseJSON :: Value -> Parser PaymentResult
parseJSON v :: Value
v@(AT.Object Object
obj) = do
    String
t <- Object
obj Object -> Key -> Parser String
forall a. FromJSON a => Object -> Key -> Parser a
A..: Key
"@type" :: AT.Parser String

    case String
t of
      String
"paymentResult" -> Value -> Parser PaymentResult
parsePaymentResult Value
v
      String
_               -> Parser PaymentResult
forall a. Monoid a => a
mempty
    
    where
      parsePaymentResult :: A.Value -> AT.Parser PaymentResult
      parsePaymentResult :: Value -> Parser PaymentResult
parsePaymentResult = String
-> (Object -> Parser PaymentResult)
-> Value
-> Parser PaymentResult
forall a. String -> (Object -> Parser a) -> Value -> Parser a
A.withObject String
"PaymentResult" ((Object -> Parser PaymentResult) -> Value -> Parser PaymentResult)
-> (Object -> Parser PaymentResult)
-> Value
-> Parser PaymentResult
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        Maybe Bool
success_          <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"success"
        Maybe Text
verification_url_ <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
A..:?  Key
"verification_url"
        PaymentResult -> Parser PaymentResult
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PaymentResult -> Parser PaymentResult)
-> PaymentResult -> Parser PaymentResult
forall a b. (a -> b) -> a -> b
$ PaymentResult
          { success :: Maybe Bool
success          = Maybe Bool
success_
          , verification_url :: Maybe Text
verification_url = Maybe Text
verification_url_
          }
  parseJSON Value
_ = Parser PaymentResult
forall a. Monoid a => a
mempty