PaymentOrderService.php 26 KB

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