Files
bodyshop/server/accounting/qbo/qbo-callback.js
2021-08-26 15:48:10 -07:00

70 lines
2.1 KiB
JavaScript

const path = require("path");
require("dotenv").config({
path: path.resolve(
process.cwd(),
`.env.${process.env.NODE_ENV || "development"}`
),
});
const OAuthClient = require("intuit-oauth");
var QuickBooks = require("node-quickbooks");
const Promise = require("bluebird");
const QuickBooksPromise = Promise.promisifyAll(QuickBooks.prototype);
const oauthClient = new OAuthClient({
clientId: process.env.QBO_CLIENT_ID,
clientSecret: process.env.QBO_SECRET,
environment: process.env.NODE_ENV === "production" ? "production" : "sandbox",
redirectUri: process.env.QBO_REDIRECT_URI,
logging: true,
});
exports.default = async (req, res) => {
// Parse the redirect URL for authCode and exchange them for tokens
try {
// Exchange the auth code retrieved from the **req.url** on the redirectUri
const authResponse = await oauthClient.createToken(req.url);
const { access_token, refresh_token } = authResponse.json;
//store this information against the assocation record.
//Send a redirect back to the imex online application
res.json(authResponse.json);
// var qbo = new QuickBooks(
// process.env.QBO_CLIENT_ID,
// process.env.QBO_SECRET,
// access_token,
// false, // no token secret for oAuth 2.0
// realmId,
// process.env.NODE_ENV !== "production", // use the sandbox?, // use the sandbox?
// true, // enable debugging?
// null, // set minorversion, or null for the latest version
// "2.0", //oAuth version
// refresh_token
// );
// qbo.findInvoices({ fetchAll: true }, (errors, invoices) =>
// console.log(errors, invoices)
// );
} catch (e) {
console.error("The error message is :" + JSON.stringify(e, null, 2));
console.error(e.intuit_tid);
res.status(500).json(e);
}
};
exports.refresh = async (req, res) => {
try {
oauthClient.setToken(req.cookies.qbo_access_token);
const authResponse = oauthClient.refreshUsingToken(
req.cookies.qbo_refresh_token
);
res.json(authResponse.json);
} catch (error) {
res.status(500).json(error);
}
};