PaymentOrderService.php 28 KB

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