PaymentOrderService.php 26 KB

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