PaymentOrderService.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. <?php
  2. namespace App\Services;
  3. use App\Constants\HttpStatus;
  4. use App\Models\PaymentOrder;
  5. use Exception;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Log;
  8. use App\Services\Payment\QianBaoService;
  9. use App\Services\Payment\SanJinService;
  10. use App\Services\ConfigService;
  11. /**
  12. * 投注
  13. */
  14. class PaymentOrderService extends BaseService
  15. {
  16. const TYPE_PAY = 1; // 代收
  17. const TYPE_PAYOUT = 2; // 代付
  18. const STATUS_STAY = 0; // 待处理
  19. const STATUS_PROCESS = 1; // 处理中
  20. const STATUS_SUCCESS = 2; // 成功
  21. const STATUS_FAIL = 3; // 失败
  22. public static string $MODEL = PaymentOrder::class;
  23. /**
  24. * @description: 模型
  25. * @return {string}
  26. */
  27. public static function model(): string
  28. {
  29. return PaymentOrder::class;
  30. }
  31. /**
  32. * @description: 枚举
  33. * @return {*}
  34. */
  35. public static function enum(): string
  36. {
  37. return '';
  38. }
  39. /**
  40. * @description: 获取查询条件
  41. * @param {array} $search 查询内容
  42. * @return {array}
  43. */
  44. public static function getWhere(array $search = []): array
  45. {
  46. $where = [];
  47. if (isset($search['member_id']) && !empty($search['member_id'])) {
  48. $where[] = ['member_id', '=', $search['member_id']];
  49. }
  50. if (isset($search['type']) && !empty($search['type'])) {
  51. $where[] = ['type', '=', $search['type']];
  52. }
  53. if (isset($search['channel']) && !empty($search['channel'])) {
  54. $where[] = ['channel', '=', $search['channel']];
  55. }
  56. if (isset($search['order_no']) && !empty($search['order_no'])) {
  57. $where[] = ['order_no', '=', $search['order_no']];
  58. }
  59. if (isset($search['id']) && !empty($search['id'])) {
  60. $where[] = ['id', '=', $search['id']];
  61. }
  62. if (isset($search['status']) && $search['status'] != '') {
  63. $where[] = ['status', '=', $search['status']];
  64. }
  65. return $where;
  66. }
  67. /**
  68. * @description: 查询单条数据
  69. * @param array $search
  70. * @return \App\Models\Coin|null
  71. */
  72. public static function findOne(array $search): ?PaymentOrder
  73. {
  74. return static::$MODEL::where(self::getWhere($search))->first();
  75. }
  76. /**
  77. * @description: 查询所有数据
  78. * @param array $search
  79. * @return \Illuminate\Database\Eloquent\Collection
  80. */
  81. public static function findAll(array $search = [])
  82. {
  83. return static::$MODEL::where(self::getWhere($search))->get();
  84. }
  85. /**
  86. * @description: 分页查询
  87. * @param array $search
  88. * @return array
  89. */
  90. public static function paginate(array $search = [])
  91. {
  92. $limit = isset($search['limit']) ? $search['limit'] : 15;
  93. $paginator = static::$MODEL::where(self::getWhere($search))
  94. ->with(['userInfo'])
  95. ->orderByDesc('created_at')
  96. ->paginate($limit);
  97. $totalAmount = 0;
  98. $totalSuccess = 0;
  99. $totalFail = 0;
  100. $list = $paginator->items();
  101. foreach ($list as $item) {
  102. $item['amount'] = floatval($item['amount']);
  103. $totalAmount += $item['amount'];
  104. if (in_array($item['status'], [1, 2])) $totalSuccess += $item['amount'];
  105. if ($item['status'] == 3) $totalFail += $item['amount'];
  106. }
  107. return [
  108. 'total' => $paginator->total(),
  109. 'total_amount' => $totalAmount,
  110. 'total_success' => $totalSuccess,
  111. 'total_fail' => $totalFail,
  112. 'data' => $list
  113. ];
  114. }
  115. /**
  116. * @description:
  117. * @param {*} $params
  118. * @return {*}
  119. */
  120. public static function submit($params = [])
  121. {
  122. $result = false;
  123. $msg['code'] = self::NOT;
  124. $msg['msg'] = '';
  125. // 2. 判断是否是更新
  126. if (!empty($params['id'])) {
  127. // 更新
  128. $info = self::findOne(['id' => $params['id']]);
  129. if (!$info) {
  130. $msg['msg'] = '数据不存在!';
  131. } else {
  132. $result = $info->update($params);
  133. $id = $params['id'];
  134. }
  135. } else {
  136. // 创建
  137. $result = $info = static::$MODEL::create($params);
  138. $id = $result->id;
  139. }
  140. if ($result) {
  141. $msg['code'] = self::YES;
  142. $msg['msg'] = '设置成功';
  143. $msg['key'] = $id;
  144. } else {
  145. $msg['msg'] = empty($msg['msg']) ? '操作失败' : $msg['msg'];
  146. }
  147. return $msg;
  148. }
  149. /**
  150. * @description: 创建代收订单
  151. * @param {*} $memberId
  152. * @param {*} $amount
  153. * @param {*} $paymentType 支付类型:支付宝、数字人民币
  154. * @return {*}
  155. */
  156. public static function createPay($memberId, $amount, $paymentType)
  157. {
  158. $result = [];
  159. $result['chat_id'] = $memberId;
  160. $channel = ''; // 支付的通道
  161. $product = SanJinService::$PRODUCT;
  162. $max = 0;
  163. $min = 0;
  164. $rate = 0;
  165. $geText = '';
  166. foreach ($product as $k => $v) {
  167. if ($v['type'] == $paymentType) {
  168. if ($v['type'] == 'zfbge') {
  169. if (in_array($amount, $v['fixed'])) {
  170. $channel = $k;
  171. } else {
  172. $geText .= "❌ 此充值通道固定充值金额为" . implode(',', $v['fixed']) . "请务必输入区间金额!";
  173. }
  174. } else {
  175. if ($amount >= $v['min'] && $amount <= $v['max']) {
  176. $channel = $k;
  177. $rate = $v['rate'];
  178. }
  179. if ($min == 0) {
  180. $min = $v['min'];
  181. }
  182. if ($max == 0) {
  183. $max = $v['max'];
  184. }
  185. if ($min > $v['min']) {
  186. $min = $v['min'];
  187. }
  188. if ($max < $v['max']) {
  189. $max = $v['max'];
  190. }
  191. }
  192. }
  193. }
  194. // 没有找到支付通道
  195. if (empty($channel)) {
  196. // $text = "发起充值失败 \n";
  197. // $text .= "最低充值:" . $min . " \n";
  198. // $text .= "最高充值:" . $max . " \n";
  199. // $text .= "请重新填写充值的金额!";
  200. $text = "❌ 此充值通道充值金额{$min}-{$max}请务必输入区间金额!";
  201. $result['text'] = $text;
  202. if ($geText) {
  203. $result['text'] = $geText;
  204. }
  205. return $result;
  206. }
  207. $data = [];
  208. $data['type'] = self::TYPE_PAY;
  209. $data['member_id'] = $memberId;
  210. $data['amount'] = $amount;
  211. $data['channel'] = $channel;
  212. $data['fee'] = $amount * $rate;
  213. $data['bank_name'] = SanJinService::getChannel($paymentType) ?? '';
  214. $order_no = self::createOrderNo('sj' . $data['type'] . '_', $memberId);
  215. $data['order_no'] = $order_no;
  216. $data['callback_url'] = SanJinService::getNotifyUrl();
  217. $data['remark'] = '充值费率:' . $rate;
  218. $data['status'] = self::STATUS_STAY;
  219. $ret = SanJinService::pay(($amount * 100), $order_no, $channel);
  220. Log::error('三斤支付发起:', $ret);
  221. if ($ret['code'] == 0) {
  222. $qrCode = asset(self::createPaymentQrCode($ret['data']['payUrl']));
  223. $result['image'] = $qrCode;
  224. $item = $ret['data'];
  225. $data['status'] = self::STATUS_PROCESS;
  226. $data['pay_no'] = $item['tradeNo'];
  227. $data['pay_url'] = $item['payUrl'];
  228. $data['pay_data'] = json_encode($ret, JSON_UNESCAPED_UNICODE);
  229. $info = static::$MODEL::create($data);
  230. // $text = "✅ 支付提示 \n";
  231. $text = "{$data['bank_name']}充值确认 \n";
  232. // $text .= "支付方式:{$data['bank_name']} \n";
  233. $text .= "请使用浏览器扫码或者复制支付地址到浏览器打开 \n";
  234. $text .= "支付地址:{$ret['data']['payUrl']}\n";
  235. $text .= "支付金额:" . $amount . " RMB \n";
  236. $text .= "请按实际支付金额进行付款,否则影响到账 \n";
  237. $text .= "支付完成后请耐心等待,支付到账会第一时间通知您! \n";
  238. $result['text'] = $text;
  239. } else {
  240. $result['text'] = $ret['message'];
  241. }
  242. return $result;
  243. }
  244. /**
  245. * @description: 接收支付的通知
  246. * @param {*} $params
  247. * @return {*}
  248. */
  249. public static function receivePay($params)
  250. {
  251. // 判断商户号
  252. if ($params['mchId'] == SanJinService::getMerchantId()) {
  253. $must = ['mchId', 'productId', 'tradeNo', 'outTradeNo', 'amount', 'payAmount', 'state', 'createTime', 'payTime'];
  254. $info = self::findOne(['order_no' => $params['outTradeNo']]);
  255. if ($info) {
  256. // 平台以分计算转成元
  257. $payAmount = $params['payAmount'] / 100;
  258. // 判断金额是不是正确认
  259. if ($info->amount != $payAmount) {
  260. $text = '❌ 支付失败提醒 \n';
  261. $text .= "订单金额:{$info->amount} \n";
  262. $text .= "实际支付:{$payAmount} \n";
  263. $text .= "订单号:{$params['outTradeNo']} \n";
  264. $text .= "失败原因:支付金额与订单金额不一致 \n";
  265. $text .= "请联系客服处理!";
  266. self::sendMessage($info->member_id, $text);
  267. return false;
  268. }
  269. if ($params['sign'] != SanJinService::signature($params, $must)) {
  270. return false;
  271. }
  272. if ($info->status != self::STATUS_PROCESS) {
  273. return false;
  274. }
  275. // 付款
  276. if ($info->type == self::TYPE_PAY) {
  277. $info->state = $params['state'];
  278. if ($params['state'] == 1) {
  279. $info->status = self::STATUS_SUCCESS;
  280. $wallet = WalletService::findOne(['member_id' => $info->member_id]);
  281. $balance = $wallet->available_balance;
  282. $available_balance = bcadd($balance, $payAmount, 10);
  283. $wallet->available_balance = $available_balance;
  284. $wallet->save();
  285. // 记录余额变动日志
  286. BalanceLogService::addLog($info->member_id, $payAmount, $balance, $available_balance, '三方充值', $info->id, '');
  287. self::rechargesBibiReturn($info->member_id, $payAmount, $info->id);
  288. $text = "✅ 支付成功 \n";
  289. $text .= "充值金额:{$payAmount} RMB \n";
  290. $text .= "订单号:{$params['outTradeNo']} \n";
  291. $text .= "您充值的金额已到账,请注意查收!";
  292. self::sendMessage($info->member_id, $text);
  293. } else {
  294. $info->status = self::STATUS_FAIL;
  295. $text = "❌ 支付失败 \n";
  296. $text .= "充值金额:{$payAmount} RMB \n";
  297. $text .= "订单号:{$params['outTradeNo']} \n";
  298. }
  299. $info->save();
  300. return true;
  301. }
  302. }
  303. }
  304. }
  305. // 充值笔笔返
  306. public static function rechargesBibiReturn($memberId,$payAmount,$info_id)
  307. {
  308. $rate = ConfigService::getVal('recharges_bibi_return');
  309. $amount = $payAmount * $rate;
  310. if($rate > 0){
  311. $wallet = WalletService::findOne(['member_id' => $memberId]);
  312. $balance = $wallet->available_balance;
  313. $available_balance = bcadd($balance, $amount, 10);
  314. $wallet->available_balance = $available_balance;
  315. $wallet->save();
  316. BalanceLogService::addLog($memberId, $amount, $balance, $available_balance, '优惠活动', $info_id, '充值笔笔返');
  317. }
  318. }
  319. /**
  320. * 拒绝提现
  321. * @description 改变订单状态为失败,将金额退给用户,并记录提现退款的资金日
  322. * @param int $orderId 订单ID PaymentOrder 表的ID
  323. * @param string $remark 失败原因
  324. * @return array
  325. */
  326. public static function withdrawalFailed($orderId, $remark): array
  327. {
  328. try {
  329. $order = PaymentOrder::where('id', $orderId)
  330. ->where('type', 2)
  331. ->where('status', self::STATUS_STAY)
  332. ->first();
  333. if (!$order) throw new Exception("订单不存在_{$orderId}", HttpStatus::CUSTOM_ERROR);
  334. // 更新提现记录状态为失败
  335. $order->status = self::STATUS_FAIL;
  336. $order->remark = $remark;
  337. if (!$order->save()) {
  338. throw new Exception("更新失败,请重试", HttpStatus::CUSTOM_ERROR);
  339. }
  340. //钱包余额增加
  341. $wallet = WalletService::findOne(['member_id' => $order->member_id]);
  342. $beforeBalance = $wallet->available_balance;
  343. $availableBalance = bcadd($wallet->available_balance, $order->amount, 10);
  344. $wallet->available_balance = $availableBalance;
  345. if (!$wallet->save()) {
  346. throw new Exception("更新失败,请重试", HttpStatus::CUSTOM_ERROR);
  347. }
  348. // 记录退款日志
  349. BalanceLogService::addLog($order->member_id, $order->amount, $beforeBalance, $availableBalance, '三方提现', $order->id, '提现失败退款');
  350. } catch (Exception $e) {
  351. return ['code' => HttpStatus::CUSTOM_ERROR, 'msg' => $e->getMessage()];
  352. }
  353. return ['code' => 0, 'msg' => 'ok'];
  354. }
  355. /**
  356. * 创建代付订单 (根据 orderId 将钱转到用户指定账户)
  357. * @param int $orderId PaymentOrder 表的ID
  358. */
  359. public static function createPayout(int $orderId): array
  360. {
  361. try {
  362. $order = PaymentOrder::where('id', $orderId)
  363. ->where('type', 2)
  364. ->where('status', self::STATUS_STAY)
  365. ->first();
  366. if (!$order) throw new Exception("订单不存在_{$orderId}", HttpStatus::CUSTOM_ERROR);
  367. $amount = $order->amount;
  368. $amount = number_format($amount, 2, '.', '');
  369. $ret = QianBaoService::payout($amount, $order->order_no, $order->bank_name, $order->account, $order->card_no);
  370. Log::error('第三方代付接口调用:' . json_encode($ret, JSON_UNESCAPED_UNICODE));
  371. if ($ret['code'] != 200) throw new Exception($ret['msg'], HttpStatus::CUSTOM_ERROR);
  372. $order->status = self::STATUS_PROCESS;
  373. $order->save();
  374. } catch (Exception $e) {
  375. return ['code' => HttpStatus::CUSTOM_ERROR, 'msg' => $e->getMessage()];
  376. }
  377. return ['code' => 0, 'msg' => 'ok'];
  378. }
  379. /**
  380. * @description: 创建代付订单 (自动直接到账,包括用户钱包的扣款和提现记录的生成以及余额日志的创建)
  381. * @param {*} $memberId 会员ID
  382. * @param {*} $amount 代付金额
  383. * @param {*} $channel 提现通道 DF001 支付宝转卡/DF002 支付宝转支付宝
  384. * @param {*} $bank_name 银行名称/支付宝
  385. * @param {*} $account 姓名
  386. * @param {*} $card_no 银行卡号/支付宝账号
  387. * @return {*}
  388. */
  389. public static function autoCreatePayout($memberId, $amount, $channel, $bank_name, $account, $card_no)
  390. {
  391. $default_amount = $amount;
  392. $result = [];
  393. $result['chat_id'] = $memberId;
  394. if ($amount < 100) {
  395. $result['text'] = '提现金额最少100';
  396. return $result;
  397. }
  398. if ($amount > 49999) {
  399. $result['text'] = '提现金额最多49999';
  400. return $result;
  401. }
  402. // 在调用三方支付前开始事务
  403. DB::beginTransaction();
  404. try {
  405. $wallet = WalletService::findOne(['member_id' => $memberId]);
  406. if (!$wallet) {
  407. $result['text'] = '钱包不存在!';
  408. return $result;
  409. }
  410. $balance = $wallet->available_balance;
  411. if (bccomp($balance, $amount, 2) < 0) {
  412. $result['text'] = '您的钱包余额不足!';
  413. return $result;
  414. }
  415. $available_balance = bcsub($balance, $amount, 10);
  416. $data = [];
  417. $data['type'] = self::TYPE_PAYOUT;
  418. $order_no = self::createOrderNo('sj' . $data['type'] . '_', $memberId);
  419. $data['order_no'] = $order_no;
  420. $data['member_id'] = $memberId;
  421. $data['fee'] = $amount * 0.002 + 2;
  422. $amount = number_format($amount, 2, '.', '');
  423. $data['amount'] = $amount;
  424. $data['channel'] = $channel;
  425. $data['bank_name'] = $bank_name;
  426. $data['account'] = $account;
  427. $data['card_no'] = $card_no;
  428. $data['callback_url'] = QianBaoService::getNotifyUrl();
  429. $data['status'] = self::STATUS_STAY;
  430. $data['remark'] = '提现费率:0.2%+2';
  431. // 先预扣款(锁定资金)
  432. $wallet->available_balance = $available_balance;
  433. if (!$wallet->save()) {
  434. DB::rollBack();
  435. $result['text'] = '钱包更新失败!';
  436. return $result;
  437. }
  438. // 创建待处理状态的提现记录
  439. $info = static::$MODEL::create($data);
  440. $id = $info->id;
  441. // 记录余额变动日志
  442. BalanceLogService::addLog($memberId, $default_amount * -1, $balance, $available_balance, '三方提现', $id, '钱宝提现费率:0.2%+2');
  443. // 提交事务,确保预扣款成功
  444. DB::commit();
  445. } //
  446. catch (Exception $e) {
  447. // 预扣款失败,回滚事务
  448. DB::rollBack();
  449. $result['text'] = '系统繁忙,请稍后重试!';
  450. Log::error('提现预扣款失败: ' . $e->getMessage(), [
  451. 'member_id' => $memberId,
  452. 'amount' => $amount
  453. ]);
  454. return $result;
  455. }
  456. // 调用三方支付接口(在事务外)
  457. $ret = QianBaoService::payout($amount, $order_no, $bank_name, $account, $card_no);
  458. Log::error('第三方代付接口调用:' . json_encode($ret, JSON_UNESCAPED_UNICODE));
  459. if ($ret['code'] == 200) {
  460. // 更新提现记录状态为处理中
  461. DB::beginTransaction();
  462. try {
  463. $info->status = self::STATUS_PROCESS;
  464. $info->save();
  465. DB::commit();
  466. $text = "✅ 提现申请已提交!\n\n";
  467. $text .= "钱包余额:{$available_balance} RMB\n";
  468. $text .= "提现金额:{$default_amount} RMB\n";
  469. $text .= "⌛️请等待系统处理, 到账时间可能需要几分钟!\n";
  470. $result['text'] = $text;
  471. } catch (Exception $e) {
  472. DB::rollBack();
  473. // 状态更新失败,但资金已扣款且三方支付已成功
  474. // 这里可以记录告警,需要人工干预
  475. Log::error('提现状态更新失败: ' . $e->getMessage(), [
  476. 'member_id' => $memberId,
  477. 'order_no' => $order_no
  478. ]);
  479. $result['text'] = '提现申请已提交,系统处理中...';
  480. }
  481. } //
  482. else {
  483. // 三方支付失败,需要回滚之前的预扣款
  484. DB::beginTransaction();
  485. try {
  486. // 恢复钱包余额
  487. $wallet->available_balance = $balance;
  488. $wallet->save();
  489. // 更新提现记录状态为失败
  490. $info->status = self::STATUS_FAIL;
  491. $info->remark = $ret['msg'];
  492. $info->save();
  493. // 记录退款日志
  494. BalanceLogService::addLog($memberId, $default_amount, $available_balance, $balance, '三方提现', $id, '提现失败退款');
  495. DB::commit();
  496. $result['text'] = $ret['msg'];
  497. } catch (Exception $e) {
  498. DB::rollBack();
  499. // 回滚失败,需要记录告警,人工干预
  500. Log::error('提现失败回滚异常: ' . $e->getMessage(), [
  501. 'member_id' => $memberId,
  502. 'order_no' => $order_no,
  503. 'amount' => $amount
  504. ]);
  505. $result['text'] = '提现失败,请联系客服处理退款!';
  506. }
  507. }
  508. return $result;
  509. }
  510. /**
  511. * @description: 接收三方订单
  512. * @param {*} $params
  513. * @return {*}
  514. */
  515. public static function receiveOrder($params)
  516. {
  517. // 判断商户号是否一致
  518. if ($params['merchantNum'] == QianBaoService::getMerchantId()) {
  519. $info = self::findOne(['order_no' => $params['orderNo']]);
  520. if ($info) {
  521. // 判断金额是不是正确认
  522. if ($info->amount != $params['amount']) {
  523. return false;
  524. }
  525. // 验证签名
  526. $sign = QianBaoService::verifyNotifySign($params['state'], $params['orderNo'], $params['amount']);
  527. if ($params['sign'] != $sign) {
  528. return false;
  529. }
  530. // 代付
  531. if ($info->type == self::TYPE_PAYOUT) {
  532. self::onSubmitPayout($params, $info);
  533. }
  534. // 代收
  535. if ($info->type == self::TYPE_PAY) {
  536. }
  537. }
  538. }
  539. }
  540. /**
  541. * @description: 处理代付订单
  542. * @param {*} $params
  543. * @return {*}
  544. */
  545. public static function onSubmitPayout($params, $info)
  546. {
  547. $memberId = $info->member_id;
  548. $amount = $params['amount'];
  549. $data = [];
  550. $result = [];
  551. $chat_id = $info->member_id;
  552. $data['callback_data'] = json_encode($params, JSON_UNESCAPED_UNICODE);
  553. DB::beginTransaction();
  554. try {
  555. if ($params['state'] == 1) {
  556. $data['status'] = self::STATUS_SUCCESS;
  557. $res = static::$MODEL::where(['order_no' => $params['orderNo']])->update($data);
  558. if ($res) {
  559. $text = "✅ 提现通知 \n";
  560. $text .= "提现平台:{$info->bank_name} \n";
  561. $text .= "收款人:{$info->account} \n";
  562. $text .= "收款卡号:{$info->card_no} \n";
  563. $text .= "提现金额:{$info->amount} \n";
  564. $text .= "提现成功,金额已到账,请注意查收!";
  565. self::sendMessage($chat_id, $text);
  566. }
  567. } else {
  568. $data['status'] = self::STATUS_FAIL;
  569. $res = static::$MODEL::where(['order_no' => $params['orderNo']])->update($data);
  570. $wallet = WalletService::findOne(['member_id' => $info->member_id]);
  571. $balance = $wallet->available_balance; // 钱包当前余额
  572. $available_balance = bcadd($balance, $params['amount'], 10);
  573. $wallet->available_balance = $available_balance;
  574. $wallet->save();
  575. // 记录退款日志
  576. BalanceLogService::addLog($memberId, $amount, $balance, $available_balance, '三方提现', $info->id, '提现失败退款');
  577. if ($res) {
  578. $text = "❌ 提现通知 \n";
  579. $text .= "提现平台:{$info->bank_name} \n";
  580. $text .= "收款人:{$info->account} \n";
  581. $text .= "收款卡号:{$info->card_no} \n";
  582. $text .= "提现金额:{$info->amount} \n";
  583. $text .= "提现失败,金额已返回钱包,请注意查收!";
  584. self::sendMessage($chat_id, $text);
  585. }
  586. }
  587. DB::commit();
  588. } catch (Exception $e) {
  589. DB::rollBack();
  590. // 回滚失败,需要记录告警,人工干预
  591. Log::error('提现失败回滚异常: ' . $e->getMessage(), $params);
  592. }
  593. }
  594. /**
  595. * @description: 查询支付订单
  596. * @param {*} $id
  597. * @return {*}
  598. */
  599. public static function singlePayOrder($id)
  600. {
  601. $msg = [];
  602. $msg['code'] = self::NOT;
  603. $info = self::findOne(['id' => $id]);
  604. if ($info && $info->status == self::STATUS_PROCESS) {
  605. $ret = SanJinService::queryOrder($info->order_no);
  606. Log::error('三斤支付查询订单:', $ret);
  607. if ($ret['code'] == 0) {
  608. $item = [];
  609. $item['state'] = $ret['data']['state'];
  610. if ($ret['data']['state'] == 1) {
  611. $item['status'] = self::STATUS_SUCCESS;
  612. $info->update($item);
  613. $wallet = WalletService::findOne(['member_id' => $info->member_id]);
  614. $balance = $wallet->available_balance;
  615. $available_balance = bcadd($balance, $info->amount, 10);
  616. $wallet->available_balance = $available_balance;
  617. $wallet->save();
  618. // 记录余额变动日志
  619. BalanceLogService::addLog($info->member_id, $info->amount, $balance, $available_balance, '三方充值', $info->id, '');
  620. $msg['code'] = self::YES;
  621. $msg['msg'] = '支付成功';
  622. } else {
  623. $msg['msg'] = '支付中';
  624. }
  625. } else {
  626. $msg['msg'] = '查询失败:' . $ret['message'];
  627. }
  628. } else {
  629. $msg['msg'] = '该状态无法查询';
  630. }
  631. return $msg;
  632. }
  633. public static function syncPayOrder()
  634. {
  635. $list = static::$MODEL::where('state', 0)->where('type', self::TYPE_PAY)->take(100)->get();
  636. // foreach($list->toArray() as $k => $v){
  637. // $item= [];
  638. // if($v['status'] == self::STATUS_SUCCESS){
  639. // $item['state'] = 1;
  640. // static::$MODEL::where(['id'=>$v['id']])->update($item);
  641. // }else{
  642. // $ret = SanJinService::queryOrder($v['order_no']);
  643. // var_dump($ret);
  644. // if($ret['code'] == 0){
  645. // $item['state'] = $ret['data']['state'];
  646. // static::$MODEL::where(['id'=>$v['id']])->update($item);
  647. // }
  648. // }
  649. // }
  650. }
  651. }