Skip to content

Commit

Permalink
add paywall to api
Browse files Browse the repository at this point in the history
  • Loading branch information
neb-b committed Apr 29, 2023
1 parent 2cf7463 commit 9e09741
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 4 deletions.
41 changes: 41 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"next": "^12.3.0",
"node-twitter": "^0.5.3",
"number-to-words": "^1.2.4",
"paymeformyapi": "^0.0.2",
"react": "^18.2.0",
"react-charts": "^3.0.0-beta.48",
"react-dom": "^18.2.0",
Expand Down
28 changes: 26 additions & 2 deletions src/pages/api/data.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
import { NextApiResponse, NextApiRequest } from "next"
import { getData } from "core/data"
import { NextApiResponse, NextApiRequest } from 'next'
import PayMeForMyAPI from 'paymeformyapi'
import { getData } from 'core/data'

const paywall = new PayMeForMyAPI({
lnBitsAdminId: process.env.LN_BITS_ADMIN_ID,
lnBitsApiKey: process.env.LN_BITS_API_KEY,
lnBitsURL: process.env.LN_BITS_URL,
lnBitsAdminInvoiceKey: process.env.LN_BITS_ADMIN_INVOICE_KEY,
refillAmount: 50, // number of satoshis to refill
requestCost: 1, // number of satoshis per API call
})

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const { api_token } = req.query

try {
const { success, updated_api_token, invoice } = await paywall.deductBalance(api_token as string)

if (!success) {
res.status(402).json({
statusCode: 402,
message: 'Payment required. Please pay the invoice and update your api token.',
updated_api_token,
invoice,
})
return
}

const { usd } = await getData()

res.status(200).json({ usd })
Expand Down
21 changes: 19 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1748,6 +1748,15 @@
"follow-redirects" "^1.14.9"
"form-data" "^4.0.0"

"axios@^1.4.0":
"integrity" "sha512-S4XCWMEmzvo64T9GfvQDOXgYRDJ/wsSZc7Jvdgx5u1sd0JwsuPLqb3SYmusag+edF6ziyMensPVqLTSc1PiSEA=="
"resolved" "https://registry.npmjs.org/axios/-/axios-1.4.0.tgz"
"version" "1.4.0"
dependencies:
"follow-redirects" "^1.15.0"
"form-data" "^4.0.0"
"proxy-from-env" "^1.1.0"

"axobject-query@^2.2.0":
"integrity" "sha512-Td525n+iPOOyUQIeBfcASuG6uJsDOITl7Mds5gFyerkWiX7qhUTdYUBlSgNMyVqtSJqwpt1kXGLdUt6SykLMRA=="
"resolved" "https://registry.npmjs.org/axobject-query/-/axobject-query-2.2.0.tgz"
Expand Down Expand Up @@ -2752,7 +2761,7 @@
dependencies:
"tslib" "^2.0.3"

"follow-redirects@^1.14.9":
"follow-redirects@^1.14.9", "follow-redirects@^1.15.0":
"integrity" "sha512-yLAMQs+k0b2m7cVxpS1VKJVvoz7SS9Td1zss3XRwXj+ZDH00RJgnuLx7E44wx02kQLrdM3aOOy+FpzS7+8OizA=="
"resolved" "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.1.tgz"
"version" "1.15.1"
Expand Down Expand Up @@ -3665,6 +3674,14 @@
"resolved" "https://registry.npmjs.org/path-type/-/path-type-4.0.0.tgz"
"version" "4.0.0"

"paymeformyapi@^0.0.2":
"integrity" "sha512-49hs5kM2rHq4NM+A1R5+kx7e1f49buqpIh3DbG76/kqhCWUWLcP8vLcdz788dlj2QTzRX+V7VuqQIGSqLl49oA=="
"resolved" "https://registry.npmjs.org/paymeformyapi/-/paymeformyapi-0.0.2.tgz"
"version" "0.0.2"
dependencies:
"axios" "^1.4.0"
"uuid" "^9.0.0"

"pend@~1.2.0":
"integrity" "sha512-F3asv42UuXchdzt+xXqfW1OGlVBe+mxa2mqI0pg5yAHZPvFmY3Y6drSf/GQ1A86WgWEN9Kzh/WrgKa6iGcHXLg=="
"resolved" "https://registry.npmjs.org/pend/-/pend-1.2.0.tgz"
Expand Down Expand Up @@ -3738,7 +3755,7 @@
"object-assign" "^4.1.1"
"react-is" "^16.13.1"

"proxy-from-env@1.1.0":
"proxy-from-env@^1.1.0", "proxy-from-env@1.1.0":
"integrity" "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg=="
"resolved" "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz"
"version" "1.1.0"
Expand Down

0 comments on commit 9e09741

Please sign in to comment.