You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Stripe向卖家打款扣5%佣金:生产环境未生效问题排查

问题分析与解决方案

问题背景

开发电商应用,买家通过Stripe完成支付后,订单状态变为已完成时需向卖家打款,并从订单金额中扣除5%佣金。测试环境中佣金扣除逻辑正常,但生产环境下未执行佣金扣除,直接向卖家全额打款。

代码回顾

收款逻辑:

$currency = 'cad';
//$customerId = $request->customerId;
$intent = \Stripe\PaymentIntent::create([
    'amount' => ($finalAmount *100),
    'currency' => $currency,
    'customer' => $customerId,
    'description' => $description
]);

佣金扣除与打款逻辑:

if($commission->commission_type == 'percentage'){
   $amount = $totalAmount - (($commission->commission_amount/100)*$totalAmount);
}
$finalAmount = (round( $amount,2));
//Paying to the seller
$transfer = \Stripe\Transfer::create([
   "amount" => ($finalAmount*100),
    "currency" => "cad",
    "destination" => $seller_account,
]);

排查与解决步骤

  1. 验证生产环境佣金配置数据
    生产环境中$commission对象的取值可能不符合预期:

    • 检查commission_type是否确实为'percentage'
    • 检查commission_amount是否设置为5(对应5%佣金)
      可以临时添加日志输出$commission的完整数据,排查生产环境的配置问题:
    // 生产环境临时添加日志,部署后记得移除
    error_log('Commission config: ' . print_r($commission, true));
    
  2. 修复变量未初始化的问题
    当前代码中,若$commission->commission_type不等于'percentage',$amount变量未被赋值,此时$finalAmount = round($amount,2)会使用未定义的$amount(可能在生产环境中意外继承了$totalAmount的数值)。修改代码,给$amount设置默认值并覆盖所有佣金类型场景:

    // 先将$amount初始化为订单全额
    $amount = $totalAmount;
    
    // 处理百分比佣金
    if($commission->commission_type == 'percentage'){
        $amount = $totalAmount - (($commission->commission_amount / 100) * $totalAmount);
    }
    // 可选:处理固定金额佣金(如果有这个场景)
    elseif($commission->commission_type == 'fixed'){
        $amount = $totalAmount - $commission->commission_amount;
    }
    
    $finalAmount = round($amount, 2);
    
  3. 确认生产环境代码与测试环境一致
    排查生产环境部署的代码是否是最新版本,避免因代码未同步导致佣金逻辑未生效。

  4. 验证金额计算的精度问题
    检查生产环境中$totalAmount的数值是否正确(比如是否存在小数点位数异常),确保佣金计算时的除法和乘法运算结果符合预期。

内容的提问来源于stack exchange,提问作者Nasir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 09:07:56