module Web.MangoPay.Users where
import Web.MangoPay.Monad
import Web.MangoPay.Types
import Data.CountryCodes (CountryCode)
import Data.Text
import Data.Typeable (Typeable)
import Data.Aeson
import Data.Time.Clock.POSIX (POSIXTime)
import Control.Applicative
createNaturalUser :: (MPUsableMonad m) => NaturalUser -> AccessToken -> MangoPayT m NaturalUser
createNaturalUser = createGeneric "/users/natural"
modifyNaturalUser :: (MPUsableMonad m) => NaturalUser -> AccessToken -> MangoPayT m NaturalUser
modifyNaturalUser u = modifyGeneric "/users/natural/" u' uId
where u' = u{uProofOfIdentity = Nothing, uProofOfAddress = Nothing}
fetchNaturalUser :: (MPUsableMonad m) => NaturalUserId -> AccessToken -> MangoPayT m NaturalUser
fetchNaturalUser = fetchGeneric "/users/natural/"
createLegalUser :: (MPUsableMonad m) => LegalUser -> AccessToken -> MangoPayT m LegalUser
createLegalUser = createGeneric "/users/legal"
modifyLegalUser :: (MPUsableMonad m) => LegalUser -> AccessToken -> MangoPayT m LegalUser
modifyLegalUser u = modifyGeneric "/users/legal/" u lId
fetchLegalUser :: (MPUsableMonad m) => LegalUserId -> AccessToken -> MangoPayT m LegalUser
fetchLegalUser = fetchGeneric "/users/legal/"
getUser :: (MPUsableMonad m) => AnyUserId -> AccessToken -> MangoPayT m (Either NaturalUser LegalUser)
getUser = fetchGeneric "/users/"
listUsers :: (MPUsableMonad m) => GenericSort -> Maybe Pagination -> AccessToken -> MangoPayT m (PagedList UserRef)
listUsers gs = genericListExtra (sortAttributes gs) ["/users/"]
getExistingUserId
:: Either NaturalUser LegalUser
-> AnyUserId
getExistingUserId u | Just uid <- either uId lId u = uid
getExistingUserId _ = error "Web.MangoPay.Users.getExistingUserId: Nothing"
instance FromJSON (Either NaturalUser LegalUser) where
parseJSON o@(Object v)=do
pt::PersonType<-v .: "PersonType"
case pt of
Natural->Left <$> parseJSON o
Legal->Right <$> parseJSON o
parseJSON _=fail "EitherUsers"
type AnyUserId = Text
type NaturalUserId = Text
data NaturalUser=NaturalUser {
uId :: Maybe NaturalUserId
,uCreationDate :: Maybe POSIXTime
,uEmail :: Text
,uFirstName :: Text
,uLastName :: Text
,uAddress :: Maybe Text
,uBirthday :: POSIXTime
,uNationality :: CountryCode
,uCountryOfResidence:: CountryCode
,uOccupation :: Maybe Text
,uIncomeRange :: Maybe IncomeRange
,uTag :: Maybe Text
,uProofOfIdentity :: Maybe Text
,uProofOfAddress :: Maybe Text
}
deriving (Show,Eq,Ord,Typeable)
instance ToJSON NaturalUser where
toJSON u=objectSN ["Tag" .= uTag u,"Email" .= uEmail u,"FirstName".= uFirstName u,"LastName" .= uLastName u,"Address" .= uAddress u, "Birthday" .= uBirthday u
,"Nationality" .= uNationality u,"CountryOfResidence" .= uCountryOfResidence u,"Occupation" .= uOccupation u, "IncomeRange" .= uIncomeRange u,"ProofOfIdentity" .= uProofOfIdentity u
,"ProofOfAddress" .= uProofOfAddress u,"PersonType" .= Natural]
instance FromJSON NaturalUser where
parseJSON (Object v) =NaturalUser <$>
v .: "Id" <*>
v .: "CreationDate" <*>
v .: "Email" <*>
v .: "FirstName" <*>
v .: "LastName" <*>
v .:? "Address" <*>
v .: "Birthday" <*>
v .: "Nationality" <*>
v .: "CountryOfResidence" <*>
v .:? "Occupation" <*>
v .:? "IncomeRange" <*>
v .:? "Tag" <*>
v .:? "ProofOfIdentity" <*>
v .:? "ProofOfAddress"
parseJSON _= fail "NaturalUser"
type LegalUserId = Text
data LegalUserType = Business | Organization
deriving (Show,Read,Eq,Ord,Enum,Bounded,Typeable)
instance ToJSON LegalUserType where
toJSON Business="BUSINESS"
toJSON Organization="ORGANIZATION"
instance FromJSON LegalUserType where
parseJSON (String "BUSINESS") =pure Business
parseJSON (String "ORGANIZATION") =pure Organization
parseJSON _= fail "LegalUserType"
data LegalUser=LegalUser {
lId :: Maybe Text
,lCreationDate :: Maybe POSIXTime
,lEmail :: Text
,lName :: Text
,lLegalPersonType :: LegalUserType
,lHeadquartersAddress :: Maybe Text
,lLegalRepresentativeFirstName :: Text
,lLegalRepresentativeLastName :: Text
,lLegalRepresentativeAddress :: Maybe Text
,lLegalRepresentativeEmail :: Maybe Text
,lLegalRepresentativeBirthday :: POSIXTime
,lLegalRepresentativeNationality :: CountryCode
,lLegalRepresentativeCountryOfResidence :: CountryCode
,lStatute :: Maybe Text
,lTag :: Maybe Text
,lProofOfRegistration :: Maybe Text
,lShareholderDeclaration :: Maybe Text
}
deriving (Show,Eq,Ord,Typeable)
instance ToJSON LegalUser where
toJSON u=objectSN ["Tag" .= lTag u,"Email" .= lEmail u,"Name".= lName u,"LegalPersonType" .= lLegalPersonType u,"HeadquartersAddress" .= lHeadquartersAddress u, "LegalRepresentativeFirstName" .= lLegalRepresentativeFirstName u
,"LegalRepresentativeLastName" .= lLegalRepresentativeLastName u,"LegalRepresentativeAddress" .= lLegalRepresentativeAddress u,"LegalRepresentativeEmail" .= lLegalRepresentativeEmail u, "LegalRepresentativeBirthday" .= lLegalRepresentativeBirthday u,"LegalRepresentativeNationality" .= lLegalRepresentativeNationality u
,"LegalRepresentativeCountryOfResidence" .= lLegalRepresentativeCountryOfResidence u,"Statute" .= lStatute u,"ProofOfRegistration" .=lProofOfRegistration u,"ShareholderDeclaration" .=lShareholderDeclaration u,"PersonType" .= Legal]
instance FromJSON LegalUser where
parseJSON (Object v) =LegalUser <$>
v .: "Id" <*>
v .: "CreationDate" <*>
v .: "Email" <*>
v .: "Name" <*>
v .: "LegalPersonType" <*>
v .:? "HeadquartersAddress" <*>
v .: "LegalRepresentativeFirstName" <*>
v .: "LegalRepresentativeLastName" <*>
v .:? "LegalRepresentativeAddress" <*>
v .:? "LegalRepresentativeEmail" <*>
v .: "LegalRepresentativeBirthday" <*>
v .: "LegalRepresentativeNationality" <*>
v .: "LegalRepresentativeCountryOfResidence" <*>
v .:? "Statute" <*>
v .:? "Tag" <*>
v .:? "ProofOfRegistration" <*>
v .:? "ShareholderDeclaration"
parseJSON _= fail "NaturalUser"
data PersonType = Natural | Legal
deriving (Show,Read,Eq,Ord,Bounded,Enum,Typeable)
instance ToJSON PersonType where
toJSON Natural="NATURAL"
toJSON Legal="LEGAL"
instance FromJSON PersonType where
parseJSON (String "NATURAL") =pure Natural
parseJSON (String "LEGAL") =pure Legal
parseJSON _= fail "PersonType"
data UserRef=UserRef {
urId :: AnyUserId
, urCreationDate :: POSIXTime
, urPersonType :: PersonType
, urEmail :: Text
, urTag :: Maybe Text
}
deriving (Show,Eq,Ord,Typeable)
instance ToJSON UserRef where
toJSON ur=objectSN [ "PersonType" .= urPersonType ur, "Email" .= urEmail ur,"Id" .= urId ur,
"Tag" .= urTag ur,"CreationDate" .= urCreationDate ur]
instance FromJSON UserRef where
parseJSON (Object v) =UserRef <$>
v .: "Id" <*>
v .: "CreationDate"<*>
v .: "PersonType" <*>
v .: "Email" <*>
v .:? "Tag"
parseJSON _=fail "UserRef"