Firebase で支払いを処理する
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Firebase の機能と Stripe を使用すると、独自のサーバー インフラストラクチャを構築することなく、ウェブアプリで支払いを処理できます。このガイドでは、オープンソースの cloud-functions-stripe-sample.web.app サンプルアプリをカスタマイズして、デプロイする方法について説明します。
最初に、Firebase コンソールでプロジェクトを作成し、Stripe アカウントを設定します。
実装の概要
- Stripe アカウントを設定します。
- Firebase コンソールでプロジェクトを作成します。
- プロジェクトを 従量課金制の Blaze 料金プランにアップグレードします。
firebase use --add
でプロジェクトを使用するように Firebase CLI を構成します。
- Firestrip サンプルアプリのソースコードを取得します。プロジェクトに適切な情報を構成し、アプリに合わせてコードをカスタマイズします。
- アプリをデプロイしたら、Firebase コンソールでユーザーとトランザクションのリストを探します。
サンプルアプリの設定とデプロイ
- ソースコードを取得します。
- 認証プロバイダの設定で Google とメールによるログインを有効にします。
- Cloud Firestore を有効にします。
- Firebase CLI をまだインストールしていない場合はインストールし、
firebase login
を使用してログインします。
firebase use --add
でプロジェクトを使用するようにこのサンプルを構成します。
cd functions; npm install; cd -
を実行して依存関係をローカルにインストールします。
Stripe API Secret Key を Cloud Functions 環境構成に追加します。
firebase functions:config:set stripe.secret=<YOUR STRIPE SECRET KEY>
/public/javascript/app.js
で Stripe の公開可能な鍵を設定します。
const STRIPE_PUBLISHABLE_KEY=<YOUR STRIPE PUBLISHABLE KEY>;
firebase deploy
を使用してプロジェクトをデプロイします。このコマンドは以下を行います。
public
ディレクトリ内のすべてのファイルを Hosting に送信して、ウェブサイトを利用可能にします。
functions
ディレクトリのコードを Cloud Functions for Firebase に送信します。
firestore.rules
で構成されているように、Cloud Firestore データベースにセキュリティ ルールを設定します。指定されたルールで、支払いと支払い方法の読み取り / 書き込みのみがユーザーに許可されます。
サンプルアプリのテスト
支払いアプリの URL(your-firebase-project-id.web.app
)にアクセスし、次のことを確認します。
- Google またはメールを使用してログインできる。
- 新しい Stripe テストカードを追加して、カード選択要素でカードを表示できる。
- カードを選択して請求できる。
- ログアウトできる。
比較については、cloud-functions-stripe-sample.web.app をご覧ください。
ユーザーに簡単なプロセスを提供するため、支払いページの外観をカスタマイズしたり、既存のアプリにプラグインとして追加したりできます。
処理された支払いの確認
支払いページを設定してデプロイしたら、Firebase コンソールからユーザーのリストを表示して、支払い方法や支払いを確認できます。
- Cloud Firestore にアクセスします。
- ユーザーのリストを確認します。ユーザーがクレジット カード情報の追加やトランザクションを行った場合は、その内容を各ユーザーのリストで確認します。
本番環境で決済を受け付ける
稼働開始の準備ができたら、テストキーをライブキーに交換する必要があります。これらのキーの詳細については、Stripe のドキュメントをご覧ください。
Stripe Secret の構成を更新します。
firebase functions:config:set stripe.secret=<YOUR STRIPE LIVE SECRET KEY>
/public/javascript/app.js
で公開可能なライブキーを設定します。
変更を有効にするために、Cloud Functions と Hosting の両方を再デプロイします。firebase deploy
。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-07-25 UTC。
[null,null,["最終更新日 2025-07-25 UTC。"],[],[],null,["Using a few different Firebase features and Stripe, you can process payments in\nyour web app without building your own server infrastructure. This guide walks\nyou through customizing and deploying your own version of the open-source\n[cloud-functions-stripe-sample.web.app](//cloud-functions-stripe-sample.web.app/) example app.\n\nBefore you start, create a project in the\n[Firebase console](https://console.firebase.google.com) and set up a\n[Stripe](https://stripe.com/) account.\n| **Note:** Our partners at Stripe have introduced two new extensions, [Run Subscription Payments with Stripe](https://firebase.google.com/products/extensions/firestore-stripe-subscriptions) and [Send Invoices using Stripe](https://firebase.google.com/products/extensions/firestore-stripe-invoices), to make it possible to process payments with even less code!\n\nImplementation overview\n\n1. Set up a [Stripe](//stripe.com/) account.\n2. Create a project in the [Firebase console](//console.firebase.google.com).\n3. Upgrade your project to the [pay-as-you-go Blaze pricing plan](/pricing).\n4. Configure the Firebase CLI to use your project with `firebase use --add`.\n5. Get the [source code](//github.com/firebase/functions-samples/tree/main/Node-1st-gen/stripe) for the sample Firestripe app. Configure it with the right information for your project and customize the code to fit your app.\n6. Once you've deployed your app, look for a list of users and transactions in the Firebase console.\n\nSet up and deploy the sample app\n\n1. Get the [source code](//github.com/firebase/functions-samples/tree/main/Node-1st-gen/stripe).\n2. Enable Google \\& Email sign-in in your [authentication provider settings](//console.firebase.google.com/project/_/authentication/providers).\n3. Enable [Cloud Firestore](//console.firebase.google.com/project/_/firestore).\n4. Install the [Firebase CLI](//github.com/firebase/firebase-tools) if you haven't already, and log in with `firebase login`.\n5. Configure this sample to use your project with `firebase use --add`.\n6. Install dependencies locally by running `cd functions; npm install; cd -`\n7. Add your [Stripe API Secret Key](//dashboard.stripe.com/account/apikeys)\n to your Cloud Functions environment configuration:\n\n `firebase functions:config:set stripe.secret=\u003cYOUR STRIPE SECRET KEY\u003e`\n8. Set your [Stripe publishable key](//dashboard.stripe.com/account/apikeys)\n in [`/public/javascript/app.js`](//github.com/firebase/functions-samples/tree/main/Node-1st-gen/stripe/public/javascript/app.js#L16):\n\n `const STRIPE_PUBLISHABLE_KEY=\u003cYOUR STRIPE PUBLISHABLE KEY\u003e;`\n9. Deploy your project using `firebase deploy`. This command:\n\n 1. Sends all the files in the `public` directory to Hosting so that your website is available.\n 2. Sends the code in the `functions` directory to Cloud Functions for Firebase.\n 3. Sets security rules on your Cloud Firestore database as configured in `firestore.rules`. The provided rules only allow a user to read and write their own payments and payment methods.\n\nTest the sample app\n\nVisit your payments app's URL at\n`your-firebase-project-id.web.app` and verify that the following features work:\n\n- You can sign in via Google or Email.\n- You can add a new [Stripe test card](//stripe.com/docs/testing) and view it in the card select element.\n- You can select one of your cards and charge it.\n- You can sign out.\n\nFor comparison, see\n[cloud-functions-stripe-sample.web.app](//cloud-functions-stripe-sample.web.app/).\n\nTo provide a streamlined experience for your users, you can further customize\nyour payment page's appearance, or plug it into your existing app.\n\nView processed payments\n\nOnce you've set up and deployed your payments page, you can check the Firebase\nconsole and see a list of users along with their payment methods and payments.\n\n1. Go to [Cloud Firestore](https://console.firebase.google.com/project/_/firestore/data).\n2. Check for a list of your users and, if they added any credit cards or made any transactions, a list of those under each user.\n\nAccept live payments\n\nOnce you're ready to go live, you'll need to exchange your test keys for your\nlive keys. See the [Stripe docs](https://stripe.com/docs/keys) to learn more\nabout these keys.\n\n1. Update your Stripe secret config:\n\n `firebase functions:config:set stripe.secret=\u003cYOUR STRIPE LIVE SECRET KEY\u003e`\n2. Set your [live publishable key](//dashboard.stripe.com/account/apikeys) in\n [`/public/javascript/app.js`](//github.com/thorsten-stripe/functions-samples/blob/thorsten-stripe/update-stripe-template/stripe/public/javascript/app.js#L16).\n\n3. Redeploy both Cloud Functions and Hosting for the changes to take effect:\n `firebase deploy`."]]