PaymentOrderService.php 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688
  1. <?php
  2. namespace App\Services;
  3. use App\Constants\HttpStatus;
  4. use App\Models\PaymentOrder;
  5. use App\Models\User;
  6. use App\Models\Wallet as WalletModel;
  7. use Exception;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Log;
  10. use App\Services\Payment\JdPayService;
  11. use App\Services\Payment\NoPayService;
  12. use App\Services\Payment\QianBaoService;
  13. use App\Services\Payment\SanJinService;
  14. use App\Services\Payment\ZimuPayService;
  15. use App\Services\ConfigService;
  16. /**
  17. * 投注
  18. */
  19. class PaymentOrderService extends BaseService
  20. {
  21. const TYPE_PAY = 1; // 代收
  22. const TYPE_PAYOUT = 2; // 代付
  23. const TYPE_SELF_PAY = 3; // 手动充值
  24. const TYPE_SELF_PAYOUT = 4; // 手动提现打款
  25. const STATUS_STAY = 0; // 待处理
  26. const STATUS_PROCESS = 1; // 处理中
  27. const STATUS_SUCCESS = 2; // 成功
  28. const STATUS_FAIL = 3; // 失败
  29. const STATUS_USER = 4; // 待用户提交凭证
  30. const STATUS_AUDIT = 5; //待人工审核
  31. const STATUS_CANCEL = 6; //已撤回
  32. public static string $MODEL = PaymentOrder::class;
  33. /**
  34. * @description: 模型
  35. * @return {string}
  36. */
  37. public static function model(): string
  38. {
  39. return PaymentOrder::class;
  40. }
  41. /**
  42. * @description: 枚举
  43. * @return {*}
  44. */
  45. public static function enum(): string
  46. {
  47. return '';
  48. }
  49. /**
  50. * @description: 获取查询条件
  51. * @param {array} $search 查询内容
  52. * @return {array}
  53. */
  54. public static function getWhere(array $search = []): array
  55. {
  56. $where = [];
  57. if (isset($search['member_id']) && !empty($search['member_id'])) {
  58. $where[] = ['payment_orders.member_id', '=', $search['member_id']];
  59. }
  60. if (isset($search['channel']) && !empty($search['channel'])) {
  61. if ($search['channel'] == 'rgcz') {
  62. $search['type'] = 3;
  63. }
  64. $where[] = ['payment_orders.channel', '=', $search['channel']];
  65. }
  66. if (isset($search['type']) && !empty($search['type'])) {
  67. $where[] = ['payment_orders.type', '=', $search['type']];
  68. }
  69. if (isset($search['order_no']) && !empty($search['order_no'])) {
  70. $where[] = ['payment_orders.order_no', '=', $search['order_no']];
  71. }
  72. if (isset($search['id']) && !empty($search['id'])) {
  73. $where[] = ['payment_orders.id', '=', $search['id']];
  74. }
  75. if (isset($search['status']) && $search['status'] != '') {
  76. $where[] = ['payment_orders.status', '=', $search['status']];
  77. }
  78. if (isset($search['first_name']) && !empty($search['first_name'])) {
  79. $where[] = ['users.first_name', 'like', "%" . $search['first_name'] . "%"];
  80. }
  81. if (isset($search['payment_type']) && $search['payment_type'] != '') {
  82. $where[] = ['payment_orders.payment_type', '=', $search['payment_type']];
  83. }
  84. return $where;
  85. }
  86. /**
  87. * @description: 查询单条数据
  88. * @param array $search
  89. * @return \App\Models\Coin|null
  90. */
  91. public static function findOne(array $search): ?PaymentOrder
  92. {
  93. return static::$MODEL::where(self::getWhere($search))->first();
  94. }
  95. /**
  96. * @description: 查询所有数据
  97. * @param array $search
  98. * @return \Illuminate\Database\Eloquent\Collection
  99. */
  100. public static function findAll(array $search = [])
  101. {
  102. return static::$MODEL::where(self::getWhere($search))->get();
  103. }
  104. /**
  105. * @description: 分页查询
  106. * @param array $search
  107. * @return array
  108. */
  109. public static function paginate(array $search = [])
  110. {
  111. $limit = isset($search['limit']) ? $search['limit'] : 15;
  112. $paginator = static::$MODEL::where(self::getWhere($search))
  113. ->join('users', 'users.member_id', '=', 'payment_orders.member_id')
  114. ->select("payment_orders.*", "users.first_name","users.admin_note as user_admin_note") // 提前查好需要的字段
  115. ->orderByDesc('payment_orders.created_at')
  116. ->paginate($limit);
  117. $totalAmount = 0;
  118. $totalSuccess = 0;
  119. $totalFail = 0;
  120. $list = $paginator->items();
  121. foreach ($list as $item) {
  122. $item['amount'] = floatval($item['amount']);
  123. $totalAmount += $item['amount'];
  124. if (in_array($item['status'], [1, 2])) $totalSuccess += $item['amount'];
  125. if ($item['status'] == 3) $totalFail += $item['amount'];
  126. }
  127. return [
  128. 'total' => $paginator->total(),
  129. 'total_amount' => $totalAmount,
  130. 'total_success' => $totalSuccess,
  131. 'total_fail' => $totalFail,
  132. 'data' => $list
  133. ];
  134. }
  135. /**
  136. * @description:
  137. * @param {*} $params
  138. * @return {*}
  139. */
  140. public static function submit($params = [])
  141. {
  142. $result = false;
  143. $msg['code'] = self::NOT;
  144. $msg['msg'] = '';
  145. // 2. 判断是否是更新
  146. if (!empty($params['id'])) {
  147. // 更新
  148. $info = self::findOne(['id' => $params['id']]);
  149. if (!$info) {
  150. $msg['msg'] = '数据不存在!';
  151. } else {
  152. $result = $info->update($params);
  153. $id = $params['id'];
  154. }
  155. } else {
  156. // 创建
  157. $result = $info = static::$MODEL::create($params);
  158. $id = $result->id;
  159. }
  160. if ($result) {
  161. $msg['code'] = self::YES;
  162. $msg['msg'] = '设置成功';
  163. $msg['key'] = $id;
  164. } else {
  165. $msg['msg'] = empty($msg['msg']) ? '操作失败' : $msg['msg'];
  166. }
  167. return $msg;
  168. }
  169. /**
  170. * @description: 创建代收订单
  171. * @param {*} $memberId
  172. * @param {*} $amount
  173. * @param {*} $paymentType 支付类型:支付宝、数字人民币
  174. * @return {*}
  175. */
  176. public static function createPay($memberId, $amount, $paymentType)
  177. {
  178. $result = [];
  179. $result['chat_id'] = $memberId;
  180. $result['code'] = 0;
  181. $result['url'] = '';
  182. $channel = ''; // 支付的通道
  183. $product = SanJinService::product();
  184. $max = 0;
  185. $min = 0;
  186. $rate = 0;
  187. $selectedProduct = null;
  188. $geText = '';
  189. foreach ($product as $k => $v) {
  190. if ($v['type'] == $paymentType) {
  191. $selectedProduct = $v;
  192. if ($v['type'] == 'zfbge') {
  193. if (in_array($amount, $v['fixed'])) {
  194. $channel = $k;
  195. } else {
  196. $geText .= "❌ 此充值通道固定充值金额为" . implode(',', $v['fixed']) . "请务必输入区间金额!";
  197. }
  198. } else {
  199. if ($amount >= $v['min'] && $amount <= $v['max']) {
  200. $channel = $k;
  201. $rate = $v['rate'];
  202. }
  203. if ($min == 0) {
  204. $min = $v['min'];
  205. }
  206. if ($max == 0) {
  207. $max = $v['max'];
  208. }
  209. if ($min > $v['min']) {
  210. $min = $v['min'];
  211. }
  212. if ($max < $v['max']) {
  213. $max = $v['max'];
  214. }
  215. }
  216. }
  217. }
  218. // 没有找到支付通道
  219. if (empty($channel) && !JdPayService::isChannel($paymentType) && !NoPayService::isRechargeChannel($paymentType) && !ZimuPayService::isRechargeChannel($paymentType)) {
  220. // $text = "发起充值失败 \n";
  221. // $text .= "最低充值:" . $min . " \n";
  222. // $text .= "最高充值:" . $max . " \n";
  223. // $text .= "请重新填写充值的金额!";
  224. $text = "❌ 此充值通道充值金额{$min}-{$max}请务必输入区间金额!";
  225. $result['text'] = $text;
  226. if ($geText) {
  227. $result['text'] = $geText;
  228. }
  229. $result['code'] = 20001;
  230. return $result;
  231. }
  232. if (ZimuPayService::isRechargeChannel($paymentType) || ZimuPayService::isRechargeChannel($channel)) {
  233. $channel = ZimuPayService::CHANNEL_RECHARGE;
  234. $bankName = $selectedProduct['name'] ?? '808充值';
  235. $data = [];
  236. $data['type'] = self::TYPE_PAY;
  237. $data['member_id'] = $memberId;
  238. $data['amount'] = ZimuPayService::amount($amount);
  239. $data['channel'] = $channel;
  240. $data['fee'] = $amount * $rate;
  241. $data['bank_name'] = $bankName;
  242. $order_no = self::createOrderNo('zm' . $data['type'] . '_', $memberId);
  243. $data['order_no'] = $order_no;
  244. $data['callback_url'] = ZimuPayService::getPayNotifyUrl();
  245. $data['remark'] = '充值费率:' . $rate;
  246. $data['status'] = self::STATUS_STAY;
  247. $ret = ZimuPayService::pay($amount, $order_no, (string)$memberId);
  248. Log::channel('payment')->info('ZIMU支付发起', [
  249. 'order_no' => $order_no,
  250. 'member_id' => $memberId,
  251. 'amount' => $data['amount'],
  252. 'response' => $ret,
  253. ]);
  254. if (($ret['code'] ?? -1) == 0) {
  255. $item = $ret['data'] ?? [];
  256. $payUrl = $item['payUrl'] ?? '';
  257. $data['status'] = self::STATUS_PROCESS;
  258. $data['pay_no'] = $item['orderNo'] ?? '';
  259. $data['pay_url'] = $payUrl;
  260. $data['pay_data'] = json_encode($ret, JSON_UNESCAPED_UNICODE);
  261. static::$MODEL::create($data);
  262. if ($payUrl) {
  263. $result['image'] = asset(self::createPaymentQrCode($payUrl));
  264. }
  265. $result['text'] = "{$bankName}确认 \n";
  266. $result['text'] .= "请使用浏览器扫码或者复制支付地址到浏览器打开 \n";
  267. $result['text'] .= "支付地址:{$payUrl}\n";
  268. $result['text'] .= "支付金额:" . $amount . " RMB \n";
  269. $result['text'] .= "支付完成后请耐心等待,支付到账会第一时间通知您! \n";
  270. $result['url'] = $payUrl;
  271. } else {
  272. $result['text'] = $ret['msg'] ?? 'ZIMU支付发起失败';
  273. $result['code'] = 20002;
  274. }
  275. return $result;
  276. }
  277. if (NoPayService::isRechargeChannel($paymentType) || NoPayService::isRechargeChannel($channel)) {
  278. $channel = NoPayService::isRechargeChannel($paymentType) ? $paymentType : $channel;
  279. $bankName = $selectedProduct['name'] ?? 'NO快捷充值';
  280. $data = [];
  281. $data['type'] = self::TYPE_PAY;
  282. $data['member_id'] = $memberId;
  283. $data['amount'] = NoPayService::amount($amount);
  284. $data['channel'] = $channel;
  285. $data['fee'] = $amount * $rate;
  286. $data['bank_name'] = $bankName;
  287. $order_no = self::createOrderNo('no' . $data['type'] . '_', $memberId);
  288. $data['order_no'] = $order_no;
  289. $data['callback_url'] = NoPayService::getDepositNotifyUrl();
  290. $data['remark'] = '充值费率:' . $rate;
  291. $data['status'] = self::STATUS_STAY;
  292. $ret = NoPayService::pay($amount, $order_no, (string)$memberId, $channel);
  293. Log::channel('payment')->info('NO支付发起', [
  294. 'order_no' => $order_no,
  295. 'member_id' => $memberId,
  296. 'amount' => $data['amount'],
  297. 'channel' => $channel,
  298. 'response' => $ret,
  299. ]);
  300. if (($ret['code'] ?? -1) == 0) {
  301. $payUrl = $ret['data']['url'] ?? '';
  302. $data['status'] = self::STATUS_PROCESS;
  303. $data['pay_url'] = $payUrl;
  304. $data['pay_data'] = json_encode($ret, JSON_UNESCAPED_UNICODE);
  305. static::$MODEL::create($data);
  306. if ($payUrl) {
  307. $result['image'] = asset(self::createPaymentQrCode($payUrl));
  308. }
  309. $result['text'] = "{$bankName}充值确认 \n";
  310. $result['text'] .= "请使用浏览器扫码或者复制支付地址到浏览器打开 \n";
  311. $result['text'] .= "支付地址:{$payUrl}\n";
  312. $result['text'] .= "支付金额:" . $amount . " RMB \n";
  313. $result['text'] .= "支付完成后请耐心等待,支付到账会第一时间通知您! \n";
  314. $result['url'] = $payUrl;
  315. } else {
  316. $result['text'] = $ret['msg'] ?? 'NO支付发起失败';
  317. $result['code'] = 20002;
  318. }
  319. return $result;
  320. }
  321. if (JdPayService::isChannel($paymentType) || JdPayService::isChannel($channel)) {
  322. $channel = JdPayService::CHANNEL;
  323. $bankName = $selectedProduct['name'] ?? 'JD钱包';
  324. $data = [];
  325. $data['type'] = self::TYPE_PAY;
  326. $data['member_id'] = $memberId;
  327. $data['amount'] = JdPayService::amount($amount);
  328. $data['channel'] = $channel;
  329. $data['fee'] = $amount * $rate;
  330. $data['bank_name'] = $bankName;
  331. $order_no = self::createOrderNo('jd' . $data['type'] . '_', $memberId);
  332. $data['order_no'] = $order_no;
  333. $data['callback_url'] = JdPayService::getNotifyUrl();
  334. $data['remark'] = '充值费率:' . $rate;
  335. $data['status'] = self::STATUS_STAY;
  336. $ret = JdPayService::pay($amount, $order_no);
  337. Log::channel('payment')->info('JD支付发起', $ret);
  338. if (($ret['code'] ?? 0) == 200) {
  339. $item = $ret['data'] ?? [];
  340. $payUrl = $item['url'] ?? '';
  341. $data['status'] = self::STATUS_PROCESS;
  342. $data['pay_no'] = $item['orderNo'] ?? '';
  343. $data['pay_url'] = $payUrl;
  344. $data['pay_data'] = json_encode($ret, JSON_UNESCAPED_UNICODE);
  345. static::$MODEL::create($data);
  346. if ($payUrl) {
  347. $result['image'] = asset(self::createPaymentQrCode($payUrl));
  348. }
  349. $text = "{$bankName}充值确认 \n";
  350. $text .= "请使用浏览器扫码或者复制支付地址到浏览器打开 \n";
  351. $text .= "支付地址:{$payUrl}\n";
  352. $text .= "支付金额:" . $amount . " RMB \n";
  353. $text .= "请按实际支付金额进行付款,否则影响到账 \n";
  354. $text .= "支付完成后请耐心等待,支付到账会第一时间通知您! \n";
  355. $result['text'] = $text;
  356. $result['url'] = $payUrl;
  357. } else {
  358. $result['text'] = $ret['message'] ?? 'JD支付发起失败';
  359. $result['code'] = 20002;
  360. }
  361. return $result;
  362. }
  363. $data = [];
  364. $data['type'] = self::TYPE_PAY;
  365. $data['member_id'] = $memberId;
  366. $data['amount'] = $amount;
  367. $data['channel'] = $channel;
  368. $data['fee'] = $amount * $rate;
  369. $data['bank_name'] = SanJinService::getChannel($paymentType) ?? '';
  370. $order_no = self::createOrderNo('sj' . $data['type'] . '_', $memberId);
  371. $data['order_no'] = $order_no;
  372. $data['callback_url'] = SanJinService::getNotifyUrl();
  373. $data['remark'] = '充值费率:' . $rate;
  374. $data['status'] = self::STATUS_STAY;
  375. $ret = SanJinService::pay(($amount * 100), $order_no, $channel);
  376. Log::error('三斤支付发起:', $ret);
  377. if ($ret['code'] == 0) {
  378. $qrCode = asset(self::createPaymentQrCode($ret['data']['payUrl']));
  379. $result['image'] = $qrCode;
  380. $item = $ret['data'];
  381. $data['status'] = self::STATUS_PROCESS;
  382. $data['pay_no'] = $item['tradeNo'];
  383. $data['pay_url'] = $item['payUrl'];
  384. $data['pay_data'] = json_encode($ret, JSON_UNESCAPED_UNICODE);
  385. $info = static::$MODEL::create($data);
  386. // $text = "✅ 支付提示 \n";
  387. $text = "{$data['bank_name']}充值确认 \n";
  388. // $text .= "支付方式:{$data['bank_name']} \n";
  389. $text .= "请使用浏览器扫码或者复制支付地址到浏览器打开 \n";
  390. $text .= "支付地址:{$ret['data']['payUrl']}\n";
  391. $text .= "支付金额:" . $amount . " RMB \n";
  392. $text .= "请按实际支付金额进行付款,否则影响到账 \n";
  393. $text .= "支付完成后请耐心等待,支付到账会第一时间通知您! \n";
  394. $result['text'] = $text;
  395. $result['url'] = $ret['data']['payUrl'];
  396. } else {
  397. $result['text'] = $ret['message'];
  398. $result['code'] = 20002;
  399. }
  400. return $result;
  401. }
  402. /**
  403. * @description: 接收支付的通知
  404. * @param {*} $params
  405. * @return {*}
  406. */
  407. public static function receivePay($params)
  408. {
  409. if (ZimuPayService::getAppId() !== '' && ($params['appId'] ?? '') === ZimuPayService::getAppId()) {
  410. $info = self::findOne(['order_no' => $params['mchOrderNo'] ?? '']);
  411. if (!$info || $info->type != self::TYPE_PAY || !ZimuPayService::isRechargeChannel($info->channel)) {
  412. Log::error('ZIMU充值回调订单不存在或类型不匹配', [
  413. 'mch_order_no' => $params['mchOrderNo'] ?? '',
  414. 'order_id' => $info->id ?? null,
  415. 'order_type' => $info->type ?? null,
  416. 'channel' => $info->channel ?? null,
  417. ]);
  418. return false;
  419. }
  420. if (!ZimuPayService::verifyNotify($params)) {
  421. Log::error('ZIMU充值回调验签失败', [
  422. 'mch_order_no' => $params['mchOrderNo'] ?? '',
  423. 'app_id' => $params['appId'] ?? '',
  424. 'signature' => ZimuPayService::notifySignatureDiagnostics($params),
  425. ]);
  426. return false;
  427. }
  428. if (bccomp(ZimuPayService::amount($info->amount), ZimuPayService::amount($params['amount'] ?? 0), 2) !== 0) {
  429. Log::error('ZIMU充值回调金额不一致', [
  430. 'mch_order_no' => $params['mchOrderNo'] ?? '',
  431. 'order_amount' => $info->amount,
  432. 'callback_amount' => $params['amount'] ?? null,
  433. ]);
  434. return false;
  435. }
  436. if ($info->status != self::STATUS_PROCESS) {
  437. return true;
  438. }
  439. $processed = self::applyPayCallback(
  440. $info,
  441. ZimuPayService::amount($info->amount),
  442. (string)$params['status'],
  443. ZimuPayService::PAY_STATUS_SUCCESS,
  444. ZimuPayService::PAY_STATUS_FAIL,
  445. $params
  446. );
  447. if ($processed && (string)$params['status'] === ZimuPayService::PAY_STATUS_SUCCESS) {
  448. self::notifyUser($info->member_id, "✅ 支付成功 \n充值金额:{$info->amount} RMB \n订单号:{$info->order_no} \n您充值的金额已到账,请注意查收!");
  449. }
  450. return true;
  451. }
  452. if (NoPayService::getDepositMerchantId() !== '' && ($params['appId'] ?? '') === NoPayService::getDepositMerchantId()) {
  453. $info = self::findOne(['order_no' => $params['merchantOrderNo'] ?? '']);
  454. if (!$info || $info->type != self::TYPE_PAY || !NoPayService::isRechargeChannel($info->channel)) {
  455. Log::error('NO钱包充值回调订单不存在或类型不匹配', [
  456. 'merchant_order_no' => $params['merchantOrderNo'] ?? '',
  457. 'order_id' => $info->id ?? null,
  458. 'order_type' => $info->type ?? null,
  459. 'channel' => $info->channel ?? null,
  460. ]);
  461. return false;
  462. }
  463. if (!NoPayService::verifyDepositNotify($params)) {
  464. Log::error('NO钱包充值回调验签失败', [
  465. 'merchant_order_no' => $params['merchantOrderNo'] ?? '',
  466. 'app_id' => $params['appId'] ?? '',
  467. 'signature' => NoPayService::depositNotifySignatureDiagnostics($params),
  468. ]);
  469. return false;
  470. }
  471. if (bccomp(NoPayService::amount($info->amount), NoPayService::amount($params['amount'] ?? 0), 2) !== 0) {
  472. Log::error('NO钱包充值回调金额不一致', [
  473. 'merchant_order_no' => $params['merchantOrderNo'] ?? '',
  474. 'order_amount' => $info->amount,
  475. 'callback_amount' => $params['amount'] ?? null,
  476. ]);
  477. return false;
  478. }
  479. if ($info->status != self::STATUS_PROCESS) {
  480. return true;
  481. }
  482. $processed = self::applyPayCallback(
  483. $info,
  484. NoPayService::amount($info->amount),
  485. (string)$params['state'],
  486. NoPayService::STATE_SUCCESS,
  487. NoPayService::STATE_FAIL,
  488. $params
  489. );
  490. if ($processed && (string)$params['state'] === NoPayService::STATE_SUCCESS) {
  491. self::notifyUser($info->member_id, "✅ 支付成功 \n充值金额:{$info->amount} RMB \n订单号:{$info->order_no} \n您充值的金额已到账,请注意查收!");
  492. }
  493. return true;
  494. }
  495. if (($params['userCode'] ?? '') === JdPayService::getMerchantId()) {
  496. $info = self::findOne(['order_no' => $params['orderCode'] ?? ''])
  497. ?: static::$MODEL::where('pay_no', $params['orderCode'] ?? '')->first();
  498. if (!$info || $info->type != self::TYPE_PAY) {
  499. return false;
  500. }
  501. if (!JdPayService::verifyPayNotify($params)) {
  502. return false;
  503. }
  504. if (bccomp(JdPayService::amount($info->amount), JdPayService::amount($params['amount'] ?? 0), 2) !== 0) {
  505. return false;
  506. }
  507. if ($info->status != self::STATUS_PROCESS) {
  508. return true;
  509. }
  510. $processed = self::applyPayCallback($info, JdPayService::amount($info->amount), (string)$params['status'], JdPayService::PAY_STATUS_SUCCESS, JdPayService::PAY_STATUS_FAIL, $params);
  511. if ($processed && (string)$params['status'] === JdPayService::PAY_STATUS_SUCCESS) {
  512. $text = "✅ 支付成功 \n";
  513. $text .= "充值金额:{$info->amount} RMB \n";
  514. $text .= "订单号:{$info->order_no} \n";
  515. $text .= "您充值的金额已到账,请注意查收!";
  516. self::notifyUser($info->member_id, $text);
  517. }
  518. return true;
  519. }
  520. // 判断商户号
  521. if (($params['mchId'] ?? '') == SanJinService::getMerchantId()) {
  522. $must = ['mchId', 'productId', 'tradeNo', 'outTradeNo', 'amount', 'payAmount', 'state', 'createTime', 'payTime'];
  523. $info = self::findOne(['order_no' => $params['outTradeNo']]);
  524. if ($info) {
  525. // 平台以分计算转成元
  526. $payAmount = $params['payAmount'] / 100;
  527. // 判断金额是不是正确认
  528. if ($info->amount != $payAmount) {
  529. $text = '❌ 支付失败提醒 \n';
  530. $text .= "订单金额:{$info->amount} \n";
  531. $text .= "实际支付:{$payAmount} \n";
  532. $text .= "订单号:{$params['outTradeNo']} \n";
  533. $text .= "失败原因:支付金额与订单金额不一致 \n";
  534. $text .= "请联系客服处理!";
  535. self::notifyUser($info->member_id, $text);
  536. return false;
  537. }
  538. if ($params['sign'] != SanJinService::signature($params, $must)) {
  539. return false;
  540. }
  541. if ($info->status != self::STATUS_PROCESS) {
  542. return false;
  543. }
  544. // 付款
  545. if ($info->type == self::TYPE_PAY) {
  546. $processed = self::applyPayCallback($info, $payAmount, (string)$params['state'], '1', null, $params);
  547. if ($processed && $params['state'] == 1) {
  548. $text = "✅ 支付成功 \n";
  549. $text .= "充值金额:{$payAmount} RMB \n";
  550. $text .= "订单号:{$params['outTradeNo']} \n";
  551. $text .= "您充值的金额已到账,请注意查收!";
  552. self::notifyUser($info->member_id, $text);
  553. } else {
  554. $text = "❌ 支付失败 \n";
  555. $text .= "充值金额:{$payAmount} RMB \n";
  556. $text .= "订单号:{$params['outTradeNo']} \n";
  557. }
  558. return true;
  559. }
  560. }
  561. }
  562. }
  563. // 充值笔笔返
  564. public static function rechargesBibiReturn($memberId,$payAmount,$info_id)
  565. {
  566. $rate = ConfigService::getVal('recharges_bibi_return');
  567. $amount = $payAmount * $rate;
  568. if($rate > 0){
  569. $wallet = WalletService::findOne(['member_id' => $memberId]);
  570. $balance = $wallet->available_balance;
  571. $available_balance = bcadd($balance, $amount, 10);
  572. $wallet->available_balance = $available_balance;
  573. $wallet->save();
  574. BalanceLogService::addLog($memberId, $amount, $balance, $available_balance, '优惠活动', $info_id, '充值笔笔返');
  575. }
  576. }
  577. private static function applyPayCallback(PaymentOrder $info, $payAmount, string $status, string $successStatus, ?string $failStatus, array $params): bool
  578. {
  579. return DB::transaction(function () use ($info, $payAmount, $status, $successStatus, $failStatus, $params) {
  580. $order = PaymentOrder::where('id', $info->id)->lockForUpdate()->first();
  581. if (!$order || $order->status != self::STATUS_PROCESS) {
  582. return false;
  583. }
  584. $order->callback_data = json_encode($params, JSON_UNESCAPED_UNICODE);
  585. $order->state = $status;
  586. if ($status === $successStatus) {
  587. $order->status = self::STATUS_SUCCESS;
  588. $order->save();
  589. $wallet = WalletModel::where('member_id', $order->member_id)->lockForUpdate()->first();
  590. if (!$wallet) {
  591. throw new Exception('钱包不存在', HttpStatus::CUSTOM_ERROR);
  592. }
  593. $balance = $wallet->available_balance;
  594. $availableBalance = bcadd($balance, $payAmount, 10);
  595. $wallet->available_balance = $availableBalance;
  596. $wallet->save();
  597. BalanceLogService::addLog($order->member_id, $payAmount, $balance, $availableBalance, '三方充值', $order->id, '');
  598. $rate = ConfigService::getVal('recharges_bibi_return');
  599. $bonusAmount = $payAmount * $rate;
  600. if ($rate > 0 && $bonusAmount > 0) {
  601. $bonusBeforeBalance = $wallet->available_balance;
  602. $bonusAfterBalance = bcadd($bonusBeforeBalance, $bonusAmount, 10);
  603. $wallet->available_balance = $bonusAfterBalance;
  604. $wallet->save();
  605. BalanceLogService::addLog($order->member_id, $bonusAmount, $bonusBeforeBalance, $bonusAfterBalance, '优惠活动', $order->id, '充值笔笔返');
  606. }
  607. return true;
  608. }
  609. if ($failStatus === null || $status === $failStatus) {
  610. $order->status = self::STATUS_FAIL;
  611. }
  612. $order->save();
  613. return true;
  614. }, 3);
  615. }
  616. /**
  617. * 拒绝提现
  618. * @description 改变订单状态为失败,将金额退给用户,并记录提现退款的资金日
  619. * @param int $orderId 订单ID PaymentOrder 表的ID
  620. * @param string $remark 失败原因
  621. * @return array
  622. */
  623. public static function withdrawalFailed($orderId, $remark): array
  624. {
  625. try {
  626. $order = PaymentOrder::where('id', $orderId)
  627. ->whereIn('type', [2,4])
  628. ->where('status', self::STATUS_STAY)
  629. ->first();
  630. if (!$order) throw new Exception("订单不存在_{$orderId}", HttpStatus::CUSTOM_ERROR);
  631. // 更新提现记录状态为失败
  632. $order->status = self::STATUS_FAIL;
  633. $order->remark = $remark;
  634. if (!$order->save()) {
  635. throw new Exception("更新失败,请重试", HttpStatus::CUSTOM_ERROR);
  636. }
  637. //钱包余额增加
  638. $wallet = WalletService::findOne(['member_id' => $order->member_id]);
  639. $beforeBalance = $wallet->available_balance;
  640. $availableBalance = bcadd($wallet->available_balance, $order->amount, 10);
  641. $wallet->available_balance = $availableBalance;
  642. if (!$wallet->save()) {
  643. throw new Exception("更新失败,请重试", HttpStatus::CUSTOM_ERROR);
  644. }
  645. // 记录退款日志
  646. BalanceLogService::addLog($order->member_id, $order->amount, $beforeBalance, $availableBalance, '三方提现', $order->id, '提现失败退款');
  647. } catch (Exception $e) {
  648. return ['code' => HttpStatus::CUSTOM_ERROR, 'msg' => $e->getMessage()];
  649. }
  650. return ['code' => 0, 'msg' => 'ok'];
  651. }
  652. /**
  653. * 创建代付订单 (根据 orderId 将钱转到用户指定账户)
  654. * @param int $orderId PaymentOrder 表的ID
  655. */
  656. public static function createPayout(int $orderId): array
  657. {
  658. try {
  659. $order = PaymentOrder::where('id', $orderId)
  660. ->where('type', 2)
  661. ->where('status', self::STATUS_STAY)
  662. ->first();
  663. if (!$order) throw new Exception("订单不存在_{$orderId}", HttpStatus::CUSTOM_ERROR);
  664. $amount = $order->amount;
  665. $amount = number_format($amount, 2, '.', '');
  666. if (NoPayService::isWithdrawChannel($order->channel)) {
  667. $ret = NoPayService::withdraw($amount, $order->order_no, (string)$order->member_id, (string)$order->account, (string)$order->card_no);
  668. Log::channel('payment')->info('NO提现接口调用', [
  669. 'order_id' => $order->id,
  670. 'order_no' => $order->order_no,
  671. 'member_id' => $order->member_id,
  672. 'amount' => $amount,
  673. 'q_account' => $order->card_no,
  674. 'response' => $ret,
  675. ]);
  676. if (($ret['code'] ?? -1) != 0) {
  677. throw new Exception($ret['msg'] ?? 'NO提现失败', HttpStatus::CUSTOM_ERROR);
  678. }
  679. $order->pay_no = $ret['data']['orderNo'] ?? '';
  680. $order->pay_data = json_encode($ret, JSON_UNESCAPED_UNICODE);
  681. } elseif (ZimuPayService::isPayoutChannel($order->channel)) {
  682. self::assertZimuBalanceEnough($amount, [
  683. 'order_id' => $order->id,
  684. 'order_no' => $order->order_no,
  685. 'member_id' => $order->member_id,
  686. 'channel' => $order->channel,
  687. ]);
  688. if (ZimuPayService::isCashChannel($order->channel)) {
  689. $ret = ZimuPayService::cash(
  690. $amount,
  691. $order->order_no,
  692. (string)$order->account,
  693. (string)$order->card_no,
  694. (string)$order->bank_name,
  695. ZimuPayService::cashPayMethod((string)$order->bank_name)
  696. );
  697. } else {
  698. $ret = ZimuPayService::withdraw($amount, $order->order_no, (string)$order->member_id, (string)$order->card_no, (string)$order->account);
  699. }
  700. Log::channel('payment')->info('ZIMU提现/代付接口调用', [
  701. 'order_id' => $order->id,
  702. 'order_no' => $order->order_no,
  703. 'member_id' => $order->member_id,
  704. 'amount' => $amount,
  705. 'channel' => $order->channel,
  706. 'response' => $ret,
  707. ]);
  708. if (($ret['code'] ?? -1) != 0) {
  709. throw new Exception($ret['msg'] ?? 'ZIMU提现/代付失败', HttpStatus::CUSTOM_ERROR);
  710. }
  711. $order->pay_no = $ret['data']['orderNo'] ?? '';
  712. $order->pay_data = json_encode($ret, JSON_UNESCAPED_UNICODE);
  713. } elseif (JdPayService::isChannel($order->channel)) {
  714. self::assertJdBalanceEnough($amount, [
  715. 'order_id' => $order->id,
  716. 'order_no' => $order->order_no,
  717. 'member_id' => $order->member_id,
  718. 'address' => $order->card_no,
  719. ]);
  720. $ret = JdPayService::remit($amount, $order->order_no, $order->card_no);
  721. Log::channel('payment')->info('JD下发接口调用', [
  722. 'order_id' => $order->id,
  723. 'order_no' => $order->order_no,
  724. 'member_id' => $order->member_id,
  725. 'amount' => $amount,
  726. 'address' => $order->card_no,
  727. 'response' => $ret,
  728. ]);
  729. if (($ret['code'] ?? 0) != 200) {
  730. $logContext = [
  731. 'order_id' => $order->id,
  732. 'order_no' => $order->order_no,
  733. 'member_id' => $order->member_id,
  734. 'amount' => $amount,
  735. 'address' => $order->card_no,
  736. 'response' => $ret,
  737. ];
  738. Log::channel('payment_error')->error('JD下发接口失败', $logContext);
  739. Log::error('JD下发接口失败', $logContext);
  740. throw new Exception($ret['message'] ?? 'JD下发失败', HttpStatus::CUSTOM_ERROR);
  741. }
  742. $order->pay_no = $ret['data']['orderNo'] ?? '';
  743. $order->pay_data = json_encode($ret, JSON_UNESCAPED_UNICODE);
  744. } else {
  745. $ret = QianBaoService::payout($amount, $order->order_no, $order->bank_name, $order->account, $order->card_no);
  746. Log::error('第三方代付接口调用:' . json_encode($ret, JSON_UNESCAPED_UNICODE));
  747. if ($ret['code'] != 200) throw new Exception($ret['msg'], HttpStatus::CUSTOM_ERROR);
  748. }
  749. $order->status = self::STATUS_PROCESS;
  750. $order->save();
  751. } catch (Exception $e) {
  752. Log::channel('payment_error')->error('创建代付订单失败', [
  753. 'order_id' => $orderId,
  754. 'error' => $e->getMessage(),
  755. ]);
  756. Log::error('创建代付订单失败', [
  757. 'order_id' => $orderId,
  758. 'error' => $e->getMessage(),
  759. ]);
  760. return ['code' => HttpStatus::CUSTOM_ERROR, 'msg' => $e->getMessage()];
  761. }
  762. return ['code' => 0, 'msg' => 'ok'];
  763. }
  764. /**
  765. * @description: 创建代付订单 (自动直接到账,包括用户钱包的扣款和提现记录的生成以及余额日志的创建)
  766. * @param {*} $memberId 会员ID
  767. * @param {*} $amount 代付金额
  768. * @param {*} $channel 提现通道 DF001 支付宝转卡/DF002 支付宝转支付宝
  769. * @param {*} $bank_name 银行名称/支付宝
  770. * @param {*} $account 姓名
  771. * @param {*} $card_no 银行卡号/支付宝账号
  772. * @return {*}
  773. */
  774. public static function autoCreatePayout($memberId, $amount, $channel, $bank_name, $account, $card_no)
  775. {
  776. $default_amount = $amount;
  777. $result = [];
  778. $result['chat_id'] = $memberId;
  779. if ($amount < 100) {
  780. $result['text'] = '提现金额最少100';
  781. return $result;
  782. }
  783. if ($amount > 49999) {
  784. $result['text'] = '提现金额最多49999';
  785. return $result;
  786. }
  787. // 在调用三方支付前开始事务
  788. DB::beginTransaction();
  789. try {
  790. $wallet = WalletService::findOne(['member_id' => $memberId]);
  791. if (!$wallet) {
  792. $result['text'] = '钱包不存在!';
  793. return $result;
  794. }
  795. $balance = $wallet->available_balance;
  796. if (bccomp($balance, $amount, 2) < 0) {
  797. $result['text'] = '您的钱包余额不足!';
  798. return $result;
  799. }
  800. $available_balance = bcsub($balance, $amount, 10);
  801. $data = [];
  802. $data['type'] = self::TYPE_PAYOUT;
  803. $order_no = self::createOrderNo('sj' . $data['type'] . '_', $memberId);
  804. $data['order_no'] = $order_no;
  805. $data['member_id'] = $memberId;
  806. $data['fee'] = $amount * 0.002 + 2;
  807. $amount = number_format($amount, 2, '.', '');
  808. $data['amount'] = $amount;
  809. $data['channel'] = $channel;
  810. $data['bank_name'] = $bank_name;
  811. $data['account'] = $account;
  812. $data['card_no'] = $card_no;
  813. if (NoPayService::isWithdrawChannel($channel)) {
  814. $data['callback_url'] = NoPayService::getWithdrawNotifyUrl();
  815. } elseif (JdPayService::isChannel($channel)) {
  816. $data['callback_url'] = JdPayService::getRemitNotifyUrl();
  817. } elseif (ZimuPayService::isPayoutChannel($channel)) {
  818. $data['callback_url'] = ZimuPayService::getPayoutNotifyUrl();
  819. } else {
  820. $data['callback_url'] = QianBaoService::getNotifyUrl();
  821. }
  822. $data['status'] = self::STATUS_STAY;
  823. $data['remark'] = '提现费率:0.2%+2';
  824. // 先预扣款(锁定资金)
  825. $wallet->available_balance = $available_balance;
  826. if (!$wallet->save()) {
  827. DB::rollBack();
  828. $result['text'] = '钱包更新失败!';
  829. return $result;
  830. }
  831. // 创建待处理状态的提现记录
  832. $info = static::$MODEL::create($data);
  833. $id = $info->id;
  834. // 记录余额变动日志
  835. BalanceLogService::addLog($memberId, $default_amount * -1, $balance, $available_balance, '三方提现', $id, '钱宝提现费率:0.2%+2');
  836. // 提交事务,确保预扣款成功
  837. DB::commit();
  838. } //
  839. catch (Exception $e) {
  840. // 预扣款失败,回滚事务
  841. DB::rollBack();
  842. $result['text'] = '系统繁忙,请稍后重试!';
  843. Log::error('提现预扣款失败: ' . $e->getMessage(), [
  844. 'member_id' => $memberId,
  845. 'amount' => $amount
  846. ]);
  847. return $result;
  848. }
  849. // 调用三方支付接口(在事务外)
  850. if (NoPayService::isWithdrawChannel($channel)) {
  851. try {
  852. $ret = NoPayService::withdraw($amount, $order_no, (string)$memberId, (string)$account, (string)$card_no);
  853. Log::channel('payment')->info('NO提现接口调用', [
  854. 'order_no' => $order_no,
  855. 'member_id' => $memberId,
  856. 'amount' => $amount,
  857. 'q_account' => $card_no,
  858. 'response' => $ret,
  859. ]);
  860. $success = (($ret['code'] ?? -1) == 0);
  861. $failureMessage = $ret['msg'] ?? 'NO提现失败';
  862. } catch (Exception $e) {
  863. $ret = ['msg' => $e->getMessage()];
  864. $success = false;
  865. $failureMessage = $e->getMessage();
  866. Log::channel('payment_error')->error('NO提现接口异常', [
  867. 'order_no' => $order_no,
  868. 'member_id' => $memberId,
  869. 'amount' => $amount,
  870. 'q_account' => $card_no,
  871. 'error' => $e->getMessage(),
  872. ]);
  873. }
  874. } elseif (ZimuPayService::isPayoutChannel($channel)) {
  875. try {
  876. self::assertZimuBalanceEnough($amount, [
  877. 'order_no' => $order_no,
  878. 'member_id' => $memberId,
  879. 'channel' => $channel,
  880. ]);
  881. if (ZimuPayService::isCashChannel($channel)) {
  882. $ret = ZimuPayService::cash(
  883. $amount,
  884. $order_no,
  885. (string)$account,
  886. (string)$card_no,
  887. (string)$bank_name,
  888. ZimuPayService::cashPayMethod((string)$bank_name)
  889. );
  890. } else {
  891. $ret = ZimuPayService::withdraw($amount, $order_no, (string)$memberId, (string)$card_no, (string)$account);
  892. }
  893. Log::channel('payment')->info('ZIMU提现/代付接口调用', [
  894. 'order_no' => $order_no,
  895. 'member_id' => $memberId,
  896. 'amount' => $amount,
  897. 'channel' => $channel,
  898. 'response' => $ret,
  899. ]);
  900. $success = (($ret['code'] ?? -1) == 0);
  901. $failureMessage = $ret['msg'] ?? 'ZIMU提现/代付失败';
  902. } catch (Exception $e) {
  903. $ret = ['msg' => $e->getMessage()];
  904. $success = false;
  905. $failureMessage = $e->getMessage();
  906. Log::channel('payment_error')->error('ZIMU提现/代付接口异常', [
  907. 'order_no' => $order_no,
  908. 'member_id' => $memberId,
  909. 'amount' => $amount,
  910. 'channel' => $channel,
  911. 'error' => $e->getMessage(),
  912. ]);
  913. }
  914. } elseif (JdPayService::isChannel($channel)) {
  915. try {
  916. self::assertJdBalanceEnough($amount, [
  917. 'order_no' => $order_no,
  918. 'member_id' => $memberId,
  919. 'address' => $card_no,
  920. ]);
  921. $ret = JdPayService::remit($amount, $order_no, $card_no);
  922. Log::channel('payment')->info('JD下发接口调用', [
  923. 'order_no' => $order_no,
  924. 'member_id' => $memberId,
  925. 'amount' => $amount,
  926. 'address' => $card_no,
  927. 'response' => $ret,
  928. ]);
  929. $success = (($ret['code'] ?? 0) == 200);
  930. $failureMessage = $ret['message'] ?? 'JD下发失败';
  931. if (!$success) {
  932. $logContext = [
  933. 'order_no' => $order_no,
  934. 'member_id' => $memberId,
  935. 'amount' => $amount,
  936. 'address' => $card_no,
  937. 'response' => $ret,
  938. ];
  939. Log::channel('payment_error')->error('JD下发接口失败', $logContext);
  940. Log::error('JD下发接口失败', $logContext);
  941. }
  942. } catch (Exception $e) {
  943. $ret = ['message' => $e->getMessage()];
  944. $success = false;
  945. $failureMessage = $e->getMessage();
  946. $logContext = [
  947. 'order_no' => $order_no,
  948. 'member_id' => $memberId,
  949. 'amount' => $amount,
  950. 'address' => $card_no,
  951. 'error' => $e->getMessage(),
  952. ];
  953. Log::channel('payment_error')->error('JD下发接口异常', $logContext);
  954. Log::error('JD下发接口异常', $logContext);
  955. }
  956. } else {
  957. $ret = QianBaoService::payout($amount, $order_no, $bank_name, $account, $card_no);
  958. Log::error('第三方代付接口调用:' . json_encode($ret, JSON_UNESCAPED_UNICODE));
  959. $success = (($ret['code'] ?? 0) == 200);
  960. $failureMessage = $ret['msg'] ?? '代付失败';
  961. }
  962. if ($success) {
  963. // 更新提现记录状态为处理中
  964. DB::beginTransaction();
  965. try {
  966. $info->status = self::STATUS_PROCESS;
  967. if (NoPayService::isWithdrawChannel($channel) || JdPayService::isChannel($channel) || ZimuPayService::isPayoutChannel($channel)) {
  968. $info->pay_no = $ret['data']['orderNo'] ?? '';
  969. $info->pay_data = json_encode($ret, JSON_UNESCAPED_UNICODE);
  970. }
  971. $info->save();
  972. DB::commit();
  973. $text = "✅ 提现申请已提交!\n\n";
  974. $text .= "钱包余额:{$available_balance} RMB\n";
  975. $text .= "提现金额:{$default_amount} RMB\n";
  976. $text .= "⌛️请等待系统处理, 到账时间可能需要几分钟!\n";
  977. $result['text'] = $text;
  978. } catch (Exception $e) {
  979. DB::rollBack();
  980. // 状态更新失败,但资金已扣款且三方支付已成功
  981. // 这里可以记录告警,需要人工干预
  982. Log::error('提现状态更新失败: ' . $e->getMessage(), [
  983. 'member_id' => $memberId,
  984. 'order_no' => $order_no
  985. ]);
  986. $result['text'] = '提现申请已提交,系统处理中...';
  987. }
  988. } //
  989. else {
  990. // 三方支付失败,需要回滚之前的预扣款
  991. DB::beginTransaction();
  992. try {
  993. // 恢复钱包余额
  994. $wallet->available_balance = $balance;
  995. $wallet->save();
  996. // 更新提现记录状态为失败
  997. $info->status = self::STATUS_FAIL;
  998. $info->remark = $failureMessage;
  999. $info->save();
  1000. // 记录退款日志
  1001. BalanceLogService::addLog($memberId, $default_amount, $available_balance, $balance, '三方提现', $id, '提现失败退款');
  1002. DB::commit();
  1003. $result['text'] = $failureMessage;
  1004. } catch (Exception $e) {
  1005. DB::rollBack();
  1006. // 回滚失败,需要记录告警,人工干预
  1007. Log::error('提现失败回滚异常: ' . $e->getMessage(), [
  1008. 'member_id' => $memberId,
  1009. 'order_no' => $order_no,
  1010. 'amount' => $amount
  1011. ]);
  1012. $result['text'] = '提现失败,请联系客服处理退款!';
  1013. }
  1014. }
  1015. return $result;
  1016. }
  1017. /**
  1018. * @description: 接收三方订单
  1019. * @param {*} $params
  1020. * @return {*}
  1021. */
  1022. public static function receiveOrder($params)
  1023. {
  1024. if (ZimuPayService::getAppId() !== '' && ($params['appId'] ?? '') === ZimuPayService::getAppId()) {
  1025. $info = self::findOne(['order_no' => $params['mchOrderNo'] ?? '']);
  1026. if (!$info || $info->type != self::TYPE_PAYOUT || !ZimuPayService::isPayoutChannel($info->channel)) {
  1027. Log::error('ZIMU提现/代付回调订单不存在或类型不匹配', [
  1028. 'mch_order_no' => $params['mchOrderNo'] ?? '',
  1029. 'order_id' => $info->id ?? null,
  1030. 'order_type' => $info->type ?? null,
  1031. 'channel' => $info->channel ?? null,
  1032. ]);
  1033. return false;
  1034. }
  1035. if (!ZimuPayService::verifyNotify($params)) {
  1036. Log::error('ZIMU提现/代付回调验签失败', [
  1037. 'mch_order_no' => $params['mchOrderNo'] ?? '',
  1038. 'app_id' => $params['appId'] ?? '',
  1039. 'signature' => ZimuPayService::notifySignatureDiagnostics($params),
  1040. ]);
  1041. return false;
  1042. }
  1043. if (bccomp(ZimuPayService::amount($info->amount), ZimuPayService::amount($params['amount'] ?? 0), 2) !== 0) {
  1044. Log::error('ZIMU提现/代付回调金额不一致', [
  1045. 'mch_order_no' => $params['mchOrderNo'] ?? '',
  1046. 'order_amount' => $info->amount,
  1047. 'callback_amount' => $params['amount'] ?? null,
  1048. ]);
  1049. return false;
  1050. }
  1051. self::onSubmitZimuPayout($params, $info);
  1052. return true;
  1053. }
  1054. if (NoPayService::getWithdrawMerchantId() !== '' && ($params['appId'] ?? '') === NoPayService::getWithdrawMerchantId()) {
  1055. $info = self::findOne(['order_no' => $params['merchantOrderNo'] ?? '']);
  1056. if (!$info || $info->type != self::TYPE_PAYOUT || !NoPayService::isWithdrawChannel($info->channel)) {
  1057. Log::error('NO钱包提现回调订单不存在或类型不匹配', [
  1058. 'merchant_order_no' => $params['merchantOrderNo'] ?? '',
  1059. 'order_id' => $info->id ?? null,
  1060. 'order_type' => $info->type ?? null,
  1061. 'channel' => $info->channel ?? null,
  1062. ]);
  1063. return false;
  1064. }
  1065. if (!NoPayService::verifyWithdrawNotify($params)) {
  1066. Log::error('NO钱包提现回调验签失败', [
  1067. 'merchant_order_no' => $params['merchantOrderNo'] ?? '',
  1068. 'app_id' => $params['appId'] ?? '',
  1069. 'signature' => NoPayService::withdrawNotifySignatureDiagnostics($params),
  1070. ]);
  1071. return false;
  1072. }
  1073. if (bccomp(NoPayService::amount($info->amount), NoPayService::amount($params['amount'] ?? 0), 2) !== 0) {
  1074. Log::error('NO钱包提现回调金额不一致', [
  1075. 'merchant_order_no' => $params['merchantOrderNo'] ?? '',
  1076. 'order_amount' => $info->amount,
  1077. 'callback_amount' => $params['amount'] ?? null,
  1078. ]);
  1079. return false;
  1080. }
  1081. self::onSubmitNoPayout($params, $info);
  1082. return true;
  1083. }
  1084. if (($params['userCode'] ?? '') === JdPayService::getMerchantId()) {
  1085. $info = self::findOne(['order_no' => $params['customerOrderCode'] ?? ''])
  1086. ?: static::$MODEL::where('pay_no', $params['orderCode'] ?? '')->first();
  1087. if (!$info || $info->type != self::TYPE_PAYOUT) {
  1088. return false;
  1089. }
  1090. if (!JdPayService::verifyRemitNotify($params)) {
  1091. return false;
  1092. }
  1093. if (bccomp(JdPayService::amount($info->amount), JdPayService::amount($params['amount'] ?? 0), 2) !== 0) {
  1094. return false;
  1095. }
  1096. self::onSubmitJdPayout($params, $info);
  1097. return true;
  1098. }
  1099. // 判断商户号是否一致
  1100. if (($params['merchantNum'] ?? '') == QianBaoService::getMerchantId()) {
  1101. $info = self::findOne(['order_no' => $params['orderNo']]);
  1102. if ($info) {
  1103. // 判断金额是不是正确认
  1104. if ($info->amount != $params['amount']) {
  1105. return false;
  1106. }
  1107. // 验证签名
  1108. $sign = QianBaoService::verifyNotifySign($params['state'], $params['orderNo'], $params['amount']);
  1109. if ($params['sign'] != $sign) {
  1110. return false;
  1111. }
  1112. // 代付
  1113. if ($info->type == self::TYPE_PAYOUT) {
  1114. self::onSubmitPayout($params, $info);
  1115. }
  1116. // 代收
  1117. if ($info->type == self::TYPE_PAY) {
  1118. }
  1119. }
  1120. }
  1121. }
  1122. /**
  1123. * @description: 处理代付订单
  1124. * @param {*} $params
  1125. * @return {*}
  1126. */
  1127. public static function onSubmitJdPayout($params, $info)
  1128. {
  1129. if ($info->status != self::STATUS_PROCESS) {
  1130. return;
  1131. }
  1132. $memberId = $info->member_id;
  1133. $amount = JdPayService::amount($params['amount'] ?? $info->amount);
  1134. $data = [];
  1135. $chat_id = $info->member_id;
  1136. $data['callback_data'] = json_encode($params, JSON_UNESCAPED_UNICODE);
  1137. DB::beginTransaction();
  1138. try {
  1139. if ((string)$params['status'] === JdPayService::REMIT_STATUS_SUCCESS) {
  1140. $data['status'] = self::STATUS_SUCCESS;
  1141. $res = static::$MODEL::where(['id' => $info->id])->update($data);
  1142. if ($res) {
  1143. $text = "✅ 提现通知 \n";
  1144. $text .= "提现平台:{$info->bank_name} \n";
  1145. $text .= "收款人:{$info->account} \n";
  1146. $text .= "收款地址:{$info->card_no} \n";
  1147. $text .= "提现金额:{$info->amount} \n";
  1148. $text .= "提现成功,金额已到账,请注意查收!";
  1149. self::notifyUser($chat_id, $text);
  1150. }
  1151. } elseif ((string)$params['status'] === JdPayService::REMIT_STATUS_FAIL) {
  1152. $data['status'] = self::STATUS_FAIL;
  1153. $res = static::$MODEL::where(['id' => $info->id])->update($data);
  1154. $wallet = WalletService::findOne(['member_id' => $info->member_id]);
  1155. $balance = $wallet->available_balance;
  1156. $available_balance = bcadd($balance, $amount, 10);
  1157. $wallet->available_balance = $available_balance;
  1158. $wallet->save();
  1159. BalanceLogService::addLog($memberId, $amount, $balance, $available_balance, '三方提现', $info->id, '提现失败退款');
  1160. if ($res) {
  1161. $text = "❌ 提现通知 \n";
  1162. $text .= "提现平台:{$info->bank_name} \n";
  1163. $text .= "收款人:{$info->account} \n";
  1164. $text .= "收款地址:{$info->card_no} \n";
  1165. $text .= "提现金额:{$info->amount} \n";
  1166. $text .= "提现失败,金额已返回钱包,请注意查收!";
  1167. self::notifyUser($chat_id, $text);
  1168. }
  1169. }
  1170. DB::commit();
  1171. } catch (Exception $e) {
  1172. DB::rollBack();
  1173. Log::error('JD提现回调处理异常: ' . $e->getMessage(), $params);
  1174. }
  1175. }
  1176. public static function onSubmitNoPayout($params, $info)
  1177. {
  1178. $notification = null;
  1179. try {
  1180. DB::transaction(function () use ($params, $info, &$notification) {
  1181. $order = PaymentOrder::where('id', $info->id)->lockForUpdate()->first();
  1182. if (!$order || $order->status != self::STATUS_PROCESS) {
  1183. return;
  1184. }
  1185. $order->callback_data = json_encode($params, JSON_UNESCAPED_UNICODE);
  1186. if ((string)$params['state'] === NoPayService::STATE_SUCCESS) {
  1187. $order->status = self::STATUS_SUCCESS;
  1188. $order->save();
  1189. $notification = "✅ 提现通知 \n提现平台:{$order->bank_name} \n收款人:{$order->account} \nNO钱包账号:{$order->card_no} \n提现金额:{$order->amount} \n提现成功,金额已到账,请注意查收!";
  1190. } elseif ((string)$params['state'] === NoPayService::STATE_FAIL) {
  1191. $order->status = self::STATUS_FAIL;
  1192. $order->save();
  1193. $wallet = WalletModel::where('member_id', $order->member_id)->lockForUpdate()->first();
  1194. if (!$wallet) {
  1195. throw new Exception('钱包不存在', HttpStatus::CUSTOM_ERROR);
  1196. }
  1197. $balance = $wallet->available_balance;
  1198. $availableBalance = bcadd($balance, NoPayService::amount($order->amount), 10);
  1199. $wallet->available_balance = $availableBalance;
  1200. $wallet->save();
  1201. BalanceLogService::addLog($order->member_id, $order->amount, $balance, $availableBalance, '三方提现', $order->id, '提现失败退款');
  1202. $notification = "❌ 提现通知 \n提现平台:{$order->bank_name} \n收款人:{$order->account} \nNO钱包账号:{$order->card_no} \n提现金额:{$order->amount} \n提现失败,金额已返回钱包,请注意查收!";
  1203. } else {
  1204. $order->save();
  1205. }
  1206. }, 3);
  1207. if ($notification !== null) {
  1208. self::notifyUser($info->member_id, $notification);
  1209. }
  1210. } catch (Exception $e) {
  1211. Log::channel('payment_error')->error('NO提现回调处理异常', [
  1212. 'order_id' => $info->id,
  1213. 'params' => $params,
  1214. 'error' => $e->getMessage(),
  1215. ]);
  1216. }
  1217. }
  1218. public static function onSubmitZimuPayout($params, $info)
  1219. {
  1220. $notification = null;
  1221. $successStatus = ZimuPayService::isCashChannel($info->channel)
  1222. ? ZimuPayService::CASH_STATUS_SUCCESS
  1223. : ZimuPayService::WITHDRAW_STATUS_SUCCESS;
  1224. $failStatus = ZimuPayService::isCashChannel($info->channel)
  1225. ? ZimuPayService::CASH_STATUS_FAIL
  1226. : ZimuPayService::WITHDRAW_STATUS_FAIL;
  1227. try {
  1228. DB::transaction(function () use ($params, $info, &$notification, $successStatus, $failStatus) {
  1229. $order = PaymentOrder::where('id', $info->id)->lockForUpdate()->first();
  1230. if (!$order || $order->status != self::STATUS_PROCESS) {
  1231. return;
  1232. }
  1233. $order->callback_data = json_encode($params, JSON_UNESCAPED_UNICODE);
  1234. if ((string)$params['status'] === $successStatus) {
  1235. $order->status = self::STATUS_SUCCESS;
  1236. $order->save();
  1237. $accountLabel = ZimuPayService::isCashChannel($order->channel) ? '收款账号' : '808钱包地址';
  1238. $notification = "✅ 提现通知 \n提现平台:{$order->bank_name} \n收款人:{$order->account} \n{$accountLabel}:{$order->card_no} \n提现金额:{$order->amount} \n提现成功,金额已到账,请注意查收!";
  1239. } elseif ((string)$params['status'] === $failStatus) {
  1240. $order->status = self::STATUS_FAIL;
  1241. $order->save();
  1242. $wallet = WalletModel::where('member_id', $order->member_id)->lockForUpdate()->first();
  1243. if (!$wallet) {
  1244. throw new Exception('钱包不存在', HttpStatus::CUSTOM_ERROR);
  1245. }
  1246. $balance = $wallet->available_balance;
  1247. $availableBalance = bcadd($balance, ZimuPayService::amount($order->amount), 10);
  1248. $wallet->available_balance = $availableBalance;
  1249. $wallet->save();
  1250. BalanceLogService::addLog($order->member_id, $order->amount, $balance, $availableBalance, '三方提现', $order->id, '提现失败退款');
  1251. $accountLabel = ZimuPayService::isCashChannel($order->channel) ? '收款账号' : '808钱包地址';
  1252. $notification = "❌ 提现通知 \n提现平台:{$order->bank_name} \n收款人:{$order->account} \n{$accountLabel}:{$order->card_no} \n提现金额:{$order->amount} \n提现失败,金额已返回钱包,请注意查收!";
  1253. } else {
  1254. $order->save();
  1255. }
  1256. }, 3);
  1257. if ($notification !== null) {
  1258. self::notifyUser($info->member_id, $notification);
  1259. }
  1260. } catch (Exception $e) {
  1261. Log::channel('payment_error')->error('ZIMU提现/代付回调处理异常', [
  1262. 'order_id' => $info->id,
  1263. 'params' => $params,
  1264. 'error' => $e->getMessage(),
  1265. ]);
  1266. }
  1267. }
  1268. public static function onSubmitPayout($params, $info)
  1269. {
  1270. $memberId = $info->member_id;
  1271. $amount = $params['amount'];
  1272. $data = [];
  1273. $result = [];
  1274. $chat_id = $info->member_id;
  1275. $data['callback_data'] = json_encode($params, JSON_UNESCAPED_UNICODE);
  1276. DB::beginTransaction();
  1277. try {
  1278. if ($params['state'] == 1) {
  1279. $data['status'] = self::STATUS_SUCCESS;
  1280. $res = static::$MODEL::where(['order_no' => $params['orderNo']])->update($data);
  1281. if ($res) {
  1282. $text = "✅ 提现通知 \n";
  1283. $text .= "提现平台:{$info->bank_name} \n";
  1284. $text .= "收款人:{$info->account} \n";
  1285. $text .= "收款卡号:{$info->card_no} \n";
  1286. $text .= "提现金额:{$info->amount} \n";
  1287. $text .= "提现成功,金额已到账,请注意查收!";
  1288. self::notifyUser($chat_id, $text);
  1289. }
  1290. } else {
  1291. $data['status'] = self::STATUS_FAIL;
  1292. $res = static::$MODEL::where(['order_no' => $params['orderNo']])->update($data);
  1293. $wallet = WalletService::findOne(['member_id' => $info->member_id]);
  1294. $balance = $wallet->available_balance; // 钱包当前余额
  1295. $available_balance = bcadd($balance, $params['amount'], 10);
  1296. $wallet->available_balance = $available_balance;
  1297. $wallet->save();
  1298. // 记录退款日志
  1299. BalanceLogService::addLog($memberId, $amount, $balance, $available_balance, '三方提现', $info->id, '提现失败退款');
  1300. if ($res) {
  1301. $text = "❌ 提现通知 \n";
  1302. $text .= "提现平台:{$info->bank_name} \n";
  1303. $text .= "收款人:{$info->account} \n";
  1304. $text .= "收款卡号:{$info->card_no} \n";
  1305. $text .= "提现金额:{$info->amount} \n";
  1306. $text .= "提现失败,金额已返回钱包,请注意查收!";
  1307. self::notifyUser($chat_id, $text);
  1308. }
  1309. }
  1310. DB::commit();
  1311. } catch (Exception $e) {
  1312. DB::rollBack();
  1313. // 回滚失败,需要记录告警,人工干预
  1314. Log::error('提现失败回滚异常: ' . $e->getMessage(), $params);
  1315. }
  1316. }
  1317. /**
  1318. * @description: 查询支付订单
  1319. * @param {*} $id
  1320. * @return {*}
  1321. */
  1322. public static function singlePayOrder($id)
  1323. {
  1324. $msg = [];
  1325. $msg['code'] = self::NOT;
  1326. $info = self::findOne(['id' => $id]);
  1327. if ($info && $info->status == self::STATUS_PROCESS) {
  1328. if (NoPayService::isRechargeChannel($info->channel)) {
  1329. $ret = NoPayService::queryPayOrder($info->order_no, (string)$info->member_id);
  1330. Log::channel('payment')->info('NO支付查询订单', $ret);
  1331. if (($ret['code'] ?? -1) == 0) {
  1332. $item = $ret['data'] ?? [];
  1333. if ((string)($item['state'] ?? '') === NoPayService::STATE_SUCCESS) {
  1334. $processed = self::applyPayCallback(
  1335. $info,
  1336. NoPayService::amount($info->amount),
  1337. NoPayService::STATE_SUCCESS,
  1338. NoPayService::STATE_SUCCESS,
  1339. NoPayService::STATE_FAIL,
  1340. $ret
  1341. );
  1342. $msg['code'] = $processed ? self::YES : self::NOT;
  1343. $msg['msg'] = $processed ? '支付成功' : '订单已处理';
  1344. } elseif ((string)($item['state'] ?? '') === NoPayService::STATE_FAIL) {
  1345. self::applyPayCallback(
  1346. $info,
  1347. NoPayService::amount($info->amount),
  1348. NoPayService::STATE_FAIL,
  1349. NoPayService::STATE_SUCCESS,
  1350. NoPayService::STATE_FAIL,
  1351. $ret
  1352. );
  1353. $msg['msg'] = '支付失败';
  1354. } else {
  1355. $msg['msg'] = '支付中';
  1356. }
  1357. } else {
  1358. $msg['msg'] = '查询失败:' . ($ret['msg'] ?? '');
  1359. }
  1360. return $msg;
  1361. }
  1362. if (ZimuPayService::isRechargeChannel($info->channel)) {
  1363. $ret = ZimuPayService::queryPayOrder($info->order_no);
  1364. Log::channel('payment')->info('ZIMU支付查询订单', $ret);
  1365. if (($ret['code'] ?? -1) == 0) {
  1366. $item = $ret['data'] ?? [];
  1367. if ((string)($item['status'] ?? '') === ZimuPayService::PAY_STATUS_SUCCESS) {
  1368. $processed = self::applyPayCallback(
  1369. $info,
  1370. ZimuPayService::amount($info->amount),
  1371. ZimuPayService::PAY_STATUS_SUCCESS,
  1372. ZimuPayService::PAY_STATUS_SUCCESS,
  1373. ZimuPayService::PAY_STATUS_FAIL,
  1374. $ret
  1375. );
  1376. $msg['code'] = $processed ? self::YES : self::NOT;
  1377. $msg['msg'] = $processed ? '支付成功' : '订单已处理';
  1378. } elseif ((string)($item['status'] ?? '') === ZimuPayService::PAY_STATUS_FAIL) {
  1379. self::applyPayCallback(
  1380. $info,
  1381. ZimuPayService::amount($info->amount),
  1382. ZimuPayService::PAY_STATUS_FAIL,
  1383. ZimuPayService::PAY_STATUS_SUCCESS,
  1384. ZimuPayService::PAY_STATUS_FAIL,
  1385. $ret
  1386. );
  1387. $msg['msg'] = '支付失败';
  1388. } else {
  1389. $msg['msg'] = '支付中';
  1390. }
  1391. } else {
  1392. $msg['msg'] = '查询失败:' . ($ret['msg'] ?? '');
  1393. }
  1394. return $msg;
  1395. }
  1396. if (JdPayService::isChannel($info->channel)) {
  1397. $ret = JdPayService::queryPayOrder($info->pay_no ?? '', $info->order_no);
  1398. Log::channel('payment')->info('JD支付查询订单', $ret);
  1399. if (($ret['code'] ?? 0) == 200) {
  1400. $item = $ret['data'] ?? [];
  1401. if ((string)($item['status'] ?? '') === JdPayService::PAY_STATUS_SUCCESS) {
  1402. $info->status = self::STATUS_SUCCESS;
  1403. $info->state = $item['status'];
  1404. $info->callback_data = json_encode($ret, JSON_UNESCAPED_UNICODE);
  1405. $info->save();
  1406. $wallet = WalletService::findOne(['member_id' => $info->member_id]);
  1407. $balance = $wallet->available_balance;
  1408. $available_balance = bcadd($balance, $info->amount, 10);
  1409. $wallet->available_balance = $available_balance;
  1410. $wallet->save();
  1411. BalanceLogService::addLog($info->member_id, $info->amount, $balance, $available_balance, '三方充值', $info->id, '');
  1412. self::rechargesBibiReturn($info->member_id, $info->amount, $info->id);
  1413. $msg['code'] = self::YES;
  1414. $msg['msg'] = '支付成功';
  1415. } else {
  1416. $msg['msg'] = '支付中';
  1417. }
  1418. } else {
  1419. $msg['msg'] = '查询失败:' . ($ret['message'] ?? '');
  1420. }
  1421. return $msg;
  1422. }
  1423. $ret = SanJinService::queryOrder($info->order_no);
  1424. Log::error('三斤支付查询订单:', $ret);
  1425. if ($ret['code'] == 0) {
  1426. $item = [];
  1427. $item['state'] = $ret['data']['state'];
  1428. if ($ret['data']['state'] == 1) {
  1429. $item['status'] = self::STATUS_SUCCESS;
  1430. $info->update($item);
  1431. $wallet = WalletService::findOne(['member_id' => $info->member_id]);
  1432. $balance = $wallet->available_balance;
  1433. $available_balance = bcadd($balance, $info->amount, 10);
  1434. $wallet->available_balance = $available_balance;
  1435. $wallet->save();
  1436. // 记录余额变动日志
  1437. BalanceLogService::addLog($info->member_id, $info->amount, $balance, $available_balance, '三方充值', $info->id, '');
  1438. $msg['code'] = self::YES;
  1439. $msg['msg'] = '支付成功';
  1440. } else {
  1441. $msg['msg'] = '支付中';
  1442. }
  1443. } else {
  1444. $msg['msg'] = '查询失败:' . $ret['message'];
  1445. }
  1446. } else {
  1447. $msg['msg'] = '该状态无法查询';
  1448. }
  1449. return $msg;
  1450. }
  1451. private static function assertJdBalanceEnough($amount, array $context = []): void
  1452. {
  1453. $ret = JdPayService::balance();
  1454. Log::channel('payment')->info('JD余额查询', $context + [
  1455. 'amount' => $amount,
  1456. 'response' => $ret,
  1457. ]);
  1458. if (($ret['code'] ?? 0) != 200) {
  1459. $logContext = $context + [
  1460. 'amount' => $amount,
  1461. 'response' => $ret,
  1462. ];
  1463. Log::channel('payment_error')->error('JD余额查询失败', $logContext);
  1464. Log::error('JD余额查询失败', $logContext);
  1465. throw new Exception($ret['message'] ?? 'JD余额查询失败', HttpStatus::CUSTOM_ERROR);
  1466. }
  1467. $balance = $ret['data']['balance'] ?? null;
  1468. if ($balance === null || bccomp((string)$balance, JdPayService::amount($amount), 2) < 0) {
  1469. $logContext = $context + [
  1470. 'amount' => $amount,
  1471. 'balance' => $balance,
  1472. 'response' => $ret,
  1473. ];
  1474. Log::channel('payment_error')->error('JD商户余额不足', $logContext);
  1475. Log::error('JD商户余额不足', $logContext);
  1476. throw new Exception('JD商户余额不足', HttpStatus::CUSTOM_ERROR);
  1477. }
  1478. }
  1479. private static function assertZimuBalanceEnough($amount, array $context = []): void
  1480. {
  1481. $ret = ZimuPayService::balance();
  1482. Log::channel('payment')->info('ZIMU余额查询', $context + [
  1483. 'amount' => $amount,
  1484. 'response' => $ret,
  1485. ]);
  1486. if (($ret['code'] ?? -1) != 0) {
  1487. $logContext = $context + [
  1488. 'amount' => $amount,
  1489. 'response' => $ret,
  1490. ];
  1491. Log::channel('payment_error')->error('ZIMU余额查询失败', $logContext);
  1492. Log::error('ZIMU余额查询失败', $logContext);
  1493. throw new Exception($ret['msg'] ?? 'ZIMU余额查询失败', HttpStatus::CUSTOM_ERROR);
  1494. }
  1495. $balance = $ret['data']['balance'] ?? null;
  1496. if ($balance === null || bccomp((string)$balance, ZimuPayService::amount($amount), 2) < 0) {
  1497. $logContext = $context + [
  1498. 'amount' => $amount,
  1499. 'balance' => $balance,
  1500. 'response' => $ret,
  1501. ];
  1502. Log::channel('payment_error')->error('ZIMU商户余额不足', $logContext);
  1503. Log::error('ZIMU商户余额不足', $logContext);
  1504. throw new Exception('ZIMU商户余额不足', HttpStatus::CUSTOM_ERROR);
  1505. }
  1506. }
  1507. private static function notifyUser($chatId, string $text): void
  1508. {
  1509. if ((int)User::where('member_id', $chatId)->value('from') !== -1) {
  1510. return;
  1511. }
  1512. try {
  1513. self::sendMessage($chatId, $text);
  1514. } catch (\Throwable $e) {
  1515. Log::channel('payment_error')->error('支付订单用户通知失败', [
  1516. 'chat_id' => $chatId,
  1517. 'error' => $e->getMessage(),
  1518. ]);
  1519. }
  1520. }
  1521. public static function syncPayOrder()
  1522. {
  1523. $list = static::$MODEL::where('state', 0)->where('type', self::TYPE_PAY)->take(100)->get();
  1524. // foreach($list->toArray() as $k => $v){
  1525. // $item= [];
  1526. // if($v['status'] == self::STATUS_SUCCESS){
  1527. // $item['state'] = 1;
  1528. // static::$MODEL::where(['id'=>$v['id']])->update($item);
  1529. // }else{
  1530. // $ret = SanJinService::queryOrder($v['order_no']);
  1531. // var_dump($ret);
  1532. // if($ret['code'] == 0){
  1533. // $item['state'] = $ret['data']['state'];
  1534. // static::$MODEL::where(['id'=>$v['id']])->update($item);
  1535. // }
  1536. // }
  1537. // }
  1538. }
  1539. }