Payment Element を使用して、ウェブサイトまたはアプリケーションにカスタムの Stripe 決済フォームを埋め込み、顧客に決済手段を提供します。高度な設定とカスタマイズについては、決済の受け付け に関する導入ガイドをご覧ください。
対応可能なビジネスの所在地 : Europe
対応可能な通貨 : eur
取引通貨 : eur
支払いモード : Yes
セットアップモード : No
サブスクリプションモード : No
Billie 支払いをサポートするには、Checkout セッションが次の条件をすべて満たしている必要があります。
ラインアイテムの価格 はすべて、同じ通貨である必要があります。 異なる通貨のラインアイテムが存在する場合は、通貨ごとに別の Checkout セッションを作成してください。 アプリケーションから API にアクセスするには、公式の Stripe ライブラリを使用します。
Checkout セッション を作成してその Client Secret をフロントエンドに返すエンドポイントを、サーバーに追加します。Checkout セッションは、顧客が 1 回限りの購入またはサブスクリプションの支払いを行う際のセッションを表します。Checkout セッションは作成後 24 時間で期限切れとなります。
動的支払い方法 を使用して、各顧客に最も関連性の高い支払い方法を動的に表示し、コンバージョンを最大化することをお勧めします。支払い方法を手動でリスト して、動的支払い方法を無効にすることもできます。
import express , { Express } from 'express' ;
const app : Express = express ( ) ;
app . post ( '/create-checkout-session' , async ( req : Express . Request , res : Express . Response ) => {
const session = await stripe . checkout . sessions . create ( {
line_items : [
{
price_data : {
currency : 'eur' ,
product_data : {
name : 'T-shirt' ,
} ,
unit_amount : 1000 ,
} ,
quantity : 1 ,
} ,
] ,
mode : 'payment' ,
ui_mode : 'custom' ,
return_url : 'https://example.com/return?session_id={CHECKOUT_SESSION_ID} '
} ) ;
res . json ( { checkoutSessionClientSecret : session . client_secret } ) ;
} ) ;
app . listen ( 3000 , ( ) => {
console . log ( 'Running on port 3000' ) ;
} ) ; Stripe.js スクリプトをチェックアウトページに含めるには、このスクリプトを HTML ファイルの head に追加します。常に js.stripe.com から Stripe.js を直接読み込むことにより、PCI への準拠が維持されます。スクリプトをバンドルに含めたり、そのコピーを自身でホストしたりしないでください。
最新の Stripe.js バージョンであるか確認し、以下のスクリプトタグ <script src=“https://js. stripe. com/clover/stripe. js”></script> を読み込んでください。Stripe.js バージョン管理 について詳しくはリンク先をご覧ください。
メモ Stripe は、Stripe.js をモジュールとして読み込むために使用できる npm パッケージを提供しています。GitHub のプロジェクト をご覧ください。バージョン 7.0.0 以降が必要です。
Stripe.js を初期化する
const stripe = Stripe (
'pk_test_TYooMQauvdEDq54NiTphI7jx'
,
) ; Client Secret を解決する clientSecret プロミスを作成するか、機密事項として直接設定します。initCheckout を呼び出し、clientSecret を渡します。initCheckout は、Checkout インスタンスに解決するプロミスを返します。
Checkout オブジェクトは、決済画面の基盤として機能し、決済セッションのデータやセッションを更新するためのメソッドを含みます。
actions.getSession() によって返されるオブジェクトには、価格情報が含まれます。セッションの total と lineItems を UI で読み込んで表示することをお勧めします。
これにより、最小限のコード変更で新機能を有効にできます。たとえば、手動通貨価格 を追加しても、total を表示する場合、UI を変更する必要はありません。
const clientSecret = fetch ( '/create-checkout-session' , { method : 'POST' } )
. then ( ( response ) => response . json ( ) )
. then ( ( json ) => json . checkoutSessionClientSecret ) ;
const checkout = stripe . initCheckout ( { clientSecret } ) ;
const loadActionsResult = await checkout . loadActions ( ) ;
if ( loadActionsResult . type === 'success' ) {
const session = loadActionsResult . actions . getSession ( ) ;
const checkoutContainer = document . getElementById ( 'checkout-container' ) ;
checkoutContainer . append ( JSON . stringify ( session . lineItems , null , 2 ) ) ;
checkoutContainer . append ( document . createElement ( 'br' ) ) ;
checkoutContainer . append ( ` Total: ${ session . total . total . amount } ` ) ;
}
< div id = "checkout-container" > </ div >
顧客のメールアドレスを収集するためのメールアドレス入力を作成します。顧客が入力を完了してメールアドレスを検証し、保存したら、updateEmail を呼び出します。
決済フォームのデザインに応じて、次の方法で updateEmail を呼び出すことができます。
支払いの送信 の直前。また、updateEmail を呼び出して、早い段階 (ぼかしの入力時など) で検証することもできます。次のステップに進む前 (フォームが複数のステップからなる場合の保存 ボタンのクリック時など)。 < input type = "text" id = "email" />
< div id = "email-errors" > </ div >
const checkout = stripe . initCheckout ( { clientSecret } ) ;
const loadActionsResult = await checkout . loadActions ( ) ;
if ( loadActionsResult . type === 'success' ) {
const { actions } = loadActionsResult ;
const emailInput = document . getElementById ( 'email' ) ;
const emailErrors = document . getElementById ( 'email-errors' ) ;
emailInput . addEventListener ( 'input' , ( ) => {
emailErrors . textContent = '' ;
} ) ;
emailInput . addEventListener ( 'blur' , ( ) => {
const newEmail = emailInput . value ;
actions . updateEmail ( newEmail ) . then ( ( result ) => {
if ( result . error ) {
emailErrors . textContent = result . error . message ;
}
} ) ;
} ) ;
}
Payment Element を使用してクライアント側で支払い詳細を収集します。Payment Element は事前構築された UI コンポーネントであり、さまざまな決済手段の詳細の収集をシンプルにします。
Checkout インスタンスから 確定 を呼び出す支払う ボタンをレンダリングして、決済を送信します。
< button id = "pay-button" > Pay </ button >
< div id = "confirm-errors" > </ div >
const checkout = stripe . initCheckout ( { clientSecret } ) ;
const loadActionsResult = await checkout . loadActions ( ) ;
if ( loadActionsResult . type === 'success' ) {
const { actions } = loadActionsResult ;
const button = document . getElementById ( 'pay-button' ) ;
const errors = document . getElementById ( 'confirm-errors' ) ;
button . addEventListener ( 'click' , ( ) => {
errors . textContent = '' ;
actions . confirm ( ) . then ( ( result ) => {
if ( result . type === 'error' ) {
errors . textContent = result . error . message ;
}
} ) ;
} ) ;
}
実装内容をテストするには、決済手段を選択して支払う をタップします。サンドボックス では、テスト決済ページにリダイレクトされ、そこで決済を承認または拒否できます。
本番環境では、支払う をタップすると、Billie のウェブサイトにリダイレクトされます。Billie で支払いを承認または拒否するオプションはありません。