module PayPal.Adaptive.Withdrawal where
import Control.Lens hiding ((.=))
import Data.Aeson
import Data.Text (Text)
import PayPal.Adaptive.Core
data Withdrawal = Withdrawal
{ _amount :: Money
, _receiverEmail :: Text
} deriving (Eq, Show)
data SerializeWithdrawal = SerializeWithdrawal Withdrawal Text
instance ToJSON SerializeWithdrawal where
toJSON (SerializeWithdrawal p accountEmail) = object
[ "actionType" .= ("PAY" :: Text)
, "currencyCode" .= (m2Currency . _amount) p
, "receiverList" .= object
[ "receiver" .=
[ object
[ "amount" .= m2PayPal (_amount p)
, "email" .= _receiverEmail p
]
]
]
, "returnUrl" .= ("https://example.com/" :: Text)
, "cancelUrl" .= ("https://example.com/" :: Text)
, "requestEnvelope" .= requestEnvelope
, "senderEmail" .= accountEmail
]
req :: Client -> Withdrawal -> IO (Either AdaptiveErr CompletePayResp)
req c w = do
resp <- ppPost c "Pay" $ SerializeWithdrawal w (_clAccountEmail c)
return $ checkComplete =<< ppDecode =<< resp
$(makeLenses ''Withdrawal)