const axios = require('axios');
var dateFormat = require('dateformat');
const express = require("@runkit/runkit/express-endpoint/1.0.0");
const app = express(exports);
// allow requests from everywhere - so that the codepen works:
// https://codepen.io/FattLabs/pen/MBMGLV/?editors=0110
const cors = require("cors");
app.use(cors());
// nice formatting when viewing in browser
app.set('json spaces', 2);
app.get("/catalog", async (req, res) => {
// make a call to the Fattmerchant api for the FIRST PAGE of catalog items
// using the `item` resource:
// apiary: https://fattmerchant.docs.apiary.io/#reference/0/catalog/retrieve-all-catalog-items
// in a better demo you could loop through and gather all page by page
const result = await axios({
method: 'GET',
url: `${process.env.fattmerchantDevApi}/item`,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.myVipTourApiKey}`,
'Accept': 'application/json'
}
});
return res.json(result.data.data); // first page of items only
})
// hit GET with https://myviptoursitems-n1t0wikq6iqq.runkit.sh/special-enrollment?customer_id=2f79328f-5d57-497e-893d-ccf53ee43d88&payment_method_id=13f74e48-5b42-4213-8f51-bc57fdd8b878
app.get("/special-enrollment", async (req, res) => {
// we need tomorrows date for the schedule RRULE
const tomorrow = new Date();
tomorrow.setTime( tomorrow.getTime() + 1 * 86400000 );
// this is the id of the "special enrollment" item in my fattmerchant catalog
const enrollmentItemId = '4c0ec212-a07f-42eb-bf9f-4203272b8085';
// get the special item from the core api
// we will use this in the line items for the schedule, below
const specialEnrollmentItemRequest = await axios({
method: 'GET',
url: `${process.env.fattmerchantDevApi}/item?id=${enrollmentItemId}`,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.myVipTourApiKey}`,
'Accept': 'application/json'
}
});
// this will be a paginated result and will only contain one item
const specialEnrollmentItem = specialEnrollmentItemRequest.data.data[0];
// based on the apiary: https://fattmerchant.docs.apiary.io/#reference/0/scheduled-invoices
const postBody = {
"rule": `FREQ=MONTHLY;DTSTART=${dateFormat(tomorrow, 'yyyymmdd')}T000000Z`,
"customer_id": req.query.customer_id,
"payment_method_id": req.query.payment_method_id,
"total": specialEnrollmentItem.price,
"meta": {
"lineItems":[specialEnrollmentItem], // just put the whole item as a line item
"memo": specialEnrollmentItem.item,
"reference":"",
"subtotal": specialEnrollmentItem.price,"tax":0
},
"url": "https://fattpay.com/#/bill/", // required for the payment button in the invoice emails
"email_notification": false // will not send an email when invoices are generated
};
// now post the new schedule to fattmerchant api
// using the postbody we defined above
const result = await axios({
method: 'POST',
url: `${process.env.fattmerchantDevApi}/invoice/schedule`,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.myVipTourApiKey}`,
'Accept': 'application/json'
},
data: postBody
})
return res.json(result.data);
})