diff --git a/node/index.js b/node/index.js index 96c1f64..df59411 100644 --- a/node/index.js +++ b/node/index.js @@ -33,7 +33,7 @@ const dotenv = require('dotenv').config(); // Loads .env file const express = require('express'); const cookieParser = require('cookie-parser'); const crypto = require('crypto') -const { ApiError, Client, Environment } = require('square'); +const { SquareError, SquareClient, SquareEnvironment } = require('square'); const app = express(); app.use(cookieParser()); app.use(express.static(__dirname + '/public')); @@ -45,10 +45,10 @@ let basePath; let environment; if (SQ_ENVIRONMENT.toLowerCase() === "production") { basePath = `https://connect.squareup.com`; - environment = Environment.Production; + environment = SquareEnvironment.Production; } else if (SQ_ENVIRONMENT.toLowerCase() === "sandbox") { basePath = `https://connect.squareupsandbox.com`; - environment = Environment.Sandbox; + environment = SquareEnvironment.Sandbox; } else { console.warn('Unsupported value for SQ_ENVIRONMENT in .env file.'); process.exit(1); @@ -64,13 +64,13 @@ const port = PORT || "8000"; const messages = require('./messages'); // Configure Square defcault client -const squareClient = new Client({ +const squareClient = new SquareClient({ environment: environment, userAgentDetail: "sample_app_oauth_node" // Remove or replace this detail when building your own app }); // Configure Square OAuth API instance -const oauthInstance = squareClient.oAuthApi; +const oauthInstance = squareClient.oAuth; // INCLUDE PERMISSIONS YOU WANT YOUR SELLER TO GRANT YOUR APPLICATION const scopes = [ @@ -137,14 +137,14 @@ app.get('/callback', async (req, res) => { }); } } - // When the response_type is "code", the seller clicked Allow - // and the authorization page returned the auth tokens. - else if ("code" === req.query["response_type"]) { + // When the code parameter is present, the seller clicked Allow + // and the authorization page returned the authorization code. + else if (req.query['code']) { // Extract the returned authorization code from the URL var { code } = req.query; try { - let { result } = await oauthInstance.obtainToken({ + let response = await oauthInstance.obtainToken({ // Provide the code in a request to the Obtain Token endpoint code, clientId: process.env.SQ_APPLICATION_ID, @@ -158,7 +158,7 @@ app.get('/callback', async (req, res) => { refreshToken, expiresAt, merchantId - } = result; + } = response; // Because we want to keep things simple and we're using Sandbox, // we call a function that writes the tokens to the page so we can easily copy and use them directly. @@ -169,7 +169,7 @@ app.get('/callback', async (req, res) => { }); } catch (error) { // The response from the Obtain Token endpoint did not include an access token. Something went wrong. - if (error instanceof ApiError) { + if (error instanceof SquareError) { content = messages.displayError('Exception', JSON.stringify(error.result)) res.render('base', { content: content