PaymentOrderService.php 26 KB

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