module Web.PayPal.Adaptive.Core where
import Prelude hiding (mapM)
import Control.Applicative
import Control.Exception
import Control.Lens hiding ((.=))
import Control.Monad ((>=>))
import Data.Aeson
import Data.Aeson.TH hiding (Options)
import Data.Aeson.Types hiding (Options)
import Data.ByteString.Lazy (ByteString)
import Data.Char
import Data.Default
import Data.Maybe
import Data.Monoid
import Data.Text (Text)
import qualified Data.Text as T
import Data.Text.Encoding
import Data.Text.Read
import Data.Traversable
import qualified Data.Vector as V
import Network.HTTP.Client (HttpException (NoResponseDataReceived))
import Network.Wreq
data PpClient = PpClient
{ _clAppId :: Text
, _clEnv :: PpEnv
, _clPassword :: Text
, _clSig :: Text
, _clUserId :: Text
} deriving (Eq, Show)
data PpEnv = PpSandbox | PpProduction deriving (Eq, Show, Read)
class AdaptiveAPI a where
toPayPal :: PpClient -> a -> IO (Either AdaptiveErr PayResp)
ppPost :: (ToJSON a) => PpClient -> Text -> a -> IO (Either AdaptiveErr (Response ByteString))
ppPost c endpoint p = catch (return . Right =<< runPost) handler
where
runPost :: IO (Response ByteString)
runPost = postWith opts (clBaseUrl <> T.unpack endpoint) (toJSON p)
clBaseUrl :: String
clBaseUrl =
case _clEnv c of
PpSandbox -> "https://svcs.sandbox.paypal.com/AdaptivePayments/"
PpProduction -> "https://svcs.paypal.com/AdaptivePayments/"
handler :: HttpException -> IO (Either AdaptiveErr (Response ByteString))
handler NoResponseDataReceived = return (Left AeNoResponse)
handler e = return . Left . AeConnectionErr . T.pack . show $ e
opts :: Options
opts = defaults
& header "X-PAYPAL-SECURITY-USERID" .~ [ encodeUtf8 (_clUserId c) ]
& header "X-PAYPAL-SECURITY-PASSWORD" .~ [ encodeUtf8 (_clPassword c) ]
& header "X-PAYPAL-SECURITY-SIGNATURE" .~ [ encodeUtf8 (_clSig c) ]
& header "X-PAYPAL-APPLICATION-ID" .~ [ encodeUtf8 (_clAppId c) ]
& header "X-PAYPAL-REQUEST-DATA-FORMAT" .~ [ "JSON" ]
& header "X-PAYPAL-RESPONSE-DATA-FORMAT" .~ [ "JSON" ]
ppDecode :: (FromJSON a) => Response ByteString -> Either AdaptiveErr a
ppDecode r =
let b = r ^. responseBody
in case eitherDecode b of
Right d -> Right d
Left e ->
case decode b of
Just (AeErrCodes codes) -> Left $ codeErr codes
_ -> Left $ AeDecodeFailed b (T.pack e)
where
codeErr :: [Int] -> AdaptiveErr
codeErr cs =
if | elem 520003 cs -> AeInvalidCredentials
| elem 589039 cs -> AeNoSuchEmail
| otherwise -> AeErrCodes cs
data AdaptiveErr
= AeNoResponse
| AeConnectionErr Text
| AeDecodeFailed ByteString Text
| AeInvalidCredentials
| AeNoSuchEmail
| AeErrCodes [Int]
| AePending PayResp
| AeRefused PayResp
| AeShouldNotHappen PayResp Text
deriving (Eq, Show)
instance FromJSON AdaptiveErr where
parseJSON = withObject "error response" $ \o ->
AeErrCodes . V.toList <$> (mapM parseErrId =<< o .: "error")
parseErrId :: Value -> Parser Int
parseErrId = withObject "error array item" $ \o -> do
a <- o .: "errorId"
case decimal a of
Left _ -> fail "Could not parse error response code to Int"
Right (b,_) -> return b
data ReceiverList = ReceiverList
{ _rlAmount :: Money
, _rlEmail :: Text
} deriving (Eq, Show)
instance ToJSON ReceiverList where
toJSON (ReceiverList a e) =
object [ "receiver" .= [ object ["amount" .= m2PayPal a, "email" .= e] ] ]
requestEnvelope :: Value
requestEnvelope = object ["errorLanguage" .= ("en_US" :: Text)]
data SendPayment = SendPayment
{ _spReceiverList :: ReceiverList
, _spReturnUrl :: Text
, _spCancelUrl :: Text
, _spSenderEmail :: Text
} deriving (Eq, Show)
instance Default SendPayment where
def = SendPayment
{ _spReceiverList = ReceiverList mempty mempty
, _spReturnUrl = "https://example.com/"
, _spCancelUrl = "https://example.com/cancel"
, _spSenderEmail = mempty
}
instance ToJSON SendPayment where
toJSON p = object
[ "actionType" .= ("PAY" :: Text)
, "currencyCode" .= (m2Currency . _rlAmount . _spReceiverList) p
, "receiverList" .= _spReceiverList p
, "returnUrl" .= _spReturnUrl p
, "cancelUrl" .= _spCancelUrl p
, "requestEnvelope" .= requestEnvelope
, "senderEmail" .= _spSenderEmail p
]
instance AdaptiveAPI SendPayment where
toPayPal c p = (>>= ppDecode >=> ensureSucceeded) <$> ppPost c "Pay" p
where
ensureSucceeded :: PayResp -> Either AdaptiveErr PayResp
ensureSucceeded a =
case _prPayExecStatus a of
PeProcessing -> Left $ AePending a
PePending -> Left $ AePending a
PeError -> Left $ AeRefused a
PeCompleted ->
let infos = _prPayInfo a
in if length infos /= 1
then Left $ AeShouldNotHappen a "sendPayment expects one PayInfo in reponse"
else
let info = head infos
in case _piTransactionStatus info of
Just TsCompleted -> Right a
Just TsPending -> Left $ AePending a
Just TsProcessing -> Left $ AePending a
Just TsDenied -> Left $ AeRefused a
Just TsFailed -> Left $ AeRefused a
_ -> Left $ AeShouldNotHappen a "got unsupported TransactionStatus"
_ -> Left $ AeShouldNotHappen a "got unsupported PayExecStatus"
data CreatePayment = CreatePayment
{ _cpReceiverList :: ReceiverList
, _cpReturnUrl :: Text
, _cpCancelUrl :: Text
, _cpSenderEmail :: Text
} deriving (Eq, Show)
instance Default CreatePayment where
def = CreatePayment
{ _cpReceiverList = ReceiverList mempty mempty
, _cpReturnUrl = "https://example.com/"
, _cpCancelUrl = "https://example.com/cancel"
, _cpSenderEmail = mempty
}
instance ToJSON CreatePayment where
toJSON p = object
[ "actionType" .= ("PAY" :: Text)
, "currencyCode" .= (m2Currency . _rlAmount . _cpReceiverList) p
, "receiverList" .= _cpReceiverList p
, "returnUrl" .= _cpReturnUrl p
, "cancelUrl" .= _cpCancelUrl p
, "requestEnvelope" .= requestEnvelope
, "senderEmail" .= _cpSenderEmail p
]
instance AdaptiveAPI CreatePayment where
toPayPal c p = (>>= ppDecode >=> ensureSucceeded) <$> ppPost c "Pay" p
where
ensureSucceeded :: PayResp -> Either AdaptiveErr PayResp
ensureSucceeded a =
case _prPayExecStatus a of
PeCreated -> Right a
PeProcessing -> Left $ AePending a
PePending -> Left $ AePending a
PeError -> Left $ AeRefused a
_ -> Left $ AeShouldNotHappen a "got unsupported PayExecStatus"
approvalUrl :: PpClient -> PayKey -> Text
approvalUrl c k =
case _clEnv c of
PpSandbox ->
"https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_ap-payment&paykey="
<> _unPayKey k
PpProduction ->
"https://www.paypal.com/cgi-bin/webscr?cmd=_ap-payment&paykey="
<> _unPayKey k
data LookupPayment a where
LookupTrID :: TransactionId -> LookupPayment TransactionId
LookupPayKey :: PayKey -> LookupPayment PayKey
instance ToJSON (LookupPayment a) where
toJSON (LookupTrID (TransactionId p)) =
object [ "transactionId" .= p
, "requestEnvelope" .= requestEnvelope
]
toJSON (LookupPayKey (PayKey p)) =
object [ "payKey" .= p
, "requestEnvelope" .= requestEnvelope
]
instance AdaptiveAPI (LookupPayment a) where
toPayPal c p = (>>= ppDecode) <$> ppPost c "PaymentDetails" p
data PayResp = PayResp
{ _prPayError :: Maybe Text
, _prPayExecStatus :: PayExecStatus
, _prPayKey :: PayKey
, _prPayInfo :: [PayInfo]
} deriving (Eq, Show)
instance FromJSON PayResp where
parseJSON = withObject "PayResp" $ \o -> do
e <- o .:? "payErrorList"
k <- o .: "payKey"
s <- o .:? "paymentExecStatus"
s' <- o .:? "status"
status <- maybe (fail "no paymentExecStatus or status") return (if isJust s then s else s')
a <- o .:? "paymentInfoList"
case a of
Nothing -> return $ PayResp e status k mempty
Just v -> do
infos <- v .:? "paymentInfo" .!= mempty
return $ PayResp e status k infos
data PayExecStatus
= PeCreated
| PeCompleted
| PeIncomplete
| PeError
| PeReversalError
| PeProcessing
| PePending
deriving (Eq, Show, Read)
newtype PayKey = PayKey { _unPayKey :: Text } deriving (Eq, Show)
instance FromJSON PayKey where
parseJSON = withText "PayKey" $ return . PayKey
instance ToJSON PayKey where
toJSON (PayKey a) = toJSON a
data PayInfo = PayInfo
{ _piReceiver :: Receiver
, _piSenderTransactionId :: Maybe TransactionId
, _piTransactionStatus :: Maybe TransactionStatus
, _piTransactionId :: Maybe TransactionId
} deriving (Eq, Show)
data Receiver = Receiver
{ _reAmount :: Text
, _reEmail :: Text
, _reAccountId :: Text
} deriving (Eq, Show)
newtype TransactionId = TransactionId { _unTransactionId :: Text } deriving (Eq, Show)
instance FromJSON TransactionId where
parseJSON = withText "TransactionId" $ return . TransactionId
instance ToJSON TransactionId where
toJSON (TransactionId a) = toJSON a
data TransactionStatus
= TsCompleted
| TsPending
| TsCreated
| TsPartiallyRefunded
| TsDenied
| TsProcessing
| TsReversed
| TsRefunded
| TsFailed
deriving (Eq, Show, Read)
data Money = USD { _usdCents :: Int } deriving (Eq, Show)
instance Monoid Money where
mempty = USD 0
mappend (USD c1) (USD c2) = USD $ c1 + c2
m2Currency :: Money -> Text
m2Currency (USD _) = "USD"
m2PayPal :: Money -> String
m2PayPal (USD c) =
let s = show (abs c)
a = case length s of
0 -> "0.00"
1 -> "0.0" <> s
2 -> "0." <> s
_ -> tailInsert 2 '.' s
in if c < 0 then '-':a else a
where
tailInsert :: Int -> a -> [a] -> [a]
tailInsert i x xs =
let (ys, zs) = splitAt i (reverse xs)
in reverse (ys <> pure x <> zs)
$(deriveFromJSON defaultOptions { fieldLabelModifier = (\(x:xs) -> toLower x:xs) . drop 3 } ''PayInfo)
$(deriveFromJSON defaultOptions { constructorTagModifier = map toUpper . drop 2 } ''PayExecStatus)
$(deriveFromJSON defaultOptions { fieldLabelModifier = (\(x:xs) -> toLower x:xs) . drop 3 } ''Receiver)
$(deriveFromJSON defaultOptions { constructorTagModifier = map toUpper . camelTo '_' . drop 2 } ''TransactionStatus)
$(makePrisms ''AdaptiveErr)
$(makeLenses ''PpClient)
$(makeLenses ''CreatePayment)
$(makePrisms ''PpEnv)
$(makePrisms ''PayExecStatus)
$(makeLenses ''PayInfo)
$(makeLenses ''PayResp)
$(makeLenses ''Receiver)
$(makeLenses ''ReceiverList)
$(makeLenses ''SendPayment)
$(makePrisms ''TransactionId)
$(makePrisms ''TransactionStatus)