From d8856abf3a97eaa4b03adba7b0a5b3aa1a610cff Mon Sep 17 00:00:00 2001 From: neb Date: Sat, 29 Apr 2023 18:53:31 -0500 Subject: [PATCH] add paywall to api --- package-lock.json | 28 ++++++++++++++++++++++++++++ package.json | 1 + src/pages/api/data.ts | 28 ++++++++++++++++++++++++++-- yarn.lock | 7 +++++++ 4 files changed, 62 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1501623..bb9b165 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,6 +22,7 @@ "next": "^12.3.0", "node-twitter": "^0.5.3", "number-to-words": "^1.2.4", + "paymeformyapi": "file:../paymeformyapi", "react": "^18.2.0", "react-charts": "^3.0.0-beta.48", "react-dom": "^18.2.0", @@ -37,6 +38,19 @@ "typescript": "4.8.3" } }, + "../paymeformyapi": { + "version": "0.0.4", + "license": "ISC", + "dependencies": { + "axios": "^1.4.0", + "uuid": "^9.0.0" + }, + "devDependencies": { + "@types/uuid": "^9.0.1", + "nodemon": "^2.0.22", + "typescript": "^5.0.4" + } + }, "node_modules/@ampproject/remapping": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/@ampproject/remapping/-/remapping-2.2.0.tgz", @@ -5678,6 +5692,10 @@ "node": ">=8" } }, + "node_modules/paymeformyapi": { + "resolved": "../paymeformyapi", + "link": true + }, "node_modules/pend": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/pend/-/pend-1.2.0.tgz", @@ -11495,6 +11513,16 @@ "resolved": "https://registry.npmjs.org/path-type/-/path-type-4.0.0.tgz", "integrity": "sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw==" }, + "paymeformyapi": { + "version": "file:../paymeformyapi", + "requires": { + "@types/uuid": "^9.0.1", + "axios": "^1.4.0", + "nodemon": "^2.0.22", + "typescript": "^5.0.4", + "uuid": "^9.0.0" + } + }, "pend": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/pend/-/pend-1.2.0.tgz", diff --git a/package.json b/package.json index 22e2a54..0b38899 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "next": "^12.3.0", "node-twitter": "^0.5.3", "number-to-words": "^1.2.4", + "paymeformyapi": "^0.0.5", "react": "^18.2.0", "react-charts": "^3.0.0-beta.48", "react-dom": "^18.2.0", diff --git a/src/pages/api/data.ts b/src/pages/api/data.ts index 2f7a292..e59968e 100644 --- a/src/pages/api/data.ts +++ b/src/pages/api/data.ts @@ -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: api_token_from_request } = req.query + try { + const { success, api_token, invoice } = await paywall.deductBalance(api_token_from_request as string) + + if (!success) { + res.status(402).json({ + statusCode: 402, + message: 'Payment required. Please pay the invoice and update your api token.', + api_token, + invoice, + }) + return + } + const { usd } = await getData() res.status(200).json({ usd }) diff --git a/yarn.lock b/yarn.lock index 57c50dd..a3ff9af 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3665,6 +3665,13 @@ "resolved" "https://registry.npmjs.org/path-type/-/path-type-4.0.0.tgz" "version" "4.0.0" +"paymeformyapi@file:../paymeformyapi": + "resolved" "file:../paymeformyapi" + "version" "0.0.4" + 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"