git.iko.soy/typeable/connexpay
on master help sign in
Fix capture.
commit450606bb3555aa3ff31daa0497a3dfd069da2935 authorNick <nick@typeable.io> dateSat 23 Mar 2024, 23:54 (3 years ago) parent799bfc1
4 files changed+19−9
src/Tool.hs+3−3 src/Web/Connexpay/Init.hs+3−2 src/Web/Connexpay/Payments.hs+12−4 src/Web/Connexpay/Types.hs+1
src/Tool.hs+3−3
@@ -18,12 +18,12 @@ import Network.HTTP.Client
1818import Network.HTTP.Client.TLS
1919import Options.Applicative
2020import Web.Connexpay
21−import Web.Connexpay (capturePayment)
2221
2322data Config = Config { login :: Text
2423 , password :: Text
2524 , host :: Text
2625 , device_guid :: UUID
26+ , use_tls :: Bool
2727 , proxy_host :: Maybe Text
2828 , proxy_port :: Maybe Word
2929 } deriving Generic
@@ -68,7 +68,7 @@ main = do cmdLine <- execParser (info cmdParser mempty)
6868 let proxy = useProxy (Proxy (Text.encodeUtf8 host) (fromIntegral port))
6969 s = managerSetProxy proxy defaultManagerSettings
7070 return (newManager s)
71− res <- initConnexpay writeLog mgr cnf.device_guid cnf.host cnf.login cnf.password
71+ res <- initConnexpay writeLog mgr cnf.device_guid cnf.host cnf.use_tls cnf.login cnf.password
7272 case res of
7373 Left err -> putStrLn ("Error: " <> show err)
7474 Right cpi -> print =<< runConnexpay cpi (doThing cmdLine.operation)
@@ -77,4 +77,4 @@ doThing :: Command -> ConnexpayM ()
7777doThing (AuthSale cc amt) = liftIO . print =<< authorisePayment cc usd
7878 where usd = Money amt
7979doThing (VoidSale guid) = liftIO . print =<< voidPayment guid Nothing
80−doThing (CaptureSale guid) = liftIO . print =<< capturePayment
80+doThing (CaptureSale guid) = liftIO . print =<< capturePayment guid
src/Web/Connexpay/Init.hs+3−2
@@ -14,8 +14,8 @@ import Data.Text (Text)
1414import Network.HTTP.Client (Manager)
1515import Numeric.Natural
1616
17−initConnexpay :: (Text -> IO ()) -> Manager -> DeviceGuid -> Text -> Text -> Text -> IO (Either PaymentError Connexpay)
18−initConnexpay logf mgr devguid url login password =
17+initConnexpay :: (Text -> IO ()) -> Manager -> DeviceGuid -> Text -> Bool -> Text -> Text -> IO (Either PaymentError Connexpay)
18+initConnexpay logf mgr devguid url tls login password =
1919 do v <- newEmptyMVar
2020 let env = Connexpay { logAction = logf
2121 , manager = mgr
@@ -23,6 +23,7 @@ initConnexpay logf mgr devguid url login password =
2323 , refreshAsync = Nothing
2424 , deviceGuid = devguid
2525 , url = url
26+ , useTLS = tls
2627 , login = login
2728 , password = password
2829 }
src/Web/Connexpay/Payments.hs+12−4
@@ -9,13 +9,12 @@ module Web.Connexpay.Payments ( CreditCard(..)
99 , cancelPayment
1010 ) where
1111
12−import Web.Connexpay.Utils
13−
1412import Control.Monad (void)
1513import Control.Monad.Reader (asks)
1614import Control.Monad.Writer.Strict
1715import Data.Aeson
1816import Data.Aeson.Types (Pair, typeMismatch)
17+import Data.Int (Int32)
1918import Data.Money
2019import Data.Proxy
2120import Data.Text (Text)
@@ -26,6 +25,7 @@ import Network.HTTP.Req
2625
2726import Web.Connexpay.Data
2827import Web.Connexpay.Types
28+import Web.Connexpay.Utils
2929
3030-- | Credit card info
3131-- No 'Show' instance should be made for this type
@@ -61,7 +61,7 @@ sendRequest' resp endpoint body =
6161 do tok <- bearerToken
6262 host <- asks (.url)
6363 let auth = header "Authorization" ("Bearer " <> Text.encodeUtf8 tok)
64− url = http host /: "api" /: "v1" /: endpoint
64+ url = https host /: "api" /: "v1" /: endpoint
6565 jbody <- ReqBodyJson . object <$> addGuid body
6666 req POST url jbody resp auth
6767 where addGuid b = do guid <- asks (.deviceGuid)
@@ -100,9 +100,17 @@ voidPayment pid amt = sendRequest_ "void" body
100100 whenJust amt $ \m ->
101101 tell [ "Amount" .= getAmount m ]
102102
103+-- | Internal data type for Capture requests.
104+data CPTransaction = CPTransaction { expectedPayments :: Int32 }
105+
106+instance ToJSON CPTransaction where
107+ toJSON t = object [ "ExpectedPayments" .= t.expectedPayments ]
108+
109+
103110capturePayment :: SaleGuid -> ConnexpayM ()
104111capturePayment pid = sendRequest_ "Captures" body
105− where body = [ "AuthOnlyGuid" .= show pid ]
112+ where body = [ "AuthOnlyGuid" .= show pid
113+ , "ConnexPayTransaction" .= CPTransaction 1 ]
106114
107115cancelPayment :: SaleGuid -> ConnexpayM ()
108116cancelPayment pid = sendRequest_ "cancel" body
src/Web/Connexpay/Types.hs+1
@@ -24,6 +24,7 @@ data Connexpay = Connexpay { logAction :: Text -> IO ()
2424 , refreshAsync :: Maybe (Async ())
2525 , deviceGuid :: DeviceGuid
2626 , url :: Text
27+ , useTLS :: Bool
2728 , login :: Text
2829 , password :: Text
2930 }