注文のフルフィルメントを実行する
Checkout と Payment Links で受け取った支払いのフルフィルメントを履行する方法をご紹介します。
When you receive a payment with the Checkout Sessions API (including Payment Links), you might need to take action to provide your customer with what they paid for. For example, you might need to grant them access to a service, or you might need to ship them physical goods. This process is known as fulfillment, and you have two ways to handle this process:
- 手動: Stripe が提供した情報を使用して、手動で注文のフルフィルメントを履行できます。例えば、ダッシュボード を監視したり、支払い通知メールを確認したり、レポート確認後に注文のフルフィルメントを履行したりすることができます。
- 自動: 自動化されたフルフィルメントシステムを構築できます。 Recommended
1 つ目のオプションは、取引額が少額のプロジェクトまたは実験的なプロジェクトに適していますが、多くの場合、フルフィルメントの自動化をお勧めします。このガイドの以降の部分では、自動フルフィルメントシステムの構築方法を説明します。
自動フルフィルメント
以下に説明する自動フルフィルメントシステムは、Webhook とお客様のウェブサイトへのリダイレクトを組み合わせてフルフィルメントをトリガーします。支払いごとにフルフィルメントが発生するように Webhook を使用する必要があり、リダイレクトによって顧客は支払い後すぐにサービスやフルフィルメントの詳細にアクセスできます。
注
Payment Links は Checkout を使用するため、以下の情報は特に記載のない限り、すべて Payment Links と Checkout の両方に適用されます。
フルフィルメント関数を作成サーバ側
Create a function on your server to fulfill successful payments. Webhooks trigger this function, and it’s called when customers are sent to your website after completing checkout. This guide refers to this function as fulfill_
, but you can name the function whatever you wish.
Your fulfill_
function must:
- Correctly handle being called multiple times with the same Checkout Session ID.
- Accept a Checkout Session ID as an argument.
- Retrieve the Checkout Session from the API with the line_items property expanded.
- Check the payment_status property to determine if it requires fulfillment.
- Perform fulfillment of the line items.
- Record fulfillment status for the provided Checkout Session.
Use the code below as a starting point for your fulfill_
function. The TODO
comments indicate any functionality you must implement.
注
The code snippets below might name the fulfill_
function fulfillCheckout
or FulfillCheckout
depending on the language selected, but they all represent the same function.
注
If a Checkout Session has many line items, use auto-pagination with the API for Checkout line items to retrieve all of them.
Depending on the payment methods you accept and your business needs, you might want to have your fulfill_
function do the following:
- Provision access to services.
- Trigger shipment of goods.
- Save a copy of the payment details and line items in your own database.
- Send the customer a custom receipt email if you don’t have Stripe’s receipts enabled.
- Reconcile line items and quantities purchased if you allow customers to adjust quantities in Checkout.
- Update inventory or stock records.
支払いイベントハンドラーを作成サーバ側
To trigger fulfillment, create a webhook event handler to listen for payment events and trigger your fulfill_
function.
When someone pays you, it creates a checkout.
event. Set up an endpoint on your server to accept, process, and confirm receipt of these events.
Some payment methods aren’t instant, such as ACH direct debit and other bank transfers. Funds won’t be immediately available when Checkout completes. Delayed payment methods generate a checkout.
event when payment succeeds later.
注
The webhook secret (whsec_
) shown in the code below comes from either the Stripe CLI or your webhook endpoint. You can use the Stripe CLI for local testing, and Stripe uses a webhook endpoint to send events to your handler when it’s running on a server. See the next section for more details.
You might also want to listen for and handle checkout.
events. For example, you can send an email to your customer when a delayed payment fails.
イベントハンドラーをローカルでテスト
The quickest way to develop and test your webhook event handler is with the Stripe CLI. If you don’t have the Stripe CLI, follow the install guide to get started.
When the Stripe CLI is installed, you can test your event handler locally. Run your server (for example, on localhost:4242
), then run the stripe listen command to have the Stripe CLI forward events to your local server:
stripe listen --forward-to localhost:4242/webhook Ready! Your webhook signing secret is 'whsec_<REDACTED>' (^C to quit)
Add the webhook secret (whsec_
) to your event handling code, then test fulfillment by going through Checkout as a customer:
- Press the checkout button that takes you to Checkout, or visit your Payment Link
- Provide the following test data in Checkout:
- Enter
4242 4242 4242 4242
as the card number - Enter any future date for card expiry
- Enter any 3-digit number for CVV
- Enter any billing postal code (
90210
)
- Enter
- Press the Pay button
When the payment completes, verify the following:
- On your command line, where
stripe listen
is running, it shows acheckout.
event forwarded to your local server.session. completed - Your server logs show the expected output from your
fulfill_
function.checkout
Webhook エンドポイントを作成
ローカルでテストした後、Webhook イベントハンドラーをサーバで設定して実行します。次に、Webhook エンドポイントを作成して checkout.
イベントをサーバーに送信し、 決済フローを再度テストします。
ランディングページの URL を設定推奨
Checkout を設定することで、顧客の決済完了後にお客様のウェブサイトのページを送信します。ページの URL に {CHECKOUT_
プレースホルダーを含めると、顧客が Checkout からリダイレクトされるときにプレースホルダーが Checkoutセッション ID に置き換えられます。
ホストされた Checkout
デフォルトの ui_mode が hosted
の Checkout セッションでは、success_
を設定します。
注
checkout.
イベントをリッスンする Webhook エンドポイントを設定し、さらに success_
を設定した場合、 Checkout はサーバーが Webhook イベントの送信に応答するのを待ってから顧客をリダイレクトします。この方法を使用する場合は、サーバーが checkout.
イベントにできるだけ早く応答するように設定してください。
Payment Links
API を使用して作成する Payment Links の場合、after_completion.redirect.url を設定します。
Payment Links の場合、次のようにダッシュボードで作成します。
- 支払い後タブに移動します。
- 確認ページを表示しないを選択します。
{CHECKOUT_
プレースホルダーが含まれたランディングページの URL を指定します (例:SESSION_ ID} https://example.
)。com/after-checkout?session_ id={CHECKOUT_ SESSION_ ID}
ランディングページでフルフィルメントをトリガーする推奨
Listening to webhooks is required to make sure you always trigger fulfillment for every payment, but webhooks can sometimes be delayed. To optimize your payment flow and guarantee immediate fulfillment when your customer is present, trigger fulfillment from your landing page as well.
Use the Checkout Session ID from the URL you specified in the previous step to do the following:
- When your server receives a request for your Checkout landing page, extract the Checkout Session ID from the URL.
- Run your
fulfill_
function with the ID provided.checkout - Render the page after the fulfillment attempt is complete.
When you render your landing page you can display the following:
- Details from the fulfillment process.
- Links or information about services the customer now has access to.
- Shipping or logistical details for physical goods.
Webhooks are required
You can’t rely on triggering fulfillment only from your Checkout landing page, because your customers aren’t guaranteed to visit that page. For example, someone can pay successfully in Checkout and then lose their connection to the internet before your landing page loads.
Set up a webhook event handler to have Stripe send payment events directly to your server, bypassing the client entirely. Webhooks are the most reliable way to know when you get paid. If webhook event delivery fails, we retry several times.