git.iko.soy/typeable/connexpay
on master help sign in
Merge pull request #1 from typeable/wip/yuras/refund

Refunds

commit82a21660c0d7009d95b17f1d6e7a262e4e241345 authorYuras <shumovichy@gmail.com> dateWed 25 Sep 2024, 11:44 (2 years ago) parentee228b2a474643
4 files changed+36−11
README.org+5−1 connexpay/src/Tool.hs+10−4 connexpay/src/Web/Connexpay/Payments.hs+20−6 connexpay/src/Web/Connexpay/Types.hs+1
README.org+5−1
@@ -35,8 +35,12 @@ Examples:
3535
3636 - ~stack run -- -c ./config.yaml auth CARDNUMBER "NAME" MMYY CVV SUM~
3737 - ~stack run -- -c ./config.yaml capture SALEGUID~
38− - ~stack run -- -c ./config.yaml void SALEGUID~
38+ - ~stack run -- -c ./config.yaml void-auth AUTHONLYGUID~
39+ - ~stack run -- -c ./config.yaml void-sale SALEGUID~
40+ - ~stack run -- -c ./config.yaml void-sale SALEGUID SUM~
3941 - ~stack run -- -c ./config.yaml cancel SALEGUID~
42+ - ~stack run -- -c ./config.yaml return SALEGUID~
43+ - ~stack run -- -c ./config.yaml return SALEGUID SUM~
4044
4145Configuration file must include the values provided to you by Connexpay:
4246
connexpay/src/Tool.hs+10−4
@@ -33,9 +33,11 @@ data Config = Config { login :: Text
3333instance FromJSON Config
3434
3535data Command = AuthSale CreditCard Centi
36− | VoidSale SaleGuid
37− | CaptureSale SaleGuid
36+ | VoidAuth AuthOnlyGuid
37+ | VoidSale SaleGuid (Maybe Centi)
38+ | CaptureSale AuthOnlyGuid
3839 | CancelSale SaleGuid
40+ | ReturnSale SaleGuid (Maybe Centi)
3941 | TestAuth
4042
4143data CmdLine = CmdLine { configPath :: FilePath
@@ -46,9 +48,11 @@ cmdParser :: Parser CmdLine
4648cmdParser = CmdLine <$> strOption (short 'c' <> metavar "FILE" <> help "Configuration file path")
4749 <*> subparser operation
4850 where operation = command "auth" (info (AuthSale <$> cc <*> amt) (progDesc "Authorise payment"))
49− <> command "void" (info (VoidSale <$> guid) (progDesc "Void payment"))
51+ <> command "void-auth" (info (VoidAuth <$> guid) (progDesc "Void payment"))
52+ <> command "void-sale" (info (VoidSale <$> guid <*> optional amt) (progDesc "Void payment"))
5053 <> command "capture" (info (CaptureSale <$> guid) (progDesc "Capture payment"))
5154 <> command "cancel" (info (CancelSale <$> guid) (progDesc "Cancel payment"))
55+ <> command "return" (info (ReturnSale <$> guid <*> optional amt) (progDesc "return payment"))
5256 <> command "test-auth" (info (pure TestAuth) (progDesc "Test token authorisation"))
5357 amt = argument auto (metavar "Payment amount")
5458 cc = CreditCard <$> argument str mempty
@@ -82,7 +86,9 @@ doThing (AuthSale cc amt) = liftIO . print =<< authorisePayment cc usd pnr vendo
8286 where usd = Money amt
8387 pnr = Just "PNRPNR"
8488 vendor = Just "Typeable payment"
85−doThing (VoidSale guid) = liftIO . print =<< voidPayment guid Nothing
89+doThing (VoidAuth guid) = liftIO . print =<< voidPayment (VoidAuthorized guid)
90+doThing (VoidSale guid amt) = liftIO . print =<< voidPayment (VoidCaptured guid (Money <$> amt))
8691doThing (CaptureSale guid) = liftIO . print =<< capturePayment guid
8792doThing (CancelSale guid) = liftIO . print =<< cancelPayment guid
93+doThing (ReturnSale guid amt) = liftIO . print =<< returnPayment guid (Money <$> amt)
8894doThing TestAuth = liftIO (forever yield)
connexpay/src/Web/Connexpay/Payments.hs+20−6
@@ -4,10 +4,12 @@
44module Web.Connexpay.Payments ( CreditCard(..)
55 , AuthResponse(..)
66 , authorisePayment
7+ , VoidRequest(..)
78 , voidPayment
89 , CaptureResponse(..)
910 , capturePayment
1011 , cancelPayment
12+ , returnPayment
1113 ) where
1214
1315import Control.Monad (when,void)
@@ -104,7 +106,7 @@ sendRequestJson = sendRequest' jsonResponse
104106sendRequest_ :: Text -> [Pair] -> ConnexpayM ()
105107sendRequest_ ep = void . sendRequest' ignoreResponse ep
106108
107−data AuthResponse = AuthResponse { paymentGuid :: SaleGuid
109+data AuthResponse = AuthResponse { paymentGuid :: AuthOnlyGuid
108110 , status :: TransactionStatus
109111 , processorStatusCode :: Maybe Text
110112 , processorMessage :: Maybe Text
@@ -147,13 +149,16 @@ authorisePayment cc amt invoice vendor =
147149 -- should the need arise.
148150 tell [ "RiskData" .= KeyMap.empty @() ]
149151
152+data VoidRequest = VoidAuthorized AuthOnlyGuid | VoidCaptured SaleGuid (Maybe (Money USD))
153+
150154-- | Void payment
151−voidPayment :: SaleGuid -- ^ Sales GUID, obtained from 'authorisePayment'.
152− -> Maybe (Money USD) -- ^ Optionally, you may only void a partial sum, with the rest being subsequently charged.
155+voidPayment :: VoidRequest
153156 -> ConnexpayM ()
154−voidPayment pid amt = sendRequest_ "void" body
157+voidPayment (VoidAuthorized pid) = sendRequest_ "void" body
158+ where body = [ "AuthOnlyGuid" .= show pid ]
159+voidPayment (VoidCaptured pid amt) = sendRequest_ "void" body
155160 where body = execWriter $
156− do tell [ "AuthOnlyGuid" .= show pid ]
161+ do tell [ "SaleGuid" .= show pid ]
157162 whenJust amt $ \m ->
158163 tell [ "Amount" .= getAmount m ]
159164
@@ -191,7 +196,16 @@ capturePayment pid =
191196-- | Cancel voided or captured payment.
192197-- In case of an authorised-only payment, voiding is performed.
193198-- Otherwise, a payment goes through a refund process.
194−cancelPayment :: SaleGuid -- ^ Sales GUID, obtained from 'authorisePayment'.
199+cancelPayment :: SaleGuid -- ^ Sales GUID, obtained from 'capturePayment'.
195200 -> ConnexpayM ()
196201cancelPayment pid = sendRequest_ "cancel" body
197202 where body = [ "SaleGuid" .= show pid ]
203+
204+returnPayment :: SaleGuid -- ^ Sales GUID, obtained from 'capturePayment'.
205+ -> Maybe (Money USD)
206+ -> ConnexpayM ()
207+returnPayment pid amt = sendRequest_ "returns" body
208+ where body = execWriter $
209+ do tell [ "SaleGuid" .= show pid ]
210+ whenJust amt $ \m ->
211+ tell [ "Amount" .= getAmount m ]
connexpay/src/Web/Connexpay/Types.hs+1
@@ -19,6 +19,7 @@ import Network.HTTP.Types
1919
2020type BearerToken = Text
2121type DeviceGuid = UUID
22+type AuthOnlyGuid = UUID
2223type SaleGuid = UUID
2324type CaptureGuid = UUID
2425