module Web.MangoPay.Wallets where
import Web.MangoPay.Monad
import Web.MangoPay.Types
import Web.MangoPay.Users
import Data.ByteString (ByteString)
import Data.Text hiding (map,toLower)
import Data.Typeable (Typeable)
import Data.Aeson
import Control.Applicative
import qualified Data.HashMap.Lazy as HM (delete)
import Data.Default (Default(..))
import Data.Char (toLower)
createWallet :: (MPUsableMonad m) => Wallet -> AccessToken -> MangoPayT m Wallet
createWallet = createGeneric "/wallets"
modifyWallet :: (MPUsableMonad m) => Wallet -> AccessToken -> MangoPayT m Wallet
modifyWallet w = modifyGGeneric (Just $ HM.delete "Currency") "/wallets/" w wId
fetchWallet :: (MPUsableMonad m) => WalletId -> AccessToken -> MangoPayT m Wallet
fetchWallet = fetchGeneric "/wallets/"
listWallets :: (MPUsableMonad m) => AnyUserId -> GenericSort -> Maybe Pagination -> AccessToken -> MangoPayT m (PagedList Wallet)
listWallets uid gs = genericListExtra (sortAttributes gs) ["/users/",uid,"/wallets"]
createTransfer :: (MPUsableMonad m) => Transfer -> AccessToken -> MangoPayT m Transfer
createTransfer = createGeneric "/transfers"
fetchTransfer :: (MPUsableMonad m) => TransferId -> AccessToken -> MangoPayT m Transfer
fetchTransfer = fetchGeneric "/transfers/"
listTransactions :: (MPUsableMonad m) => WalletId -> TransactionFilter -> TransactionSort -> Maybe Pagination -> AccessToken -> MangoPayT m (PagedList Transaction)
listTransactions wid tf ts = genericListExtra (transactionFilterAttributes tf ++ transactionSortAttributes ts)
["/wallets/",wid,"/transactions"]
listTransactionsForUser :: (MPUsableMonad m) => AnyUserId -> TransactionFilter -> TransactionSort -> Maybe Pagination -> AccessToken -> MangoPayT m (PagedList Transaction)
listTransactionsForUser uid tf ts = genericListExtra (transactionFilterAttributes tf ++ transactionSortAttributes ts)
["/users/",uid,"/transactions"]
type WalletId=Text
data Wallet = Wallet {
wId :: Maybe WalletId
,wCreationDate :: Maybe MpTime
,wTag :: Maybe Text
,wOwners :: [Text]
,wDescription :: Text
,wCurrency :: Currency
,wBalance :: Maybe Amount
}
deriving (Show,Eq,Ord,Typeable)
instance ToJSON Wallet where
toJSON w=objectSN ["Tag" .= wTag w,"Owners" .= wOwners w,"Description" .= wDescription w,"Currency" .= wCurrency w]
instance FromJSON Wallet where
parseJSON (Object v) =Wallet <$>
v .:? "Id" <*>
v .:? "CreationDate" <*>
v .:? "Tag" <*>
v .: "Owners" <*>
v .: "Description" <*>
v .: "Currency" <*>
v .: "Balance"
parseJSON _=fail "Wallet"
type TransferId=Text
data TransferStatus= Created | Succeeded | Failed
deriving (Show,Read,Eq,Ord,Bounded,Enum,Typeable)
instance ToJSON TransferStatus where
toJSON Created="CREATED"
toJSON Succeeded="SUCCEEDED"
toJSON Failed="FAILED"
instance FromJSON TransferStatus where
parseJSON (String "CREATED") =pure Created
parseJSON (String "SUCCEEDED") =pure Succeeded
parseJSON (String "FAILED") =pure Failed
parseJSON _= fail "TransferStatus"
data Transfer = Transfer{
tId :: Maybe TransferId
,tCreationDate :: Maybe MpTime
,tTag :: Maybe Text
,tAuthorId :: AnyUserId
,tCreditedUserId :: Maybe AnyUserId
,tDebitedFunds :: Amount
,tFees :: Amount
,tDebitedWalletId :: WalletId
,tCreditedWalletId:: WalletId
,tCreditedFunds :: Maybe Amount
,tStatus :: Maybe TransferStatus
,tResultCode :: Maybe Text
,tResultMessage :: Maybe Text
,tExecutionDate :: Maybe MpTime
}
deriving (Show,Eq,Ord,Typeable)
instance ToJSON Transfer where
toJSON t=objectSN ["AuthorId" .= tAuthorId t,"CreditedUserId" .= tCreditedUserId t,"DebitedFunds" .= tDebitedFunds t,
"Fees" .= tFees t,"DebitedWalletId" .= tDebitedWalletId t,"CreditedWalletId" .= tCreditedWalletId t,
"Tag" .= tTag t]
instance FromJSON Transfer where
parseJSON (Object v) =Transfer <$>
v .:? "Id" <*>
v .:? "CreationDate" <*>
v .:? "Tag" <*>
v .: "AuthorId" <*>
v .:? "CreditedUserId" <*>
v .: "DebitedFunds" <*>
v .: "Fees" <*>
v .: "DebitedWalletId" <*>
v .: "CreditedWalletId" <*>
v .:? "CreditedFunds" <*>
v .:? "Status" <*>
v .:? "ResultCode" <*>
v .:? "ResultMessage" <*>
v .:? "ExecutionDate"
parseJSON _=fail "Transfer"
data TransactionType = PAYIN
| PAYOUT
| TRANSFER
deriving (Show,Read,Eq,Ord,Bounded,Enum,Typeable)
instance ToJSON TransactionType where
toJSON =toJSON . show
instance FromJSON TransactionType where
parseJSON (String s)
| ((a,_):_)<-reads $ unpack s=pure a
parseJSON _ =fail "TransactionType"
data TransactionNature = REGULAR
| REFUND
| REPUDIATION
deriving (Show,Read,Eq,Ord,Bounded,Enum,Typeable)
instance ToJSON TransactionNature where
toJSON =toJSON . show
instance FromJSON TransactionNature where
parseJSON (String s)
| ((a,_):_)<-reads $ unpack s=pure a
parseJSON _ =fail "TransactionNature"
type TransactionId = Text
data Transaction = Transaction{
txId :: Maybe TransactionId
,txCreationDate :: Maybe MpTime
,txTag :: Maybe Text
,txAuthorId :: AnyUserId
,txCreditedUserId :: Maybe AnyUserId
,txDebitedFunds :: Amount
,txFees :: Amount
,txDebitedWalletId :: Maybe WalletId
,txCreditedWalletId :: Maybe WalletId
,txCreditedFunds :: Maybe Amount
,txStatus :: Maybe TransferStatus
,txResultCode :: Maybe Text
,txResultMessage :: Maybe Text
,txExecutionDate :: Maybe MpTime
,txType :: TransactionType
,txNature :: TransactionNature
}
deriving (Show,Eq,Ord,Typeable)
instance ToJSON Transaction where
toJSON t=objectSN ["AuthorId" .= txAuthorId t,"CreditedUserId" .= txCreditedUserId t,"DebitedFunds" .= txDebitedFunds t,
"Fees" .= txFees t,"DebitedWalletID" .= txDebitedWalletId t,"CreditedWalletID" .= txCreditedWalletId t,
"Tag" .= txTag t,"Type" .= txType t,"Nature" .= txNature t]
instance FromJSON Transaction where
parseJSON (Object v) =Transaction <$>
v .:? "Id" <*>
v .:? "CreationDate" <*>
v .:? "Tag" <*>
v .: "AuthorId" <*>
v .: "CreditedUserId" <*>
v .: "DebitedFunds" <*>
v .: "Fees" <*>
v .:? "DebitedWalletId" <*>
v .:? "CreditedWalletId" <*>
v .:? "CreditedFunds" <*>
v .:? "Status" <*>
v .:? "ResultCode" <*>
v .:? "ResultMessage" <*>
v .:? "ExecutionDate" <*>
v .: "Type" <*>
v .: "Nature"
parseJSON _=fail "Transfer"
data TransactionFilter = TransactionFilter
{ tfBefore :: Maybe MpTime
, tfAfter :: Maybe MpTime
, tfNature :: Maybe TransactionNature
, tfStatus :: Maybe TransferStatus
, tfType :: Maybe TransactionType
} deriving (Show,Eq,Ord,Typeable)
instance Default TransactionFilter where
def = TransactionFilter Nothing Nothing Nothing Nothing Nothing
transactionFilterAttributes :: TransactionFilter -> [(ByteString,Maybe ByteString)]
transactionFilterAttributes f=[ "BeforeDate" ?+ tfBefore f
, "AfterDate" ?+ tfAfter f
, "Nature" ?+ (show <$> (tfNature f))
, "Status" ?+ (show <$> (tfStatus f))
, "Type" ?+ (show <$> (tfType f))]
data TransactionSort = TxNoSort | TxByCreationDate SortDirection | TxByExecutionDate SortDirection
deriving (Show,Eq,Ord,Typeable)
instance Default TransactionSort where
def = TxNoSort
transactionSortAttributes :: TransactionSort -> [(ByteString,Maybe ByteString)]
transactionSortAttributes TxNoSort = []
transactionSortAttributes (TxByCreationDate dir)=["Sort" ?+ ("CreationDate:" ++ (map toLower $ show dir))]
transactionSortAttributes (TxByExecutionDate dir)=["Sort" ?+ ("ExecutionDate:" ++ (map toLower $ show dir))]