Stripe

Stripe 作为海外最广泛使用的在线支付处理平台,方便的接入集成,基本是开发者接入海外支付的首

官网地址:https://stripe.com/

目前接入Stripe需要海外企业认证,需要做海外应用的可以选择。

stripe1

配置变量
json
1NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=stripe提供的key
2STRIPE_WEBHOOK_SECRET_CLI=测试stripe的cli key (可选)
3STRIPE_WEBHOOK_SECRET=使用webhook作为回调的secret

项目中使用/api/webhook来作为Stripe的支付回调。

SDK 创建支付页面
tsx
1import { config } from '@/config';
2
3export async function createCheckoutSession(key: string, id: string) {
4 if (!id) {
5 window.location.href = '/auth/signin';
6 return;
7 }
8
9 const stripe = require('stripe')(process.env.NEXT_PUBLIC_STRIPE_SECRET_KEY);
10
11 const good = config.goods.find((item) => item.key === key);
12
13 const session = await stripe.checkout.sessions.create({
14 payment_method_types: ['card'], // 支持的支付方式
15 line_items: [
16 {
17 price_data: {
18 currency: config.paySize,
19 product_data: {
20 name: good.name, // 商品名称
21 },
22 unit_amount: good.price * 100, // 商品价格(单位是分,如 $20.00 = 2000)
23 },
24 quantity: 1, // 商品数量
25 },
26 ],
27 mode: 'payment', // 支付模式
28 success_url: `${window.location.origin}/dashboard/home`,
29 cancel_url: `${window.location.origin}/`,
30 metadata: {
31 userId: id, // 自定义用户 ID 参数
32 goodType: good.key,
33 goodPrice: good.price,
34 goodName: good.name,
35 custom_info: 'any_custom_data', // 其他自定义信息
36 },
37 });
38
39 return session.url;
40}
优点
  • Stripe 提供很快捷的接入方式,使用 SDK 即可快速创建支付页面
  • 支持丰富的支付方式,包含全球 135+货币