{-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE MultiWayIf #-}
-- CHANGE WITH CAUTION: This is a generated code file generated by https://github.com/Haskell-OpenAPI-Code-Generator/Haskell-OpenAPI-Client-Code-Generator.
{-# LANGUAGE OverloadedStrings #-}

-- | Contains the different functions to run the operation postPaymentMethodsPaymentMethodAttach
module StripeAPI.Operations.PostPaymentMethodsPaymentMethodAttach where

import qualified Control.Monad.Fail
import qualified Control.Monad.Trans.Reader
import qualified Data.Aeson
import qualified Data.Aeson as Data.Aeson.Encoding.Internal
import qualified Data.Aeson as Data.Aeson.Types
import qualified Data.Aeson as Data.Aeson.Types.FromJSON
import qualified Data.Aeson as Data.Aeson.Types.Internal
import qualified Data.Aeson as Data.Aeson.Types.ToJSON
import qualified Data.ByteString.Char8
import qualified Data.ByteString.Char8 as Data.ByteString.Internal
import qualified Data.Either
import qualified Data.Functor
import qualified Data.Scientific
import qualified Data.Text
import qualified Data.Text.Internal
import qualified Data.Time.Calendar as Data.Time.Calendar.Days
import qualified Data.Time.LocalTime as Data.Time.LocalTime.Internal.ZonedTime
import qualified Data.Vector
import qualified GHC.Base
import qualified GHC.Classes
import qualified GHC.Int
import qualified GHC.Show
import qualified GHC.Types
import qualified Network.HTTP.Client
import qualified Network.HTTP.Client as Network.HTTP.Client.Request
import qualified Network.HTTP.Client as Network.HTTP.Client.Types
import qualified Network.HTTP.Simple
import qualified Network.HTTP.Types
import qualified Network.HTTP.Types as Network.HTTP.Types.Status
import qualified Network.HTTP.Types as Network.HTTP.Types.URI
import qualified StripeAPI.Common
import StripeAPI.Types
import qualified Prelude as GHC.Integer.Type
import qualified Prelude as GHC.Maybe

-- | > POST /v1/payment_methods/{payment_method}/attach
--
-- \<p>Attaches a PaymentMethod object to a Customer.\<\/p>
--
-- \<p>To attach a new PaymentMethod to a customer for future payments, we recommend you use a \<a href=\"\/docs\/api\/setup_intents\">SetupIntent\<\/a>
-- or a PaymentIntent with \<a href=\"\/docs\/api\/payment_intents\/create\#create_payment_intent-setup_future_usage\">setup_future_usage\<\/a>.
-- These approaches will perform any necessary steps to ensure that the PaymentMethod can be used in a future payment. Using the
-- \<code>\/v1\/payment_methods\/:id\/attach\<\/code> endpoint does not ensure that future payments can be made with the attached PaymentMethod.
-- See \<a href=\"\/docs\/payments\/payment-intents\#future-usage\">Optimizing cards for future payments\<\/a> for more information about setting up future payments.\<\/p>
--
-- \<p>To use this PaymentMethod as the default for invoice or subscription payments,
-- set \<a href=\"\/docs\/api\/customers\/update\#update_customer-invoice_settings-default_payment_method\">\<code>invoice_settings.default_payment_method\<\/code>\<\/a>,
-- on the Customer to the PaymentMethod’s ID.\<\/p>
postPaymentMethodsPaymentMethodAttach ::
  forall m.
  StripeAPI.Common.MonadHTTP m =>
  -- | payment_method | Constraints: Maximum length of 5000
  Data.Text.Internal.Text ->
  -- | The request body to send
  PostPaymentMethodsPaymentMethodAttachRequestBody ->
  -- | Monadic computation which returns the result of the operation
  StripeAPI.Common.ClientT m (Network.HTTP.Client.Types.Response PostPaymentMethodsPaymentMethodAttachResponse)
postPaymentMethodsPaymentMethodAttach :: Text
-> PostPaymentMethodsPaymentMethodAttachRequestBody
-> ClientT
     m (Response PostPaymentMethodsPaymentMethodAttachResponse)
postPaymentMethodsPaymentMethodAttach
  Text
paymentMethod
  PostPaymentMethodsPaymentMethodAttachRequestBody
body =
    (Response ByteString
 -> Response PostPaymentMethodsPaymentMethodAttachResponse)
-> ClientT m (Response ByteString)
-> ClientT
     m (Response PostPaymentMethodsPaymentMethodAttachResponse)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
GHC.Base.fmap
      ( \Response ByteString
response_0 ->
          (ByteString -> PostPaymentMethodsPaymentMethodAttachResponse)
-> Response ByteString
-> Response PostPaymentMethodsPaymentMethodAttachResponse
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
GHC.Base.fmap
            ( (String -> PostPaymentMethodsPaymentMethodAttachResponse)
-> (PostPaymentMethodsPaymentMethodAttachResponse
    -> PostPaymentMethodsPaymentMethodAttachResponse)
-> Either String PostPaymentMethodsPaymentMethodAttachResponse
-> PostPaymentMethodsPaymentMethodAttachResponse
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
Data.Either.either String -> PostPaymentMethodsPaymentMethodAttachResponse
PostPaymentMethodsPaymentMethodAttachResponseError PostPaymentMethodsPaymentMethodAttachResponse
-> PostPaymentMethodsPaymentMethodAttachResponse
forall a. a -> a
GHC.Base.id
                (Either String PostPaymentMethodsPaymentMethodAttachResponse
 -> PostPaymentMethodsPaymentMethodAttachResponse)
-> (ByteString
    -> Either String PostPaymentMethodsPaymentMethodAttachResponse)
-> ByteString
-> PostPaymentMethodsPaymentMethodAttachResponse
forall b c a. (b -> c) -> (a -> b) -> a -> c
GHC.Base.. ( \Response ByteString
response ByteString
body ->
                               if
                                   | (\Status
status_1 -> Status -> Int
Network.HTTP.Types.Status.statusCode Status
status_1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
GHC.Classes.== Int
200) (Response ByteString -> Status
forall body. Response body -> Status
Network.HTTP.Client.Types.responseStatus Response ByteString
response) ->
                                     PaymentMethod -> PostPaymentMethodsPaymentMethodAttachResponse
PostPaymentMethodsPaymentMethodAttachResponse200
                                       (PaymentMethod -> PostPaymentMethodsPaymentMethodAttachResponse)
-> Either String PaymentMethod
-> Either String PostPaymentMethodsPaymentMethodAttachResponse
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Data.Functor.<$> ( ByteString -> Either String PaymentMethod
forall a. FromJSON a => ByteString -> Either String a
Data.Aeson.eitherDecodeStrict ByteString
body ::
                                                            Data.Either.Either
                                                              GHC.Base.String
                                                              PaymentMethod
                                                        )
                                   | Bool -> Status -> Bool
forall a b. a -> b -> a
GHC.Base.const Bool
GHC.Types.True (Response ByteString -> Status
forall body. Response body -> Status
Network.HTTP.Client.Types.responseStatus Response ByteString
response) ->
                                     Error -> PostPaymentMethodsPaymentMethodAttachResponse
PostPaymentMethodsPaymentMethodAttachResponseDefault
                                       (Error -> PostPaymentMethodsPaymentMethodAttachResponse)
-> Either String Error
-> Either String PostPaymentMethodsPaymentMethodAttachResponse
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Data.Functor.<$> ( ByteString -> Either String Error
forall a. FromJSON a => ByteString -> Either String a
Data.Aeson.eitherDecodeStrict ByteString
body ::
                                                            Data.Either.Either
                                                              GHC.Base.String
                                                              Error
                                                        )
                                   | Bool
GHC.Base.otherwise -> String
-> Either String PostPaymentMethodsPaymentMethodAttachResponse
forall a b. a -> Either a b
Data.Either.Left String
"Missing default response type"
                           )
                  Response ByteString
response_0
            )
            Response ByteString
response_0
      )
      (Text
-> Text
-> [QueryParameter]
-> Maybe PostPaymentMethodsPaymentMethodAttachRequestBody
-> RequestBodyEncoding
-> ClientT m (Response ByteString)
forall (m :: * -> *) body.
(MonadHTTP m, ToJSON body) =>
Text
-> Text
-> [QueryParameter]
-> Maybe body
-> RequestBodyEncoding
-> ClientT m (Response ByteString)
StripeAPI.Common.doBodyCallWithConfigurationM (Text -> Text
Data.Text.toUpper (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
GHC.Base.$ String -> Text
Data.Text.pack String
"POST") (String -> Text
Data.Text.pack (String
"/v1/payment_methods/" String -> String -> String
forall a. [a] -> [a] -> [a]
GHC.Base.++ (ByteString -> String
Data.ByteString.Char8.unpack (Bool -> ByteString -> ByteString
Network.HTTP.Types.URI.urlEncode Bool
GHC.Types.True (ByteString -> ByteString) -> ByteString -> ByteString
forall a b. (a -> b) -> a -> b
GHC.Base.$ (String -> ByteString
Data.ByteString.Char8.pack (String -> ByteString) -> String -> ByteString
forall a b. (a -> b) -> a -> b
GHC.Base.$ Text -> String
forall a. StringifyModel a => a -> String
StripeAPI.Common.stringifyModel Text
paymentMethod)) String -> String -> String
forall a. [a] -> [a] -> [a]
GHC.Base.++ String
"/attach"))) [QueryParameter]
forall a. Monoid a => a
GHC.Base.mempty (PostPaymentMethodsPaymentMethodAttachRequestBody
-> Maybe PostPaymentMethodsPaymentMethodAttachRequestBody
forall a. a -> Maybe a
GHC.Maybe.Just PostPaymentMethodsPaymentMethodAttachRequestBody
body) RequestBodyEncoding
StripeAPI.Common.RequestBodyEncodingFormData)

-- | Defines the object schema located at @paths.\/v1\/payment_methods\/{payment_method}\/attach.POST.requestBody.content.application\/x-www-form-urlencoded.schema@ in the specification.
data PostPaymentMethodsPaymentMethodAttachRequestBody = PostPaymentMethodsPaymentMethodAttachRequestBody
  { -- | customer: The ID of the customer to which to attach the PaymentMethod.
    --
    -- Constraints:
    --
    -- * Maximum length of 5000
    PostPaymentMethodsPaymentMethodAttachRequestBody -> Text
postPaymentMethodsPaymentMethodAttachRequestBodyCustomer :: Data.Text.Internal.Text,
    -- | expand: Specifies which fields in the response should be expanded.
    PostPaymentMethodsPaymentMethodAttachRequestBody -> Maybe [Text]
postPaymentMethodsPaymentMethodAttachRequestBodyExpand :: (GHC.Maybe.Maybe ([Data.Text.Internal.Text]))
  }
  deriving
    ( Int
-> PostPaymentMethodsPaymentMethodAttachRequestBody
-> String
-> String
[PostPaymentMethodsPaymentMethodAttachRequestBody]
-> String -> String
PostPaymentMethodsPaymentMethodAttachRequestBody -> String
(Int
 -> PostPaymentMethodsPaymentMethodAttachRequestBody
 -> String
 -> String)
-> (PostPaymentMethodsPaymentMethodAttachRequestBody -> String)
-> ([PostPaymentMethodsPaymentMethodAttachRequestBody]
    -> String -> String)
-> Show PostPaymentMethodsPaymentMethodAttachRequestBody
forall a.
(Int -> a -> String -> String)
-> (a -> String) -> ([a] -> String -> String) -> Show a
showList :: [PostPaymentMethodsPaymentMethodAttachRequestBody]
-> String -> String
$cshowList :: [PostPaymentMethodsPaymentMethodAttachRequestBody]
-> String -> String
show :: PostPaymentMethodsPaymentMethodAttachRequestBody -> String
$cshow :: PostPaymentMethodsPaymentMethodAttachRequestBody -> String
showsPrec :: Int
-> PostPaymentMethodsPaymentMethodAttachRequestBody
-> String
-> String
$cshowsPrec :: Int
-> PostPaymentMethodsPaymentMethodAttachRequestBody
-> String
-> String
GHC.Show.Show,
      PostPaymentMethodsPaymentMethodAttachRequestBody
-> PostPaymentMethodsPaymentMethodAttachRequestBody -> Bool
(PostPaymentMethodsPaymentMethodAttachRequestBody
 -> PostPaymentMethodsPaymentMethodAttachRequestBody -> Bool)
-> (PostPaymentMethodsPaymentMethodAttachRequestBody
    -> PostPaymentMethodsPaymentMethodAttachRequestBody -> Bool)
-> Eq PostPaymentMethodsPaymentMethodAttachRequestBody
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PostPaymentMethodsPaymentMethodAttachRequestBody
-> PostPaymentMethodsPaymentMethodAttachRequestBody -> Bool
$c/= :: PostPaymentMethodsPaymentMethodAttachRequestBody
-> PostPaymentMethodsPaymentMethodAttachRequestBody -> Bool
== :: PostPaymentMethodsPaymentMethodAttachRequestBody
-> PostPaymentMethodsPaymentMethodAttachRequestBody -> Bool
$c== :: PostPaymentMethodsPaymentMethodAttachRequestBody
-> PostPaymentMethodsPaymentMethodAttachRequestBody -> Bool
GHC.Classes.Eq
    )

instance Data.Aeson.Types.ToJSON.ToJSON PostPaymentMethodsPaymentMethodAttachRequestBody where
  toJSON :: PostPaymentMethodsPaymentMethodAttachRequestBody -> Value
toJSON PostPaymentMethodsPaymentMethodAttachRequestBody
obj = [Pair] -> Value
Data.Aeson.Types.Internal.object (Text
"customer" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Data.Aeson.Types.ToJSON..= PostPaymentMethodsPaymentMethodAttachRequestBody -> Text
postPaymentMethodsPaymentMethodAttachRequestBodyCustomer PostPaymentMethodsPaymentMethodAttachRequestBody
obj Pair -> [Pair] -> [Pair]
forall a. a -> [a] -> [a]
: Text
"expand" Text -> Maybe [Text] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Data.Aeson.Types.ToJSON..= PostPaymentMethodsPaymentMethodAttachRequestBody -> Maybe [Text]
postPaymentMethodsPaymentMethodAttachRequestBodyExpand PostPaymentMethodsPaymentMethodAttachRequestBody
obj Pair -> [Pair] -> [Pair]
forall a. a -> [a] -> [a]
: [Pair]
forall a. Monoid a => a
GHC.Base.mempty)
  toEncoding :: PostPaymentMethodsPaymentMethodAttachRequestBody -> Encoding
toEncoding PostPaymentMethodsPaymentMethodAttachRequestBody
obj = Series -> Encoding
Data.Aeson.Encoding.Internal.pairs ((Text
"customer" Text -> Text -> Series
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Data.Aeson.Types.ToJSON..= PostPaymentMethodsPaymentMethodAttachRequestBody -> Text
postPaymentMethodsPaymentMethodAttachRequestBodyCustomer PostPaymentMethodsPaymentMethodAttachRequestBody
obj) Series -> Series -> Series
forall a. Semigroup a => a -> a -> a
GHC.Base.<> (Text
"expand" Text -> Maybe [Text] -> Series
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Data.Aeson.Types.ToJSON..= PostPaymentMethodsPaymentMethodAttachRequestBody -> Maybe [Text]
postPaymentMethodsPaymentMethodAttachRequestBodyExpand PostPaymentMethodsPaymentMethodAttachRequestBody
obj))

instance Data.Aeson.Types.FromJSON.FromJSON PostPaymentMethodsPaymentMethodAttachRequestBody where
  parseJSON :: Value -> Parser PostPaymentMethodsPaymentMethodAttachRequestBody
parseJSON = String
-> (Object
    -> Parser PostPaymentMethodsPaymentMethodAttachRequestBody)
-> Value
-> Parser PostPaymentMethodsPaymentMethodAttachRequestBody
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Data.Aeson.Types.FromJSON.withObject String
"PostPaymentMethodsPaymentMethodAttachRequestBody" (\Object
obj -> ((Text
 -> Maybe [Text]
 -> PostPaymentMethodsPaymentMethodAttachRequestBody)
-> Parser
     (Text
      -> Maybe [Text]
      -> PostPaymentMethodsPaymentMethodAttachRequestBody)
forall (f :: * -> *) a. Applicative f => a -> f a
GHC.Base.pure Text
-> Maybe [Text] -> PostPaymentMethodsPaymentMethodAttachRequestBody
PostPaymentMethodsPaymentMethodAttachRequestBody Parser
  (Text
   -> Maybe [Text]
   -> PostPaymentMethodsPaymentMethodAttachRequestBody)
-> Parser Text
-> Parser
     (Maybe [Text] -> PostPaymentMethodsPaymentMethodAttachRequestBody)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
GHC.Base.<*> (Object
obj Object -> Text -> Parser Text
forall a. FromJSON a => Object -> Text -> Parser a
Data.Aeson.Types.FromJSON..: Text
"customer")) Parser
  (Maybe [Text] -> PostPaymentMethodsPaymentMethodAttachRequestBody)
-> Parser (Maybe [Text])
-> Parser PostPaymentMethodsPaymentMethodAttachRequestBody
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
GHC.Base.<*> (Object
obj Object -> Text -> Parser (Maybe [Text])
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
Data.Aeson.Types.FromJSON..:? Text
"expand"))

-- | Create a new 'PostPaymentMethodsPaymentMethodAttachRequestBody' with all required fields.
mkPostPaymentMethodsPaymentMethodAttachRequestBody ::
  -- | 'postPaymentMethodsPaymentMethodAttachRequestBodyCustomer'
  Data.Text.Internal.Text ->
  PostPaymentMethodsPaymentMethodAttachRequestBody
mkPostPaymentMethodsPaymentMethodAttachRequestBody :: Text -> PostPaymentMethodsPaymentMethodAttachRequestBody
mkPostPaymentMethodsPaymentMethodAttachRequestBody Text
postPaymentMethodsPaymentMethodAttachRequestBodyCustomer =
  PostPaymentMethodsPaymentMethodAttachRequestBody :: Text
-> Maybe [Text] -> PostPaymentMethodsPaymentMethodAttachRequestBody
PostPaymentMethodsPaymentMethodAttachRequestBody
    { postPaymentMethodsPaymentMethodAttachRequestBodyCustomer :: Text
postPaymentMethodsPaymentMethodAttachRequestBodyCustomer = Text
postPaymentMethodsPaymentMethodAttachRequestBodyCustomer,
      postPaymentMethodsPaymentMethodAttachRequestBodyExpand :: Maybe [Text]
postPaymentMethodsPaymentMethodAttachRequestBodyExpand = Maybe [Text]
forall a. Maybe a
GHC.Maybe.Nothing
    }

-- | Represents a response of the operation 'postPaymentMethodsPaymentMethodAttach'.
--
-- The response constructor is chosen by the status code of the response. If no case matches (no specific case for the response code, no range case, no default case), 'PostPaymentMethodsPaymentMethodAttachResponseError' is used.
data PostPaymentMethodsPaymentMethodAttachResponse
  = -- | Means either no matching case available or a parse error
    PostPaymentMethodsPaymentMethodAttachResponseError GHC.Base.String
  | -- | Successful response.
    PostPaymentMethodsPaymentMethodAttachResponse200 PaymentMethod
  | -- | Error response.
    PostPaymentMethodsPaymentMethodAttachResponseDefault Error
  deriving (Int
-> PostPaymentMethodsPaymentMethodAttachResponse
-> String
-> String
[PostPaymentMethodsPaymentMethodAttachResponse] -> String -> String
PostPaymentMethodsPaymentMethodAttachResponse -> String
(Int
 -> PostPaymentMethodsPaymentMethodAttachResponse
 -> String
 -> String)
-> (PostPaymentMethodsPaymentMethodAttachResponse -> String)
-> ([PostPaymentMethodsPaymentMethodAttachResponse]
    -> String -> String)
-> Show PostPaymentMethodsPaymentMethodAttachResponse
forall a.
(Int -> a -> String -> String)
-> (a -> String) -> ([a] -> String -> String) -> Show a
showList :: [PostPaymentMethodsPaymentMethodAttachResponse] -> String -> String
$cshowList :: [PostPaymentMethodsPaymentMethodAttachResponse] -> String -> String
show :: PostPaymentMethodsPaymentMethodAttachResponse -> String
$cshow :: PostPaymentMethodsPaymentMethodAttachResponse -> String
showsPrec :: Int
-> PostPaymentMethodsPaymentMethodAttachResponse
-> String
-> String
$cshowsPrec :: Int
-> PostPaymentMethodsPaymentMethodAttachResponse
-> String
-> String
GHC.Show.Show, PostPaymentMethodsPaymentMethodAttachResponse
-> PostPaymentMethodsPaymentMethodAttachResponse -> Bool
(PostPaymentMethodsPaymentMethodAttachResponse
 -> PostPaymentMethodsPaymentMethodAttachResponse -> Bool)
-> (PostPaymentMethodsPaymentMethodAttachResponse
    -> PostPaymentMethodsPaymentMethodAttachResponse -> Bool)
-> Eq PostPaymentMethodsPaymentMethodAttachResponse
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PostPaymentMethodsPaymentMethodAttachResponse
-> PostPaymentMethodsPaymentMethodAttachResponse -> Bool
$c/= :: PostPaymentMethodsPaymentMethodAttachResponse
-> PostPaymentMethodsPaymentMethodAttachResponse -> Bool
== :: PostPaymentMethodsPaymentMethodAttachResponse
-> PostPaymentMethodsPaymentMethodAttachResponse -> Bool
$c== :: PostPaymentMethodsPaymentMethodAttachResponse
-> PostPaymentMethodsPaymentMethodAttachResponse -> Bool
GHC.Classes.Eq)