PaymentOrderService.php 28 KB

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