如何在Stripe结账会话中向客户收取手续费
如何将Stripe手续费转嫁给客户
核心逻辑
Stripe的手续费(以GBP为例,常规费率为1.4% + 20便士)需要附加到客户支付的总金额中。为了确保扣除手续费后你能拿到预期的原始金额,需要反向计算包含手续费的总金额,而不是直接把手续费加到原始金额上。
具体实现步骤
明确Stripe费率规则
根据你所在地区的费率,定义百分比手续费和固定手续费(注意统一使用货币最小单位,比如GBP用便士)。计算含手续费的总金额
使用反向公式计算总金额,确保最终到账金额等于你的原始目标金额:总金额(便士)= (原始金额×100) / (1 - 手续费百分比) + 固定手续费修改代码实现
调整Price创建时的unit_amount计算逻辑,替换为包含手续费的总金额,同时在metadata中记录原始金额和手续费,方便后续对账。
修改后的完整代码
// 定义Stripe GBP费率:1.4% + 20便士(根据实际费率调整) const stripeFeePercentage = 0.014; const stripeFixedFee = 20; // 单位:便士 // 原始目标金额(单位:GBP) const originalAmount = createCheckOutSessionDto?.amount; // 计算包含手续费的总金额(转成便士) const totalAmountInPence = Math.round( (originalAmount * 100) / (1 - stripeFeePercentage) + stripeFixedFee ); const price = await this.stripeClient.prices.create({ unit_amount: totalAmountInPence, currency: 'gbp', product: 'prod_OgNbjDxP7OVnrl', }); const response = await this.stripeClient.checkout.sessions.create({ customer: stripeId || customer, payment_method_types: ['paypal', 'card'], line_items: [ { price: price.id, quantity: 1, }, ], metadata: { paymentId: createPaymentDetail?.id, customerId: stripeId || customer, userId: authUser?.userId, bidId: serviceRequestDto?.serviceRequestId, originalAmount: originalAmount.toString(), // 记录原始目标金额 stripeFees: (totalAmountInPence - originalAmount * 100).toString(), // 记录收取的手续费 }, mode: 'payment', success_url: `${environment?.successUrl}?serviceRequestId=${serviceRequestDto?.serviceRequestId}`, cancel_url: environment?.cancelUrl, });
关键注意事项
- 透明告知客户:必须在结账页面明确拆分显示原始金额和附加的Stripe手续费,避免客户纠纷。可以在前端展示明细,或者修改Price的名称/描述说明费用构成。
- 费率动态配置:建议将Stripe费率存入环境变量或数据库,方便后续费率调整时快速更新。
- 多货币适配:如果支持其他货币,需对应调整费率规则(比如USD常规费率为2.9% + 30美分)。
内容的提问来源于stack exchange,提问作者RAHUL VIRAT
相关产品推荐
相关产品推荐

