# オーストラリアでの BECS ダイレクトデビットによるサブスクリプションを設定する BECS ダイレクトデビットを使用したサブスクリプションの作成と請求の方法をご紹介します。 > 新規ユーザーの場合は、このガイドで説明する Stripe Elements ではなく [Payment Element](https://docs.stripe.com/payments/payment-element.md) を使用してください。Payment Element は、コンバージョン最適化が組み込まれたローコードの連携パスを提供します。手順については、[サブスクリプションの構築](https://docs.stripe.com/billing/subscriptions/build-subscriptions.md?payment-ui=elements)をご覧ください。 このガイドを使用して、決済手段として [BECS ダイレクトデビット](https://docs.stripe.com/payments/au-becs-debit.md)を使用する*サブスクリプション* (A Subscription represents the product details associated with the plan that your customer subscribes to. Allows you to charge the customer on a recurring basis)を設定します。 ## 商品と価格を作成する [ダッシュボード] [Products (商品)](https://docs.stripe.com/api/products.md) は、販売しているアイテムまたはサービスを表します。[Prices (価格)](https://docs.stripe.com/api/prices.md) は、商品の価格と請求頻度を定義します。これには、商品の価格、受け付ける通貨、および 1 回限りの支払いか継続支払いかが含まれます。商品と価格が数個のみの場合は、ダッシュボードでそれらを作成および管理します。 このガイドでは、例としてストックフォトサービスを使用し、15 AUD の月次サブスクリプションを顧客に請求します。これをモデル化するには、次のようにします。 1. [商品](https://dashboard.stripe.com/products?active=true)ページに移動し、**商品を作成**をクリックします。 1. 商品の**名前**を入力します。オプションで**説明**を追加して、商品の画像をアップロードできます。 1. **商品税コード**を選択します。[商品税コード](https://docs.stripe.com/tax/tax-codes.md)の詳細をご確認ください。 1. **継続**を選択します。次に、価格に**15**を入力し、通貨として**\**を選択します。 1. **価格に税金を含める**かどうかを選択します。[税金設定](https://dashboard.stripe.com/test/settings/tax)のデフォルト値を使用するか、値を手動で設定できます。この例では、**自動**を選択します。 1. **請求期間**で**月次**を選択します。 1. **その他の料金体系オプション**をクリックします。次に、この例の料金体系モデルとして**定額**を選択します。[定額料金](https://docs.stripe.com/products-prices/pricing-models.md#flat-rate)とその他の[料金体系モデル](https://docs.stripe.com/products-prices/pricing-models.md)の詳細をご確認ください。 1. 将来的に特定の価格を整理、クエリ、更新するために、内部**価格の説明**と[検索キー](https://docs.stripe.com/products-prices/manage-prices.md#lookup-keys) 追加します。 1. **次へ**をクリックします。次に、**商品を追加**をクリックします。 商品と価格を作成したら、価格 ID を記録しておき、後続のステップで使用できるようにします。ID は料金体系ページで `price_G0FvDp6vZvdwRZ` のように表示されます。 ## SetupIntent を作成する [サーバ側] [SetupIntent (支払い方法設定インテント)](https://docs.stripe.com/api/setup_intents.md) は、今後の支払いのために顧客の決済手段をセットアップするお客様の意図を表すオブジェクトです。`SetupIntent` は、この設定プロセスのステップを追跡します。BECS ダイレクトデビットの場合、このステップには顧客からの同意書の収集と、ライフサイクル全体にわたる同意書の有効性の追跡が含まれます。 次のように、[payment_method_types](https://docs.stripe.com/api/setup_intents/create.md#create_setup_intent-payment_method_types) を `au_becs_debit` に設定して、サーバー上で [SetupIntent (支払い方法設定インテント)](https://docs.stripe.com/api/setup_intents.md) を作成します。 #### curl ```bash curl https://api.stripe.com/v1/setup_intents \ -u <>: \ -d "payment_method_types[]"="au_becs_debit" ``` 返される `SetupIntent` オブジェクトには、`client_secret` プロパティが含まれています。この client secret をクライアント側のアプリケーションに渡して、設定プロセスを続行します。 ## 支払い方法の詳細と同意書承認を収集する [クライアント側] [Stripe Elements](https://docs.stripe.com/payments/elements.md) を使用してクライアントで支払い情報を収集する準備ができました。Elements は、支払いの詳細を収集するための構築済み UI コンポーネントのセットです。 Stripe Element には、HTTPS 接続を介して支払い情報を Stripe に安全に送信する iframe が含まれています。組み込みを機能させるには、決済ページのアドレスの先頭を http:// ではなく https:// にする必要があります。 HTTPS を使用せずに実装をテストできます。本番環境で決済を受け付ける準備が整ったら、HTTPS を[有効化](https://docs.stripe.com/security/guide.md#tls)します。 ### Stripe Elements を設定する #### HTML + JS Stripe Elements は Stripe.js の機能として自動的に使用できるようになります。支払いページに Stripe.js スクリプトを含めるには、HTML ファイルの `head` にこのスクリプトを追加します。常に js.stripe.com から Stripe.js を直接読み込むことにより、PCI への準拠が維持されます。スクリプトをバンドルに含めたり、そのコピーを自身でホストすることがないようにしてください。 ```html Payment Setup ``` 次の JavaScript を使用して、お客様の決済ページに [Elements](https://docs.stripe.com/js.md#stripe-elements) のインスタンスを作成します。 ```javascript const stripe = Stripe('<>'); const elements = stripe.elements(); ``` ### ダイレクトデビットのリクエスト お客様が BECS ダイレクトデビット支払いを作成する前に、顧客がダイレクトデビットリクエスト利用規約に同意する必要があります。顧客は入力を完了したダイレクトデビットリクエスト (DDR) を送信することで、この規約に同意したことになります。この承認により、お客様は顧客の口座から引き落としを行う同意書を得られます。`Mandate` は支払い方法へのデビットが許可されたことの記録です。 同意書にオンラインで同意してもらうために、フォームを作成して必要な情報を収集できます。フォームは [HTTPS](https://docs.stripe.com/security/guide.md#tls) で提供し、以下の情報を取得します。 | 情報 | 説明 | | ---------- | ----------------------------------------- | | **口座名義人名** | 口座名義人の氏名 | | **BSB 番号** | 銀行口座の Bank-State-Branch 番号 (例: `123-456`) | | **口座番号** | 銀行口座番号 (例: `87654321`) | ダイレクトデビットリクエストを収集する際には、[BECS ダイレクトデビット規約](https://stripe.com/au-becs/legal)に従い、決済フォームの一環として次のことを行います。 - [Stripe の DDR 利用規約](https://stripe.com/au-becs-dd-service-agreement/legal)の正確な規約をフォーム上にインラインで、またはフォームからリンクしたページに表示し、それが「DDR 利用規約」であると明記します。 - 同意された DDR とこれに伴う [DDR 利用規約](https://stripe.com/au-becs-dd-service-agreement/legal)を顧客がいつでも参照できるようにします。これは印刷物でも、変更不可の電子的なコピー (メールなど) の形でもかまいません。お客様が利用できるように Stripe がこの DDR をオンラインで提供します - 以下に示す許可のための標準的なテキストを表示し、顧客に BECS DDR に同意してもらいます。「Rocketship Inc」 を自社名に置き換えてください。顧客が同意すると、顧客の銀行口座からの BECS ダイレクトデビット支払いが許可されたことになります。 > 銀行口座の詳細を提供することにより、このダイレクトデビットリクエストと[ダイレクトデビットリクエスト利用規約](https://stripe.com/au-becs-dd-service-agreement/legal)に同意し、Stripe Payments Australia Pty Ltd ACN 160 180 343 ダイレクトデビットユーザー ID 番号 507156 (「Stripe」) が *Rocketship Inc* (「加盟店」) に代わり、加盟店との利用規約の詳細に従って、一括電子決済システム (BECS) を通じてアカウントから決済を引き落とすことを承認します。上記の口座のアカウント所有者または権限のある署名者であることを証明します。 同意された同意書の詳細情報は、[PaymentMethod](https://docs.stripe.com/payments/payment-methods.md) を設定する際、または `PaymentIntent` を確認する際に生成されます。この同意書 (同意された DDR とこれに伴う DDR 利用規約) はいつでも顧客に提示できる必要があります。これは印刷物でも、変更不可の電子的なコピー (メールなど) でもかまいません。Stripe はお客様が利用できるように、この利用規約を `PaymentMethod` にリンクされた `Mandate` オブジェクトの `url` プロパティでホストします。 ### オーストラリアの銀行口座 Element を追加および設定する Australia Bank Account Element は、BSB 番号と口座番号の両方の収集と検証に役立ちます。この Element は、決済フォーム上に保存場所が必要です。決済フォームに、一意の ID を指定して空の DOM ノード (コンテナー) を作成してください。また、顧客は[ダイレクトデビットリクエスト利用規約](https://stripe.com/au-becs-dd-service-agreement/legal)を読んで同意する必要もあります。 #### HTML ```html
By providing your bank account details, you agree to this Direct Debit Request and the Direct Debit Request service agreement, and authorise Stripe Payments Australia Pty Ltd ACN 160 180 343 Direct Debit User ID number 507156 (“Stripe”) to debit your account through the Bulk Electronic Clearing System (BECS) on behalf ofRocket Rides(the "Merchant") for any amounts separately communicated to you by the Merchant. You certify that you are either an account holder or an authorised signatory on the account listed above.
``` フォームが読み込まれるときに、次のように、Australia Bank Account Element の[インスタンスを作成](https://docs.stripe.com/js/elements_object/create_element?type=au_bank_account)し、それを Element コンテナーにマウントできます。 ```javascript // Custom styling can be passed to options when creating an Element const style = { base: { color: '#32325d', fontSize: '16px', '::placeholder': { color: '#aab7c4' }, ':-webkit-autofill': { color: '#32325d', }, }, invalid: { color: '#fa755a', iconColor: '#fa755a', ':-webkit-autofill': { color: '#fa755a', }, } }; const options = { style: style, disabled: false, hideIcon: false, iconStyle: "default", // or "solid" } // Create an instance of the auBankAccount Element. const auBankAccount = elements.create('auBankAccount', options); // Add an instance of the auBankAccount Element into // the `au-bank-account-element`
. auBankAccount.mount('#au-bank-account-element'); ``` #### React #### npm npm パブリックレジストリーから [React Stripe.js](https://www.npmjs.com/package/@stripe/react-stripe-js) と [Stripe.js ローダー](https://www.npmjs.com/package/@stripe/stripe-js)をインストールします。 ```bash npm install --save @stripe/react-stripe-js @stripe/stripe-js ``` #### umd Stripe は npm やモジュールを使用しないサイト向けに UMD ビルドも提供しています。 グローバルな `Stripe` 機能をエクスポートする Stripe.js スクリプトと、グローバルな `ReactStripe` オブジェクトをエクスポートする React Stripe.js の UMD ビルドを含めてください。常に **js.stripe.com** から Stripe.js スクリプトを直接読み込むことにより、PCI への準拠が維持されます。スクリプトをバンドルに含めたり、そのコピーを自身でホストすることがないようにしてください。 ```html ``` > [CodeSandbox でのデモ](https://codesandbox.io/s/react-stripe-official-q1loc?fontsize=14&hidenavigation=1&theme=dark)を使用すると、新しいプロジェクトを作成することなく、React Stripe.js を試してみることができます。 ### お客様のページへの Stripe.js および Elements の追加 Element コンポーネントを使用するには、決済ページコンポーネントを [Elements プロバイダー](https://docs.stripe.com/sdks/stripejs-react.md#elements-provider)でラップします。公開キーを使用して `loadStripe` を呼び出し、返された `Promise` を `Elements` プロバイダーに渡します。 ```jsx import React from 'react'; import ReactDOM from 'react-dom'; import {Elements} from '@stripe/react-stripe-js'; import {loadStripe} from '@stripe/stripe-js'; import PaymentSetupForm from './PaymentSetupForm'; // Make sure to call `loadStripe` outside of a component's render to avoid // recreating the `Stripe` object on every render. const stripePromise = loadStripe('<>'); function App() { return ( ); }; ReactDOM.render(, document.getElementById('root')); ``` ### ダイレクトデビットのリクエスト お客様が BECS ダイレクトデビット支払いを作成する前に、顧客がダイレクトデビットリクエスト利用規約に同意する必要があります。顧客は入力を完了したダイレクトデビットリクエスト (DDR) を送信することで、この規約に同意したことになります。この承認により、お客様は顧客の口座から引き落としを行う同意書を得られます。`Mandate` は支払い方法へのデビットが許可されたことの記録です。 同意書にオンラインで同意してもらうために、フォームを作成して必要な情報を収集できます。フォームは [HTTPS](https://docs.stripe.com/security/guide.md#tls) で提供し、以下の情報を取得します。 | 情報 | 説明 | | ---------- | ----------------------------------------- | | **口座名義人名** | 口座名義人の氏名 | | **BSB 番号** | 銀行口座の Bank-State-Branch 番号 (例: `123-456`) | | **口座番号** | 銀行口座番号 (例: `87654321`) | ダイレクトデビットリクエストを収集する際には、[BECS ダイレクトデビット規約](https://stripe.com/au-becs/legal)に従い、決済フォームの一環として次のことを行います。 - [Stripe の DDR 利用規約](https://stripe.com/au-becs-dd-service-agreement/legal)の正確な規約をフォーム上にインラインで、またはフォームからリンクしたページに表示し、それが「DDR 利用規約」であると明記します。 - 同意された DDR とこれに伴う [DDR 利用規約](https://stripe.com/au-becs-dd-service-agreement/legal)を顧客がいつでも参照できるようにします。これは印刷物でも、変更不可の電子的なコピー (メールなど) の形でもかまいません。お客様が利用できるように Stripe がこの DDR をオンラインで提供します - 以下に示す許可のための標準的なテキストを表示し、顧客に BECS DDR に同意してもらいます。「Rocketship Inc」 を自社名に置き換えてください。顧客が同意すると、顧客の銀行口座からの BECS ダイレクトデビット支払いが許可されたことになります。 > 銀行口座の詳細を提供することにより、このダイレクトデビットリクエストと[ダイレクトデビットリクエスト利用規約](https://stripe.com/au-becs-dd-service-agreement/legal)に同意し、Stripe Payments Australia Pty Ltd ACN 160 180 343 ダイレクトデビットユーザー ID 番号 507156 (「Stripe」) が *Rocketship Inc* (「加盟店」) に代わり、加盟店との利用規約の詳細に従って、一括電子決済システム (BECS) を通じてアカウントから決済を引き落とすことを承認します。上記の口座のアカウント所有者または権限のある署名者であることを証明します。 同意された同意書の詳細情報は、[PaymentMethod](https://docs.stripe.com/payments/payment-methods.md) を設定する際、または `PaymentIntent` を確認する際に生成されます。この同意書 (同意された DDR とこれに伴う DDR 利用規約) はいつでも顧客に提示できる必要があります。これは印刷物でも、変更不可の電子的なコピー (メールなど) でもかまいません。Stripe はお客様が利用できるように、この利用規約を `PaymentMethod` にリンクされた `Mandate` オブジェクトの `url` プロパティでホストします。 ### AuBankAccountElement コンポーネントを追加および設定する `AuBankAccountElement` コンポーネントは、BSB 番号と口座番号の両方を収集して検証するために役立ちます。また、顧客は[ダイレクトデビットリクエスト利用規約](https://stripe.com/au-becs-dd-service-agreement/legal)を読んで同意する必要もあります。 #### JSX ```jsx /** * Use the CSS tab above to style your Element's container. */ import React from 'react'; import {AuBankAccountElement} from '@stripe/react-stripe-js'; import './BecsFormStyles.css' // Custom styling can be passed as options when creating an Element. const AU_BANK_ACCOUNT_STYLE = { base: { color: '#32325d', fontSize: '16px', '::placeholder': { color: '#aab7c4' }, ':-webkit-autofill': { color: '#32325d', }, }, invalid: { color: '#fa755a', iconColor: '#fa755a', ':-webkit-autofill': { color: '#fa755a', }, } }; const AU_BANK_ACCOUNT_ELEMENT_OPTIONS = { style: AU_BANK_ACCOUNT_STYLE, disabled: false, hideIcon: false, iconStyle: "default", // or "solid" }; export default function BecsForm({onSubmit, disabled}) { return (
{/* Display mandate acceptance text. */}
By providing your bank account details, you agree to this Direct Debit Request and the Direct Debit Request service agreement, and authorise Stripe Payments Australia Pty Ltd ACN 160 180 343 Direct Debit User ID number 507156 (“Stripe”) to debit your account through the Bulk Electronic Clearing System (BECS) on behalf ofRocket Rides(the "Merchant") for any amounts separately communicated to you by the Merchant. You certify that you are either an account holder or an authorised signatory on the account listed above.
``` Element は完全にカスタマイズ可能です。お客様のサイトのデザインと雰囲気に合わせて [Element のスタイルを設定](https://docs.stripe.com/js/elements_object/create_element?type=au_bank_account#elements_create-options)して、顧客にシームレスな決済体験を提供できます。また、Element が選択されているときなど、さまざまな入力状態のスタイルを設定することもできます。 ## Stripe に支払い方法の詳細を送信する [クライアント側] `SetupIntent` オブジェクト全体をクライアントに送信するのではなく、[ステップ 2](https://docs.stripe.com/billing/subscriptions/au-becs-debit.md#web-create-setup-intent) で取得した [client secret](https://docs.stripe.com/api/setup_intents/object.md#setup_intent_object-client_secret) を使用します。これは、Stripe API リクエストを認証する API キーとは異なります。 client secret は設定を完了できるため、慎重に取り扱う必要があります。記録したり、URL に埋め込んだり、当該の顧客以外に漏洩することがないようにしてください。 #### HTML + JS ユーザーがフォームを送信したら、[stripe.confirmAuBecsDebitSetup](https://docs.stripe.com/js/setup_intents/confirm_au_becs_debit_setup) を使用してセットアップを完了します。セットアップに成功すると、SetupIntent の `status` プロパティに `succeeded` 値が返されます。セットアップが成功しなかった場合は、返された `error` を調べて原因を判断してください。 ```javascript const form = document.getElementById('setup-form'); const accountholderName = document.getElementById('accountholder-name'); const email = document.getElementById('email'); const submitButton = document.getElementById('submit-button'); const clientSecret = submitButton.dataset.secret; form.addEventListener('submit', async (event) => { event.preventDefault(); stripe.confirmAuBecsDebitSetup( clientSecret, { payment_method: { au_becs_debit: auBankAccount, billing_details: { name: accountholderName.value, email: email.value } } } ); }); ``` `SetupIntent` が正常に確定した後、[Mandate (同意書) オブジェクト](https://docs.stripe.com/api/mandates.md)から取得した[同意書の URL](https://docs.stripe.com/api/mandates/object.md#mandate_object-payment_method_details-au_becs_debit-url) を顧客に提示する必要があります。また顧客に同意書が確定したことを伝える際には、下記の詳細情報を含めることを推奨します。 - ダイレクトデビットの準備が整ったことを示す明確な確認メッセージ - 口座から引き落とされるたびに顧客の銀行明細書に表示される[ビジネス名](https://docs.stripe.com/billing/subscriptions/au-becs-debit.md#statement-descriptors) - 支払い金額とスケジュール (該当する場合) - 生成された DDR 同意書 URL へのリンク `Mandate` オブジェクトの ID は、SetupIntent オブジェクトの `mandate` から取得できます。この ID は、確定後に送信される `setup_intent.succeeded` イベントの一部として送信され、[API から取得](https://docs.stripe.com/api/setup_intents/retrieve.md)することもできます。 ```curl curl -G https://api.stripe.com/v1/setup_intents/{{SETUP_INTENT_ID}} \ -u "<>:" \ -d "expand[]"=mandate ``` #### React ユーザーがフォームを送信したら、[stripe.confirmAuBecsDebitSetup](https://docs.stripe.com/js/setup_intents/confirm_au_becs_debit_setup) を使用して、同意書の収集を完了します。BECS ダイレクトデビットの `PaymentMethod` を作成するには、`payment_method` パラメーターの `billing_details` プロパティに顧客のメールアドレスと口座名義人を含める必要があります。 支払いフォームコンポーネントから `stripe.confirmAuBecsDebitSetup` を呼び出すには、[useStripe](https://docs.stripe.com/sdks/stripejs-react.md#usestripe-hook) フックと [useElements](https://docs.stripe.com/sdks/stripejs-react.md#useelements-hook) フックを使用します。フックではなく従来のクラスコンポーネントを使用する場合は、代わりに [ElementsConsumer](https://docs.stripe.com/sdks/stripejs-react.md#elements-consumer) を使用します。 #### フック ```jsx import React from 'react'; import {useStripe, useElements, AuBankAccountElement} from '@stripe/react-stripe-js'; import BecsForm from './BecsForm'; export default function PaymentSetupForm() { const stripe = useStripe(); const elements = useElements(); const handleSubmit = async (event) => { // We don't want to let default form submission happen here, // which would refresh the page. event.preventDefault(); if (!stripe || !elements) { // Stripe.js hasn't yet loaded. // Make sure to disable form submission until Stripe.js has loaded. return; } const auBankAccount = elements.getElement(AuBankAccountElement); // For brevity, this example is using uncontrolled components for // the accountholder's name and email. In a real world app you will // probably want to use controlled components. // https://reactjs.org/docs/uncontrolled-components.html // https://reactjs.org/docs/forms.html#controlled-components const accountholderName = event.target['accountholder-name']; const email = event.target.email; const result = await stripe.confirmAuBecsDebitSetup('{{CLIENT_SECRET}}', { payment_method: { au_becs_debit: auBankAccount, billing_details: { name: accountholderName.value, email: email.value, }, } }); if (result.error) { // Show error to your customer. console.log(result.error.message); } else { // Show a confirmation message to your customer. // The SetupIntent is in the 'succeeded' state. } }; return ( ); } ``` #### クラスコンポーネント ```jsx import React from 'react'; import {ElementsConsumer, AuBankAccountElement} from '@stripe/react-stripe-js'; import BecsForm from './BecsForm'; class PaymentSetupForm extends React.Component { handleSubmit = async (event) => { // We don't want to let default form submission happen here, // which would refresh the page. event.preventDefault(); const {stripe, elements} = this.props; if (!stripe || !elements) { // Stripe.js hasn't yet loaded. // Make sure to disable form submission until Stripe.js has loaded. return; } const auBankAccount = elements.getElement(AuBankAccountElement); // For brevity, this example is using uncontrolled components for // the accountholder's name and email. In a real world app you will // probably want to use controlled components. // https://reactjs.org/docs/uncontrolled-components.html // https://reactjs.org/docs/forms.html#controlled-components const accountholderName = event.target['accountholder-name']; const email = event.target.email; const result = await stripe.confirmAuBecsDebitSetup('{{CLIENT_SECRET}}', { payment_method: { au_becs_debit: auBankAccount, billing_details: { name: accountholderName.value, email: email.value, }, } }); if (result.error) { // Show error to your customer. console.log(result.error.message); } else { // Show a confirmation message to your customer. // The SetupIntent is in the 'succeeded' state. } }; render() { const {stripe} = this.props; return ( ); } } export default function InjectedPaymentSetupForm() { return ( {({stripe, elements}) => ( )} ); } ``` `SetupIntent` が正常に確定した後、[Mandate (同意書) オブジェクト](https://docs.stripe.com/api/mandates.md)から取得した[同意書の URL](https://docs.stripe.com/api/mandates/object.md#mandate_object-payment_method_details-au_becs_debit-url) を顧客に提示する必要があります。また顧客に同意書が確定したことを伝える際には、下記の詳細情報を含めることを推奨します。 - ダイレクトデビットの準備が整ったことを示す明確な確認メッセージ - 口座から引き落とされるたびに顧客の銀行明細書に表示される[ビジネス名](https://docs.stripe.com/billing/subscriptions/au-becs-debit.md#statement-descriptors) - 支払い金額とスケジュール (該当する場合) - 生成された DDR 同意書 URL へのリンク `Mandate` オブジェクトの ID は、SetupIntent オブジェクトの `mandate` から取得できます。この ID は、確定後に送信される `setup_intent.succeeded` イベントの一部として送信され、[API から取得](https://docs.stripe.com/api/setup_intents/retrieve.md)することもできます。 ```curl curl -G https://api.stripe.com/v1/setup_intents/{{SETUP_INTENT_ID}} \ -u "<>:" \ -d "expand[]"=mandate ``` ## PaymentMethod 付きで顧客を作成 [サーバ側] *サブスクリプション* (A Subscription represents the product details associated with the plan that your customer subscribes to. Allows you to charge the customer on a recurring basis)を作成するには、商品を購入する顧客を表す [Customer (顧客)](https://docs.stripe.com/api.md#customer_object) オブジェクトが必要です。作成した料金は月次ベースで請求されるため、以降の支払いを完了させるには、格納された支払い方法を顧客に追加する必要があります。これを実行するには、先ほど収集した支払い方法を *Customer* (Customer objects represent customers of your business. They let you reuse payment methods and give you the ability to track multiple payments) オブジェクトの最上位レベルに設定し、*請求書* (Invoices are statements of amounts owed by a customer. They track the status of payments from draft through paid or otherwise finalized. Subscriptions automatically generate invoices, or you can manually create a one-off invoice)の[デフォルトの支払い方法](https://docs.stripe.com/api/customers/create.md#create_customer-invoice_settings-default_payment_method)として設定します。 ```curl curl https://api.stripe.com/v1/customers \ -u "<>:" \ --data-urlencode email="jenny.rosen@example.com" \ -d payment_method=pm_1FU2bgBF6ERF9jhEQvwnA7sX \ -d "invoice_settings[default_payment_method]"=pm_1FU2bgBF6ERF9jhEQvwnA7sX ``` これにより `Customer` オブジェクトが返されます。デフォルトの支払い方法は、`invoice_settings` オブジェクトで確認できます。 ```json { "id": "cus_Gk0uVzT2M4xOKD", "object": "customer", "address": null, "balance": 0, "created": 1581797088, "currency": null, "default_source": null, "delinquent": false, "description": null, "discount": null, "email": "jenny.rosen@example.com", "invoice_prefix": "11D0B3D7", "invoice_settings": { "custom_fields": null, "default_payment_method": "pm_1FU2bgBF6ERF9jhEQvwnA7sX", "footer": null }, "livemode": false, "metadata": { }, "name": null, "phone": null, "preferred_locales": [ ], "shipping": null, "sources": { "object": "list", "data": [ ], "has_more": false, "total_count": 0, "url": "/v1/customers/cus_Gk0uVzT2M4xOKD/sources" }, "subscriptions": { "object": "list", "data": [ ], "has_more": false, "total_count": 0, "url": "/v1/customers/cus_Gk0uVzT2M4xOKD/subscriptions" }, "tax_exempt": "none", "tax_ids": { "object": "list", "data": [ ], "has_more": false, "total_count": 0, "url": "/v1/customers/cus_Gk0uVzT2M4xOKD/tax_ids" } } ``` 顧客を作成したら、後で使用できるようにお客様自身のデータベースに `id` 値を保存しておきます。次のステップでもこの ID が必要になります。 ## サブスクリプションを作成する [サーバ側] 料金と顧客を指定して、[Subscription (サブスクリプション)](https://docs.stripe.com/api/subscriptions.md) を作成します。 ```curl curl https://api.stripe.com/v1/subscriptions \ -u "<>:" \ -d customer=cus_Gk0uVzT2M4xOKD \ -d "items[0][price]"=price_F52b2UdntfQsfR ``` [デフォルトの支払い方法](https://docs.stripe.com/api/customers/create.md#create_customer-invoice_settings-default_payment_method)が設定されているため、サブスクリプションを作成すると、自動的に顧客に対して請求が行われます。支払いが完了すると、[Stripe ダッシュボード](https://dashboard.stripe.com/test/subscriptions)内のステータスが**有効**に変わります。以前作成した料金によって、以降の請求が決定されます。 ## サブスクリプションステータスを管理する [クライアント側] 初回の支払いが完了すると、*サブスクリプション* (A Subscription represents the product details associated with the plan that your customer subscribes to. Allows you to charge the customer on a recurring basis) のステータスは `active` になり、それ以上のアクションは不要になります。支払いが失敗した場合は、ステータスが [自動回収設定](https://docs.stripe.com/invoicing/automatic-collection.md) で設定された **Subscription status** に変わります。支払いが失敗した場合は顧客にその旨を通知して、[別の支払い方法で請求してください](https://docs.stripe.com/billing/subscriptions/overview.md#requires-payment-method)。 > BECS ダイレクトデビット支払いは、他の支払い方法に[再試行スケジュール](https://docs.stripe.com/invoicing/automatic-collection.md)が設定されている場合でも、自動的に再試行することはありません。 ## 組み込みをテストする テスト用の BSB 番号 `000000` と以下のいずれかのテストアカウント番号を [confirmAuBecsDebitSetup](https://docs.stripe.com/js/setup_intents/confirm_au_becs_debit_setup) リクエストで使用することで、フォームをテストできます。 | BSB 番号 | 口座番号 | 説明 | | -------- | ----------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `000000` | `000123456` | 生成された PaymentMethod で作成された PaymentIntent は、`processing` から `succeeded` に変わります。同意書のステータスは引き続き `active` です。 | | `000000` | `900123456` | 生成された PaymentMethod で作成された PaymentIntent は、`processing` から `succeeded` に変わります (3 分の遅延あり)。同意書のステータスは引き続き `active` です。 | | `000000` | `111111113` | 生成された PaymentMethod で作成された PaymentIntent は、`processing` から `requires_payment_method` に変わり、`account_closed` エラーコードが返されます。同意書のステータスはその時点で `inactive` になります。 | | `000000` | `111111116` | 生成された PaymentMethod で作成された PaymentIntent は、`processing` から `requires_payment_method` に変わり、`no_account` エラーコードが返されます。同意書のステータスはその時点で `inactive` になります。 | | `000000` | `222222227` | 生成された PaymentMethod で作成された PaymentIntent は、`processing` から `requires_payment_method` に変わり、`refer_to_customer` エラーコードが返されます。同意書のステータスは引き続き `active` です。 | | `000000` | `922222227` | 生成された PaymentMethod で作成された PaymentIntent は、`processing` から `requires_payment_method` に変わり、`refer_to_customer` エラーコードが返されます (3 分の遅延あり)。同意書のステータスは引き続き `active` です。 | | `000000` | `333333335` | 生成された PaymentMethod で作成された PaymentIntent は、`processing` から `requires_payment_method` に変わり、`debit_not_authorized` エラーコードが返されます。同意書のステータスはその時点で `inactive` になります。 | | `000000` | `666666660` | 生成された PaymentMethod で作成された PaymentIntent は、`processing` から `succeeded` に変わりますが、不審請求の申請が直ちに作成されます。 | | `000000` | `343434343` | 結果として生じる PaymentMethod により作成された PaymentIntent は、支払い金額が原因でアカウントの週次支払い金額の上限を超えてしまうため、`charge_exceeds_source_limit` エラーで失敗します。 | | `000000` | `121212121` | 結果として生じる PaymentMethod により作成された PaymentIntent は、支払い金額がアカウントの取引金額の上限を超えているため、`charge_exceeds_transaction_limit` エラーで失敗します。 | ## Optional: 請求期間の設定 デフォルトでは、サブスクリプションの作成時にその請求サイクルが自動的に設定されます。たとえば、顧客が 9 月 7 日に月額プランのサブスクリプションに登録した場合、それ以降毎月 7 日に請求されます。一部のビジネスでは、顧客に対してサイクルごとに同時に請求できるように、請求サイクルの手動設定を希望する場合があります。[billing cycle anchor](https://docs.stripe.com/api/subscriptions/create.md#create_subscription-billing_cycle_anchor) 引数を使用すると、これを実現できます。 ```curl curl https://api.stripe.com/v1/subscriptions \ -u "<>:" \ -d customer="{{CUSTOMER_ID}}" \ -d "items[0][price]"="{{PRICE_ID}}" \ -d billing_cycle_anchor=1611008505 ``` 請求サイクルを手動で設定すると、作成されたサブスクリプションと請求サイクルのアンカーとの間の期間の分として比例配分 (日割り / 秒割り計算) された金額が顧客に自動的に請求されます。この期間分を顧客に請求しない場合は、[proration_behavior](https://docs.stripe.com/api/subscriptions/create.md#create_subscription-proration_behavior) 引数を `none` に設定できます。請求サイクルのアンカーを[トライアル期間](https://docs.stripe.com/billing/subscriptions/au-becs-debit.md#trial-periods)と組み合わせ、商品への無料アクセスをユーザーに提供してから、比例配分された金額を請求することもできます。 ## Optional: サブスクリプションのトライアル 無料トライアルを使用すると、顧客は商品に一定期間無料でアクセスできます。無料トライアルの使用は、[proration_behavior](https://docs.stripe.com/api/subscriptions/create.md#create_subscription-proration_behavior) を `none` に設定した場合とは異なり、無料の継続期間をカスタマイズできます。トライアル期間を設定するには、[trial end](https://docs.stripe.com/api/subscriptions/create.md#create_subscription-trial_end) にタイムスタンプを渡します。 ```curl curl https://api.stripe.com/v1/subscriptions \ -u "<>:" \ -d customer="{{CUSTOMER_ID}}" \ -d "items[0][price]"="{{PRICE_ID}}" \ -d trial_end=1610403705 ``` 必要に応じて、[請求サイクルのアンカー](https://docs.stripe.com/billing/subscriptions/au-becs-debit.md#billing-cycle)を無料トライアルと組み合わせることができます。たとえば、現在が 9 月 15 日だとした場合、顧客に 7 日間の無料トライアルを付与してから、通常の請求サイクルを 10 月 1 日に開始するとします。この場合 9 月 22 日に無料トライアルが終了し、請求サイクルのアンカーが 10 月 1 日となるように設定できます。これにより顧客には 7 日間の無料トライアルが付与され、トライアル終了日から 10 月 1 日までの期間については比例配分を適用した金額が請求されます。10 月 1 日に、初めての完全な請求サイクルとして、顧客は通常のサブスクリプション金額を請求されます。