@@ -1,211 +1,81 @@
1−{-# LANGUAGE DataKinds #-}
2−{-# LANGUAGE OverloadedStrings #-}
3−{-# LANGUAGE UndecidableInstances #-}
4−module Web.Connexpay.Payments ( CreditCard(..)
1+module Web.Connexpay.Payments
5− , AuthResponse(..)
9+ , AuthResponse(..)
10+ , TransactionStatus(..)
6− , authorisePayment
11+ , authorisePayment
7− , VoidRequest(..)
12+ , VoidRequest(..)
8− , voidPayment
13+ , voidPayment
9− , CaptureResponse(..)
14+ , CaptureResponse(..)
10− , capturePayment
16+ , capturePayment
11− , cancelPayment
17+ , cancelPayment
12− , returnPayment
18+ , returnPayment
15−import Control.Monad (when,void)
16−import Control.Monad.Except (throwError)
17−import Control.Monad.Reader (asks)
18−import Control.Monad.Writer.Strict
19import Data.Aeson
21import Data.Aeson
20−import Data.Aeson.KeyMap qualified as KeyMap
21−import Data.Aeson.Types (Pair, typeMismatch)
22−import Data.ByteString.Lazy qualified as ByteString
23−import Data.Int (Int32)
26−import Data.Text (Text)
27−import Data.Text qualified as Text
28−import Data.Text.Encoding qualified as Text
29−import GHC.TypeError as TypeError
30−import Network.HTTP.Req
32−import Web.Connexpay.Data
23+import Web.Connexpay.Http
24+import Web.Connexpay.Payments.Types
33import Web.Connexpay.Types
25import Web.Connexpay.Types
34−import Web.Connexpay.Utils
37−-- No 'Show' instance should be made for this type
38−-- in order to avoid sensitive data leaks.
39−data CreditCard = CreditCard { number :: Text -- ^ Credit card number, as 'Text'.
40− , cardholder :: Maybe Text -- ^ Cardholder name, optional.
41− , expiration :: (Word, Word) -- ^ Expiration date (month,year).
42− , cvv :: Maybe Text -- ^ CVC/CVV code.
45−type ShowError = TypeError.Text "CreditCard must not be shown in order to avoid leaking sensitive data"
47−instance TypeError ShowError => Show CreditCard where
48− show = error "UNREACHABLE"
50−instance ToJSON CreditCard where
53− $ do tell ["CardNumber" .= cc.number]
54− whenJust cc.cardholder $ \name ->
55− tell ["CardHolderName" .= name]
56− whenJust cc.cvv $ \cvv ->
58− let expDate = padDate (tshow (snd cc.expiration)) <> padDate (tshow (fst cc.expiration))
59− tell ["ExpirationDate" .= expDate]
61−padDate :: Text -> Text
62−padDate t | Text.length t == 1 = "0" <> t
65−sendRequest' :: HttpResponse resp => Proxy resp -> Text -> [Pair] -> ConnexpayM resp
66−sendRequest' resp endpoint body =
67− do tok <- bearerToken
69− tls <- asks (.useTLS)
70− let auth = header "Authorization" ("Bearer " <> Text.encodeUtf8 tok)
71− url s = s host /: "api" /: "v1" /: endpoint
72− obj <- object <$> addGuid body
73− let jbody = ReqBodyJson obj
75− then reqCb POST (url https) jbody resp auth (logRequest obj)
76− else reqCb POST (url http) jbody resp auth (logRequest obj)
81− do guid <- asks (.deviceGuid)
82− return (b <> [ "DeviceGuid" .= show guid ])
84− do log_ <- asks (.logAction)
85− -- Remove card info from logs
88− | Just c <- KeyMap.lookup "Card" obj -> Object (KeyMap.insert "Card" (replaceCard c) obj)
90− msg = Text.unlines [ "Connexpay request:"
92− , Text.decodeUtf8 (ByteString.toStrict $ encode v')
94− _ <- liftIO (log_ msg)
96− replaceCard (Object c) =
98− $ KeyMap.insert "CardNumber" (String "<REDACTED>")
99− $ KeyMap.insert "Cvv2" (String "<REDACTED>")
103−sendRequestJson :: FromJSON a => Text -> [Pair] -> ConnexpayM (JsonResponse a)
104−sendRequestJson = sendRequest' jsonResponse
106−sendRequest_ :: Text -> [Pair] -> ConnexpayM ()
107−sendRequest_ ep = void . sendRequest' ignoreResponse ep
109−data AuthResponse = AuthResponse { paymentGuid :: AuthOnlyGuid
110− , status :: TransactionStatus
111− , processorStatusCode :: Maybe Text
112− , processorMessage :: Maybe Text
115−instance FromJSON AuthResponse where
116− parseJSON (Object o) = AuthResponse <$> o .: "guid"
118− <*> o .:? "processorStatusCode"
119− <*> o .:? "processorResponseMessage"
120− parseJSON v = typeMismatch "AuthReponse" v
122-- | Authorise a credit card payment.
28-- | Authorise a credit card payment.
123−authorisePayment :: CreditCard -- ^ Credit card details (see 'CreditCard')
29+authorisePayment
124− -> Money USD -- ^ Amount to charge, USD
30+ :: Connexpay -> Env -> AuthRequest -> IO (Response AuthError AuthorizeResult)
125− -> Maybe Text -- ^ Invoice description
126− -> Maybe Text -- ^ Merchant description that will appear in a customer's statement.
127− -> ConnexpayM AuthResponse
128−authorisePayment cc amt invoice vendor =
31+authorisePayment connexpay env raw =
129− do resp <- sendRequestJson "authonlys" body
32+ bimapResponse (guessErrorType guessAuthError) postProcessAuthResponse <$>
130− let rbody = responseBody resp
33+ doRequest connexpay env "authonlys" RequestBody
131− -- Special case for Connexpay local transaction
34+ { raw
132− -- This status means that the transaction was registered,
35+ , logMasker = maskAuthRequest
133− -- but Connexpay stopped its processing and it won't be
36+ }
134− -- moved any further.
135− -- Also, when I asked Ken from Connexpay about this,
136− -- he told me he had never seen this status before.
137− when (rbody.status == TransactionCreatedLocal) $
138− throwError (PaymentFailure LocalTransaction)
140− where body = execWriter $
141− do tell [ "Card" .= cc ]
142− tell [ "Amount" .= getAmount amt ]
143− whenJust invoice $ \i ->
144− tell [ "OrderNumber" .= i ]
145− whenJust vendor $ \v ->
146− tell [ "StatementDescription" .= v ]
147− -- We are supposed to pass RiskData, but it still
148− -- can be an empty object. Consider population this
149− -- should the need arise.
150− tell [ "RiskData" .= KeyMap.empty @() ]
152−data VoidRequest = VoidAuthorized AuthOnlyGuid | VoidCaptured SaleGuid (Maybe (Money USD))
154-- | Void payment
38-- | Void payment
155−voidPayment :: VoidRequest
39+voidPayment :: Connexpay -> Env -> VoidRequest -> IO (Response () ())
156− -> ConnexpayM ()
40+voidPayment connexpay env raw = doRequest_ connexpay env "void" RequestBody
157−voidPayment (VoidAuthorized pid) = sendRequest_ "void" body
41+ { raw
158− where body = [ "AuthOnlyGuid" .= show pid ]
42+ , logMasker = id
159−voidPayment (VoidCaptured pid amt) = sendRequest_ "void" body
43+ }
160− where body = execWriter $
161− do tell [ "SaleGuid" .= show pid ]
162− whenJust amt $ \m ->
163− tell [ "Amount" .= getAmount m ]
165−-- | Internal data type for Capture requests.
166−data CPTransaction = CPTransaction { expectedPayments :: Int32 }
168−instance ToJSON CPTransaction where
169− toJSON t = object [ "ExpectedPayments" .= t.expectedPayments ]
171−-- | Response for the payment capture request
172−data CaptureResponse = CaptureResponse { captureGuid :: CaptureGuid
173− , saleGuid :: SaleGuid
174− , saleStatus :: TransactionStatus
177−instance FromJSON CaptureResponse where
178− parseJSON (Object o) =
179− do cguid <- o .: "guid"
181− saleGuid <- sale .: "guid"
182− status <- sale .: "status"
183− pure (CaptureResponse cguid saleGuid status)
184− parseJSON v = typeMismatch "CaptureResponse" v
186-- | Capture payment, previously authorised through 'authorisePayment'.
45-- | Capture payment, previously authorised through 'authorisePayment'.
46+capturePayment :: Connexpay
187−capturePayment :: SaleGuid -- ^ Sales GUID, obtained from 'authorisePayment'.
48+ -> AuthOnlyGuid -- ^ Sales GUID, obtained from 'authorisePayment'.
188− -> ConnexpayM CaptureResponse
49+ -> IO (Response () CaptureResponse)
189−capturePayment pid =
50+capturePayment connexpay env pid = doRequest connexpay env "Captures" RequestBody
190− do resp <- sendRequestJson "Captures" body
51+ { raw = object
191− let rbody = responseBody resp
193− where body = [ "AuthOnlyGuid" .= show pid
52+ [ "AuthOnlyGuid" .= pid
194− , "ConnexPayTransaction" .= CPTransaction 1 ]
53+ , "ConnexPayTransaction" .= CPTransaction 1
196-- | Cancel voided or captured payment.
58-- | Cancel voided or captured payment.
197-- In case of an authorised-only payment, voiding is performed.
59-- In case of an authorised-only payment, voiding is performed.
198-- Otherwise, a payment goes through a refund process.
60-- Otherwise, a payment goes through a refund process.
61+cancelPayment :: Connexpay
199−cancelPayment :: SaleGuid -- ^ Sales GUID, obtained from 'capturePayment'.
63+ -> SaleGuid -- ^ Sales GUID, obtained from 'capturePayment'.
200− -> ConnexpayM ()
64+ -> IO (Response () ())
201−cancelPayment pid = sendRequest_ "cancel" body
65+cancelPayment connexpay env pid = doRequest_ connexpay env "cancel" RequestBody
202− where body = [ "SaleGuid" .= show pid ]
66+ { raw = object [ "SaleGuid" .= pid ]
70+returnPayment :: Connexpay
204−returnPayment :: SaleGuid -- ^ Sales GUID, obtained from 'capturePayment'.
72+ -> SaleGuid -- ^ Sales GUID, obtained from 'capturePayment'.
205− -> Maybe (Money USD)
73+ -> USD
206− -> ConnexpayM ()
74+ -> IO (Response () ())
207−returnPayment pid amt = sendRequest_ "return" body
75+returnPayment connexpay env pid amt = doRequest_ connexpay env "returns" RequestBody
208− where body = execWriter $
76+ { raw = object
209− do tell [ "SaleGuid" .= show pid ]
77+ [ "SaleGuid" .= pid
210− whenJust amt $ \m ->
211− tell [ "Amount" .= getAmount m ]
78+ , "Amount" .= amt