Skip to content

Commit

Permalink
Added small server app to serve keys and create session with Defined …
Browse files Browse the repository at this point in the history
…README.md (#16)

* Added small server app to serve keys and create session with Defined README.md

* Formatted LICENSE

* Fixed Readme

* Removed server/.gitignore
  • Loading branch information
AmniX authored Sep 27, 2024
1 parent 2ed022c commit f349902
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 0 deletions.
77 changes: 77 additions & 0 deletions server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
firebase-debug.log*
firebase-debug.*.log*

# Firebase cache
.firebase/

# Firebase config

# Uncomment this if you'd like others to create their own Firebase project.
# For a team working on the same Firebase project(s), it is recommended to leave
# it commented so all members can deploy to the same project(s) in .firebaserc.
# .firebaserc

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# dataconnect generated files
.dataconnect

# Don't push firebase related things
.firebaserc
firebase.json
/functions/node_modules/
/functions/*.local
/functions/package.json
/functions/package-lock.json
21 changes: 21 additions & 0 deletions server/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Degica

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
55 changes: 55 additions & 0 deletions server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
## Description / 説明

This is a lightweight server application designed to function as a backend for the demo app.

デモアプリのバックエンドとして機能する軽量なサーバーアプリです。

### Setup / 設定

<details>
<summary>English / 英語</summary>

* Ensure you have an existing Firebase project in your Firebase console, or create a new one.
* Enable Cloud Functions.
* Install Firebase CLI tools using the command `npm install -g firebase-tools`.
* Log in to Firebase CLI with `firebase login`.
* After successful login, initialize Firebase in the server directory using `firebase init`.
* Choose ` ◉ Functions: Configure a Cloud Functions directory and its files` and press enter.
* Select `Use an existing project`.
* Choose the project created in step 1.
* Choose `JavaScript`.
* Respond **No** to ` Do you want to use ESLint to catch probable bugs and enforce style?`.
* Respond **No** to `File functions/index.js already exists. Overwrite?` (ensure this is correct).
* Respond **Yes** to `Do you want to install dependencies with npm now?`.
* Configure the `.env` file according to the `.env.sample`.
* Run `firebase deploy` to build and deploy the server to Firebase Cloud Functions.
* Wait for the process to complete, and your server should be up and running.

</details>


<details>
<summary>Japanese / 日本語</summary>

* FirebaseコンソールにFirebaseプロジェクトを作成するか、既存のプロジェクトを確認してください。
* Cloud Functionsを有効にします。
* Firebase CLIツールをインストールします: `npm install -g firebase-tools`.
* `firebase login` を使ってFirebase CLIにログインします。
* ログインが成功したら、サーバーディレクトリで `firebase init` を実行してFirebaseを初期化します。
* ` ◉ Functions: Configure a Cloud Functions directory and its files` を選択してEnterを押します。
* `既存のプロジェクトを使用`を選択します。
* ステップ1で作成したプロジェクトを選択します。
* `JavaScript` を選択します。
* ` Do you want to use ESLint to catch probable bugs and enforce style?` に対して**いいえ**と回答します。
* `File functions/index.js already exists. Overwrite?` に対して**いいえ**と回答します(必ず確認してください)。
* `Do you want to install dependencies with npm now?` に対して**はい**と回答します。
* `.env.sample`に基づいて `.env` ファイルを設定します。
* `firebase deploy` を実行して、サーバーをFirebase Cloud Functionsにデプロイします。
* デプロイが完了するまで待ち、サーバーが稼働していることを確認します。

</details>

### Support / サポート
If you encounter any issues, please open an issue.

何か問題が発生した場合は、問題を報告してください。
3 changes: 3 additions & 0 deletions server/functions/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PUBLISHABLE_KEY=<YOUR_PUBLISHABLE_KEY_FROM_KOMOJU_DASHBOARD>
SECRET_KEY=<YOUR_SECRET_KEY_FROM_KOMOJU_DASHBOARD>
RETURN_URL=<YOUR_REDIRECT_URL>
76 changes: 76 additions & 0 deletions server/functions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const httpsOptions = {
region: "asia-northeast1" // Tokyo
// region: "asia-northeast2" // Osaka
};

const { onRequest } = require("firebase-functions/v2/https");
const express = require('express');
const app = express();

app.get('/:serve-key', (req, res) => res.send({
publishableKey: process.env.PUBLISHABLE_KEY
}));

app.post("/:create-session", async (req, res) => {
const {
amount,
currency,
language
} = req.body;
const secretKey = process.env.SECRET_KEY;

try {
const sessionId = await createSession({
amount,
currency,
language,
secretKey,
});
res.send({
sessionId
});
console.log(sessionId)
} catch (error) {
res.status(500);
res.send({
error: error.message
})
}
});

async function createSession({
amount,
currency,
language,
secretKey
}) {
if (!secretKey) {
throw new Error("Secret Key Required");
}

const url = "https://komoju.com/api/v1/sessions";
const options = {
method: "POST",
headers: {
accept: "application/json",
"content-type": "application/json",
Authorization: `Basic ${Buffer.from(secretKey + ":").toString("base64")}`,
"X-KOMOJU-API-VERSION": "2024-07-15",
},
body: JSON.stringify({
default_locale: language ?? "ja",
amount,
currency,
return_url: process.env.RETURN_URL,
}),
};
const response = await fetch(url, options);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const {
id
} = await response.json();
return id;
}
exports.komoju = onRequest(httpsOptions, app);

0 comments on commit f349902

Please sign in to comment.