Răsfoiți Sursa

Updated site to accept square oauth flow all the way through

Lee Morgan 6 ani în urmă
părinte
comite
91e035e9c7

+ 4 - 0
controllers/merchantData.js

@@ -124,6 +124,10 @@ module.exports = {
             });
     },
 
+    createMerchantSquare: function(req, res){
+        
+    },
+
     //DELETE - removes a single recipe from the merchant
     removeRecipe: function(req, res){
         if(!req.session.user){

+ 58 - 1
controllers/otherData.js

@@ -52,6 +52,11 @@ module.exports = {
         return res.redirect(`${process.env.CLOVER_ADDRESS}/oauth/authorize?client_id=${process.env.SUBLINE_CLOVER_APPID}&redirect_uri=${process.env.SUBLINE_CLOVER_URI}`);
     },
 
+    //GET - Redirects user to Square OAuth page
+    squareRedirect: function(req, res){
+        return res.redirect(`https://connect.squareupsandbox.com/oauth2/authorize?client_id=${process.env.SUBLINE_SQUARE_APPID}&scope=INVENTORY_READ+ITEMS_READ+MERCHANT_PROFILE_READ+ORDERS_READ+PAYMENTS_READ`);
+    },
+
     //GET - Get access token from clover and  redirect to merchant creation
     cloverAuth: function(req, res){
         let dataArr = req.url.slice(req.url.indexOf("?") + 1).split("&");
@@ -97,5 +102,57 @@ module.exports = {
                 req.session.error = "ERROR: UNABLE TO RETRIEVE DATA FROM CLOVER";
                 return res.redirect("/");
             });
+    },
+
+    squareAuth: function(req, res){
+        const code = req.url.slice(req.url.indexOf("code=") + 5, req.url.indexOf("&"));
+        const url = `${process.env.SQUARE_ADDRESS}/oauth2/token?`;
+        let data = {
+            client_id: process.env.SUBLINE_SQUARE_APPID,
+            client_secret: process.env.SUBLINE_SQUARE_APPSECRET,
+            grant_type: "authorization_code",
+            code: code
+        };
+
+        axios.post(url, data)
+            .then((response)=>{
+                data = response.data;
+                return Merchant.findOne({posId: data.merchant_id});
+            })
+            .then((merchant)=>{
+                if(merchant){
+                    merchant.posAccessToken = data.access_token;
+
+                    return merchant.save()
+                        .then((merchant)=>{
+                            req.session.user = merchant._id;
+                            return res.redirect("/dashboard");
+                        })
+                        .catch((err)=>{
+                            req.session.error = "ERROR: UNABLE TO CREATE NEW USER";
+                            return res.redirect("/");
+                        })
+                }else{
+                    req.session.merchantId = data.merchant_id;
+                    req.session.accessToken = data.access_token;
+
+                    return res.redirect("/merchant/create/square");
+                }
+            })
+            .catch((err)=>{
+                req.session.error = "ERROR: UNABLE TO RETRIEVE DATA FROM SQUARE";
+                return res.redirect("/");
+            });
     }
-} 
+} 
+
+/*
+{
+  access_token: 'EAAAEM5tPhVM2L2MkxzjgA2ATIeyDKFiZzzZEsvPwSB_YSRKyXVrTVZrg_qHNgXe',
+  token_type: 'bearer',
+  expires_at: '2020-08-29T09:39:11Z',
+  merchant_id: 'MLGGD4TQVAM4G',
+  refresh_token: 'EQAAEOGMweecHJhOnxYyQP95Pj3oP-9KaQP027Ad6RQTSAN3He5A55lN47dXHjV2'
+}
+
+*/

+ 4 - 1
routes.js

@@ -16,6 +16,7 @@ module.exports = function(app){
     //Merchant
     app.post("/merchant/create/none", merchantData.createMerchantNone);
     app.get("/merchant/create/clover", merchantData.createMerchantClover);
+    app.get("/merchant/create/square", merchantData.createMerchantSquare);
     app.delete("/merchant/recipes/remove/:id", merchantData.removeRecipe);
     app.post("/merchant/ingredients/add", merchantData.addMerchantIngredient);
     app.delete("/merchant/ingredients/remove/:id", merchantData.removeMerchantIngredient);
@@ -48,5 +49,7 @@ module.exports = function(app){
     app.post("/login", otherData.login);
     app.get("/logout", otherData.logout);
     app.get("/cloverlogin", otherData.cloverRedirect);
-    app.get("/cloverauth*", otherData.cloverAuth);   
+    app.get("/squarelogin", otherData.squareRedirect);
+    app.get("/cloverauth*", otherData.cloverAuth);
+    app.get("/squareauth", otherData.squareAuth);
 }

+ 0 - 6
views/dashboardPage/bundle.js

@@ -1856,7 +1856,6 @@ module.exports = {
 },{}],13:[function(require,module,exports){
 module.exports = {
     display: function(Recipe){
-        console.log("display");
         let ingredientsSelect = document.querySelector("#recipeInputIngredients select");
         let categories = merchant.categorizeIngredients();
 
@@ -1883,7 +1882,6 @@ module.exports = {
 
     //Updates the number of ingredient inputs displayed for new recipes
     changeRecipeCount: function(){
-        console.log("doing things");
         let newCount = document.getElementById("ingredientCount").value;
         let ingredientsDiv = document.getElementById("recipeInputIngredients");
         let oldCount = ingredientsDiv.children.length;
@@ -1959,7 +1957,6 @@ module.exports = {
                 }
             })
             .catch((err)=>{
-                console.log(err);
                 banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
             })
             .finally(()=>{
@@ -2152,7 +2149,6 @@ module.exports = {
                     }
                 })
                 .catch((err)=>{
-                    console.log(err);
                     banner.createError("SOMETHING WENT WRONG. TRY REFRESHING THE PAGE");
                 })
                 .finally(()=>{
@@ -2658,7 +2654,6 @@ module.exports = {
                 }
             })
             .catch((err)=>{
-                console.log(err);
                 banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
             })
             .finally(()=>{
@@ -2814,7 +2809,6 @@ module.exports = {
                 }
             })
             .catch((err)=>{
-                console.log(err);
                 banner.createError("UNABLE TO DISPLAY THE TRANSACTIONS");
             })
             .finally(()=>{

+ 0 - 1
views/dashboardPage/js/transactions.js

@@ -145,7 +145,6 @@ module.exports = {
                 }
             })
             .catch((err)=>{
-                console.log(err);
                 banner.createError("UNABLE TO DISPLAY THE TRANSACTIONS");
             })
             .finally(()=>{

+ 6 - 3
views/landingPage/landing.ejs

@@ -80,10 +80,11 @@
 
                 <h1>OR</h1>
 
-                <a class="button buttonWithBorder" href="/cloverlogin"> Sign Up with Clover </a>
+                <a class="button buttonWithBorder" href="/cloverlogin">Sign Up with Clover</a>
 
-                <h3 class="link" style="margin-top: 30px;" onclick="loginObj.display()"> Already have an account? Please Sign In </h3>
+                <a class="button buttonWithBorder" href="/squarelogin">Sign Up with Square</a>
 
+                <h3 class="link" style="margin-top: 30px;" onclick="loginObj.display()"> Already have an account? Please Sign In </h3>
             </form>
         </div>
 
@@ -103,7 +104,9 @@
 
                 <h1>OR</h1>
 
-                <a class="button buttonWithBorder" href="/cloverlogin"> Sign In with Clover </a>
+                <a class="button buttonWithBorder" href="/cloverlogin">Sign In with Clover</a>
+
+                <a class="button buttonWithBorder" href="/squarelogin">Sign In with Square</a>
 
                 <h3 class="link" style="margin-top: 30px;" onclick="registerObj.display()"> New Here? Please Sign Up </h3>
             </form>