コンテンツにスキップ
アカウントを作成
または
サインイン
Stripe ドキュメントのロゴ
/
AI に質問する
アカウントを作成
サインイン
始める
支払い
財務の自動化
プラットフォームおよびマーケットプレイス
資金管理
開発者向けのツール
始める
支払い
財務の自動化
始める
支払い
財務の自動化
プラットフォームおよびマーケットプレイス
資金管理

注

このページはまだ日本語ではご利用いただけません。より多くの言語で文書が閲覧できるように現在取り組んでいます。準備が整い次第、翻訳版を提供いたしますので、もう少しお待ちください。

Webhook なしでカード支払いを受け付ける

サーバでカード決済を確定して、カードの認証リクエストを処理する方法をご紹介します。

注意

Stripe recommends using the newer Payment Element instead of the Card Element. It allows you to accept multiple payment methods with a single Element. Learn more about when to use the Card Element and Payment Element.

For a wider range of support and future proofing, use the standard integration for asynchronous payments.

この組み込みは、Webhook を使用したりオフラインイベントを処理したりせずに、クライアントから返される応答を待って、サーバーで支払いを確定します。この組み込みはシンプルに見えるかもしれませんが、ビジネスの成長に合わせて拡張するのが難しく、以下のような制限があります。

移行をお考えの方へ

If you’re migrating an existing Stripe integration from the Charges API, follow the migration guide.

  • カードのみをサポート: ACH や現地で一般的な支払い方法に個別に対応するには、追加のコードを記述する必要があります。
  • Double-charge risk—By synchronously creating a new PaymentIntent each time your customer attempts to pay, you risk accidentally double-charging your customer. Be sure to follow best practices.
  • クライアントへの追加トリップ: 3D セキュアを備えたカード、または強力な顧客認証 (SCA) などの規制の対象となるカードでは、クライアント側での追加の手順が必要になります。

Keep these limitations in mind if you decide to use this integration. Otherwise, use the standard integration.

Stripe を設定する

まず、Stripe アカウントが必要です。今すぐ登録してください。

アプリケーションから Stripe API にアクセスするには、Stripe の公式ライブラリを使用します。

Command Line
Ruby
# Available as a gem sudo gem install stripe
Gemfile
Ruby
# If you use bundler, you can add this line to your Gemfile gem 'stripe'

カード詳細を収集する
クライアント側

Stripe.js と Stripe Elements を使用して、クライアント側でカード情報を収集します。Elements は、カード番号、郵便番号、有効期限を収集して検証するための事前構築された UI コンポーネントのセットです。

Stripe Element には、HTTPS 接続を介して支払い情報を Stripe に安全に送信する iframe が含まれています。組み込みを機能させるには、決済ページのアドレスの先頭を http:// ではなく https:// にする必要があります。

HTTPS を使用せずに実装をテストできます。本番環境で決済を受け付ける準備が整ったら、HTTPS を有効化します。

Include the Stripe.js script in the head of every page on your site. Elements is automatically available as a feature of Stripe.js.

<script src="https://js.stripe.com/v3/"></script>

Including the script on every page of your site lets you take advantage of Stripe’s advanced fraud functionality and ability to detect anomalous browsing behavior.

支払いフォームを構築する

顧客からカード詳細を安全に収集するために、Stripe がオンラインで提供する UI コンポーネントが Elements によって作成され、iframe として支払いフォームに配置されます。これらのコンポーネントの挿入先を決定するには、一意の ID を持つ空の DOM Elements (コンテナー) を支払いフォーム内に作成します。

index.html
HTML
<form id='payment-form'> <label> Card details <!-- placeholder for Elements --> <div id="card-element"></div> </label> <button type="submit">Submit Payment</button> </form>

Next, create an instance of the Stripe object, providing your publishable API key as the first parameter. Afterwards, create an instance of the Elements object and use it to mount a Card element in the relevant placeholder in the page.

script.js
サンプル全体を表示
const stripe = Stripe(
'pk_test_TYooMQauvdEDq54NiTphI7jx'
); const elements = stripe.elements(); // Set up Stripe.js and Elements to use in checkout form const style = { base: { color: "#32325d", fontFamily: '"Helvetica Neue", Helvetica, sans-serif', fontSmoothing: "antialiased", fontSize: "16px", "::placeholder": { color: "#aab7c4" } }, invalid: { color: "#fa755a", iconColor: "#fa755a" }, }; const cardElement = elements.create('card', {style}); cardElement.mount('#card-element');

card Element は、カードに関する必要な詳細情報をすべて安全に収集する 1 つの柔軟な入力フィールドを挿入することによって、フォームを簡素化し、必要なフィールドの数を最小限に抑えます。

あるいは、cardNumber、cardExpiry、cardCvc の Element を組み合わせて、複数入力の柔軟なカードフォームを作成します。

注

カードの支払い成功率を向上し、不正使用を削減するため、常に郵便番号を収集してください。

単一行の Card Element は、顧客の郵便番号を自動的に収集して Stripe に送信します。分割 Element (Card Number、Expiry、CVC) を使用して支払いフォームを構築する場合、顧客の郵便番号用に別個の入力フィールドを追加します。

PaymentMethod を作成する

最後に、クライアント側で stripe.createPaymentMethod を使用してカード詳細を収集し、ユーザーが送信ボタンをクリックしたときに PaymentMethod を作成します。

script.js
const form = document.getElementById('payment-form'); form.addEventListener('submit', async (event) => { // We don't want to let default form submission happen here, // which would refresh the page. event.preventDefault(); const result = await stripe.createPaymentMethod({ type: 'card', card: cardElement, billing_details: { // Include any additional collected billing details. name: 'Jenny Rosen', }, }) stripePaymentMethodHandler(result); });

サーバへ PaymentMethod を送信する
クライアント側

PaymentMethod が正常に作成されたら、その ID をサーバーに送信します。

script.js
const stripePaymentMethodHandler = async (result) => { if (result.error) { // Show error in payment form } else { // Otherwise send paymentMethod.id to your server (see Step 4) const res = await fetch('/pay', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ payment_method_id: result.paymentMethod.id, }), }) const paymentResponse = await res.json(); // Handle server response (see Step 4) handleServerResponse(paymentResponse); } }

PaymentIntent を作成する
サーバ側

リクエストを受信するためにサーバにエンドポイントを設定します。このエンドポイントは、後で でも、追加の認証ステップが必要なカードの処理に使用されます。

Create a new PaymentIntent with the ID of the PaymentMethod created on your client. You can confirm the PaymentIntent by setting the confirm property to true when the PaymentIntent is created or by calling confirm after creation. Separate authorization and capture is also supported for card payments.

支払いで 3D セキュアの認証など、追加のアクションが求められる場合、PaymentIntent のステータスは requires_action に設定されます。支払いが失敗すると、ステータスは requires_payment_method に戻され、ユーザにエラーを表示する必要があります。支払いで追加認証が求められない場合は、支払いが作成され、PaymentIntent のステータスは succeeded に設定されます。

注

2019-02-11 以前の API のバージョンでは、requires_payment_method の代わりに requires_source、requires_action の代わりに requires_source_action が表示されます。

Command Line
curl
curl https://api.stripe.com/v1/payment_intents \ -u
sk_test_BQokikJOvBiI2HlWgH4olfQ2
:
\ -d "payment_method"="{{PAYMENT_METHOD_ID}}" \ -d "amount"=1099 \ -d "currency"="usd" \ -d "confirmation_method"="manual" \ -d "confirm"="true"

If you want to save the card to reuse later, create a Customer to store the PaymentMethod and pass the following additional parameters when creating the PaymentIntent:

  • customer. Set to the ID of the Customer.
  • setup_future_usage. Set to off_session to tell Stripe that you plan to reuse this PaymentMethod for off-session payments when your customer is not present. Setting this property saves the PaymentMethod to the Customer after the PaymentIntent is confirmed and any required actions from the user are complete. See the code sample on saving cards after a payment for more details.

次のアクションを処理する
クライアント側

顧客による操作が必要な状況を処理するためのコードを記述します。通常、ステップ 4 のサーバーでの確定後に支払いは成功しますが、3D セキュアによる認証など、PaymentIntent で顧客による追加の対応が必要になったときに、このコードが機能します。

Use stripe.handleCardAction to trigger the UI for handling customer action. If authentication succeeds, the PaymentIntent has a status of requires_confirmation. Confirm the PaymentIntent again on your server to finish the payment.

While testing, use a test card number that requires authentication (for example, ) to force this flow. Using a card that doesn’t require authentication (for example, ) skips this part of the flow and completes at step 4.

script.js
const handleServerResponse = async (response) => { if (response.error) { // Show error from server on payment form } else if (response.requires_action) { // Use Stripe.js to handle the required card action const { error: errorAction, paymentIntent } = await stripe.handleCardAction(response.payment_intent_client_secret); if (errorAction) { // Show error from Stripe.js in payment form } else { // The card action has been handled // The PaymentIntent can be confirmed again on the server const serverResponse = await fetch('/pay', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ payment_intent_id: paymentIntent.id }) }); handleServerResponse(await serverResponse.json()); } } else { // Show success message } }

注

stripe.handleCardAction の完了には数秒かかる場合があります。この間、フォームが再送信されないように無効化し、スピナーのような待機中のインジケータを表示します。エラーが発生した場合は、エラーを顧客に知らせ、フォームを再度有効化し、待機中のインジケータを非表示にします。支払いを完了するために、顧客が認証などの追加ステップを実行する必要がある場合は、Stripe.js がそのプロセスをステップごとに顧客に提示します。

PaymentIntent を再度確定する
サーバ側

このコードは、前のステップでの処理と同様に、支払いに追加の認証が必要なときにのみ実行されます。どの支払いでもこの追加ステップが必要になる場合があるため、コード自体はオプションではありません。

上記で設定したものと同じエンドポイントを使用し、PaymentIntent を再度確定することにより、支払いを完了して注文のフルフィルメントを実行します。この確定は支払い試行から 1 時間以内に実行してください。実行されない場合は、支払いが失敗して取引が requires_payment_method に戻されます。

Command Line
curl
curl https://api.stripe.com/v1/payment_intents/{{PAYMENT_INTENT_ID}}/confirm \ -u
sk_test_BQokikJOvBiI2HlWgH4olfQ2
:
\ -X "POST"

組み込みをテストする

​​Several test cards are available for you to use in a sandbox to make sure this integration is ready. Use them with any CVC and an expiration date in the future.

番号説明
支払いが成功し、すぐに処理されます。
認証が必要です。Stripe は、顧客に認証を求めるモーダルをトリガーします。
常に支払い拒否コード insufficient_funds で失敗します。

テストカードの一覧については、テストに関するガイドを参照してください。

オプションセキュリティコードを再収集する

このページはお役に立ちましたか。
はいいいえ
お困りのことがございましたら 、サポートにお問い合わせください。
早期アクセスプログラムにご参加ください。
変更ログをご覧ください。
ご不明な点がございましたら、お問い合わせください。
LLM ですか?llms.txt を読んでください。
Powered by Markdoc