> ## Documentation Index
> Fetch the complete documentation index at: https://docs.devin.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Stripe Checkout フローを構築する

export const UseCaseHero = ({title, description, prompt, category, features, devinUrl, agent, intent, playbookId, type}) => {
  const encodedPrompt = encodeURIComponent(prompt || '');
  const tag = 'docs-use-case-gallery';
  const utm = 'utm_source=docs&utm_medium=use-case-gallery&utm_campaign=hero-cta';
  const agentParams = (agent ? '&agent=' + agent : '') + (intent ? '&intent=' + intent : '') + (playbookId ? '&playbookId=' + playbookId : '');
  const devinHref = type === 'schedule' ? 'https://app.devin.ai/settings/schedules/create?' + utm + agentParams + (prompt ? '&prompt=' + encodedPrompt : '') : type === 'review' ? 'https://app.devin.ai/review?' + utm : agent === 'ada' ? 'https://app.devin.ai/search?' + utm + '&noSubmit=true' + (prompt ? '&prompt=' + encodedPrompt : '') : devinUrl ? devinUrl.includes('?') ? devinUrl + '&' + utm + agentParams : devinUrl + '?' + utm + agentParams : prompt ? 'https://app.devin.ai/?tags=' + tag + '&' + utm + agentParams + '&prompt=' + encodedPrompt : 'https://app.devin.ai/?' + utm + agentParams;
  const buttonLabel = type === 'schedule' ? 'Schedule in Devin ↗' : type === 'review' ? 'Set Up Devin Review ↗' : agent === 'advanced' ? 'Try in Devin ↗' : agent === 'dana' ? 'Try in Dana ↗' : agent === 'ada' ? 'Try in Ask Devin ↗' : 'Try in Devin ↗';
  const featureList = features ? features.split(',').map(f => f.trim()) : [];
  return <div className="uc-hero">
      <div className="uc-hero-inner">
        <div className="uc-hero-left">
          <h1 className="uc-hero-title">{title}</h1>
          <p className="uc-hero-desc">{description}</p>
          <div>
            <a href={devinHref} target="_blank" rel="noopener noreferrer" className="try-in-devin-btn">
              {buttonLabel}
            </a>
          </div>
        </div>
        <div className="uc-hero-meta">
          <div className="uc-meta-item">
            <span className="uc-meta-label">Author</span>
            <span className="uc-meta-value">Cognition</span>
          </div>
          <div className="uc-meta-item">
            <span className="uc-meta-label">Category</span>
            <span className="uc-meta-value">{category}</span>
          </div>
          {featureList.length > 0 && <div className="uc-meta-item">
              <span className="uc-meta-label">Features</span>
              <span className="uc-meta-value">{featureList.join(', ')}</span>
            </div>}
        </div>
      </div>
    </div>;
};

export const PromptBlock = ({children, type, agent, intent, playbookId}) => {
  var utm = 'utm_source=docs&utm_medium=use-case-gallery&utm_campaign=prompt-block';
  var tag = 'docs-use-case-gallery';
  var agentParams = (agent ? '&agent=' + agent : '') + (intent ? '&intent=' + intent : '') + (playbookId ? '&playbookId=' + playbookId : '');
  var label = type === 'schedule' ? 'Schedule in Devin' : type === 'playbook' ? 'Create Playbook' : type === 'knowledge' ? 'Add to Knowledge' : agent === 'advanced' ? 'Try in Devin' : agent === 'dana' ? 'Try in Dana' : agent === 'ada' ? 'Try in Ask Devin' : 'Try in Devin';
  var buildUrl = function (text) {
    var encoded = encodeURIComponent(text);
    if (type === 'schedule') return 'https://app.devin.ai/settings/schedules/create?' + utm + agentParams + '&prompt=' + encoded;
    if (type === 'playbook') return 'https://app.devin.ai/settings/playbooks/create?' + utm + '&body=' + encoded;
    if (type === 'knowledge') return 'https://app.devin.ai/knowledge?' + utm + '&body=' + encoded;
    if (agent === 'ada') return 'https://app.devin.ai/search?' + utm + '&noSubmit=true&prompt=' + encoded;
    return 'https://app.devin.ai/?tags=' + tag + '&' + utm + agentParams + '&prompt=' + encoded;
  };
  const ref = React.useRef(null);
  const [href, setHref] = React.useState('#');
  React.useEffect(() => {
    if (!ref.current) return;
    var codeEl = ref.current.querySelector('pre code');
    if (codeEl) {
      var text = codeEl.textContent.trim();
      if (text) setHref(buildUrl(text));
    }
    var header = ref.current.querySelector('[data-component-part="code-block-header"]');
    if (header && !header.querySelector('.prompt-block-devin-link')) {
      var link = document.createElement('a');
      link.href = href;
      link.target = '_blank';
      link.rel = 'noopener noreferrer';
      link.className = 'prompt-block-devin-link';
      link.style.cssText = 'display:inline-flex;align-items:center;gap:6px;text-decoration:none;color:#fff;font-size:11px;font-weight:500;padding:4px 10px;border-radius:6px;white-space:nowrap;background:#317CFF;transition:background 0.2s;margin-left:8px;';
      link.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"/><polyline points="15 3 21 3 21 9"/><line x1="10" y1="14" x2="21" y2="3"/></svg> ' + label;
      link.onmouseenter = function () {
        link.style.background = '#2968D9';
      };
      link.onmouseleave = function () {
        link.style.background = '#317CFF';
      };
      header.appendChild(link);
    }
    var existingLink = ref.current.querySelector('.prompt-block-devin-link');
    if (existingLink && href !== '#') existingLink.href = href;
  });
  return <div className="prompt-block" ref={ref}>{children}</div>;
};

<UseCaseHero title="Stripe Checkout フローを構築する" description="Stripe のサンドボックスキー付きのチェックアウト仕様を Devin に渡すだけで、料金ページ、チェックアウトセッション、Webhook ハンドラー、確認ページまで含めた動作する決済フローを構築し、ブラウザ上で検証済みの状態にできます。" prompt="私たちの SaaS アプリ向けに Stripe のチェックアウトフローを実装してください。/pricing に 3 つの料金プランを用意します: Starter $19/mo (5 projects, 10GB, email support)、Pro $49/mo (unlimited projects, 100GB, priority support)、Team $99/mo (Pro + team management, SSO（シングルサインオン）, audit log)。各カードには Subscribe ボタンを配置し、POST /api/checkout/sessions を通じて Stripe Checkout セッションを作成します。/api/webhooks/stripe の Webhook では checkout.session.completed を処理し、データベース内のユーザープランと subscription_id を更新し、STRIPE_WEBHOOK_SECRET を使って署名を検証します。/checkout/success の Success ページでは、プラン名、金額、そして Go to Dashboard ボタンを表示します。コードを書く前に、まずレビュー用の実装方針をまとめて提示してください。Next.js アプリと Prisma ORM を使用し、src/pages/settings/billing.tsx 内のパターンに従ってください。STRIPE_SECRET_KEY と STRIPE_WEBHOOK_SECRET は環境変数から取得します。Webhook 検証、セッション作成、プラン更新ロジックについてテストを書いてください。開発サーバーを起動し、Stripe テストカード 4242 4242 4242 4242 を使って、ブラウザ上でフロー全体が動作することを確認してください。エンドツーエンドで全てが動作するまで PR を作成しないでください。" category="機能開発" features="" />

<div className="uc-detail-wrapper">
  <Tip>手動でのセットアップが面倒ですか？このページのリンクを Devin セッションに貼り付けて、すべてのセットアップを依頼してください。</Tip>

  <Steps>
    <Step title="（オプション）Ask Devin でコードベースの範囲を指定する">
      現在アプリがどのように決済を処理しているか、また仕様でどのファイルやパターンを参照すべきかよく分からない場合は、まず [Ask Devin](https://app.devin.ai/search?utm_source=docs\&utm_medium=use-case-gallery) を使って調査してください:

      <PromptBlock agent="ada">
        ```txt チェックアウト実装のスコープを把握する theme={null}
        現在、アプリではどのように請求処理を行っていますか？次を教えてください:
        1. ユーザーのプラン／サブスクリプションがデータベースのどこに保存されているか
        2. 既存の Stripe 連携用ファイルや API ルート
        3. その他の決済関連ページ（例: settings/billing）がどのような構造になっているか
        4. どの ORM を使っているかと、マイグレーションを通常どのように作成しているか
        ```
      </PromptBlock>

      回答をもとに仕様を補完し、Devin があなたのコードベースに自然にフィットするものを構築できるよう、特定のファイル名、テーブル名、パターンを必ず明記してください。また、Ask Devin からそのまま Devin セッションを開始でき、そこで得られた内容はすべてコンテキストとして引き継がれます。
    </Step>

    <Step title="Stripe のサンドボックスキーを追加する">
      Devin がチェックアウトセッションを作成し、webhook ハンドラーを検証するには、Stripe の **テストモード** キーが必要です。必ずサンドボックス用の認証情報のみを使用し、本番用の Stripe キーを Devin に渡さないでください。

      最も簡単な方法は、セッションを開始する前にそれらを [組織シークレット](/ja/product-guides/secrets) として保存しておくことです。

      1. **Settings > Secrets** に移動し、次を追加します:
         * `STRIPE_SECRET_KEY` — [Stripe Dashboard](https://dashboard.stripe.com/test/apikeys) から取得したテストモードのシークレットキー
         * `STRIPE_WEBHOOK_SECRET` — [webhook endpoint settings](https://dashboard.stripe.com/test/webhooks) に表示される署名シークレット
      2. Devin はこれらを環境変数として参照するため、ソースコード内にハードコードされることはありません。

      <Note>組織シークレットはセッション開始時に注入されるため、セッションを開始する**前に**追加しておく必要があります。別の方法として、セッション中にチャットを使ってシークレットを渡すこともできますし、環境変数が不足している場合には、Devin が必要な認証情報を求めてプロアクティブに確認します。</Note>
    </Step>

    <Step title="チェックアウト仕様を渡す">
      PRD、Linear のチケット、あるいは詳しい Slack メッセージなどから、仕様をそのまま Devin に貼り付けてください。良いチェックアウト用の仕様書では、料金プラン、決済フロー、決済成功後に何が起きるかをカバーします。構造化されているほど効果的です。

      <PromptBlock>
        ```txt Implement Stripe checkout flow theme={null}
        私たちの SaaS アプリ向けに Stripe のチェックアウトフローを実装してください:

        ## Pricing page
        - /pricing に 3 つの料金プランカードを表示する新しいルートを作成する:
          - Starter: $19/mo — プロジェクト 5 件、ストレージ 10GB、メールサポート
          - Pro: $49/mo — プロジェクト無制限、ストレージ 100GB、優先サポート
          - Team: $99/mo — Pro のすべて + チーム管理、SSO、監査ログ
        - 各カードにチェックアウトを開始する "Subscribe" ボタンを配置する

        ## Checkout
        - POST /api/checkout/sessions — 選択された price ID、顧客メールアドレス、
          success/cancel URL を使って Stripe Checkout セッションを作成する
        - ユーザーを Stripe がホストするチェックアウトページへリダイレクトする
        - 成功時は /checkout/success?session_id={CHECKOUT_SESSION_ID} にリダイレクトする

        ## Webhook
        - POST /api/webhooks/stripe — Stripe のイベントを受信する
        - checkout.session.completed を処理し、ユーザーのプランと
          データベース内の subscription_id を更新する
        - STRIPE_WEBHOOK_SECRET を使って webhook シグネチャを検証する

        ## Success page
        - /checkout/success — Stripe からセッション詳細を取得し、
          プラン名、金額、および "Go to Dashboard" ボタンを表示する

        ## Planning
        - コードを書く前に、実装プランのアウトラインを作成し、
          承認のために私と共有すること。作成・変更するファイル、
          データベースの変更内容、および実装の順序を列挙すること。

        ## Technical notes
        - UI については src/pages/settings/billing.tsx のパターンに従うこと
        - Prisma を使用する: subscriptions テーブル
          (id, user_id, stripe_subscription_id, plan, status, current_period_end)
          を追加する
        - STRIPE_SECRET_KEY と STRIPE_WEBHOOK_SECRET は環境変数として利用可能

        ## Testing & verification
        - Write tests for webhook signature verification, session creation,
          and the plan-update logic
        - After implementing, spin up the local dev server and verify the
          entire flow in the browser: navigate to /pricing, click Subscribe,
          complete a test payment with Stripe's test card (4242 4242 4242 4242),
          confirm the success page renders, and verify the database was updated
        - Do not open a PR until you've confirmed everything works end-to-end
        ```
      </PromptBlock>

      A good spec for Devin includes three things: **what** to build (pricing tiers, checkout flow, webhook handler), **where** it lives (routes, tables, files), and **how** it fits in (existing patterns to follow). You don't need to specify every implementation detail — Devin investigates your codebase to fill in the gaps.
    </Step>

    <Step title="Devin builds and verifies in the browser">
      Devin reads your spec, explores the codebase for matching patterns, then implements across the full stack. Before opening a PR, it runs your app locally and opens its [built-in browser](/ja/work-with-devin/devin-session-tools) to verify the checkout flow works end-to-end.

      Here's what that looks like for the Stripe checkout example:

      1. **Creates the migration** — Adds the `subscriptions` table with columns for `user_id`, `stripe_subscription_id`, `plan`, `status`, and `current_period_end`
      2. **Builds the pricing page** — Creates the three-tier pricing cards at `/pricing`, each with a "Subscribe" button that posts to the checkout API
      3. **Implements checkout session creation** — Builds `POST /api/checkout/sessions` that creates a Stripe Checkout session with the correct price ID, customer email, and redirect URLs
      4. **Adds the webhook handler** — Implements `POST /api/webhooks/stripe` with signature verification, `checkout.session.completed` event handling, and database updates
      5. **Builds the success page** — Creates `/checkout/success` that fetches the Stripe session, displays the plan name, amount charged, and a "Go to Dashboard" link
      6. **Writes tests** — Tests for webhook signature verification (valid, invalid, missing), checkout session creation, and the plan-update database logic
      7. **Opens the browser** — Starts the dev server, navigates to `/pricing`, clicks "Subscribe" on the Pro tier, verifies the Stripe Checkout redirect works, and checks that the success page renders correctly after a test payment
      8. **Opens a PR** — Delivers all changes with a summary of what was implemented and how it was verified

      The browser verification step catches issues that unit tests miss — a pricing card that doesn't trigger checkout, a redirect URL that 404s, or a success page that fails to load session details. [ローカルテストのスキル](/ja/product-guides/skills) を定義していれば、Devin は作成するすべての機能に対して、その手順を自動的に実行します。
    </Step>

    <Step title="PR から反復する">
      PR が作成されたら、同じセッションでフォローアッププロンプトを送り、チェックアウトフローを拡張または調整します。

      <PromptBlock>
        ```txt Add subscription management theme={null}
        /settings/billing に "Manage Subscription" セクションを追加し、現在の
        プラン、次回請求日、そして "Cancel Subscription" ボタンを表示します。
        解約には Stripe のカスタマーポータルを使用し、ポータルセッションを作成して
        ユーザーをリダイレクトします。
        ```
      </PromptBlock>

      <PromptBlock>
        ```txt Add annual pricing toggle theme={null}
        料金ページに月額／年額の toggle を追加します。年額プランには 20%
        の割引を適用します。年額が選択されている場合は、各カードに月あたりの価格、
        "billed annually" の注記、そして "Save 20%" バッジを表示します。
        ```
      </PromptBlock>
    </Step>

    <Step title="Review the PR with Devin Review">
      Once Devin opens the PR, use [Devin Review](https://app.devin.ai/review?utm_source=docs\&utm_medium=use-case-gallery) to review the changes. Devin Review has full context of your codebase and can catch bugs, security issues, and style inconsistencies across the diff. You can ask follow-up questions in the review chat — for instance, "Does the webhook handler validate the event type before processing?" — and Devin will answer grounded in the actual code.
    </Step>
  </Steps>
</div>
