Skip to content

Commit

Permalink
Feat - adding prompt script (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
prafulkoppalkar authored Nov 9, 2023
1 parent 4a234ba commit f092e03
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 15 deletions.
6 changes: 3 additions & 3 deletions Hyperswitch-React-Demo-App/.env
Original file line number Diff line number Diff line change
@@ -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"
HYPERSWITCH_PUBLISHABLE_KEY="GET_PUBLISHABLE_KEY_FROM_DASHBOARD"
HYPERSWITCH_SECRET_KEY="GET_SECRET_KEY_FROM_DASHBOARD"
HYPERSWITCH_SERVER_URL="SELF_HOSTED_SERVER_URL"
2 changes: 2 additions & 0 deletions Hyperswitch-React-Demo-App/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
37 changes: 37 additions & 0 deletions Hyperswitch-React-Demo-App/promptScript.js
Original file line number Diff line number Diff line change
@@ -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);
28 changes: 17 additions & 11 deletions Hyperswitch-React-Demo-App/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit f092e03

Please sign in to comment.