checkout-integration

安装量: 195
排名: #4413

安装

npx skills add https://github.com/dodopayments/skills --skill checkout-integration

Dodo Payments Checkout Integration

Reference: docs.dodopayments.com/developer-resources/integration-guide

Create seamless payment experiences with hosted checkout pages or overlay checkout modals.

Checkout Methods Method Best For Integration Hosted Checkout Simple integration, full-page redirect Server-side SDK Overlay Checkout Seamless UX, stays on your site JavaScript SDK Payment Links No-code, shareable links Dashboard Hosted Checkout Basic Implementation import DodoPayments from 'dodopayments';

const client = new DodoPayments({ bearerToken: process.env.DODO_PAYMENTS_API_KEY, });

// Create checkout session const session = await client.checkoutSessions.create({ product_cart: [ { product_id: 'prod_xxxxx', quantity: 1 } ], customer: { email: 'customer@example.com', name: 'John Doe', }, return_url: 'https://yoursite.com/checkout/success', });

// Redirect customer to checkout // session.checkout_url

With Multiple Products const session = await client.checkoutSessions.create({ product_cart: [ { product_id: 'prod_item_1', quantity: 2 }, { product_id: 'prod_item_2', quantity: 1 }, ], customer: { email: 'customer@example.com', }, return_url: 'https://yoursite.com/success', });

With Customer ID (Existing Customer) const session = await client.checkoutSessions.create({ product_cart: [ { product_id: 'prod_xxxxx', quantity: 1 } ], customer_id: 'cust_existing_customer', return_url: 'https://yoursite.com/success', });

With Metadata const session = await client.checkoutSessions.create({ product_cart: [ { product_id: 'prod_xxxxx', quantity: 1 } ], customer: { email: 'customer@example.com', }, metadata: { order_id: 'order_12345', referral_code: 'FRIEND20', user_id: 'internal_user_id', }, return_url: 'https://yoursite.com/success', });

Next.js Implementation API Route // app/api/checkout/route.ts import { NextRequest, NextResponse } from 'next/server'; import DodoPayments from 'dodopayments';

const client = new DodoPayments({ bearerToken: process.env.DODO_PAYMENTS_API_KEY!, });

export async function POST(req: NextRequest) { try { const { productId, quantity = 1, email, name, metadata } = await req.json();

if (!productId || !email) {
  return NextResponse.json(
    { error: 'Missing required fields' },
    { status: 400 }
  );
}

const session = await client.checkoutSessions.create({
  product_cart: [{ product_id: productId, quantity }],
  customer: { email, name },
  metadata,
  return_url: `${process.env.NEXT_PUBLIC_APP_URL}/checkout/success`,
});

return NextResponse.json({ 
  checkoutUrl: session.checkout_url,
  sessionId: session.checkout_session_id,
});

} catch (error: any) { console.error('Checkout error:', error); return NextResponse.json( { error: error.message || 'Failed to create checkout' }, { status: 500 } ); } }

Client Component // components/CheckoutButton.tsx 'use client';

import { useState } from 'react';

interface CheckoutButtonProps { productId: string; email: string; name?: string; children: React.ReactNode; }

export function CheckoutButton({ productId, email, name, children }: CheckoutButtonProps) { const [loading, setLoading] = useState(false);

const handleCheckout = async () => { setLoading(true);

try {
  const response = await fetch('/api/checkout', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ productId, email, name }),
  });

  const data = await response.json();

  if (data.checkoutUrl) {
    window.location.href = data.checkoutUrl;
  } else {
    throw new Error(data.error || 'Failed to create checkout');
  }
} catch (error) {
  console.error('Checkout error:', error);
  alert('Failed to start checkout. Please try again.');
} finally {
  setLoading(false);
}

};

return ( ); }

Success Page // app/checkout/success/page.tsx import { Suspense } from 'react';

function SuccessContent() { return (

Payment Successful!

Thank you for your purchase. You will receive a confirmation email shortly.

Return to Home
); }

export default function SuccessPage() { return ( Loading...\