cards and invoices

This commit is contained in:
Karol Wypchlo 2021-02-22 18:39:01 +01:00
parent bc76402300
commit 97b6f02369
2 changed files with 74 additions and 4 deletions

View File

@ -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) => {

View File

@ -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);
};