Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -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);
Expand All @@ -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 = [
Expand Down Expand Up @@ -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,
Expand All @@ -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.
Expand All @@ -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
Expand Down