PaymentOrderService.php 26 KB

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