PaymentOrderService.php 27 KB

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