Dynamically update trial durations
Learn how to modify subscription trial periods during checkout.
Private preview
Dynamic trial updates is in private preview. Request access to dynamic trial updates.
サブスクリプションの Checkout セッションのトライアル期間を動的に更新する方法をご紹介します。
ユースケース
- 動的なトライアル管理:プロモーション条件や顧客のアクションに基づいてトライアルを追加または削除します。
- アップセル時のトライアル期間の延長:顧客が上位プランにアップグレードする際に、トライアル期間を延長します(例: 月額の 7 日間を年額では 14 日間に延長)。
Payment Intents API
If you use the Payment Intents API, you can use the Subscriptions API to adjust trial settings.
Set up the SDKServer-side
Stripe の公式ライブラリを使用して、アプリケーションから Stripe API にアクセスします。
Update the server SDKServer-side
To use this beta, first update your SDK to use the private preview API version and the checkout_
beta version header.
Dynamically update trial durationsServer-side
Create an endpoint on your server to update the trial duration for a yearly upsell on the Checkout Session. You call this from the front end in a later step.
セキュリティのヒント
Client-side code runs in an environment that’s controlled by the user. A malicious user can bypass your client-side validation, intercept and modify requests, or create new requests to your server.
When creating an endpoint, we recommend the following:
- Create endpoints for specific customer interactions instead of making them generic. For example, “extend trial for yearly upgrade” instead of a general “update” action. Specific endpoints can help with writing and maintaining validation logic.
- Don’t pass session data directly from the client to your endpoint. Malicious clients can modify request data, making it an unreliable source for determining the Checkout Session state. Instead, pass the session ID to your server and use it to securely retrieve the data from the Stripe API.
You can update trial durations using either:
trial_
: An integer that represents the number of days for the trial period, or an empty string to remove the trial.period_ days trial_
: A Unix timestamp that represents when the trial should end, or an empty string to remove the trial.end
Keep in mind the following:
- The
trial_
andperiod_ days trial_
parameters are mutually exclusive. You can only specify one of them in a single update request.end - When removing a trial, use the same field that it was set with. You can only use
trial_
to remove a trial set withperiod_ days: "" trial_
. You can only useperiod_ days trial_
to remove a trial set withend: "" trial_
.end
Test the integration
実装内容をテストして、トライアル期間の更新が正しく機能していることを確認します。
- 初回トライアル期間を指定してサブスクリプションの Checkout セッションを作成します。
- 作成した UI 要素を操作して、サーバーエンドポイントをトリガーします。
- トライアル期間が Checkout セッションで正しく更新されていることを確認します。
- 決済を完了して、サブスクリプションが正しいトライアル設定で作成されていることを確認します。
注
テストする際は、Stripe のテスト環境を使用して本番サブスクリプションが作成されないようにします。トライアル期間の変更を確認するには、Checkout Session オブジェクトまたは作成されたサブスクリプションを調べます。
一般的なシナリオのテスト
- トライアルの延長:7 日間のトライアルを開始した後、期間を 14 日間に延長し、その変更が UI と セッションオブジェクトに反映されていることを確認します。
- 既存のトライアルの削除:7 日間のトライアルを開始した後、トライアルを削除し、その変更が UI とセッションオブジェクトに反映されていることを確認します。設定されたフィールドと同じフィールドを使用してトライアルを削除します。
- エラー処理:無効なリクエストをテストして、エラー処理が正しく機能することを確認します。