Browse Source

delete card api (#17)

- implemented delete card record
- implemented ToRequest instance for delete card record
- tested delete api
master
piq9117 1 year ago
committed by Gitea
parent
commit
5c4e116a6f
4 changed files with 31 additions and 4 deletions
  1. +1
    -1
      src/Square/Internal/CardNonce.hs
  2. +2
    -0
      src/Square/Internal/UrlSchema.hs
  3. +18
    -0
      src/Square/Request/Customer.hs
  4. +10
    -3
      test/CustomerSpec.hs

+ 1
- 1
src/Square/Internal/CardNonce.hs View File

@@ -30,7 +30,7 @@ data CardNonceReq = CardNonceReq
} deriving ( Eq, Show )

data CardNonceRes = CardNonceRes
{ _CardNonceResCardNonce :: Text
{ _cardNonceResCardNonce :: Text
} deriving ( Eq, Show )

data CardData = CardData


+ 2
- 0
src/Square/Internal/UrlSchema.hs View File

@@ -20,6 +20,7 @@ data UrlSchema = UrlSchema
, _urlSchemaRetrieveCustomer :: SquareEndpoint
, _urlSchemaUpdateCustomer :: SquareEndpoint
, _urlSchemaCreateCard :: SquareEndpoint
, _urlSchemaDeleteCard :: SquareEndpoint
} deriving ( Eq, Show )

newtype SquareEndpoint = SquareEndpoint Text
@@ -42,6 +43,7 @@ urlSchema rootUrl = UrlSchema
, _urlSchemaSearchCustomer = SquareEndpoint $ rootUrlToText rootUrl <> "/v2/customers/search"
, _urlSchemaRetrieveCustomer = SquareEndpoint $ rootUrlToText rootUrl <> "/v2/customers/"
, _urlSchemaUpdateCustomer = SquareEndpoint $ rootUrlToText rootUrl <> "/v2/customers/"
, _urlSchemaDeleteCard = SquareEndpoint $ rootUrlToText rootUrl <> "/v2/customers/"
-- The url for this is v2/customers/{customer_id}/cards, the segment {customer_id}/cards
-- is added in the ToRequest instance
, _urlSchemaCreateCard = SquareEndpoint $ rootUrlToText rootUrl <> "/v2/customers/"


+ 18
- 0
src/Square/Request/Customer.hs View File

@@ -230,6 +230,7 @@ instance ToRequest CreateCustomerCardReq where
Nothing
( Just $ toJSON cccr )
where
mkSquareUrl :: SquareEndpoint -> SquareEndpoint
mkSquareUrl root = addSegment "/cards" $ addUrlParam _createCustomerCardReqCustomerId $ root

defaultCreateCustomerReq :: CreateCustomerCardReq
@@ -247,6 +248,23 @@ defaultCreateCustomerReq = CreateCustomerCardReq
Nothing
)

data DeleteCardReq = DeleteCardReq
{ _deleteCardReqCustomerId :: Text
, _deleteCardReqCardId :: Text
} deriving ( Eq, Show )

instance ToRequest DeleteCardReq where
toRequest rootUrl accessToken DeleteCardReq{..} = buildRequest
( mkSquareUrl $ urlSchema rootUrl ^. urlSchemaDeleteCard )
accessToken
DELETE
Nothing
Nothing
where
mkSquareUrl :: SquareEndpoint -> SquareEndpoint
mkSquareUrl root = addSegment
( _deleteCardReqCustomerId <> "/cards/" <> _deleteCardReqCardId ) root

---
-- json derivation
---


+ 10
- 3
test/CustomerSpec.hs View File

@@ -4,6 +4,7 @@ module CustomerSpec where
import TestImport
-- json
import Data.Aeson
import Data.Aeson.Types
-- uuid
import Data.UUID as UUID
import Data.UUID.V4 as UUID
@@ -65,7 +66,7 @@ spec = do
let result = concat $ join $ res ^? _Right . getCustomerResCustomers
assertEqual "list will not be empty" ( length result > 0 ) True

it "will create a customer card" $ do
focus $ it "will create and delete customer card" $ do
token <- accessToken
appId <- applicationId
analToken <- analyticsToken
@@ -85,6 +86,8 @@ spec = do
& cardNonceReqAnalyticsToken .~ analToken
& cardNonceReqSessionId .~ sessId

assertNotEqual "customer id will not be empty" mCustomerId ( Just "" )
assertNotEqual "customer id will not be empty" mCustomerId Nothing

( cardNonceRes :: Either Error CardNonceRes ) <- runExceptT $ runSquare cardNonceRequest squareEnv
let cardNonceResult = cardNonceRes ^? _Right . cardNonceResCardNonce
@@ -94,9 +97,13 @@ spec = do
& createCustomerCardReqCardNonce .~ ( fromMaybe "" cardNonceResult )
& createCustomerCardReqCardholderName .~ "customer1 customer1"
& createCustomerCardReqBillingAddress . customerAddressPostalCode .~ Just "111111"

( res :: Either Error CreateCardRes ) <- runExceptT $ runSquare createCardRequest squareEnv
let result = ( res ^? _Right . createCardResCard & join ) <&> ( ^. createCardResBodyId ) & join
assertNotEqual "" result ( Just "" )
let mCardId = ( res ^? _Right . createCardResCard & join ) <&> ( ^. createCardResBodyId ) & join
assertNotEqual "credit card id will be present" mCardId ( Just "" )
let deleteCardRequest = DeleteCardReq ( fromMaybe "" mCustomerId ) ( fromMaybe "" mCardId )
( deleteCardRes :: Either Error Value ) <- runExceptT $ runSquare deleteCardRequest squareEnv
assertEqual "" ( deleteCardRes ^? _Right ) ( Just emptyObject )

it "will search for customers" $ do
token <- accessToken


Loading…
Cancel
Save