決済フォームにカスタムフィールドを追加すると、顧客から追加情報を収集できます。この情報は、支払いの完了後に使用できるようになり、購入のフルフィルメントに利用できます。
カスタムフィールドには以下の制限があります。
- フィールドは 3 つまで使用できます。
setup
モードでは使用できません。- テキストフィールドで対応できるのは 255 文字までです。
- 数値フィールドは、最大 255 桁に対応できます。
- ドロップダウンフィールドでは最大 200 件のオプションを使用できます。
注意
カスタムフィールドは、個人情報、保護対象のデータ、機密データ、法律によって制限されている情報の収集には使用しないでください。
カスタムフィールドの配列を指定する際に、Checkout セッションを作成します。各フィールドには、実装でフィールドの照合に使用する一意の key
が必要です。また、顧客に表示するフィールドのラベルも指定してください。カスタムフィールドのラベルは翻訳されませんが、locale パラメーターを使用して、Checkout セッションの言語をラベルの言語に一致させることができます。
curl https://api.stripe.com/v1/checkout/sessions \
-u "sk_test_4eC39HqLyjWDarjtT1zdp7dc
:" \
-d mode=payment \
--data-urlencode success_url="https://example.com/success" \
--data-urlencode cancel_url="https://example.com/cancel" \
-d "line_items[0][price]"= \
-d "line_items[0][quantity]"=1 \
-d "custom_fields[0][key]"=engraving \
-d "custom_fields[0][label][type]"=custom \
-d "custom_fields[0][label][custom]"="Personalized engraving" \
-d "custom_fields[0][type]"=text
顧客が Checkout セッションを完了すると、フィールドが入力されたことを示す checkout.session.completed Webhook が送信されます。
以下に checkout.session.completed
ペイロードの例を示します。
{
"id": "evt_1Ep24XHssDVaQm2PpwS19Yt0",
"object": "event",
"api_version": "2022-11-15",
"created": 1664928000,
"data": {
"object": {
"id": "cs_test_MlZAaTXUMHjWZ7DcXjusJnDU4MxPalbtL5eYrmS2GKxqscDtpJq8QM0k",
"object": "checkout.session",
"custom_fields": [{
"key": "engraving",
"label": {
"type": "custom",
"custom": "Personalized engraving"
},
"optional": false,
"type": "text",
"text": {
"value": "Jane",
}
}],
"mode": "payment",
}
},
"livemode": false,
"pending_webhooks": 1,
"request": {
"id": null,
"idempotency_key": null
},
"type": "checkout.session.completed"
}
フィールドにオプションのマークを付ける
デフォルトでは、顧客は、支払いを完了する前に、すべてのフィールドに入力する必要があります。フィールドにオプションのマークを付けるには、optional=true
を渡します。
curl https://api.stripe.com/v1/checkout/sessions \
-u "sk_test_4eC39HqLyjWDarjtT1zdp7dc
:" \
-d mode=payment \
--data-urlencode success_url="https://example.com/success" \
--data-urlencode cancel_url="https://example.com/cancel" \
-d "line_items[0][price]"= \
-d "line_items[0][quantity]"=1 \
-d "custom_fields[0][key]"=engraving \
-d "custom_fields[0][label][type]"=custom \
-d "custom_fields[0][label][custom]"="Personalized engraving" \
-d "custom_fields[0][type]"=text \
-d "custom_fields[0][optional]"=true
ドロップダウンフィールドを追加する
ドロップダウンフィールドに、選択するオプションのリストが顧客に表示されます。ドロップダウンフィールドを作成するには、type=dropdown
とオプションのリストを指定し、それぞれにlabel
と value
を指定します。label
は顧客に表示され、実装では、value
を使用して、顧客が選択したオプションを照合します。
curl https://api.stripe.com/v1/checkout/sessions \
-u "sk_test_4eC39HqLyjWDarjtT1zdp7dc
:" \
-d mode=payment \
--data-urlencode success_url="https://example.com/success" \
--data-urlencode cancel_url="https://example.com/cancel" \
-d "line_items[0][price]"= \
-d "line_items[0][quantity]"=1 \
-d "custom_fields[0][key]"=sample \
-d "custom_fields[0][label][type]"=custom \
-d "custom_fields[0][label][custom]"="Free sample" \
-d "custom_fields[0][optional]"=true \
-d "custom_fields[0][type]"=dropdown \
-d "custom_fields[0][dropdown][options][0][label]"="Balm sample" \
-d "custom_fields[0][dropdown][options][0][value]"=balm \
-d "custom_fields[0][dropdown][options][1][label]"="BB cream sample" \
-d "custom_fields[0][dropdown][options][1][value]"=cream
数値のみのフィールドを追加する
数値のみのフィールドは、255 桁までの数値のみを受け付けるテキストフィールドを顧客に提供します。数値のみのフィールドを作成するには、type=numeric
を指定します。
curl https://api.stripe.com/v1/checkout/sessions \
-u "sk_test_4eC39HqLyjWDarjtT1zdp7dc
:" \
-d mode=payment \
--data-urlencode success_url="https://example.com/success" \
--data-urlencode cancel_url="https://example.com/cancel" \
-d "line_items[0][price]"= \
-d "line_items[0][quantity]"=1 \
-d "custom_fields[0][key]"=invoice \
-d "custom_fields[0][label][type]"=custom \
-d "custom_fields[0][label][custom]"="Invoice number" \
-d "custom_fields[0][type]"=numeric
サブスクリプションのカスタムフィールドを取得する
subscription パラメーターを使用して作成した Checkout セッションのクエリを実行することで、サブスクリプションに関連付けられているカスタムフィールドを取得できます。
curl -G https://api.stripe.com/v1/checkout/sessions \
-u "sk_test_4eC39HqLyjWDarjtT1zdp7dc
:" \
-d subscription=
文字数の検証を追加する
必要に応じて text
フィールドタイプと numeric
フィールドタイプの最小文字数と最大文字数の要件を指定できます。
curl https://api.stripe.com/v1/checkout/sessions \
-u "sk_test_4eC39HqLyjWDarjtT1zdp7dc
:" \
-d mode=payment \
--data-urlencode success_url="https://example.com/success" \
--data-urlencode cancel_url="https://example.com/cancel" \
-d "line_items[0][price]"= \
-d "line_items[0][quantity]"=1 \
-d "custom_fields[0][key]"=engraving \
-d "custom_fields[0][label][type]"=custom \
-d "custom_fields[0][label][custom]"="Personalized engraving" \
-d "custom_fields[0][type]"=text \
-d "custom_fields[0][text][minimum_length]"=10 \
-d "custom_fields[0][text][maximum_length]"=20 \
-d "custom_fields[0][optional]"=true
デフォルト値を追加する
必要に応じて、テキスト、数値、およびドロップダウンフィールドタイプのデフォルト値を指定できます。デフォルト値は、支払いページに事前入力されています。
curl https://api.stripe.com/v1/checkout/sessions \
-u "sk_test_4eC39HqLyjWDarjtT1zdp7dc
:" \
-d mode=payment \
--data-urlencode success_url="https://example.com/success" \
--data-urlencode cancel_url="https://example.com/cancel" \
-d "line_items[0][price]"= \
-d "line_items[0][quantity]"=1 \
-d "custom_fields[0][key]"=engraving \
-d "custom_fields[0][label][type]"=custom \
-d "custom_fields[0][label][custom]"="Personalized engraving" \
-d "custom_fields[0][type]"=text \
-d "custom_fields[0][text][default_value]"=Stella \
-d "custom_fields[1][key]"=size \
-d "custom_fields[1][label][type]"=custom \
-d "custom_fields[1][label][custom]"=Size \
-d "custom_fields[1][type]"=dropdown \
-d "custom_fields[1][dropdown][default_value]"=small \
-d "custom_fields[1][dropdown][options][0][value]"=small \
-d "custom_fields[1][dropdown][options][0][label]"=Small \
-d "custom_fields[1][dropdown][options][1][value]"=large \
-d "custom_fields[1][dropdown][options][1][label]"=Large