db;//事务处理 $transaction = $connection->beginTransaction(); try { $typeList = configDict::getConfig('capitalType');//流水类型列表 $order = []; switch ($capitalType) { case $typeList['xhOrder']['id']://购买商品 $order = xhOrderService::getByOrderSn($orderSn); break; case $typeList['xhActiveOrder']['id']://活动报名 $order = xhActiveOrderService::getById($orderSn); break; case $typeList['xhApplyOrder']['id']: $order = xhApplyOrderService::getById($orderSn); break; default: Yii::warning('获取订单信息失败,回调传过来的 capitalType 不符合要求,capitalType:' . $capitalType . ' orderSn:' . $orderSn); util::fail('订单流水类型不明确'); } if (empty($order)) { Yii::warning('order info empty,capitalType:' . $capitalType . ' orderSn:' . $orderSn); util::fail('订单信息为空'); } if ($order['payStatus'] == 1) { Yii::warning('already pay,capitalType:' . $capitalType . ' orderSn:' . $orderSn); util::fail('已经付款过了'); } if ($totalFee != $order['actPrice']) { Yii::warning('第三方回调金额与订单表里金额不一致,capitalType:' . $capitalType . ' orderSn:' . $orderSn); util::fail('订单出错了(第三方回调金额与订单金额不一致)'); } $orderId = $order['id']; $orderSn = $order['orderSn']; $userId = $order['userId']; $totalFee = $order['actPrice']; $merchantId = $order['merchantId']; $alipayId = isset($alipayParams['alipayId']) ? $alipayParams['alipayId'] : ''; $alipayAccount = isset($alipayParams['alipayAccount']) ? $alipayParams['alipayAccount'] : ''; $addPoint = floor($totalFee); $addIntegral = $addPoint; $userIntegral = $addPoint; $userPoint = $addPoint; $totalExpend = $totalFee; $userTotalExpend = $totalExpend; $merchant = xhMerchantService::getById($merchantId); $merchantExtend = xhMerchantExtendService::getByMerchantId($merchantId); $shopId = isset($order['shopId']) ? $order['shopId'] : 0; if (empty($shopId)) { $shopId = ShopClass::getDefaultShopId($merchant); } $now = time(); $date = date("Y-m-d H:i:s", $now); //余额支付 $balancePay = configDict::getConfig('payWay', 'balancePay');//余额支付 if ($payWay == $balancePay && !empty($order['deadline']) && $now > $order['deadline']) { util::fail('订单已经过期'); } if (empty($userId) && $payWay == $balancePay) { $transaction->rollBack(); Yii::warning('未登陆不能使用余额支付,capitalType:' . $capitalType . ' orderSn:' . $orderSn); util::fail('没有登陆不能使用余额支付'); } //使用优惠券 CouponClass::useCoupon($couponId, $orderId); $updateData = []; $updateData['alipayId'] = $alipayId; //支付宝支付 $alipayWay = configDict::getConfig('payWay', 'alipay'); $wxWay = configDict::getConfig('payWay', 'weixinPay'); //确认h5还是小程序支付 $wxPayType = isset($callbackParams['wxPayType']) ? $callbackParams['wxPayType'] : 0; if (empty($userId)) { if ($payWay == $alipayWay) { $source = UserClass::$userSourceId['alipay']['name']; $info = ['alipayId' => $alipayId, 'merchantId' => $merchantId]; $user = UserService::replaceUser($info, $source, $merchantId); $userId = $user['id']; $userAsset = UserAssetService::getByUserId($userId); $updateData['userId'] = $userId; //更新访问时间 UserService::updateVisitTime($merchantId, $userId); } } else { $user = xhUserService::getById($userId); $userAsset = xhUserAssetService::getByUserId($userId); } if (empty($user)) { util::fail('没有客户信息'); } $balance = $userAsset['balance']; if ($payWay == $balancePay && $balance < $totalFee) { Yii::warning('余额不足,capitalType:' . $capitalType . ' orderSn:' . $orderSn); util::fail('余额不足'); } $mAsset = xhMerchantAssetService::getByMerchantId($merchantId); $mBalance = $mAsset['balance']; $mExpend = $mAsset['totalExpend']; $mIncome = stringUtil::calcAdd($mAsset['totalIncome'], $totalFee); $mUseRecharge = stringUtil::calcAdd($mAsset['usedRecharge'], $totalFee); $mTotalDeal = $mAsset['totalDeal'] + 1;//付款成功成交量+1 $mUnSendOrder = $mAsset['unSendOrder'] + 1;//待配送订单+1 $mUnPayOrder = $mAsset['unPayOrder'] - 1;//待付款订单-1 $mPayOrder = $mAsset['payOrder'] + 1;//付款订单+1 $mUpdateData = [ 'totalIncome' => $mIncome, 'usedRecharge' => $mUseRecharge, 'totalDeal' => $mTotalDeal, 'payOrder' => $mPayOrder, 'unPayOrder' => $mUnPayOrder, 'unSendOrder' => $mUnSendOrder, ]; //支付方式要转到商家资产变更里去 $order['payWay'] = $payWay; xhMerchantAssetService::incomeChangeAsset($merchant, $mAsset, $shopId, $mUpdateData, $order, $capitalType);//商家资产改变 $userName = isset($user['userName']) ? $user['userName'] : '游客'; $mCapitalEvent = ''; switch ($capitalType) { case $typeList['xhOrder']['id']://购买商品 $mCapitalEvent = '购买商品'; $uCapitalEvent = '购买商品'; $uIntegralEvent = '购买商品'; break; case $typeList['xhActiveOrder']['id']://活动报名 $mCapitalEvent = '活动报名'; $uCapitalEvent = '活动报名'; $uIntegralEvent = '活动报名'; break; case $typeList['xhApplyOrder']['id']://申请服务 $mCapitalEvent = '申请服务'; $uCapitalEvent = '申请服务'; $uIntegralEvent = '申请服务'; break; default: } $capitalData = [ 'relateId' => $orderId, 'balance' => $mBalance, 'totalIncome' => $mIncome, 'totalExpend' => $mExpend, 'amount' => $totalFee, 'io' => 1, 'shopId' => $order['shopId'], 'payWay' => $payWay, 'event' => $mCapitalEvent, 'merchantId' => $merchantId, 'userId' => $userId, 'userName' => $userName, 'alipayId' => $alipayId, 'createTime' => $date, 'addTime' => $now, 'capitalType' => $capitalType, ]; $merchantCapital = xhMerchantCapitalService::add($capitalData);//商家资金流水记录增加 $merchantCapitalId = $merchantCapital['id']; $updateData['capitalId'] = $merchantCapitalId; $updateData['payStatus'] = 1;//支付成功 $updateData['status'] = 1;//支付成功待配送 $updateData['couponId'] = $couponId; $updateData['payWay'] = $payWay; $updateData['payTime'] = $now; //确认h5还是小程序支付 $updateData['wxPayType'] = $wxPayType; switch ($capitalType) { case $typeList['xhOrder']['id']: OrderClass::payAfter($order, $updateData); break; case $typeList['xhActiveOrder']['id']://活动报名 xhActiveOrderService::updateById($orderId, $updateData); break; case $typeList['xhApplyOrder']['id']://申请服务 $updateData['inviteCodeStatus'] = 1; xhApplyOrderService::updateById($orderId, $updateData); break; default: Yii::warning('更新订单失败,回调的capitalType不符合要求,capitalType:' . $capitalType . ' orderSn:' . $orderSn); util::fail('订单流水类型不明确'); } //累计消费 $userTotalExpend = stringUtil::calcAdd($userAsset['totalExpend'], $totalFee); //成长值增加 $userGrowth = $userAsset['growth'] + floor($totalFee); $userTotalBuyNum = $userAsset['totalBuyNum'] + 1; $remainBalance = $payWay == $balancePay ? stringUtil::calcSub($balance, $totalFee) : $balance; $upAssetData = []; $upAssetData['balance'] = $remainBalance; $upAssetData['growth'] = $userGrowth; $upAssetData['totalExpend'] = $userTotalExpend; $upAssetData['totalBuyNum'] = $userTotalBuyNum; xhUserAssetService::ioChangeAsset($user, $userAsset, $upAssetData, $merchant, $merchantExtend, $order, $capitalType);//用户资产改变 $growData = [ 'userId' => $userId, 'userName' => $userName, 'merchantId' => $merchantId, 'relateId' => $orderId, 'growth' => $userGrowth, 'num' => floor($totalFee), 'io' => 1, 'payWay' => $payWay, 'alipayId' => $alipayId, 'event' => $uIntegralEvent, 'operatorId' => $userId, 'createTime' => $date, 'gainType' => 1, 'addTime' => $now, ]; xhUserGrowthService::add($growData);//用户兑换型积分流水增加 $userCapitalData = [ 'relateId' => $orderId, 'balance' => $remainBalance, 'totalIncome' => $userAsset['totalIncome'], 'totalExpend' => $userTotalExpend, 'amount' => $totalFee, 'io' => 0, 'payWay' => $payWay, 'event' => $uCapitalEvent, 'merchantId' => $merchantId, 'userId' => $userId, 'alipayId' => $alipayId, 'userName' => $userName, 'operateId' => 0, 'createTime' => $date, 'addTime' => $now, 'capitalType' => $capitalType, ]; xhUserCapitalService::add($userCapitalData);//用户资金流水增加 $transaction->commit(); //如果是小程序支付可以获取unionId并进行更新关联 if ($payWay == $wxWay && $wxPayType == 1) { OrderClass::payToUpdateUnionId($merchant, $merchantExtend, $orderSn, $user); } return ['balance' => $remainBalance]; } catch (Exception $e) { $transaction->rollBack(); util::fail('支付没有成功'); } } }