PaymentOrderService.php 26 KB

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