PaymentOrderService.php 28 KB

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