Skip to content

Commit 78b3623

Browse files
committed
remove unnecessary webhook call but add it again for crypto payment
1 parent 36550e3 commit 78b3623

File tree

4 files changed

+31
-22
lines changed

4 files changed

+31
-22
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
Install [Node.js](https://nodejs.org/) and [pnpm](https://pnpm.io/).
66

7-
Use [clovyr.app](https://clovyr.app/)'s free plan (7-day free trial) for [testing BTCPayServer](https://clovyr.app/apps/btcpayserver).
7+
Use [clovyr.app](https://clovyr.app/)'s free plan (7-day free trial) for [testing BTCPayServer](https://clovyr.app/apps/btcpayserver). Or maybe don't as Clovyr doesn't update BTCPayServer often. Use Voltage Cloud only even for testing because I had a webhook error as Clovyr was using old BTCPayServer version. Or use [kyun host](https://kyun.host) to deploy your own BTCPayServer on a Linux VPS for $15/month.
88

99
After testing, you can use Lunanode or [Voltage Cloud's hosted BTCPayServer](https://www.youtube.com/playlist?list=PLuMtKGSqizH2sxmKdy52gdbqSVKkyLX-t) Plan for $8/month or buy a VPS from Kyun Host ($16/month) to self-host it yourself using [btcpayserver-docker](https://github.com/btcpayserver/btcpayserver-docker).
1010

@@ -26,7 +26,7 @@ BTCPAY_STORE_ID=xxxx
2626
# Under `Settings > Store Settings > Webhooks` => Payload URL = http://localhost:3000/api/btcpayserver/webhook (replace http://localhost:3000 with ngrok url as localhost (specifically, http) won't work with webhooks) & Events > Send Me Everything
2727
BTCPAY_WEBHOOK_SECRET=xxxx
2828

29-
NEXT_PUBLIC_EBOOK_PRICE=57
29+
NEXT_PUBLIC_EBOOK_PRICE=107
3030

3131
NEXT_TELEMETRY_DISABLED=1
3232
```

src/app/api/btcpayserver/checkout/route.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const POST = async (request: NextRequest, response: NextResponse) => {
3030
const redirectURL = `${BASE_URL}/payment/successful`
3131

3232
const project_name = 'BTC Ebook'
33-
const amount = ((EBOOK_PRICE || 97) as number) - 20
33+
const amount = Number(EBOOK_PRICE)
3434
const currency = 'USD'
3535

3636
try {
@@ -66,7 +66,7 @@ export const POST = async (request: NextRequest, response: NextResponse) => {
6666
)
6767

6868
const session = await response.json()
69-
console.log({ session })
69+
console.log(JSON.stringify({ session }, null, 2))
7070
console.info(`[BTCPayServer] ✅ Successfully created Checkout Page!`)
7171
return NextResponse.json({ success: true, url: session.checkoutLink })
7272
} catch (error) {

src/app/api/btcpayserver/webhook/route.ts

+24-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import crypto from 'crypto'
2-
import { error } from 'next/dist/build/output/log'
32
import { NextRequest, NextResponse } from 'next/server'
43

54
import {
@@ -36,19 +35,17 @@ export const POST = async (req: NextRequest, res: NextResponse) => {
3635
}
3736

3837
console.info(`[BTCPayServer] Listening to Webhook Event!`)
39-
} catch (err) {
40-
error(err as string)
41-
return new Response(`Webhook Error: ${(err as Error).message}`, {
42-
status: 400,
43-
})
38+
} catch (error) {
39+
return NextResponse.json({ error }, { status: 400 })
4440
}
4541

4642
try {
4743
const event = JSON.parse(body)
44+
4845
const BTCPAY_INVOICE_ID = event.invoiceId
4946

5047
const response = await fetch(
51-
`${BTCPAY_URL}/stores/${BTCPAY_STORE_ID}/invoices/${BTCPAY_INVOICE_ID}`,
48+
`${BTCPAY_URL}/stores/${BTCPAY_STORE_ID}/invoices/${BTCPAY_INVOICE_ID}/payment-methods`,
5249
{
5350
method: 'GET',
5451
headers: {
@@ -58,16 +55,25 @@ export const POST = async (req: NextRequest, res: NextResponse) => {
5855
}
5956
)
6057

61-
const { metadata } = await response.json()
58+
const result = await response.json()
59+
60+
console.warn(`[BTCPayServer] 🧨 ${event.type} 🧨`)
6261

6362
switch (event.type) {
64-
case 'InvoicePaymentSettled':
65-
console.log({ metadata })
63+
case 'InvoiceSettled':
64+
const { buyerEmail } = event.metadata
65+
const cryptoCurrency = result[0].cryptoCode
6666

67-
const { buyerEmail } = metadata
68-
const cryptoCurrency = event.paymentMethod
69-
const cryptoPayment = event.payment.value
70-
const cryptoFees = event.payment.fee
67+
type Crypto = { totalPaid: number; networkFee: number }
68+
69+
const cryptoPayment = result.reduce(
70+
(acc: number, crypto: Crypto) => acc + Number(crypto.totalPaid),
71+
0
72+
)
73+
const cryptoFees = result.reduce(
74+
(acc: number, crypto: Crypto) => acc + Number(crypto.networkFee),
75+
0
76+
)
7177

7278
try {
7379
// create user here with info from `metadata`
@@ -88,9 +94,10 @@ export const POST = async (req: NextRequest, res: NextResponse) => {
8894
}
8995
} catch (err) {
9096
console.error({ err }, `[BTCPayServer] Webhook Error`)
91-
return new Response('Webhook handler failed. View logs.', {
92-
status: 400,
93-
})
97+
return NextResponse.json(
98+
{ error: 'Webhook handler failed. View logs.' },
99+
{ status: 400 }
100+
)
94101
}
95102

96103
console.info(`[BTCPayServer] Successfully ran Webhook!`)

src/app/components/HomePage.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use client'
22

3+
import { EBOOK_PRICE } from '@/app/lib/constants'
4+
35
export const HomePage = () => {
46
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
57
e.preventDefault()
@@ -53,7 +55,7 @@ export const HomePage = () => {
5355
</div>
5456

5557
<button type="submit" className="border border-white p-2 w-64">
56-
Submit
58+
Buy for ${EBOOK_PRICE}
5759
</button>
5860
</form>
5961
</div>

0 commit comments

Comments
 (0)