PaymentOrderService.php 28 KB

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