|
| 1 | +module UON.Payment |
| 2 | + ( create, |
| 3 | + SearchParam (..), |
| 4 | + TypeId (..), |
| 5 | + CioId (..), |
| 6 | + ) |
| 7 | +where |
| 8 | + |
| 9 | +import Data.Aeson (FromJSON) |
| 10 | +import Data.Aeson qualified as Aeson |
| 11 | +import Data.Text qualified as T |
| 12 | +import GHC.Generics (Generic) |
| 13 | +import Network.HTTP.Req ((=:)) |
| 14 | +import UON qualified (Key) |
| 15 | +import UON.Internal.Param (Param (toQuery)) |
| 16 | +import UON.Internal.Request qualified as Request |
| 17 | + |
| 18 | +data SearchParam |
| 19 | + = Id Int -- ID заявки |
| 20 | + | CioId CioId -- Вид платежа |
| 21 | + | TypeId TypeId -- Тип платежа |
| 22 | + | Price Double -- Стоимость клиенту |
| 23 | + | FormId Int -- ID вида платежа |
| 24 | + | Note T.Text -- Примечание по платежу |
| 25 | + | Reason T.Text -- Основание платежа |
| 26 | + |
| 27 | +data TypeId = Clients | Partners |
| 28 | + |
| 29 | +data CioId = Arrival | Expense |
| 30 | + |
| 31 | +instance Param SearchParam where |
| 32 | + toQuery (Id x) = "r_id" =: x |
| 33 | + toQuery (Price x) = "price" =: x |
| 34 | + toQuery (Note x) = "note" =: x |
| 35 | + toQuery (Reason x) = "reason" =: x |
| 36 | + toQuery (FormId x) = "form_id" =: x |
| 37 | + toQuery (TypeId x) = |
| 38 | + "type_id" =: case x of |
| 39 | + Clients -> 1 :: Int |
| 40 | + Partners -> 2 |
| 41 | + toQuery (CioId x) = |
| 42 | + "cio_id" =: case x of |
| 43 | + Arrival -> 1 :: Int |
| 44 | + Expense -> 2 |
| 45 | + |
| 46 | +newtype Res = Res {result :: Maybe Int} |
| 47 | + deriving (Generic) |
| 48 | + |
| 49 | +instance FromJSON Res |
| 50 | + |
| 51 | +create :: UON.Key -> [SearchParam] -> IO (Either T.Text ()) |
| 52 | +create k p = do |
| 53 | + a <- Request.post ["payment", "create"] k p |
| 54 | + case a of |
| 55 | + Request.Ok (v :: Aeson.Value) -> |
| 56 | + case Aeson.fromJSON v of |
| 57 | + (Aeson.Success (Res {result = Just 200})) -> pure $ Right () |
| 58 | + _ -> pure $ Left $ "unknown result: " <> T.pack (show v) |
| 59 | + Request.Status _ t -> pure $ Left t |
| 60 | + Request.Error t -> pure $ Left t |
0 commit comments