PaymentOrderService.php 27 KB

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