Skip to content

Commit e53f505

Browse files
committed
new example
1 parent 5f43bc1 commit e53f505

File tree

9 files changed

+496
-0
lines changed

9 files changed

+496
-0
lines changed

examples/simple-billing/.gitignore

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
2+
3+
# Logs
4+
5+
logs
6+
_.log
7+
npm-debug.log_
8+
yarn-debug.log*
9+
yarn-error.log*
10+
lerna-debug.log*
11+
.pnpm-debug.log*
12+
13+
# Caches
14+
15+
.cache
16+
17+
# Diagnostic reports (https://nodejs.org/api/report.html)
18+
19+
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
20+
21+
# Runtime data
22+
23+
pids
24+
_.pid
25+
_.seed
26+
*.pid.lock
27+
28+
# Directory for instrumented libs generated by jscoverage/JSCover
29+
30+
lib-cov
31+
32+
# Coverage directory used by tools like istanbul
33+
34+
coverage
35+
*.lcov
36+
37+
# nyc test coverage
38+
39+
.nyc_output
40+
41+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
42+
43+
.grunt
44+
45+
# Bower dependency directory (https://bower.io/)
46+
47+
bower_components
48+
49+
# node-waf configuration
50+
51+
.lock-wscript
52+
53+
# Compiled binary addons (https://nodejs.org/api/addons.html)
54+
55+
build/Release
56+
57+
# Dependency directories
58+
59+
node_modules/
60+
jspm_packages/
61+
62+
# Snowpack dependency directory (https://snowpack.dev/)
63+
64+
web_modules/
65+
66+
# TypeScript cache
67+
68+
*.tsbuildinfo
69+
70+
# Optional npm cache directory
71+
72+
.npm
73+
74+
# Optional eslint cache
75+
76+
.eslintcache
77+
78+
# Optional stylelint cache
79+
80+
.stylelintcache
81+
82+
# Microbundle cache
83+
84+
.rpt2_cache/
85+
.rts2_cache_cjs/
86+
.rts2_cache_es/
87+
.rts2_cache_umd/
88+
89+
# Optional REPL history
90+
91+
.node_repl_history
92+
93+
# Output of 'npm pack'
94+
95+
*.tgz
96+
97+
# Yarn Integrity file
98+
99+
.yarn-integrity
100+
101+
# dotenv environment variable files
102+
103+
.env
104+
.env.development.local
105+
.env.test.local
106+
.env.production.local
107+
.env.local
108+
109+
# parcel-bundler cache (https://parceljs.org/)
110+
111+
.parcel-cache
112+
113+
# Next.js build output
114+
115+
.next
116+
out
117+
118+
# Nuxt.js build / generate output
119+
120+
.nuxt
121+
dist
122+
123+
# Gatsby files
124+
125+
# Comment in the public line in if your project uses Gatsby and not Next.js
126+
127+
# https://nextjs.org/blog/next-9-1#public-directory-support
128+
129+
# public
130+
131+
# vuepress build output
132+
133+
.vuepress/dist
134+
135+
# vuepress v2.x temp and cache directory
136+
137+
.temp
138+
139+
# Docusaurus cache and generated files
140+
141+
.docusaurus
142+
143+
# Serverless directories
144+
145+
.serverless/
146+
147+
# FuseBox cache
148+
149+
.fusebox/
150+
151+
# DynamoDB Local files
152+
153+
.dynamodb/
154+
155+
# TernJS port file
156+
157+
.tern-port
158+
159+
# Stores VSCode versions used for testing VSCode extensions
160+
161+
.vscode-test
162+
163+
# yarn v2
164+
165+
.yarn/cache
166+
.yarn/unplugged
167+
.yarn/build-state.yml
168+
.yarn/install-state.gz
169+
.pnp.*
170+
171+
# IntelliJ based IDEs
172+
.idea
173+
174+
# Finder (MacOS) folder config
175+
.DS_Store

examples/simple-billing/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Simple billing (no error handling)
2+
3+
4+
![image](https://github.com/user-attachments/assets/85e384b6-d55e-4ab1-82d6-428a58d1dd2b)
5+
6+
7+
![image](https://github.com/user-attachments/assets/1ac8e6de-eb37-4638-b5a5-a8b4b4ac7278)

examples/simple-billing/bun.lockb

38.1 KB
Binary file not shown.

examples/simple-billing/package.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "simple-billing",
3+
"module": "index.ts",
4+
"type": "module",
5+
"devDependencies": {
6+
"@types/bun": "latest"
7+
},
8+
"peerDependencies": {
9+
"typescript": "^5.0.0"
10+
},
11+
"dependencies": {
12+
"@types/express": "^5.0.0",
13+
"ejs": "^3.1.10",
14+
"express": "^4.21.2"
15+
}
16+
}
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
* {
2+
box-sizing: border-box;
3+
}
4+
5+
body {
6+
font-family: Arial, sans-serif;
7+
margin: 0;
8+
padding: 0;
9+
display: flex;
10+
justify-content: center;
11+
align-items: center;
12+
height: 100vh;
13+
background-color: #121212;
14+
color: #ffffff;
15+
}
16+
17+
.container {
18+
background: #1e1e1e;
19+
padding: 20px;
20+
border-radius: 8px;
21+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.5);
22+
max-width: 100%;
23+
width: auto;
24+
text-align: center;
25+
box-sizing: border-box;
26+
}
27+
28+
.input-field {
29+
width: 100%;
30+
padding: 10px;
31+
margin-bottom: 15px;
32+
border: 1px solid #333;
33+
border-radius: 4px;
34+
background-color: #2c2c2c;
35+
color: #ffffff;
36+
box-sizing: border-box;
37+
}
38+
39+
.container h1 {
40+
font-size: 1.5em;
41+
margin-bottom: 20px;
42+
}
43+
44+
45+
46+
.container button {
47+
background-color: #2a2a2b;
48+
color: white;
49+
border: none;
50+
padding: 10px 15px;
51+
border-radius: 4px;
52+
cursor: pointer;
53+
width: 100%;
54+
}
55+
56+
.container button:hover {
57+
background-color: #0056b3;
58+
}
59+
60+
.message {
61+
margin-top: 15px;
62+
font-size: 0.9em;
63+
}
64+
65+
.qr-code {
66+
margin-top: 15px;
67+
max-width: 100%;
68+
height: auto;
69+
}

examples/simple-billing/src/app.ts

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import express, { type Request, type Response } from 'express';
2+
import path from 'path';
3+
4+
5+
const app = express();
6+
const PORT = 3000;
7+
8+
app.set('view engine', 'ejs');
9+
10+
11+
const API_URL = "http://127.0.0.1:8888/v1";
12+
const API_KEY = "f176ab8c801bf7576e6944fff0f24b3477087ad8f664e2a0bd811d0230d9a82a";
13+
const LIFETIME = 10;
14+
15+
app.use(express.static(path.join(__dirname, '../public')));
16+
app.use(express.json());
17+
app.use(express.urlencoded({ extended: true }));
18+
19+
20+
app.get("/", async (req: Request, res: Response) => {
21+
res.render('index');
22+
});
23+
24+
25+
app.post("/create", async (req: Request, res: Response) => {
26+
console.log(req.body);
27+
28+
const body = {
29+
amount: Number(req.body.amount),
30+
lifetime: LIFETIME,
31+
api_key: API_KEY,
32+
cryptocurrency: req.body.cryptocurrency,
33+
};
34+
35+
try {
36+
const response = await fetch(API_URL + '/invoice/create', {
37+
method: 'POST',
38+
headers: {
39+
'Content-Type': 'application/json',
40+
},
41+
body: JSON.stringify(body),
42+
});
43+
44+
45+
if (!response.ok) {
46+
throw response.statusText;
47+
}
48+
49+
const json = await response.json() as any;
50+
51+
const qrCode = await fetch(json.invoice.wallet.qr_code).then(res => res.arrayBuffer());
52+
53+
const encode = (str: any): string => Buffer.from(str, 'binary').toString('base64');
54+
const qrCodeData = `data:image/png;base64,${encode(qrCode)}`;
55+
56+
res.render('check', {
57+
invoice_id: json.invoice.id,
58+
amount: json.invoice.wallet.amount_to_pay,
59+
cryptocurrency: json.invoice.wallet.cryptocurrency,
60+
address: json.invoice.wallet.address,
61+
qr_code: qrCodeData
62+
});
63+
64+
} catch (error) {
65+
console.log(error);
66+
return;
67+
}
68+
});
69+
70+
71+
72+
app.post("/check", async (req: Request, res: Response) => {
73+
const body = {
74+
invoice_id: req.body.invoice_id,
75+
api_key: API_KEY,
76+
};
77+
78+
79+
try {
80+
const response = await fetch(API_URL + '/invoice/info', {
81+
method: 'POST',
82+
headers: {
83+
'Content-Type': 'application/json',
84+
},
85+
body: JSON.stringify(body),
86+
});
87+
88+
89+
if (!response.ok) {
90+
throw response.statusText;
91+
}
92+
93+
const json = await response.json() as any;
94+
95+
res.json(json);
96+
} catch (error) {
97+
console.log(error);
98+
return;
99+
}
100+
});
101+
102+
103+
app.listen(PORT, () => {
104+
console.log(`Started http://localhost:${PORT}`);
105+
});

0 commit comments

Comments
 (0)