Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Buffer } from 'buffer';
- import { paymentMiddleware } from 'x402-next';
- import { decodeXPaymentResponse } from 'x402-fetch';
- import { facilitator } from '@coinbase/x402';
- const baseClient = createPublicClient({
- chain: base,
- transport: http(),
- });
- const USDC_ADDRESS = '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913';
- const pathname = '/api/gigs/create/x402';
- export async function POST(req: NextRequest) {
- const xPaymentHeader = req.headers.get('x-payment');
- const body = await req.json();
- const participants = await getTransactionParticipants(xPaymentHeader);
- const paymentResponse = await requestPayment(
- req,
- participants.gigWalletAddress,
- pathname,
- {
- address: validatedData.token_address as `0x${string}`,
- decimals: asset.decimals,
- amount: asset.tokenAmount!,
- },
- );
- if (paymentResponse.status === 402) return paymentResponse;
- const gig = await createGig(validatedData, {
- address: participants.gigWalletAddress,
- id: participants.gigWalletId,
- });
- await startGig((gig as { gig_id: number }).gig_id, participants.from);
- return NextResponse.json({
- message: '✅ Access granted after payment',
- gig_id: (gig as { gig_id: number }).gig_id,
- });
- }
- async function requestPayment(
- req: NextRequest,
- gigWalletAddress: `0x${string}`,
- pathname: string,
- asset: {
- address: `0x${string}`;
- decimals: number;
- amount: number;
- },
- ) {
- const mw = paymentMiddleware(
- gigWalletAddress,
- {
- [pathname]: {
- price: {
- amount: parseUnits(
- asset.amount.toString(),
- asset.decimals,
- ).toString(),
- asset: {
- address: asset.address,
- decimals: asset.decimals,
- eip712: {
- name: 'USD Coin',
- version: '2',
- },
- },
- },
- network: 'base',
- config: {
- description: `Create gig on Gig.bot`,
- },
- },
- },
- facilitator,
- );
- return await mw(req as any);
- }
Advertisement
Add Comment
Please, Sign In to add comment