Woocommerce+Stripe集成佣金拆分问题求助:如何在Stripe留存佣金
解决方案:在Stripe中拆分房费与15%佣金并显示为Collected Fee
针对你的场景,核心是通过**Stripe Connect的应用费用(Application Fee)**机制,在向酒店转账时自动扣除佣金并将其标记为Stripe的Collected Fee,同时结合WooCommerce的订单元数据管理房费与佣金的拆分。
步骤1:在WooCommerce订单中存储酒店应得金额
首先需要在订单结算时,将纯房费(不含佣金)存储为订单元数据,后续转账时以此金额为准打给酒店。
// 结算后保存酒店应得金额到订单元数据 add_action('woocommerce_checkout_update_order_meta', 'store_hotel_payout_amount', 10, 2); function store_hotel_payout_amount($order_id, $checkout_data) { $order = wc_get_order($order_id); // 根据实际逻辑提取纯房费(示例:匹配订单中的房费项目) $room_total = 0; foreach ($order->get_items() as $item) { if (strpos($item->get_name(), '房费') !== false) { $room_total += $item->get_total(); } } // 保存到订单元数据 update_post_meta($order_id, '_hotel_net_payout', $room_total); }
步骤2:触发转账时通过Stripe API扣除佣金
当用户点击打款按钮时,调用Stripe Transfer API,指定酒店的Connect账户作为收款方,同时设置application_fee_amount参数(即你的15%佣金),这笔费用会自动显示在Stripe后台的Collected Fee中。
// 处理向酒店的Stripe转账并扣除佣金 add_action('hivepress_vendor_payout_processed', 'execute_stripe_payout_with_commission', 10, 2); function execute_stripe_payout_with_commission($order_id, $vendor) { $order = wc_get_order($order_id); $hotel_net_payout = get_post_meta($order_id, '_hotel_net_payout', true); $commission = $order->get_total() - $hotel_net_payout; // 初始化Stripe客户端 \Stripe\Stripe::setApiKey('你的Stripe私密密钥'); try { // 创建转账,自动扣除佣金作为Collected Fee \Stripe\Transfer::create([ 'amount' => $hotel_net_payout * 100, // Stripe API使用分作为单位 'currency' => 'usd', 'destination' => $vendor->get_meta('stripe_account_id'), // 从Hivepress供应商数据中获取酒店的Stripe Connect账户ID 'application_fee_amount' => $commission * 100, // 佣金金额,会显示在Stripe的Collected Fee中 ]); // 标记订单转账状态(可选) $order->add_order_note('已向酒店转账 ' . wc_price($hotel_net_payout) . ',扣除佣金 ' . wc_price($commission)); $order->save(); } catch (\Stripe\Exception\ApiErrorException $e) { // 记录错误日志 error_log('Stripe转账失败:' . $e->getMessage()); $order->add_order_note('转账失败:' . $e->getMessage()); $order->save(); } }
关键注意事项
- Stripe Connect配置:确保你的Stripe账户已启用Connect,且所有酒店都已关联为Connect子账户(Hivepress通常支持存储供应商的Stripe账户ID)。
- 金额单位:Stripe API使用分作为金额单位,所以需要将美元金额乘以100。
- 房费提取逻辑:根据你实际的订单结构调整
store_hotel_payout_amount函数中提取纯房费的代码(比如Hivepress可能有专门的预订元数据字段存储房费)。 - 测试:先在Stripe测试环境中验证转账流程,确认佣金正确计入Collected Fee,且酒店收到的金额为纯房费。
内容的提问来源于stack exchange,提问作者Cerealkey
相关产品推荐
相关产品推荐

