From f092e030f181800f1dcc8bb952989ae8ed809a26 Mon Sep 17 00:00:00 2001 From: Praful Koppalkar <126236898+prafulkoppalkar@users.noreply.github.com> Date: Thu, 9 Nov 2023 14:48:38 +0530 Subject: [PATCH] Feat - adding prompt script (#15) --- Hyperswitch-React-Demo-App/.env | 6 ++-- Hyperswitch-React-Demo-App/package.json | 2 ++ Hyperswitch-React-Demo-App/promptScript.js | 37 ++++++++++++++++++++++ Hyperswitch-React-Demo-App/server.js | 28 +++++++++------- package.json | 3 +- 5 files changed, 61 insertions(+), 15 deletions(-) create mode 100644 Hyperswitch-React-Demo-App/promptScript.js diff --git a/Hyperswitch-React-Demo-App/.env b/Hyperswitch-React-Demo-App/.env index 60d00ff2f..f1e54809d 100644 --- a/Hyperswitch-React-Demo-App/.env +++ b/Hyperswitch-React-Demo-App/.env @@ -1,4 +1,4 @@ STATIC_DIR="./public" -HYPERSWITCH_PUBLISHABLE_KEY="GET_THIS_FROM_DASHBOARD" -HYPERSWITCH_SECRET_KEY="GET_THIS_FROM_DASHBOARD" -HYPERSWITCH_SERVER_URL="SERVER_URL" \ No newline at end of file +HYPERSWITCH_PUBLISHABLE_KEY="GET_PUBLISHABLE_KEY_FROM_DASHBOARD" +HYPERSWITCH_SECRET_KEY="GET_SECRET_KEY_FROM_DASHBOARD" +HYPERSWITCH_SERVER_URL="SELF_HOSTED_SERVER_URL" \ No newline at end of file diff --git a/Hyperswitch-React-Demo-App/package.json b/Hyperswitch-React-Demo-App/package.json index 95af70735..1e0659065 100644 --- a/Hyperswitch-React-Demo-App/package.json +++ b/Hyperswitch-React-Demo-App/package.json @@ -14,6 +14,8 @@ "express": "^4.17.1", "node-fetch": "2.6.1", "patch-package": "^8.0.0", + "prompt": "^1.3.0", + "prompt-sync": "^4.2.0", "react": "^17.0.2", "react-dom": "^17.0.2", "react-router-dom": "^6.3.0", diff --git a/Hyperswitch-React-Demo-App/promptScript.js b/Hyperswitch-React-Demo-App/promptScript.js new file mode 100644 index 000000000..99055a3a0 --- /dev/null +++ b/Hyperswitch-React-Demo-App/promptScript.js @@ -0,0 +1,37 @@ +const fs = require("fs"); +const prompt = require("prompt-sync")({ sigint: true }); +const publishableKey = prompt("Publishable Key : "); +const secretKey = prompt("Secret Key : "); +const serverURL = prompt("Server URL : "); + +const envPath = "./.env"; + +const publishableKeyDesc = "Publishable key added"; +const secretKeyDesc = "Secret key added"; +const serverURLDesc = "Server URL added"; + +function replace(filePath, oldLine, newLine, desc) { + try { + // Step 1: Read the file + let data = fs.readFileSync(filePath, "utf8"); + + // Step 2: Replace the line + data = data.replace(oldLine, newLine); + + // Step 3: Write the updated content back to the file + fs.writeFileSync(filePath, data); + + console.log(`${desc} successfully.`); + } catch (error) { + console.error(`Error: ${error.message}`); + } +} + +replace( + envPath, + "GET_PUBLISHABLE_KEY_FROM_DASHBOARD", + publishableKey, + publishableKeyDesc +); +replace(envPath, "GET_SECRET_KEY_FROM_DASHBOARD", secretKey, secretKeyDesc); +replace(envPath, "SELF_HOSTED_SERVER_URL", serverURL, serverURLDesc); diff --git a/Hyperswitch-React-Demo-App/server.js b/Hyperswitch-React-Demo-App/server.js index 093cac9ad..490e1d5ff 100644 --- a/Hyperswitch-React-Demo-App/server.js +++ b/Hyperswitch-React-Demo-App/server.js @@ -16,7 +16,10 @@ const hyper = require("@juspay-tech/hyperswitch-node")( process.env.HYPERSWITCH_SECRET_KEY ); -const SERVER_URL = process.env.HYPERSWITCH_SERVER_URL == "SERVER_URL" ? "" : process.env.HYPERSWITCH_SERVER_URL; +const SERVER_URL = + process.env.HYPERSWITCH_SERVER_URL == "SELF_HOSTED_SERVER_URL" + ? "" + : process.env.HYPERSWITCH_SERVER_URL; app.get("/config", (req, res) => { res.send({ @@ -36,17 +39,20 @@ app.get("/create-payment-intent", async (req, res) => { const request = { currency: "USD", amount: 2999, - } + }; if (SERVER_URL) { - const apiResponse = await fetch(`${process.env.HYPERSWITCH_SERVER_URL}/payments`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Accept': 'application/json', - 'api-key': process.env.HYPERSWITCH_SECRET_KEY, - }, - body: JSON.stringify(request), - }); + const apiResponse = await fetch( + `${process.env.HYPERSWITCH_SERVER_URL}/payments`, + { + method: "POST", + headers: { + "Content-Type": "application/json", + Accept: "application/json", + "api-key": process.env.HYPERSWITCH_SECRET_KEY, + }, + body: JSON.stringify(request), + } + ); paymentIntent = await apiResponse.json(); if (paymentIntent.error) { diff --git a/package.json b/package.json index d7e93927e..3fd5459e5 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,8 @@ "re:build": "rescript", "re:clean": "rescript clean", "re:start": "rescript build -w", - "start:playground": "cd Hyperswitch-React-Demo-App && npm run start", + "start:dev": "npm run re:start & npm run start", + "start:playground": "cd Hyperswitch-React-Demo-App && node promptScript.js && npm run start", "prepare": "husky install", "deploy-to-s3": "node ./scripts/pushToS3.js", "postinstall": "cd Hyperswitch-React-Demo-App && npm i && npx patch-package"