first(); } /** * @description: 查询所有数据 * @param array $search * @return \Illuminate\Database\Eloquent\Collection */ public static function findAll(array $search = []) { return self::model()::where(self::getWhere($search))->get(); } /** * @description: 分页查询 * @param array $search * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator */ public static function paginate(array $search = []) { $limit = isset($search['limit'])?$search['limit']:15; $paginator = self::model()::where(self::getWhere($search))->paginate($limit); return ['total' => $paginator->total(), 'data' => $paginator->items()]; } /** * @description: * @param {*} $params * @return {*} */ public static function submit($params = []) { $result = false; $msg['code'] = self::NOT; $msg['msg'] = ''; // 2. 判断是否是更新 if (!empty($params['id'])) { // 更新 $info = self::findOne(['id'=>$params['id']] ); if (!$info) { $msg['msg'] = '数据不存在!'; }else{ $result = $info->update($params); $id = $params['id']; } } else { // 创建 $result = $info = self::model()::create($params); $id = $result->id; } if($result){ $msg['code'] = self::YES; $msg['msg'] = '设置成功'; $msg['key'] = $id; }else{ $msg['msg'] = empty($msg['msg']) ?'操作失败':$msg['msg']; } return $msg; } /** * @description: 创建代付订单 * @param {*} $memberId 会员 * @param {*} $amount 金额 * @param {*} $channel 提现通道 DF001 支付宝转卡/DF002 支付宝转支付宝 * @param {*} $bank_name 银行名称/支付宝 * @param {*} $account 姓名 * @param {*} $card_no 银行卡号/支付宝账号 * @return {*} */ public static function createPayout($memberId, $amount, $channel, $bank_name, $account, $card_no) { $default_amount = $amount; $result = []; $result['chat_id'] = $memberId; if($amount < 100){ $result['text'] = '提现金额最少100'; return $result; } if($amount > 49999){ $result['text'] = '提现金额最多49999'; return $result; } // 在调用三方支付前开始事务 DB::beginTransaction(); try { $wallet = WalletService::findOne(['member_id' => $memberId]); if (!$wallet) { $result['text'] = '钱包不存在!'; return $result; } $balance = $wallet->available_balance; if (bccomp($balance, $amount, 2) < 0) { $result['text'] = '您的钱包余额不足!'; return $result; } $available_balance = bcsub($balance, $amount, 10); $data = []; $data['type'] = self::TYPE_PAYOUT; $order_no = self::createOrderNo('sj'.$data['type'].'_', $memberId); $data['order_no'] = $order_no; $data['member_id'] = $memberId; $data['free'] = $amount * 0.002 + 2; $amount = number_format($amount, 2, '.', ''); $data['amount'] = $amount; $data['channel'] = $channel; $data['bank_name'] = $bank_name; $data['account'] = $account; $data['card_no'] = $card_no; $data['callback_url'] = SanJinService::getNotifyUrl(); $data['status'] = self::STATUS_STAY; $data['remark'] = '提现费率:0.2%+2'; // 先预扣款(锁定资金) $wallet->available_balance = $available_balance; if (!$wallet->save()) { DB::rollBack(); $result['text'] = '钱包更新失败!'; return $result; } // 创建待处理状态的提现记录 $info = self::model()::create($data); $id = $info->id; // 记录余额变动日志 BalanceLogService::addLog( $memberId, $default_amount, $balance, $available_balance, '三方提现', $id, '钱宝提现费率:0.2%+2' ); // 提交事务,确保预扣款成功 DB::commit(); } catch (\Exception $e) { // 预扣款失败,回滚事务 DB::rollBack(); $result['text'] = '系统繁忙,请稍后重试!'; Log::error('提现预扣款失败: ' . $e->getMessage(), [ 'member_id' => $memberId, 'amount' => $amount ]); return $result; } // 调用三方支付接口(在事务外) $ret = SanJinService::payout($amount, $order_no, $bank_name, $account, $card_no); Log::error('第三方代付接口调用:' . json_encode($ret, JSON_UNESCAPED_UNICODE)); if ($ret['code'] == 200) { // 更新提现记录状态为处理中 DB::beginTransaction(); try { $info->status = self::STATUS_PROCESS; $info->save(); DB::commit(); $text = "✅ 提现申请已提交!\n\n"; $text .= "钱包余额:{$available_balance} RMB\n"; $text .= "提现金额:{$default_amount} RMB\n"; $text .= "⌛️请等待系统处理, 到账时间可能需要几分钟!\n"; $result['text'] = $text; } catch (\Exception $e) { DB::rollBack(); // 状态更新失败,但资金已扣款且三方支付已成功 // 这里可以记录告警,需要人工干预 Log::error('提现状态更新失败: ' . $e->getMessage(), [ 'member_id' => $memberId, 'order_no' => $order_no ]); $result['text'] = '提现申请已提交,系统处理中...'; } } else { // 三方支付失败,需要回滚之前的预扣款 DB::beginTransaction(); try { // 恢复钱包余额 $wallet->available_balance = $balance; $wallet->save(); // 更新提现记录状态为失败 $info->status = self::STATUS_FAIL; $info->remark = $ret['msg']; $info->save(); // 记录退款日志 BalanceLogService::addLog( $memberId, $default_amount, $available_balance, $balance, '三方提现', $id, '提现失败退款' ); DB::commit(); $result['text'] = $ret['msg']; } catch (\Exception $e) { DB::rollBack(); // 回滚失败,需要记录告警,人工干预 Log::error('提现失败回滚异常: ' . $e->getMessage(), [ 'member_id' => $memberId, 'order_no' => $order_no, 'amount' => $amount ]); $result['text'] = '提现失败,请联系客服处理退款!'; } } return $result; } /** * @description: 接收三方订单 * @param {*} $params * @return {*} */ public static function receiveOrder($params) { // 判断商户号是否一致 if($params['merchantNum'] == SanJinService::getMerchantId()){ $info = self::findOne(['order_no' => $params['orderNo']]); if($info){ // 判断金额是不是正确认 if($info->amount != $params['amount']){ return 'amount'; } // 验证签名 $sign = SanJinService::verifyNotifySign($params['state'],$params['orderNo'],$params['amount']); if($params['sign'] != $sign){ return 'sign'; } // 代付 if($info->type == self::TYPE_PAYOUT){ self::onSubmitPayout($params,$info); } // 代收 if($info->type == self::TYPE_PAY){ } } } } /** * @description: 处理代付订单 * @param {*} $params * @return {*} */ public static function onSubmitPayout($params,$info) { $data = []; $result = []; $chat_id = $info->member_id; $data['callback_data'] =json_encode($params,JSON_UNESCAPED_UNICODE); DB::beginTransaction(); try{ if($params['state'] == 1){ $data['status'] = self::STATUS_SUCCESS; $res = self::model()::where(['order_no' => $params['orderNo']])->update($data); if($res){ DB::commit(); $text = "✅ 提现通知 \n"; $text .= "提现平台:{$info->bank_name} \n"; $text .= "收款人:{$info->account} \n"; $text .= "收款卡号:{$info->card_no} \n"; $text .= "提现金额:{$info->amount} \n"; $text .= "提现成功,金额已到账,请注意查收!"; self::sendMessage($chat_id,$text); } }else{ $data['status'] = self::STATUS_FAIL; $res = self::model()::where(['order_no' => $params['orderNo']])->update($data); if($res){ DB::commit(); $text = "❌ 提现通知 \n"; $text .= "提现平台:{$info->bank_name} \n"; $text .= "收款人:{$info->account} \n"; $text .= "收款卡号:{$info->card_no} \n"; $text .= "提现金额:{$info->amount} \n"; $text .= "提现失败,金额已返回钱包,请注意查收!"; self::sendMessage($chat_id,$text); } } }catch(\Exception $e){ DB::rollBack(); // 回滚失败,需要记录告警,人工干预 Log::error('提现失败回滚异常: ' . $e->getMessage(), $params); } } }