Skip to content

Commit 076cba8

Browse files
committed
Uploaded server side code to Firebase functions
1 parent 8d5d9e3 commit 076cba8

14 files changed

+1418
-165
lines changed

Diff for: .babelrc

-4
This file was deleted.

Diff for: .firebaserc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"projects": {
3+
"default": "stripe-payment-id"
4+
}
5+
}

Diff for: .gitignore

-2
This file was deleted.

Diff for: firebase.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"functions": [
3+
{
4+
"source": "functions",
5+
"codebase": "default",
6+
"ignore": [
7+
"node_modules",
8+
".git",
9+
"firebase-debug.log",
10+
"firebase-debug.*.log"
11+
],
12+
"predeploy": [
13+
"npm --prefix \"$RESOURCE_DIR\" run build"
14+
]
15+
}
16+
]
17+
}

Diff for: functions/.babelrc

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"presets": [["@babel/preset-env", {
3+
"targets": {
4+
"node": "18"
5+
}
6+
}]],
7+
"plugins": ["@babel/plugin-transform-runtime"]
8+
}

Diff for: functions/.gitignore

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Compiled JavaScript files
2+
lib/**/*.js
3+
lib/**/*.js.map
4+
5+
# TypeScript v1 declaration files
6+
typings/
7+
8+
# Node.js dependency directory
9+
node_modules/
10+
11+
# Environment variables file
12+
.env

Diff for: package.json renamed to functions/package.json

+26-17
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,39 @@
11
{
2-
"name": "stripe-payment",
3-
"version": "1.0.0",
4-
"description": "Stripe payment using Node with TypeScript, Express, and Stripe",
5-
"main": "./src/app.ts",
2+
"name": "functions",
63
"scripts": {
7-
"dev": "nodemon ./src/app.ts",
8-
"start": "node ./dist/app.js"
4+
"build": "tsc",
5+
"build:watch": "tsc --watch",
6+
"serve": "npm run build && firebase emulators:start --only functions",
7+
"shell": "npm run build && firebase functions:shell",
8+
"start": "npm run shell",
9+
"deploy": "firebase deploy --only functions",
10+
"logs": "firebase functions:log"
911
},
12+
"engines": {
13+
"node": "18"
14+
},
15+
"main": "lib/index.js",
1016
"keywords": [
1117
"Payment",
18+
"Firebase",
1219
"TypeScript",
1320
"Node",
1421
"Express",
1522
"Stripe"
1623
],
1724
"author": "gigaamiridze",
18-
"license": "ISC",
25+
"dependencies": {
26+
"firebase-admin": "^11.8.0",
27+
"firebase-functions": "^4.3.1",
28+
"cors": "^2.8.5",
29+
"dotenv": "^16.3.1",
30+
"express": "^4.18.2",
31+
"morgan": "^1.10.0",
32+
"stripe": "^13.3.0"
33+
},
1934
"devDependencies": {
35+
"typescript": "^4.9.0",
36+
"firebase-functions-test": "^3.1.0",
2037
"@babel/cli": "^7.22.10",
2138
"@babel/core": "^7.22.11",
2239
"@babel/node": "^7.22.10",
@@ -26,15 +43,7 @@
2643
"@types/express": "^4.17.17",
2744
"@types/morgan": "^1.9.5",
2845
"@types/node": "^20.5.6",
29-
"nodemon": "^3.0.1",
30-
"ts-node": "^10.9.1",
31-
"typescript": "^5.2.2"
46+
"ts-node": "^10.9.1"
3247
},
33-
"dependencies": {
34-
"cors": "^2.8.5",
35-
"dotenv": "^16.3.1",
36-
"express": "^4.18.2",
37-
"morgan": "^1.10.0",
38-
"stripe": "^13.3.0"
39-
}
48+
"private": true
4049
}

Diff for: functions/src/env.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import * as dotenv from 'dotenv';
2+
3+
dotenv.config();
4+
5+
export const { STRIPE_PUBLISHABLE_KEY, STRIPE_SECRET_KEY } = process.env;

Diff for: src/app.ts renamed to functions/src/index.ts

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import express, { Application, Request, Response } from 'express';
1+
import { onRequest } from 'firebase-functions/v2/https';
2+
import * as express from 'express';
3+
import * as morgan from 'morgan';
4+
import * as cors from 'cors';
25
import Stripe from 'stripe';
3-
import morgan from 'morgan';
4-
import cors from 'cors';
5-
import { PORT, STRIPE_PUBLISHABLE_KEY, STRIPE_SECRET_KEY } from './env';
66

7-
const app: Application = express();
7+
const app = express();
88

99
app.use(cors());
1010
app.use(morgan('dev'));
1111
app.use(express.json());
1212

13-
app.post('/create-payment-intent', async (req: Request, res: Response) => {
13+
app.post('/create-payment-intent', async (req, res) => {
1414
const { email, currency, amount } = req.body;
1515

16-
if (STRIPE_SECRET_KEY) {
17-
const stripe = new Stripe(STRIPE_SECRET_KEY, {
16+
if (process.env.STRIPE_SECRET_KEY) {
17+
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
1818
apiVersion: '2023-08-16',
1919
});
2020

@@ -44,8 +44,10 @@ app.post('/create-payment-intent', async (req: Request, res: Response) => {
4444
});
4545
}
4646
}
47-
});
4847

49-
app.listen(PORT, () => {
50-
console.log(`Server listening on port ${PORT}`);
48+
return res.status(500).send({
49+
error: 'Stripe secret key is missing or invalid.',
50+
});
5151
});
52+
53+
export const stripePayment = onRequest({ maxInstances: 10 }, app);

Diff for: functions/tsconfig.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"noImplicitReturns": true,
5+
"noUnusedLocals": true,
6+
"outDir": "lib",
7+
"sourceMap": true,
8+
"strict": true,
9+
"target": "es2017",
10+
},
11+
"compileOnSave": true,
12+
"include": [
13+
"src"
14+
]
15+
}

Diff for: functions/ui-debug.log

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
C:\Users\User\.cache\firebase\emulators\ui-v1.11.5\server\server.js:569
2+
re"use strict";
3+
^^^^^^^^^^^^
4+
5+
SyntaxError: Unexpected string
6+
at internalCompileFunction (node:internal/vm:73:18)
7+
at wrapSafe (node:internal/modules/cjs/loader:1178:20)
8+
at Module._compile (node:internal/modules/cjs/loader:1220:27)
9+
at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
10+
at Module.load (node:internal/modules/cjs/loader:1119:32)
11+
at Module._load (node:internal/modules/cjs/loader:960:12)
12+
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
13+
at node:internal/main/run_main_module:23:47
14+
15+
Node.js v18.16.1

0 commit comments

Comments
 (0)