添加支付费率扣除

This commit is contained in:
Dev
2026-10-27 18:10:51 +08:00
parent 20f9113d94
commit f53c142a56
14 changed files with 225 additions and 41 deletions
@@ -91,15 +91,42 @@ class DemandService
}
/**
* 计算扣除平台服务费后开发者实得金额
* @param mixed $amount 订单金额(元)
* 虚拟支付渠道费率。iOS 走苹果,其余终端按安卓费率
* @param string $platform android|ios
* @return string
*/
public static function getDeveloperIncome($amount): string
public static function getPayFeeRate($platform): string
{
$config = config('common.virtual_pay');
if (!is_array($config)) {
$config = [];
}
$isIos = $platform === 'ios';
$raw = $isIos ? ($config['ios_pay_fees'] ?? '0.12') : ($config['android_pay_fees'] ?? '0.01');
if (!is_numeric($raw) || $raw < 0) {
$raw = $isIos ? '0.12' : '0.01';
}
$rate = sprintf('%.4f', (float)$raw);
return bccomp($rate, '1', 4) > 0 ? '1.0000' : $rate;
}
/**
* 开发者实得 = 支付金额 - 平台服务费 - 安卓/iOS 支付费率
* @param mixed $amount 订单金额(元)
* @param string $platform android|ios
* @return string
*/
public static function getDeveloperIncome($amount, $platform = 'android'): string
{
$ratio = self::getPlatformCommissionRatio();
$payFee = self::getPayFeeRate($platform);
$remain = bcsub(bcsub('1', $ratio, 4), $payFee, 4);
if (bccomp($remain, '0', 4) < 0) {
$remain = '0';
}
return bcmul((string)$amount, bcsub('1', $ratio, 4), 2);
return bcmul((string)$amount, $remain, 2);
}
/**
@@ -630,7 +657,8 @@ class DemandService
$userId = $contract->demandBidding->user_id;
//开发者冻结金额增加
$demandOrder = OrderService::getDemandOrder($demand->parent_order_no, OrderService::ORDER_TYPE_FIRST_PAYMENT);
$amount = self::getDeveloperIncome($demandOrder->payment_amount);
$payPlatform = OrderPaymentService::where('order_no', $demandOrder->order_no)->order('id desc')->value('pay_platform');
$amount = self::getDeveloperIncome($demandOrder->payment_amount, $payPlatform ?: 'android');
$title = OrderService::ORDER_TYPE_MAP[$demandOrder->order_type] ?? '需求收益';
$log_res = AccountLogService::addLog(AccountLogService::ACCOUNT_TYPE_FREEZE, $userId, $amount, $title . '-冻结金额增加', AccountLogService::TYPE_FREEZE, 0, AccountLogService::INC_EXP_INCREASE, $demandOrder->id);
AccountService::updateData($userId, 0, $amount, 0, 0, $log_res->id);
@@ -667,7 +695,8 @@ class DemandService
$userId = $demand->winBidding->user_id;
//开发者冻结金额增加
$demandOrder = OrderService::getDemandOrder($demand->parent_order_no, OrderService::ORDER_TYPE_FINAL_PAYMENT);
$amount = self::getDeveloperIncome($demandOrder->payment_amount);
$payPlatform = OrderPaymentService::where('order_no', $demandOrder->order_no)->order('id desc')->value('pay_platform');
$amount = self::getDeveloperIncome($demandOrder->payment_amount, $payPlatform ?: 'android');
$title = OrderService::ORDER_TYPE_MAP[$demandOrder->order_type] ?? '需求收益';
$log_res = AccountLogService::addLog(AccountLogService::ACCOUNT_TYPE_FREEZE, $userId, $amount, $title . '-冻结金额增加', AccountLogService::TYPE_FREEZE, 0, AccountLogService::INC_EXP_INCREASE, $demandOrder->id);
AccountService::updateData($userId, 0, $amount, 0, 0, $log_res->id);
@@ -768,7 +797,15 @@ class DemandService
$demand->status = DemandService::STATUS_FINISHED;
$demand->settle_status = CommonService::SUCCESS;
$demand->save();
$amount = self::getDeveloperIncome($demand->demandContracts->total_amount ?? 0);
$amount = '0';
foreach ([OrderService::ORDER_TYPE_FIRST_PAYMENT, OrderService::ORDER_TYPE_FINAL_PAYMENT] as $orderType) {
$demandOrder = OrderService::getDemandOrder($demand->parent_order_no, $orderType);
if (!$demandOrder) {
continue;
}
$payPlatform = OrderPaymentService::where('order_no', $demandOrder->order_no)->order('id desc')->value('pay_platform');
$amount = bcadd($amount, self::getDeveloperIncome($demandOrder->payment_amount, $payPlatform ?: 'android'), 2);
}
//写入余额日志
$log_res = AccountLogService::addLog(AccountLogService::ACCOUNT_TYPE_FREEZE, -$amount, '需求收益解冻-冻结扣除', AccountLogService::TYPE_UNFREEZE, 0, $demand->id, $demand->order_amount);
$log_res = AccountLogService::addLog(AccountLogService::ACCOUNT_TYPE_AMOUNT, +$amount, '需求收益解冻-余额增加', AccountLogService::TYPE_UNFREEZE, 0, $demand->id, $demand->order_amount);
@@ -148,6 +148,7 @@ class OrderService
'promoter_id' => PromotionService::getPromoterId(intval($userId)),
];
$orderData['parent_order_no'] = $orderData['order_no'];
VirtualPayService::rememberPayPlatform($orderData['order_no'], $requestData['payPlatform'] ?? '');
$existed = OrderService::getOne(['order_no' => $orderData['order_no']]);
if (!$existed) {
//创建订单及订单商品
@@ -290,6 +291,7 @@ class OrderService
throw new \Exception('该订单不存在');
}
$orderData = $orderInfo->toArray();
VirtualPayService::rememberPayPlatform($orderNo, $requestData['payPlatform'] ?? '');
Log::info('微信下单[发起支付] ' . json_encode([
'order_no' => $orderNo,
'user_id' => $userId,
@@ -516,14 +518,22 @@ class OrderService
* @param string $payWay
* @param int $paidTime
* @param string $traceId
* @param string $payPlatform android|ios,空则用下单时记下的终端
* @return bool
* @throws \Exception
*/
public static function finishPaid($orderNo, $amountFen, $transactionId, $payWay, $paidTime, $traceId = '')
public static function finishPaid($orderNo, $amountFen, $transactionId, $payWay, $paidTime, $traceId = '', $payPlatform = '')
{
$payPlatform = VirtualPayService::normalizePayPlatform($payPlatform);
if ($payPlatform === '') {
$payPlatform = VirtualPayService::payPlatformOf($orderNo);
}
if ($payPlatform === '') {
$payPlatform = 'android';
}
$prefix = '订单支付入账 trace=' . $traceId . ' ';
try {
Db::transaction(function () use ($orderNo, $amountFen, $transactionId, $payWay, $paidTime, $prefix) {
Db::transaction(function () use ($orderNo, $amountFen, $transactionId, $payWay, $paidTime, $prefix, $payPlatform) {
$orderInfo = self::where('order_no', $orderNo)->lock(true)->find();
if (!$orderInfo) {
Log::error($prefix . '订单不存在: ' . $orderNo);
@@ -551,6 +561,7 @@ class OrderService
'currency' => 'CNY',
'pay_type' => '1',
'pay_way' => $payWay ?: 'XPAY',
'pay_platform' => $payPlatform,
'trace_no' => $transactionId,
'pay_time' => time(),
'pay_success_time' => $paidTime > 0 ? $paidTime : time(),
@@ -615,8 +626,9 @@ class OrderService
* @return array
* @throws \Exception
*/
public static function confirmVirtualPay($orderNo, $userId)
public static function confirmVirtualPay($orderNo, $userId, $payPlatform = '')
{
VirtualPayService::rememberPayPlatform($orderNo, $payPlatform);
$order = self::getOne(['order_no' => $orderNo, 'user_id' => $userId]);
if (!$order) {
throw new \Exception('订单不存在');
@@ -18,6 +18,7 @@ class VirtualPayService
const SESSION_PREFIX = 'wx_session_key:';
const TRADE_PREFIX = 'vp_trade:';
const LATEST_PREFIX = 'vp_order_latest:';
const PLATFORM_PREFIX = 'vp_pay_platform:';
const CACHE_TTL = 1296000; // 15天
/**
@@ -33,6 +34,50 @@ class VirtualPayService
Cache::set(self::SESSION_PREFIX . $userId, $sessionKey, self::CACHE_TTL);
}
/**
* 记下客户拉起支付时的终端。iOS 单独记,鸿蒙、Windows 等按安卓费率
* @param string $orderNo
* @param string $platform
* @return string
*/
public static function rememberPayPlatform($orderNo, $platform)
{
$normalized = self::normalizePayPlatform($platform);
if ($orderNo !== '' && $normalized !== '') {
Cache::set(self::PLATFORM_PREFIX . $orderNo, $normalized, self::CACHE_TTL);
}
return $normalized;
}
/**
* @param string $orderNo
* @return string
*/
public static function payPlatformOf($orderNo)
{
$cached = $orderNo ? Cache::get(self::PLATFORM_PREFIX . $orderNo) : '';
return self::normalizePayPlatform($cached);
}
/**
* @param string $platform
* @return string android|ios|空
*/
public static function normalizePayPlatform($platform)
{
$platform = strtolower(trim((string)$platform));
if ($platform === 'ios' || $platform === 'iphone') {
return 'ios';
}
if ($platform === '') {
return '';
}
return 'android';
}
/**
* 生成 wx.requestVirtualPayment 参数。signData 必须原样传给客户端,不能再 JSON.stringify
* @param string $orderNo
@@ -239,7 +284,8 @@ class VirtualPayService
$transactionId,
'XPAY',
$paidTime > 0 ? $paidTime : time(),
'settle'
'settle',
self::payPlatformOf($order->order_no)
);
}
@@ -266,7 +312,8 @@ class VirtualPayService
(string)($payInfo['TransactionId'] ?? ''),
'XPAY',
$paidTime > 0 ? $paidTime : time(),
'coin_notify'
'coin_notify',
self::payPlatformOf($orderNo)
);
}