PaymentOrderService.php 74 KB

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