From 97b6f0236941e69577b65ec9060c8d3994ef55f6 Mon Sep 17 00:00:00 2001 From: Karol Wypchlo Date: Mon, 22 Feb 2021 18:39:01 +0100 Subject: [PATCH] cards and invoices --- .../dashboard/src/pages/api/square/cards.js | 36 ++++++++++++++-- .../src/pages/api/square/invoices.js | 42 +++++++++++++++++++ 2 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 packages/dashboard/src/pages/api/square/invoices.js diff --git a/packages/dashboard/src/pages/api/square/cards.js b/packages/dashboard/src/pages/api/square/cards.js index dcf7daec..603da8f4 100644 --- a/packages/dashboard/src/pages/api/square/cards.js +++ b/packages/dashboard/src/pages/api/square/cards.js @@ -8,17 +8,45 @@ const client = new Client({ const api = { GET: async (req, res) => { - console.log(req.headers); + const user = "R7R0NY1Z8WT11D43564EEFKTYR"; // req.headers["x-user"]; try { - const { result } = await client.customersApi.retrieveCustomer("R7R0NY1Z8WT11D43564EEFKTYR"); + const { result: customerResult } = await client.customersApi.retrieveCustomer(user); + const { customer } = customerResult; - res.json(result?.customer?.cards ?? []); + res.json(customer.cards); } catch (error) { - // console.log(error); res.json([]); } }, + // POST: async (req, res) => { + // const user = req.headers["x-user"]; + // const card = { + // cardNonce: "YOUR_CARD_NONCE", + // cardholderName: "Amelia Earhart", + // billingAddress: {}, + // verificationToken: "verification_token0", + // }; + + // card.bodyBillingAddress.addressLine1 = "500 Electric Ave"; + // card.bodyBillingAddress.addressLine2 = "Suite 600"; + // card.bodyBillingAddress.addressLine3 = "address_line_38"; + // card.bodyBillingAddress.locality = "New York"; + // card.bodyBillingAddress.sublocality = "sublocality2"; + // card.bodyBillingAddress.administrativeDistrictLevel1 = "NY"; + // card.bodyBillingAddress.postalCode = "10003"; + // card.bodyBillingAddress.country = "US"; + + // try { + // const { result } = await client.customersApi.createCustomerCard(user, card); + + // res.status(StatusCodes.NO_CONTENT); + // } catch (error) { + // console.log(Object.keys(error)); + + // res.status(StatusCodes.BAD_REQUEST); + // } + // }, }; export default (req, res) => { diff --git a/packages/dashboard/src/pages/api/square/invoices.js b/packages/dashboard/src/pages/api/square/invoices.js new file mode 100644 index 00000000..3a9c465c --- /dev/null +++ b/packages/dashboard/src/pages/api/square/invoices.js @@ -0,0 +1,42 @@ +import { Client, Environment } from "square"; +import { StatusCodes } from "http-status-codes"; + +const client = new Client({ + environment: Environment.Sandbox, + accessToken: process.env.SQUARE_ACCESS_TOKEN, +}); + +const api = { + GET: async (req, res) => { + const user = req.headers["x-user"]; + + try { + // get locations for invoices search query + const { result: locationsResponse } = await client.locationsApi.listLocations(); + const { locations } = locationsResponse; + + // create invoices serach query + const locationIds = locations.map(({ id }) => id); + const customerIds = [user, "NBE7TRXZPGZXNBD64JB6DR5AGR"].filter(Boolean); + const filter = { locationIds, customerIds }; + const sort = { field: "INVOICE_SORT_DATE", order: "DESC" }; + const query = { filter, sort }; + + // query invoices with given search criteria + const { result: invoicesResponse } = await client.invoicesApi.searchInvoices({ query, limit: 10 }); + const { invoices } = invoicesResponse; + + res.json(invoices); + } catch (error) { + res.json([]); // todo: error handling + } + }, +}; + +export default (req, res) => { + if (req.method in api) { + return api[req.method](req, res); + } + + return res.status(StatusCodes.NOT_FOUND); +};