|
1 | 1 | import { NextRequest, NextResponse } from 'next/server'; |
2 | 2 | import { getDb } from '@/lib/db'; |
3 | 3 | import { getSessionDid } from '@/lib/auth'; |
| 4 | +import { generatePublicId } from '@/lib/id'; |
4 | 5 |
|
5 | 6 | const COINPAY_BASE = 'https://coinpayportal.com'; |
6 | 7 | const API_KEY = process.env.COINPAY_API_KEY!; |
@@ -36,63 +37,68 @@ export async function POST(req: NextRequest) { |
36 | 37 | if (isNaN(reward) || reward < 0.10) return NextResponse.json({ error: 'Minimum reward is $0.10' }, { status: 400 }); |
37 | 38 | if (!store_id && !store_name?.trim()) return NextResponse.json({ error: 'Store is required' }, { status: 400 }); |
38 | 39 |
|
39 | | - const db = getDb(); |
| 40 | + try { |
| 41 | + const db = getDb(); |
| 42 | + const publicId = generatePublicId(); |
40 | 43 |
|
41 | | - // Insert bounty as 'open' first to get an ID |
42 | | - await db.sql` |
43 | | - INSERT INTO bounties (creator_did, store_id, store_name, title, description, reward_usd, status) |
44 | | - VALUES ( |
45 | | - ${did}, |
46 | | - ${store_id ?? null}, |
47 | | - ${store_name?.trim() ?? null}, |
48 | | - ${title.trim()}, |
49 | | - ${description?.trim() ?? null}, |
50 | | - ${reward}, |
51 | | - 'open' |
52 | | - ) |
53 | | - `; |
54 | | - const [{ id: bountyId }] = await db.sql` |
55 | | - SELECT id FROM bounties WHERE creator_did = ${did} ORDER BY id DESC LIMIT 1 |
56 | | - `; |
| 44 | + // Insert bounty as 'open' with a non-sequential public id for URLs. |
| 45 | + await db.sql` |
| 46 | + INSERT INTO bounties (public_id, creator_did, store_id, store_name, title, description, reward_usd, status) |
| 47 | + VALUES ( |
| 48 | + ${publicId}, |
| 49 | + ${did}, |
| 50 | + ${store_id ?? null}, |
| 51 | + ${store_name?.trim() ?? null}, |
| 52 | + ${title.trim()}, |
| 53 | + ${description?.trim() ?? null}, |
| 54 | + ${reward}, |
| 55 | + 'open' |
| 56 | + ) |
| 57 | + `; |
57 | 58 |
|
58 | | - // Create a CoinPay payment for the creator to fund the bounty |
59 | | - // Docs: POST /api/payments/create |
60 | | - let paymentAddress: string | null = null; |
61 | | - let paymentId: string | null = null; |
| 59 | + // Create a CoinPay payment for the creator to fund the bounty |
| 60 | + // Docs: POST /api/payments/create |
| 61 | + let paymentAddress: string | null = null; |
| 62 | + let paymentId: string | null = null; |
62 | 63 |
|
63 | | - try { |
64 | | - const res = await fetch(`${COINPAY_BASE}/api/payments/create`, { |
65 | | - method: 'POST', |
66 | | - headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${API_KEY}` }, |
67 | | - body: JSON.stringify({ |
68 | | - business_id: MERCHANT_ID, |
69 | | - amount_usd: reward, |
70 | | - currency: 'usdc_pol', // default to USDC on Polygon — low fees |
71 | | - description: `Coupon bounty: ${title.trim()}`, |
72 | | - redirect_url: `${APP_URL}/bounties/${bountyId}?funded=1`, |
73 | | - metadata: { type: 'bounty_fund', bounty_id: bountyId, creator_did: did }, |
74 | | - }), |
75 | | - }); |
| 64 | + try { |
| 65 | + const res = await fetch(`${COINPAY_BASE}/api/payments/create`, { |
| 66 | + method: 'POST', |
| 67 | + headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${API_KEY}` }, |
| 68 | + body: JSON.stringify({ |
| 69 | + business_id: MERCHANT_ID, |
| 70 | + amount_usd: reward, |
| 71 | + currency: 'usdc_pol', // default to USDC on Polygon — low fees |
| 72 | + description: `Coupon bounty: ${title.trim()}`, |
| 73 | + redirect_url: `${APP_URL}/bounties/${publicId}?funded=1`, |
| 74 | + metadata: { type: 'bounty_fund', bounty_id: publicId, creator_did: did }, |
| 75 | + }), |
| 76 | + }); |
76 | 77 |
|
77 | | - if (res.ok) { |
78 | | - const data = await res.json(); |
79 | | - paymentId = data.payment_id ?? data.id ?? null; |
80 | | - paymentAddress = data.payment_address ?? null; |
81 | | - if (paymentId) { |
82 | | - await db.sql`UPDATE bounties SET payment_id = ${paymentId} WHERE id = ${bountyId}`; |
| 78 | + if (res.ok) { |
| 79 | + const data = await res.json(); |
| 80 | + paymentId = data.payment_id ?? data.id ?? null; |
| 81 | + paymentAddress = data.payment_address ?? null; |
| 82 | + if (paymentId) { |
| 83 | + await db.sql`UPDATE bounties SET payment_id = ${paymentId} WHERE public_id = ${publicId}`; |
| 84 | + } |
| 85 | + } else { |
| 86 | + const err = await res.text(); |
| 87 | + console.error('CoinPay payment create error:', err); |
83 | 88 | } |
84 | | - } else { |
85 | | - const err = await res.text(); |
86 | | - console.error('CoinPay payment create error:', err); |
| 89 | + } catch (e) { |
| 90 | + console.error('CoinPay payment create failed:', e); |
87 | 91 | } |
| 92 | + |
| 93 | + return NextResponse.json({ |
| 94 | + id: publicId, |
| 95 | + public_id: publicId, |
| 96 | + payment_id: paymentId, |
| 97 | + payment_address: paymentAddress, |
| 98 | + pay_url: paymentId ? `${COINPAY_BASE}/pay/${paymentId}` : null, |
| 99 | + }, { status: 201 }); |
88 | 100 | } catch (e) { |
89 | | - console.error('CoinPay payment create failed:', e); |
| 101 | + console.error('Failed to create bounty:', e); |
| 102 | + return NextResponse.json({ error: 'Failed to create bounty. Please try again.' }, { status: 500 }); |
90 | 103 | } |
91 | | - |
92 | | - return NextResponse.json({ |
93 | | - id: bountyId, |
94 | | - payment_id: paymentId, |
95 | | - payment_address: paymentAddress, |
96 | | - pay_url: paymentId ? `${COINPAY_BASE}/pay/${paymentId}` : null, |
97 | | - }, { status: 201 }); |
98 | 104 | } |
0 commit comments