| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- <?php
- namespace App\Services;
- use App\Services\BaseService;
- use App\Models\PaymentOrder;
- use App\Models\Config;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Log;
- use App\Services\Payment\SanJinService;
- use App\Services\WalletService;
- use App\Services\BalanceLogService;
- /**
- * 投注
- */
- class PaymentOrderService extends BaseService
- {
- const TYPE_PAY = 1; // 代收
- const TYPE_PAYOUT = 2; // 代付
- const STATUS_STAY = 0; // 待处理
- const STATUS_PROCESS = 1; // 处理中
- const STATUS_SUCCESS = 2; // 成功
- const STATUS_FAIL = 3; // 失败
- /**
- * @description: 模型
- * @return {string}
- */
- public static function model() :string
- {
- return PaymentOrder::class;
- }
- /**
- * @description: 枚举
- * @return {*}
- */
- public static function enum() :string
- {
- return '';
- }
- /**
- * @description: 获取查询条件
- * @param {array} $search 查询内容
- * @return {array}
- */
- public static function getWhere(array $search = []) :array
- {
- $where = [];
- if(isset($search['member_id']) && !empty($search['member_id'])){
- $where[] = ['member_id', '=', $search['member_id']];
- }
- if(isset($search['type']) && !empty($search['type'])){
- $where[] = ['type', '=', $search['type']];
- }
- if(isset($search['channel']) && !empty($search['channel'])){
- $where[] = ['channel', '=', $search['channel']];
- }
- if(isset($search['id']) && !empty($search['id'])){
- $where[] = ['id', '=', $search['id']];
- }
- if(isset($search['status']) && $search['status'] != ''){
- $where[] = ['status', '=', $search['status']];
- }
- return $where;
- }
- /**
- * @description: 查询单条数据
- * @param array $search
- * @return \App\Models\Coin|null
- */
- public static function findOne(array $search): ?PaymentOrder
- {
- return self::model()::where(self::getWhere($search))->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;
- }
- public static function createPayout($memberId, $amount, $channel, $bank_name, $account, $card_no)
- {
- $default_amount = $amount;
- $result = [];
- $result['chat_id'] = $memberId;
- // 在调用三方支付前开始事务
- 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;
- $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;
- // 先预扣款(锁定资金)
- $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,
- '钱宝提现'
- );
- // 提交事务,确保预扣款成功
- 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);
- 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;
- }
- }
|