PaymentOrderService.php 27 KB

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