@@ -6,6 +6,7 @@ module Web.Connexpay.Payments.Types where
66import Data.Aeson
77import Data.Aeson.TH
88import Data.Fixed
9+ import Data.Function
910import Data.Int (Int32 )
1011import Data.Maybe
1112import Data.Text (Text )
@@ -116,13 +117,38 @@ instance ToJSON RiskData where
116117maskRiskData :: LogMasker RiskData
117118maskRiskData = id
118119
120+ data AuthorizeResult
121+ = Authorized AuthResponse
122+ -- ^ Transaction has been created and authorized successfully
123+ | Declined AuthResponse
124+ -- ^ Transaction has been created, but not authorized,
125+ -- e.g. declined by internal Connexpay fraud checks
126+ | Ambiguous AuthResponse
127+ -- ^ We can't interpret the result, e.g. wasProcess=true,
128+ -- but transaction status is not approved.
129+ -- Something for further investigation.
130+ deriving stock (Show )
131+
132+ postProcessAuthResponse :: AuthResponse -> AuthorizeResult
133+ postProcessAuthResponse resp = resp & case (completed, statusOk) of
134+ (True , True ) -> Authorized
135+ (False , False ) -> Declined
136+ _ -> Ambiguous
137+ where
138+ -- Connexpay documentation is not very clear about 'wasProcessed' semantics
139+ -- when it is missing, so we are intentionally strict here with the idea
140+ -- to warn about all 'Ambiguous' cases and adjust this code accordingly.
141+ completed = resp. wasProcessed == Just True
142+ statusOk = resp. status `elem` [TransactionApproved , TransactionApprovedWarning ]
143+
119144data AuthResponse = AuthResponse
120145 { guid :: AuthOnlyGuid
121146 , status :: TransactionStatus
122147 , processorStatusCode :: Maybe Text
123148 , processorResponseMessage :: Maybe Text
124149 , addressVerificationCode :: Maybe Text
125150 , cvvVerificationCode :: Maybe Text
151+ , wasProcessed :: Maybe Bool
126152 } deriving stock (Show )
127153
128154-- | Transaction status in Connexpay
@@ -139,7 +165,7 @@ data TransactionStatus
139165 -- ^ Processor errored out
140166 | TransactionOther Text
141167 -- ^ In case they return something unexpected
142- deriving stock (Show )
168+ deriving stock (Eq , Show )
143169
144170instance FromJSON TransactionStatus where
145171 parseJSON = withText " TransactionStatus" $ pure . \ case
0 commit comments