使用 Stripe Elements 和 Checkout Sessions 在您的网站上构建一个结账页面,这是一种可管理税费、折扣、运费等内容的集成方式。
仅当您的任一钱包绑定了有效的卡时,演示中才会显示 Google Pay 或 Apple Pay。
在开始之前,您需要注册 一个 Stripe 账户。
用官方 Stripe 库从您的应用程序访问 API。
npm install stripe@18.0.0 --save
将 SDK 设置为至少使用 2025-03-31. basil
API 版本。
import Stripe from 'stripe' ;
const stripe = new Stripe ( 'sk_test_BQokikJOvBiI2HlWgH4olfQ2'
, {
apiVersion : '2025-03-31.basil' as any ,
} ) ;
在服务器上添加一个终结点,用于创建 Checkout Session 并将其客户端私钥 返回到前端。Checkout Session 表示您的客户在进行一次性付款或订阅时的会话。Checkout Sessions 创建后 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 : 'usd' ,
product_data : {
name : 'T-shirt' ,
} ,
unit_amount : 2000 ,
} ,
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 合规。请勿将脚本包含在捆绑包中,也不要自己托管其副本。
您需要通过包含以下脚本标签 <script src="https://js. stripe. com/basil/stripe. js"></script>
将 Stripe.js 从 v3 更新为 basil
。了解有关 Stripe.js 版本控制 的更多信息。
初始化 stripe.js。
const stripe = Stripe (
'pk_test_TYooMQauvdEDq54NiTphI7jx'
,
) ;
创建一个 fetchClientSecret
函数。此函数从服务器检索客户端私钥,并返回一个使用客户端私钥解析的 promise。通过传入 fetchClientSecret
来调用 initCheckout 。initCheckout
返回解析为 checkout 实例的 promise。
checkout 对象充当结账页面的基础,包含来自 Checkout Session 的数据以及更新 Session 的方法。
checkout.session() 返回的对象包含您的价格信息。建议在您的 UI 中读取并显示会话中的 total
和 lineItems
。
这样,您只需最少量的代码修改即可开启新功能。例如,添加手动货币价格 ,如果显示 total
,则不需要更改 UI。
const fetchClientSecret = ( ) => {
return fetch ( '/create-checkout-session' , { method : 'POST' } )
. then ( ( response ) => response . json ( ) )
. then ( ( json ) => json . checkoutSessionClientSecret ) ;
} ;
stripe . initCheckout ( { fetchClientSecret } )
. then ( ( checkout ) => {
const checkoutContainer = document . getElementById ( 'checkout-container' ) ;
checkoutContainer . append ( JSON . stringify ( checkout . lineItems , null , 2 ) ) ;
checkoutContainer . append ( document . createElement ( 'br' ) ) ;
checkoutContainer . append ( ` Total: ${ checkout . session ( ) . total . total . amount } ` ) ;
} ) ;
< div id = "checkout-container" > </ div >
创建电子邮件输入以收集客户的电子邮件地址。当客户完成输入来验证并保存电子邮件地址时,调用 updateEmail 。
根据您的结账表单的设计,可通过以下方式调用 updateEmail
:
在提交付款 之前。也可以调用 updateEmail
来提前验证,例如模糊输入。 在转到下一步之前,例如点击保存 按钮(如果您的表单包含多个步骤)。 < input type = "text" id = "email" />
< div id = "email-errors" > </ div >
stripe . initCheckout ( { fetchClientSecret } ) . then ( ( checkout ) => {
const emailInput = document . getElementById ( 'email' ) ;
const emailErrors = document . getElementById ( 'email-errors' ) ;
emailInput . addEventListener ( 'input' , ( ) => {
emailErrors . textContent = '' ;
} ) ;
emailInput . addEventListener ( 'blur' , ( ) => {
const newEmail = emailInput . value ;
checkout . updateEmail ( newEmail ) . then ( ( result ) => {
if ( result . error ) {
emailErrors . textContent = result . error . message ;
}
} ) ;
} ) ;
} ) ;
用 Payment Element 在客户端收集支付信息。Payment Element 是一个预构建的 UI 组件,它简化了多种支付方式的收集支付详情的流程。
呈现一个“支付”按钮,该按钮从 checkout 实例调用 confirm 以提交付款。
< button id = "pay-button" > Pay </ button >
< div id = "confirm-errors" > </ div >
stripe . initCheckout ( { fetchClientSecret } ) . then ( ( checkout ) => {
const button = document . getElementById ( 'pay-button' ) ;
const errors = document . getElementById ( 'confirm-errors' ) ;
button . addEventListener ( 'click' , ( ) => {
errors . textContent = '' ;
checkout . confirm ( ) . then ( ( result ) => {
if ( result . type === 'error' ) {
errors . textContent = result . error . message ;
}
} ) ;
} ) ;
} ) ;
导航到您的结账页面。 使用下表中的支付方式填写付款详情。对于银行卡付款:输入一个任意的未来日期作为有效期。 输入 3 位数 CVC 码。 输入账单地址邮编。 将付款提交至 Stripe。 前往管理平台,在交易页面 上查找支付。如果您支付成功,就会在列表中看到它。 点击您的付款,查看更多详情,例如账单信息及已购商品列表。您可以此信息来履行订单 。 卡号 场景 如何测试 4242 4242 4242 4242 该卡付款成功,不需要验证。 使用信用卡号以及有效期和 CVC 和邮编填写我们的信用卡表单。 4000 0025 0000 3155 该卡付款时需要验证 。 使用信用卡号以及有效期和 CVC 和邮编填写我们的信用卡表单。 4000 0000 0000 9995 该卡被拒绝,显示拒付代码,例如 insufficient_ funds
。 使用信用卡号以及有效期和 CVC 和邮编填写我们的信用卡表单。 6205 5000 0000 0000 004 银联卡的长度为 13-19 位。 使用信用卡号以及有效期和 CVC 和邮编填写我们的信用卡表单。
有关测试您的集成的更多信息,请参阅测试 部分。
另见