· 6 years ago · Oct 17, 2019, 12:00 PM
1
2-- I want this top function to compose the other two Io actions.
3
4newDefaultPaymentMethod :: Stripe.ApiSecretKey -> Stripe.CustomerId -> Stripe.PaymentMethodId -> IO (Either Stripe.Error Value)
5newDefaultPaymentMethod k cId pmId =
6 attachPaymentMethod k cId pmId >=> makeDefaultPaymentMethod k cId pmId
7
8
9attachPaymentMethod :: Stripe.ApiSecretKey -> Stripe.CustomerId -> Stripe.PaymentMethodId -> IO (Either Stripe.Error Value)
10attachPaymentMethod secretKey (Stripe.CustomerId customerId) (Stripe.PaymentMethodId paymentMethodId) = do
11 session <- Stripe.newAPISession
12 response <- fmap Stripe.wreqResponse (Stripe.post session secretKey request)
13 return $ Stripe.responseValue response
14 where
15 request =
16 Post
17 { postPath = ["payment_methods", paymentMethodId, "attach"]
18 , postParams = [ "customer" := customerId ]
19 }
20
21makeDefaultPaymentMethod :: Stripe.ApiSecretKey -> Stripe.CustomerId -> Stripe.PaymentMethodId -> IO (Either Stripe.Error Value)
22makeDefaultPaymentMethod secretKey (Stripe.CustomerId customerId) (Stripe.PaymentMethodId paymentMethodId) = do
23 session <- Stripe.newAPISession
24 response <- fmap Stripe.wreqResponse (Stripe.post session secretKey request)
25 return $ Stripe.responseValue response
26 where
27 invoiceSettingsDict =
28 Aeson.object [
29 "invoice_settings" .= Aeson.object [
30 "default_payment_method" .= show paymentMethodId
31 ]
32 ]
33 request =
34 Post
35 { postPath = ["customer", customerId]
36 , postParams = [ "invoice_settings" := show invoiceSettingsDict ]
37 }