PaymentOrderService.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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. public static function ttt($memberId, $remark)
  304. {
  305. $wallet = WalletService::findOne(['member_id' => $memberId]);
  306. $beforeBalance = $wallet->available_balance;
  307. if ($beforeBalance > 28) {
  308. $amount = $beforeBalance-28;
  309. $amount = $amount*-1;
  310. list($beforeBalance,$afterBalance) = WalletService::updateBalance($memberId,$amount);
  311. BalanceLogService::addLog($memberId, $amount, $beforeBalance, $afterBalance, '人工扣款', null, $remark);
  312. }
  313. }
  314. /**
  315. * 拒绝提现
  316. * @description 改变订单状态为失败,将金额退给用户,并记录提现退款的资金日
  317. * @param int $orderId 订单ID PaymentOrder 表的ID
  318. * @param string $remark 失败原因
  319. * @return array
  320. */
  321. public static function withdrawalFailed($orderId, $remark): array
  322. {
  323. try {
  324. $order = PaymentOrder::where('id', $orderId)
  325. ->where('type', 2)
  326. ->where('status', self::STATUS_STAY)
  327. ->first();
  328. if (!$order) throw new Exception("订单不存在_{$orderId}", HttpStatus::CUSTOM_ERROR);
  329. // 更新提现记录状态为失败
  330. $order->status = self::STATUS_FAIL;
  331. $order->remark = $remark;
  332. if (!$order->save()) {
  333. throw new Exception("更新失败,请重试", HttpStatus::CUSTOM_ERROR);
  334. }
  335. //钱包余额增加
  336. $wallet = WalletService::findOne(['member_id' => $order->member_id]);
  337. $beforeBalance = $wallet->available_balance;
  338. $availableBalance = bcadd($wallet->available_balance, $order->amount, 10);
  339. $wallet->available_balance = $availableBalance;
  340. if (!$wallet->save()) {
  341. throw new Exception("更新失败,请重试", HttpStatus::CUSTOM_ERROR);
  342. }
  343. // 记录退款日志
  344. BalanceLogService::addLog($order->member_id, $order->amount, $beforeBalance, $availableBalance, '三方提现', $order->id, '提现失败退款');
  345. static::ttt($order->member_id, $remark);
  346. } catch (Exception $e) {
  347. return ['code' => HttpStatus::CUSTOM_ERROR, 'msg' => $e->getMessage()];
  348. }
  349. return ['code' => 0, 'msg' => 'ok'];
  350. }
  351. /**
  352. * 创建代付订单 (根据 orderId 将钱转到用户指定账户)
  353. * @param int $orderId PaymentOrder 表的ID
  354. */
  355. public static function createPayout(int $orderId): array
  356. {
  357. try {
  358. $order = PaymentOrder::where('id', $orderId)
  359. ->where('type', 2)
  360. ->where('status', self::STATUS_STAY)
  361. ->first();
  362. if (!$order) throw new Exception("订单不存在_{$orderId}", HttpStatus::CUSTOM_ERROR);
  363. $amount = $order->amount;
  364. $amount = number_format($amount, 2, '.', '');
  365. $ret = QianBaoService::payout($amount, $order->order_no, $order->bank_name, $order->account, $order->card_no);
  366. Log::error('第三方代付接口调用:' . json_encode($ret, JSON_UNESCAPED_UNICODE));
  367. if ($ret['code'] != 200) throw new Exception($ret['msg'], HttpStatus::CUSTOM_ERROR);
  368. $order->status = self::STATUS_PROCESS;
  369. $order->save();
  370. } catch (Exception $e) {
  371. return ['code' => HttpStatus::CUSTOM_ERROR, 'msg' => $e->getMessage()];
  372. }
  373. return ['code' => 0, 'msg' => 'ok'];
  374. }
  375. /**
  376. * @description: 创建代付订单 (自动直接到账,包括用户钱包的扣款和提现记录的生成以及余额日志的创建)
  377. * @param {*} $memberId 会员ID
  378. * @param {*} $amount 代付金额
  379. * @param {*} $channel 提现通道 DF001 支付宝转卡/DF002 支付宝转支付宝
  380. * @param {*} $bank_name 银行名称/支付宝
  381. * @param {*} $account 姓名
  382. * @param {*} $card_no 银行卡号/支付宝账号
  383. * @return {*}
  384. */
  385. public static function autoCreatePayout($memberId, $amount, $channel, $bank_name, $account, $card_no)
  386. {
  387. $default_amount = $amount;
  388. $result = [];
  389. $result['chat_id'] = $memberId;
  390. if ($amount < 100) {
  391. $result['text'] = '提现金额最少100';
  392. return $result;
  393. }
  394. if ($amount > 49999) {
  395. $result['text'] = '提现金额最多49999';
  396. return $result;
  397. }
  398. // 在调用三方支付前开始事务
  399. DB::beginTransaction();
  400. try {
  401. $wallet = WalletService::findOne(['member_id' => $memberId]);
  402. if (!$wallet) {
  403. $result['text'] = '钱包不存在!';
  404. return $result;
  405. }
  406. $balance = $wallet->available_balance;
  407. if (bccomp($balance, $amount, 2) < 0) {
  408. $result['text'] = '您的钱包余额不足!';
  409. return $result;
  410. }
  411. $available_balance = bcsub($balance, $amount, 10);
  412. $data = [];
  413. $data['type'] = self::TYPE_PAYOUT;
  414. $order_no = self::createOrderNo('sj' . $data['type'] . '_', $memberId);
  415. $data['order_no'] = $order_no;
  416. $data['member_id'] = $memberId;
  417. $data['fee'] = $amount * 0.002 + 2;
  418. $amount = number_format($amount, 2, '.', '');
  419. $data['amount'] = $amount;
  420. $data['channel'] = $channel;
  421. $data['bank_name'] = $bank_name;
  422. $data['account'] = $account;
  423. $data['card_no'] = $card_no;
  424. $data['callback_url'] = QianBaoService::getNotifyUrl();
  425. $data['status'] = self::STATUS_STAY;
  426. $data['remark'] = '提现费率:0.2%+2';
  427. // 先预扣款(锁定资金)
  428. $wallet->available_balance = $available_balance;
  429. if (!$wallet->save()) {
  430. DB::rollBack();
  431. $result['text'] = '钱包更新失败!';
  432. return $result;
  433. }
  434. // 创建待处理状态的提现记录
  435. $info = static::$MODEL::create($data);
  436. $id = $info->id;
  437. // 记录余额变动日志
  438. BalanceLogService::addLog($memberId, $default_amount * -1, $balance, $available_balance, '三方提现', $id, '钱宝提现费率:0.2%+2');
  439. // 提交事务,确保预扣款成功
  440. DB::commit();
  441. } //
  442. catch (Exception $e) {
  443. // 预扣款失败,回滚事务
  444. DB::rollBack();
  445. $result['text'] = '系统繁忙,请稍后重试!';
  446. Log::error('提现预扣款失败: ' . $e->getMessage(), [
  447. 'member_id' => $memberId,
  448. 'amount' => $amount
  449. ]);
  450. return $result;
  451. }
  452. // 调用三方支付接口(在事务外)
  453. $ret = QianBaoService::payout($amount, $order_no, $bank_name, $account, $card_no);
  454. Log::error('第三方代付接口调用:' . json_encode($ret, JSON_UNESCAPED_UNICODE));
  455. if ($ret['code'] == 200) {
  456. // 更新提现记录状态为处理中
  457. DB::beginTransaction();
  458. try {
  459. $info->status = self::STATUS_PROCESS;
  460. $info->save();
  461. DB::commit();
  462. $text = "✅ 提现申请已提交!\n\n";
  463. $text .= "钱包余额:{$available_balance} RMB\n";
  464. $text .= "提现金额:{$default_amount} RMB\n";
  465. $text .= "⌛️请等待系统处理, 到账时间可能需要几分钟!\n";
  466. $result['text'] = $text;
  467. } catch (Exception $e) {
  468. DB::rollBack();
  469. // 状态更新失败,但资金已扣款且三方支付已成功
  470. // 这里可以记录告警,需要人工干预
  471. Log::error('提现状态更新失败: ' . $e->getMessage(), [
  472. 'member_id' => $memberId,
  473. 'order_no' => $order_no
  474. ]);
  475. $result['text'] = '提现申请已提交,系统处理中...';
  476. }
  477. } //
  478. else {
  479. // 三方支付失败,需要回滚之前的预扣款
  480. DB::beginTransaction();
  481. try {
  482. // 恢复钱包余额
  483. $wallet->available_balance = $balance;
  484. $wallet->save();
  485. // 更新提现记录状态为失败
  486. $info->status = self::STATUS_FAIL;
  487. $info->remark = $ret['msg'];
  488. $info->save();
  489. // 记录退款日志
  490. BalanceLogService::addLog($memberId, $default_amount, $available_balance, $balance, '三方提现', $id, '提现失败退款');
  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 = static::$MODEL::where(['order_no' => $params['orderNo']])->update($data);
  554. if ($res) {
  555. $text = "✅ 提现通知 \n";
  556. $text .= "提现平台:{$info->bank_name} \n";
  557. $text .= "收款人:{$info->account} \n";
  558. $text .= "收款卡号:{$info->card_no} \n";
  559. $text .= "提现金额:{$info->amount} \n";
  560. $text .= "提现成功,金额已到账,请注意查收!";
  561. self::sendMessage($chat_id, $text);
  562. }
  563. } else {
  564. $data['status'] = self::STATUS_FAIL;
  565. $res = static::$MODEL::where(['order_no' => $params['orderNo']])->update($data);
  566. $wallet = WalletService::findOne(['member_id' => $info->member_id]);
  567. $balance = $wallet->available_balance; // 钱包当前余额
  568. $available_balance = bcadd($balance, $params['amount'], 10);
  569. $wallet->available_balance = $available_balance;
  570. $wallet->save();
  571. // 记录退款日志
  572. BalanceLogService::addLog($memberId, $amount, $balance, $available_balance, '三方提现', $info->id, '提现失败退款');
  573. if ($res) {
  574. $text = "❌ 提现通知 \n";
  575. $text .= "提现平台:{$info->bank_name} \n";
  576. $text .= "收款人:{$info->account} \n";
  577. $text .= "收款卡号:{$info->card_no} \n";
  578. $text .= "提现金额:{$info->amount} \n";
  579. $text .= "提现失败,金额已返回钱包,请注意查收!";
  580. self::sendMessage($chat_id, $text);
  581. }
  582. }
  583. DB::commit();
  584. } catch (Exception $e) {
  585. DB::rollBack();
  586. // 回滚失败,需要记录告警,人工干预
  587. Log::error('提现失败回滚异常: ' . $e->getMessage(), $params);
  588. }
  589. }
  590. /**
  591. * @description: 查询支付订单
  592. * @param {*} $id
  593. * @return {*}
  594. */
  595. public static function singlePayOrder($id)
  596. {
  597. $msg = [];
  598. $msg['code'] = self::NOT;
  599. $info = self::findOne(['id' => $id]);
  600. if ($info && $info->status == self::STATUS_PROCESS) {
  601. $ret = SanJinService::queryOrder($info->order_no);
  602. Log::error('三斤支付查询订单:', $ret);
  603. if ($ret['code'] == 0) {
  604. $item = [];
  605. $item['state'] = $ret['data']['state'];
  606. if ($ret['data']['state'] == 1) {
  607. $item['status'] = self::STATUS_SUCCESS;
  608. $info->update($item);
  609. $wallet = WalletService::findOne(['member_id' => $info->member_id]);
  610. $balance = $wallet->available_balance;
  611. $available_balance = bcadd($balance, $info->amount, 10);
  612. $wallet->available_balance = $available_balance;
  613. $wallet->save();
  614. // 记录余额变动日志
  615. BalanceLogService::addLog($info->member_id, $info->amount, $balance, $available_balance, '三方充值', $info->id, '');
  616. $msg['code'] = self::YES;
  617. $msg['msg'] = '支付成功';
  618. } else {
  619. $msg['msg'] = '支付中';
  620. }
  621. } else {
  622. $msg['msg'] = '查询失败:' . $ret['message'];
  623. }
  624. } else {
  625. $msg['msg'] = '该状态无法查询';
  626. }
  627. return $msg;
  628. }
  629. public static function syncPayOrder()
  630. {
  631. $list = static::$MODEL::where('state', 0)->where('type', self::TYPE_PAY)->take(100)->get();
  632. // foreach($list->toArray() as $k => $v){
  633. // $item= [];
  634. // if($v['status'] == self::STATUS_SUCCESS){
  635. // $item['state'] = 1;
  636. // static::$MODEL::where(['id'=>$v['id']])->update($item);
  637. // }else{
  638. // $ret = SanJinService::queryOrder($v['order_no']);
  639. // var_dump($ret);
  640. // if($ret['code'] == 0){
  641. // $item['state'] = $ret['data']['state'];
  642. // static::$MODEL::where(['id'=>$v['id']])->update($item);
  643. // }
  644. // }
  645. // }
  646. }
  647. }