LemonSqueezy 结账接口
后端创建自定义 LemonSqueezy 结账时传入 Talivia 会话。
后端调用 POST /v1/checkouts 并把生成的结账网址返回浏览器时,请使用此流程。
将会话发送给后端
在已追踪页面上读取当前会话,并放入自己的结账请求:
const sessionId = window.talivia.getSessionId();
const response = await fetch('/api/create-checkout', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ sessionId }),
});
const { checkoutUrl } = await response.json();
window.location.assign(checkoutUrl);会话编号只能作为归因上下文。客户、商品、价格和权限仍须由后端独立验证。
添加自定义结账数据
将收到的值合并到 LemonSqueezy 请求的 attributes.checkout_data.custom:
const checkout = await fetch('https://api.lemonsqueezy.com/v1/checkouts', {
method: 'POST',
headers: {
Accept: 'application/vnd.api+json',
'Content-Type': 'application/vnd.api+json',
Authorization: `Bearer ${process.env.LEMONSQUEEZY_API_KEY}`,
},
body: JSON.stringify({
data: {
type: 'checkouts',
attributes: {
checkout_data: { custom: { talivia_session_id: sessionId } },
},
relationships: {
store: { data: { type: 'stores', id: process.env.LEMONSQUEEZY_STORE_ID } },
variant: { data: { type: 'variants', id: variantId } },
},
},
}),
}).then(response => response.json());
return Response.json({ checkoutUrl: checkout.data.attributes.url });LemonSqueezy 会把自定义结账数据复制到带签名网络回调的 meta.custom_data。托管自定义结账和通过程序打开的浮层都适用。
请将 LemonSqueezy 接口密钥保存在服务器,并保留应用已经发送的其他自定义字段。