WechatService.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. <?php
  2. namespace liuniu;
  3. use app\admin\model\Company;
  4. use app\admin\model\WechatPlanRecord;
  5. use app\api\controller\Lave;
  6. use app\common\model\LaveMonth;
  7. use app\common\model\WechatContext;
  8. use EasyWeChat\Factory;
  9. use EasyWeChat\Kernel\Messages\Article;
  10. use EasyWeChat\Kernel\Messages\Image;
  11. use EasyWeChat\Kernel\Messages\News;
  12. use EasyWeChat\Kernel\Messages\Text;
  13. use EasyWeChat\Kernel\Messages\Video;
  14. use think\Hook;
  15. use think\Request;
  16. use think\Response;
  17. class WechatService
  18. {
  19. private static $instance = null;
  20. private static $app = null;
  21. public static function options($cid)
  22. {
  23. $info = Company::where('id', $cid)->find();
  24. $config = [
  25. 'app_id' => isset($info['wechat_appid']) ? trim($info['wechat_appid']) : '',
  26. 'secret' => isset($info['wechat_appsecret']) ? trim($info['wechat_appsecret']) : '',
  27. 'token' => isset($info['wechat_token']) ? trim($info['wechat_token']) : '',
  28. 'guzzle' => [
  29. 'timeout' => 10.0, // 超时时间(秒)
  30. 'verify' => false
  31. ],
  32. ];
  33. if (isset($info['wechat_encode']) && (int)$info['wechat_encode'] > 0 && isset($info['wechat_encodingaeskey']) && !empty($info['wechat_encodingaeskey']))
  34. $config['aes_key'] = $info['wechat_encodingaeskey'];
  35. if (isset($info['pay_weixin_open']) && $info['pay_weixin_open'] == 1) {
  36. $config1 = [
  37. 'mch_id' => trim($info['pay_weixin_mchid']),
  38. 'key' => trim($info['pay_weixin_key']),
  39. 'cert_path' => realpath('.' . $info['pay_weixin_client_certfile']),
  40. 'key_path' => realpath('.' . $info['pay_weixin_client_keyfile']),
  41. 'notify_url' => Request::instance()->domain() . "/api/wechat/notify/" . $cid
  42. ];
  43. $config = array_merge($config, $config1);
  44. }
  45. return $config;
  46. }
  47. public static function application($cache = false, $cid = 0)
  48. {
  49. (self::$instance[$cid] === null || $cache === true) && (self::$instance[$cid] = Factory::officialAccount(self::options($cid)));
  50. return self::$instance[$cid];
  51. }
  52. /**
  53. * 支付接口
  54. * @param false $cache
  55. * @param int $cid
  56. * @return \EasyWeChat\Payment\Application|mixed
  57. */
  58. public static function payment($cache = false, $cid = 0)
  59. {
  60. (self::$app[$cid] === null || $cache === true) && (self::$app[$cid] = Factory::payment(self::options($cid)));
  61. return self::$app[$cid];
  62. }
  63. public static function payment2($cache = false, $cid = 0) {
  64. // 使用 isset() 检查下标,避免直接访问未定义键
  65. if (!isset(self::$app[$cid])) {
  66. $options = self::options($cid);
  67. // 校验必要配置是否存在
  68. // if (empty($options['app_id']) || empty($options['mch_id'])) {
  69. // throw new \Exception("微信支付配置缺失: cid={$cid}");
  70. // }
  71. self::$app[$cid] = Factory::payment($options);
  72. }
  73. return self::$app[$cid];
  74. }
  75. public static function serve($cid = 0): Response
  76. {
  77. $wechat = self::application(true, $cid);
  78. $server = $wechat->server;
  79. self::hook($server);
  80. $response = $server->serve();
  81. return Response($response->getContent());
  82. }
  83. /**
  84. * 监听行为
  85. * @param Guard $server
  86. */
  87. private static function hook($server)
  88. {
  89. $server->push(function ($message) {
  90. switch ($message['MsgType']) {
  91. case 'event':
  92. switch (strtolower($message['Event'])) {
  93. case 'subscribe':
  94. $response = WechatReply::reply('subscribe');
  95. if (isset($message->EventKey)) {
  96. if ($message->EventKey && ($qrInfo = QrcodeService::getQrcode($message->Ticket, 'ticket'))) {
  97. QrcodeService::scanQrcode($message->Ticket, 'ticket');
  98. if (strtolower($qrInfo['third_type']) == 'spread') {
  99. }
  100. }
  101. }
  102. break;
  103. case 'unsubscribe':
  104. event('WechatEventUnsubscribeBefore', [$message]);
  105. break;
  106. case 'scan':
  107. $response = WechatReply::reply('subscribe');
  108. if ($message->EventKey && ($qrInfo = QrcodeService::getQrcode($message->Ticket, 'ticket'))) {
  109. QrcodeService::scanQrcode($message->Ticket, 'ticket');
  110. if (strtolower($qrInfo['third_type']) == 'spread') {
  111. }
  112. }
  113. break;
  114. case 'location':
  115. $response = MessageRepositories::wechatEventLocation($message);
  116. break;
  117. case 'click':
  118. $response = WechatReply::reply($message->EventKey);
  119. break;
  120. case 'view':
  121. $response = MessageRepositories::wechatEventView($message);
  122. break;
  123. }
  124. break;
  125. case 'text':
  126. @file_put_contents("tt.txt", '1');
  127. $response = self::textMessage('绑定推荐人失败');
  128. @file_put_contents("tt.txt", '2', 8);
  129. @file_put_contents("tt.txt", json_encode($response), 8);
  130. break;
  131. $response = WechatReply::reply($message->Content);
  132. break;
  133. case 'image':
  134. $response = MessageRepositories::wechatMessageImage($message);
  135. break;
  136. case 'voice':
  137. $response = MessageRepositories::wechatMessageVoice($message);
  138. break;
  139. case 'video':
  140. $response = MessageRepositories::wechatMessageVideo($message);
  141. break;
  142. case 'location':
  143. $response = MessageRepositories::wechatMessageLocation($message);
  144. break;
  145. case 'link':
  146. $response = MessageRepositories::wechatMessageLink($message);
  147. break;
  148. // ... 其它消息
  149. default:
  150. $response = MessageRepositories::wechatMessageOther($message);
  151. break;
  152. }
  153. return $response ?? false;
  154. });
  155. }
  156. /**
  157. * 多客服消息转发
  158. * @param string $account
  159. * @return \EasyWeChat\Message\Transfer
  160. */
  161. public static function transfer($account = '')
  162. {
  163. $transfer = new \EasyWeChat\Message\Transfer();
  164. return empty($account) ? $transfer : $transfer->to($account);
  165. }
  166. /**
  167. * 上传永久素材接口
  168. * @return \EasyWeChat\Material\Material
  169. */
  170. public static function materialService($cid = 0)
  171. {
  172. return self::application(false, $cid)->material;
  173. }
  174. /**
  175. * 上传临时素材接口
  176. * @return \EasyWeChat\Material\Temporary
  177. */
  178. public static function materialTemporaryService($cid = 0)
  179. {
  180. return self::application(false, $cid)->media;
  181. }
  182. /**
  183. * 用户接口
  184. * @return \EasyWeChat\User\User
  185. */
  186. public static function userService($cid = 0)
  187. {
  188. return self::application(false, $cid)->user;
  189. }
  190. /**
  191. * 客服消息接口
  192. * @param null $to
  193. * @param null $message
  194. */
  195. public static function staffService($cid = 0)
  196. {
  197. return self::application(false, $cid)->staff;
  198. }
  199. /**
  200. * 微信公众号菜单接口
  201. * @return \EasyWeChat\Menu\Menu
  202. */
  203. public static function menuService($cid = 0)
  204. {
  205. return self::application(false, $cid)->menu;
  206. }
  207. /**
  208. * 微信二维码生成接口
  209. * @return \EasyWeChat\QRCode\QRCode
  210. */
  211. public static function qrcodeService($cid = 0)
  212. {
  213. return self::application(false, $cid)->qrcode;
  214. }
  215. /**
  216. * 微信永久二维码生成接口 小于10万个
  217. * @return \EasyWeChat\QRCode\QRCode
  218. */
  219. public static function qrcodeForeverService($sceneValue, $cid = 0)
  220. {
  221. return self::application(false, $cid)->qrcode->forever($sceneValue);
  222. }
  223. /**
  224. * 微信临时二维码生成接口 30天有效期
  225. * @return \EasyWeChat\QRCode\QRCode
  226. */
  227. public static function qrcodeTempService($sceneValue, $expireSeconds = 2592000, $cid = 0)
  228. {
  229. return self::application(false, $cid)->qrcode->temporary($sceneValue, $expireSeconds);
  230. }
  231. /**
  232. * 短链接生成接口
  233. * @return \EasyWeChat\Url\Url
  234. */
  235. public static function urlService($cid = 0)
  236. {
  237. return self::application(false, $cid)->url;
  238. }
  239. /**
  240. * 用户授权
  241. * @return \Overtrue\Socialite\Providers\WeChatProvider
  242. */
  243. public static function oauthService($cid = 0)
  244. {
  245. return self::application(false, $cid)->oauth;
  246. }
  247. /**
  248. * 模板消息接口
  249. * @return \EasyWeChat\Notice\Notice
  250. */
  251. public static function noticeService($cid = 0)
  252. {
  253. return self::application(false, $cid)->template_message;
  254. }
  255. public static function sendTemplate($openid, $templateId, array $data, $url = null, $cid = 0)
  256. {
  257. $notice = self::noticeService($cid);
  258. return $notice->send([
  259. 'touser' => $openid,
  260. 'template_id' => $templateId,
  261. 'url' => $url,
  262. 'data' => $data,
  263. ]);
  264. }
  265. public static function userTagService($cid = 0)
  266. {
  267. return self::application(false, $cid)->user_tag;
  268. }
  269. /**
  270. * 生成支付订单对象
  271. * @param $openid
  272. * @param $out_trade_no
  273. * @param $total_fee
  274. * @param $attach
  275. * @param $body
  276. * @param string $detail
  277. * @param string $trade_type
  278. * @param array $options
  279. * @return Order
  280. */
  281. public static function paymentOrder($openid, $out_trade_no, $total_fee, $attach, $body, $detail = '', $trade_type = 'JSAPI', $options = [], $cid = 0)
  282. {
  283. $total_fee = bcmul($total_fee, 100, 0);
  284. $order = array_merge(compact('out_trade_no', 'total_fee', 'attach', 'body', 'detail', 'trade_type', 'openid'), $options);
  285. if ($order['detail'] == '') unset($order['detail']);
  286. @file_put_contents("quanju.txt", json_encode($order)."-微信支付传值\r\n", 8);
  287. $result = self::payment(false, $cid)->order->unify(
  288. $order
  289. );
  290. return $result;
  291. }
  292. /**
  293. * 使用商户订单号退款
  294. * @param $orderNo
  295. * @param $opt
  296. */
  297. public static function payOrderRefund($cid, $orderNo, array $opt)
  298. {
  299. if (!isset($opt['pay_price'])) exception('缺少pay_price');
  300. $totalFee = floatval(bcmul($opt['pay_price'], 100, 0));
  301. $refundFee = isset($opt['refund_price']) ? floatval(bcmul($opt['refund_price'], 100, 0)) : null;
  302. $refundReason = isset($opt['desc']) ? $opt['desc'] : '';
  303. $refundNo = isset($opt['refund_id']) ? $opt['refund_id'] : $orderNo;
  304. $opUserId = isset($opt['op_user_id']) ? $opt['op_user_id'] : null;
  305. $type = isset($opt['type']) ? $opt['type'] : 'out_trade_no';
  306. /*仅针对老资金流商户使用
  307. REFUND_SOURCE_UNSETTLED_FUNDS---未结算资金退款(默认使用未结算资金退款)
  308. REFUND_SOURCE_RECHARGE_FUNDS---可用余额退款*/
  309. $refundAccount = isset($opt['refund_account']) ? $opt['refund_account'] : 'REFUND_SOURCE_UNSETTLED_FUNDS';
  310. try {
  311. $res = self::payment('false', $cid)->byOutTradeNumber($orderNo, $refundNo, $totalFee, $refundFee, ['refund_desc' => $refundReason]);
  312. if ($res->return_code == 'FAIL') exception('退款失败:' . $res->return_msg);
  313. if (isset($res->err_code)) exception('退款失败:' . $res->err_code_des);
  314. } catch (\Exception $e) {
  315. exception($e->getMessage());
  316. }
  317. return true;
  318. }
  319. /**
  320. * 微信支付成功回调接口
  321. */
  322. public static function handleNotify($cid)
  323. {
  324. $response = self::payment(true, $cid)->handlePaidNotify(function ($notify, $successful) use ($cid) {
  325. if ($successful && isset($notify['out_trade_no'])) {
  326. if (isset($notify['attach']) && $notify['attach']) {
  327. if (($count = strpos($notify['out_trade_no'], '_')) !== false) {
  328. $notify['out_trade_no'] = substr($notify['out_trade_no'], $count + 1);
  329. }
  330. $params = [$cid, $notify['out_trade_no']];
  331. Hook::exec("\\liuniu\\repositories\\PaymentRepositories", "wechat" . ucfirst($notify['attach']), $params);
  332. }
  333. $data = ['eventkey' => 'notify', 'command' => '', 'refreshtime' => time(), 'openid' => $notify['openid'], 'message' => json_encode($notify)];
  334. $wechatContext = WechatContext::create($data, true);
  335. return true;
  336. }
  337. @file_put_contents("quanju.txt", json_encode($notify)."-签约返回内容\r\n", 8);
  338. // @file_put_contents("quanju.txt", json_encode($successful)."-这是什么\r\n", 8);
  339. if (isset($notify['plan_id'])) {
  340. if (isset($notify['change_type']) && $notify['change_type']) {
  341. @file_put_contents("quanju.txt", $notify['change_type']."-状态\r\n", 8);
  342. if ($notify['change_type']=='ADD'){
  343. $cs=WechatPlanRecord::where('contract_code',$notify['contract_code'])->Update(['contract_id' => $notify['contract_id'],'is_signing'=>0]);
  344. if (($count = strpos($notify['contract_code'], '_')) !== false) {
  345. $notify['contract_code'] = substr($notify['contract_code'], $count + 1);
  346. }
  347. $params = [$cid, $notify['contract_code']];
  348. Hook::exec("\\liuniu\\repositories\\PaymentRepositories", "wechat" . ucfirst('MonthLave'), $params);
  349. @file_put_contents("quanju.txt", json_encode($params)."-不知道行不行\r\n", 8);
  350. $tf = Lave::paySignPap($notify['contract_code']);
  351. @file_put_contents("quanju3.txt", $tf."-第一次扣款\r\n", 8);
  352. }elseif ($notify['change_type']=='DELETE'){
  353. @file_put_contents("quanju.txt", "-关闭签约\r\n", 8);
  354. $cs=WechatPlanRecord::where('contract_code',$notify['contract_code'])->Update(['is_signing' => 1,'deletetime'=>time()]);
  355. }
  356. @file_put_contents("quanju.txt", $cs."-修改记录\r\n", 8);
  357. }
  358. $data = ['eventkey' => 'notify', 'command' => '', 'refreshtime' => time(), 'openid' => $notify['openid'], 'message' => json_encode($notify)];
  359. $wechatContext = WechatContext::create($data, true);
  360. return true;
  361. }
  362. if (isset($notify['nonce_str'])) {
  363. $cz = LaveMonth::Where(['contract_code' => $notify['nonce_str']])->find();
  364. @file_put_contents("quanju.txt", json_encode($cz)."-随机字符搜索\r\n", 8);
  365. if (!empty($cz)){
  366. $params = [$cid, $notify['nonce_str']];
  367. Hook::exec("\\liuniu\\repositories\\PaymentRepositories", "wechat" . ucfirst('MonthLavePay'), $params);
  368. }
  369. $data = ['eventkey' => 'notify', 'command' => '', 'refreshtime' => time(), 'openid' => $notify['openid'], 'message' => json_encode($notify)];
  370. $wechatContext = WechatContext::create($data, true);
  371. return true;
  372. }
  373. });
  374. $response->send();
  375. }
  376. /**
  377. * jsSdk
  378. * @return \EasyWeChat\Js\Js
  379. */
  380. public static function jsService($cid = 0)
  381. {
  382. return self::payment(false, $cid)->jssdk;
  383. }
  384. public static function WeixinJSBridge($cid, $prepayId)
  385. {
  386. $json = self::jsService($cid)->bridgeConfig($prepayId);
  387. return $json;
  388. }
  389. public static function jspay($cid, $prepayId)
  390. {
  391. $json = self::jsService($cid)->sdkConfig($prepayId);
  392. return $json;
  393. }
  394. public static function jsSdk($url = '', $cid = 0)
  395. {
  396. $apiList = ['editAddress', 'openAddress', 'updateTimelineShareData', 'updateAppMessageShareData', 'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone', 'startRecord', 'stopRecord', 'onVoiceRecordEnd', 'playVoice', 'pauseVoice', 'stopVoice', 'onVoicePlayEnd', 'uploadVoice', 'downloadVoice', 'chooseImage', 'previewImage', 'uploadImage', 'downloadImage', 'translateVoice', 'getNetworkType', 'openLocation', 'getLocation', 'hideOptionMenu', 'showOptionMenu', 'hideMenuItems', 'showMenuItems', 'hideAllNonBaseMenuItem', 'showAllNonBaseMenuItem', 'closeWindow', 'scanQRCode', 'chooseWXPay', 'openProductSpecificView', 'addCard', 'chooseCard', 'openCard'];
  397. $jsService = self::jsService($cid);
  398. if ($url) $jsService->setUrl($url);
  399. try {
  400. return $jsService->buildConfig($apiList, false);
  401. } catch (\Exception $e) {
  402. // var_dump($e->getMessage());
  403. return '{}';
  404. }
  405. }
  406. /**
  407. * 回复文本消息
  408. * @param string $content 文本内容
  409. * @return Text
  410. */
  411. public static function textMessage($content)
  412. {
  413. return new Text($content);
  414. }
  415. /**
  416. * 回复图片消息
  417. * @param string $media_id 媒体资源 ID
  418. * @return Image
  419. */
  420. public static function imageMessage($media_id)
  421. {
  422. return new Image('media_id');
  423. }
  424. /**
  425. * 回复视频消息
  426. * @param string $media_id 媒体资源 ID
  427. * @param string $title 标题
  428. * @param string $description 描述
  429. * @param null $thumb_media_id 封面资源 ID
  430. * @return Video
  431. */
  432. public static function videoMessage($media_id, $title = '', $description = '...', $thumb_media_id = null)
  433. {
  434. return new Video(compact('media_id', 'title', 'description', 'thumb_media_id'));
  435. }
  436. /**
  437. * 回复声音消息
  438. * @param string $media_id 媒体资源 ID
  439. * @return Voice
  440. */
  441. public static function voiceMessage($media_id)
  442. {
  443. return new Voice(compact('media_id'));
  444. }
  445. /**
  446. * 回复图文消息
  447. * @param string|array $title 标题
  448. * @param string $description 描述
  449. * @param string $url URL
  450. * @param string $image 图片链接
  451. */
  452. public static function newsMessage($title, $description = '...', $url = '', $image = '')
  453. {
  454. if (is_array($title)) {
  455. if (isset($title[0]) && is_array($title[0])) {
  456. $newsList = [];
  457. foreach ($title as $news) {
  458. $newsList[] = self::newsMessage($news);
  459. }
  460. return $newsList;
  461. } else {
  462. $data = $title;
  463. }
  464. } else {
  465. $data = compact('title', 'description', 'url', 'image');
  466. }
  467. return new News($data);
  468. }
  469. /**
  470. * 回复文章消息
  471. * @param string|array $title 标题
  472. * @param string $thumb_media_id 图文消息的封面图片素材id(必须是永久 media_ID)
  473. * @param string $source_url 图文消息的原文地址,即点击“阅读原文”后的URL
  474. * @param string $content 图文消息的具体内容,支持HTML标签,必须少于2万字符,小于1M,且此处会去除JS
  475. * @param string $author 作者
  476. * @param string $digest 图文消息的摘要,仅有单图文消息才有摘要,多图文此处为空
  477. * @param int $show_cover_pic 是否显示封面,0为false,即不显示,1为true,即显示
  478. * @param int $need_open_comment 是否打开评论,0不打开,1打开
  479. * @param int $only_fans_can_comment 是否粉丝才可评论,0所有人可评论,1粉丝才可评论
  480. * @return Article
  481. */
  482. public static function articleMessage($title, $thumb_media_id, $source_url, $content = '', $author = '', $digest = '', $show_cover_pic = 0, $need_open_comment = 0, $only_fans_can_comment = 1)
  483. {
  484. $data = is_array($title) ? $title : compact('title', 'thumb_media_id', 'source_url', 'content', 'author', 'digest', 'show_cover_pic', 'need_open_comment', 'only_fans_can_comment');
  485. return new Article($data);
  486. }
  487. /**
  488. * 作为客服消息发送
  489. * @param $to
  490. * @param $message
  491. * @return bool
  492. */
  493. public static function staffTo($to, $message)
  494. {
  495. $staff = self::staffService();
  496. $staff = is_callable($message) ? $staff->message($message()) : $staff->message($message);
  497. $res = $staff->to($to)->send();
  498. return $res;
  499. }
  500. /**
  501. * 获得用户信息
  502. * @param array|string $openid
  503. * @return \EasyWeChat\Support\Collection
  504. */
  505. public static function getUserInfo($cid, $openid)
  506. {
  507. $userService = self::userService($cid);
  508. $userInfo = is_array($openid) ? $userService->select($openid) : $userService->get($openid);
  509. return $userInfo;
  510. }
  511. /**
  512. * 生成支付签约订单对象
  513. * @param $openid
  514. * @param $out_trade_no
  515. * @param $total_fee
  516. * @param $attach
  517. * @param $body
  518. * @param string $detail
  519. * @param string $trade_type
  520. * @param array $options
  521. * @return Order
  522. */
  523. public static function paysignedOrder($openid, $out_trade_no, $total_fee, $attach, $body,$contract_code, $plan_id,$spbill_create_ip,$detail = '', $trade_type = 'JSAPI', $options = [], $cid = 0,$contract_display_account='')
  524. {
  525. $total_fee = bcmul($total_fee, 100, 0);
  526. $order = array_merge(compact('out_trade_no', 'total_fee', 'attach', 'body', 'detail', 'trade_type', 'openid','contract_code','plan_id','spbill_create_ip','contract_display_account'), $options);
  527. if ($order['detail'] == '') unset($order['detail']);
  528. $order['contract_notify_url']=Request::instance()->domain() . "/api/wechat/notify/" . $cid;
  529. $result = self::payment(false, $cid)->order->unify(
  530. $order,true
  531. );
  532. // var_dump($result);die();
  533. @file_put_contents("quanju.txt", json_encode($result) . "-签约返回结果\r\n", 8);
  534. return $result;
  535. }
  536. /**
  537. * 纯签约订单对象
  538. * @param $openid
  539. * @param $out_trade_no
  540. * @param $total_fee
  541. * @param $attach
  542. * @param $body
  543. * @param string $detail
  544. * @param string $trade_type
  545. * @param array $options
  546. */
  547. public static function signedOrder($contract_code, $plan_id, $cid = 0,$contract_display_account='')
  548. {
  549. // $total_fee = bcmul($total_fee, 100, 0);
  550. // $timestamp=time();
  551. // $version = '1.0';
  552. $company = Company::where('id',$cid)->find();
  553. if (empty($company)){
  554. return'企业不存在';
  555. }
  556. // 微信支付配置参数
  557. // $mch_id = "1623907696";
  558. $mch_id = $company['pay_weixin_mchid'];
  559. // $key = "1wm55KpF5tgZFW1TYs6TBX9MWBpI5FmT";
  560. $key = $company['wechat_encodingaeskey'];
  561. $app_id = $company['wechat_appid'];
  562. // 使用动态时间戳
  563. $current_timestamp = time();
  564. // $contract_code = preg_replace("/[^0-9]/", "", $contract_code);
  565. $params = [
  566. // 'appid' => 'wx5681205d1ef4d9d3',
  567. 'appid' => $app_id,
  568. 'mch_id' => $mch_id,
  569. 'plan_id' => $plan_id,
  570. 'contract_code' => $contract_code,
  571. 'request_serial' => $current_timestamp, // 使用时间戳作为序列号
  572. 'contract_display_account' => $contract_display_account,
  573. 'notify_url' => 'http://red.igxys.com/api/wechat/notify/'.$cid,
  574. 'timestamp' => $current_timestamp, // 动态时间戳
  575. 'version' => '1.0',
  576. ];
  577. // 关键步骤1:先对需要编码的参数进行原始值存储
  578. $raw_params = $params;
  579. // 关键步骤2:对需要URL编码的参数值进行编码(签名前)
  580. // $params['contract_display_account'] = urlencode($params['contract_display_account']);
  581. // 步骤3:参数按ASCII排序
  582. ksort($params);
  583. // 步骤4:构建待签名字符串(使用编码后的值)
  584. $stringA = "";
  585. foreach ($params as $k => $v) {
  586. $stringA .= $k . '=' . $v . '&';
  587. }
  588. $stringSignTemp = $stringA . 'key=' . $key;
  589. // 关键步骤5:正确生成HMAC-SHA256签名
  590. // $sign = strtoupper(hash_hmac('sha256', $stringSignTemp, '')); // 注意:第三个参数为空字符串
  591. $sign = self::generateWechatMD5Sign($params, $key);
  592. $params['sign'] = $sign;
  593. $params['notify_url'] = rawurlencode($params['notify_url']);
  594. // $params['contract_display_account'] = urlencode($params['contract_display_account']);
  595. // 构建最终URL
  596. $queryString = "";
  597. foreach ($params as $k => $v) {
  598. // 已编码的参数不需要再次编码
  599. $queryString .= $k . '=' . $v . '&';
  600. }
  601. $url = 'https://api.mch.weixin.qq.com/papay/entrustweb?' . rtrim($queryString, '&');
  602. // 记录请求URL
  603. @file_put_contents("quanju3.txt", $url . "-请求URL\r\n", 8);
  604. // return $url;
  605. //// 发送请求(禁止自动重定向)
  606. // $ch = curl_init();
  607. // curl_setopt($ch, CURLOPT_URL, $url);
  608. // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  609. // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
  610. // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  611. // curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); // 关键:禁止自动重定向
  612. // curl_setopt($ch, CURLOPT_HEADER, true); // 获取响应头
  613. //
  614. // $response = curl_exec($ch);
  615. // $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  616. //
  617. //// 记录完整响应
  618. // @file_put_contents("quanju3.txt", "HTTP状态码: " . $http_code . "\n响应内容: " . $response . "\n", FILE_APPEND);
  619. //
  620. //// 分析响应
  621. // if ($http_code == 302) {
  622. // // 解析重定向地址
  623. // preg_match('/Location: (.*)/i', $response, $matches);
  624. // $redirect_url = trim($matches[1] ?? '');
  625. //
  626. // if ($redirect_url) {
  627. // // 成功重定向到微信签约页面
  628. // @file_put_contents("quanju3.txt", "签约URL: " . $redirect_url . "\n", FILE_APPEND);
  629. // } else {
  630. // @file_put_contents("quanju3.txt", "错误:未找到重定向地址\n", FILE_APPEND);
  631. // }
  632. // } else {
  633. // // 提取可能的错误信息
  634. // preg_match('/<error_description><!\[CDATA\[(.*?)\]\]><\/error_description>/', $response, $error_matches);
  635. // $error_msg = $error_matches[1] ?? '未知错误';
  636. //
  637. // @file_put_contents("quanju3.txt", "错误信息: " . $error_msg . "\n", FILE_APPEND);
  638. // }
  639. //
  640. // curl_close($ch);
  641. // if (curl_errno($ch)) {
  642. // echo 'CURL Error: ' . curl_error($ch);
  643. // } else {
  644. // echo "Response:\n" . $response;
  645. // }
  646. // $request_serial = time();
  647. // $timestamp = time();
  648. // $order = array_merge(compact( 'contract_code','plan_id','contract_display_account','timestamp','version','request_serial'), $options);
  649. //// if ($order['detail'] == '') unset($order['detail']);
  650. // $order['notify_url']=Request::instance()->domain() . "/api/wechat/notify/" . $cid;
  651. // $result = self::payment(false, $cid)->contract->web(
  652. // $order
  653. // );
  654. // var_dump($result);die();
  655. // @file_put_contents("quanju3.txt", json_encode($response) . "-签约返回结果测试\r\n", 8);
  656. return $url;
  657. }
  658. public static function generateWechatMD5Sign($params, $key) {
  659. // 1. 过滤空值和签名参数
  660. $filteredParams = array_filter($params, function($value, $key) {
  661. return $value !== '' && $key !== 'sign';
  662. }, ARRAY_FILTER_USE_BOTH);
  663. // 2. 按键名ASCII字典序排序
  664. ksort($filteredParams);
  665. // 3. 拼接键值对
  666. $stringA = '';
  667. foreach ($filteredParams as $k => $v) {
  668. $stringA .= "{$k}={$v}&";
  669. }
  670. // 4. 拼接API密钥
  671. $stringSignTemp = $stringA . "key={$key}";
  672. // 5. MD5加密并转为大写
  673. return strtoupper(md5($stringSignTemp));
  674. }
  675. /**
  676. * 签约申请扣款
  677. * @param $openid
  678. * @param $out_trade_no
  679. * @param $total_fee
  680. * @param $attach
  681. * @param $body
  682. * @param string $detail
  683. * @param string $trade_type
  684. * @param array $options
  685. * @return Order
  686. */
  687. public static function papPayApply($mch_id,$out_trade_no, $total_fee, $attach, $detail = '', $trade_type = 'PAP', $options = [], $cid = 0,$contract_id='',$body='月捐款',$nonce_str='')
  688. {
  689. $total_fee = bcmul($total_fee, 100, 0);
  690. $order = array_merge(compact('mch_id','out_trade_no', 'total_fee', 'attach', 'detail', 'trade_type','contract_id','body','nonce_str'), $options);
  691. if ($order['detail'] == '') unset($order['detail']);
  692. $order['notify_url']=Request::instance()->domain() . "/api/wechat/notify/" . $cid;
  693. $result = self::payment2(false, $cid)->contract->apply(
  694. $order
  695. );
  696. // var_dump($result);die();
  697. return $result;
  698. }
  699. /**
  700. * 解除签约
  701. */
  702. public static function deleteSign($mch_id,$contract_code,$plan_id,$version='1.0',$options=[],$cid=0)
  703. {
  704. $contract_termination_remark = '月捐款解约';
  705. $order = array_merge(compact('mch_id','contract_code','plan_id','version','contract_termination_remark'), $options);
  706. // if ($order['detail'] == '') unset($order['detail']);
  707. // $order['notify_url']=Request::instance()->domain() . "/api/wechat/notify/" . $cid;
  708. $result = self::payment(false, $cid)->contract->delete(
  709. $order
  710. );
  711. return $result;
  712. }
  713. /**
  714. * 查询签约
  715. */
  716. public static function querySign($mch_id,$contract_code,$pan_id,$version='1.0',$options=[],$cid=0)
  717. {
  718. $appid='wx5681205d1ef4d9d3';
  719. $order = array_merge(compact('appid','mch_id','contract_code','pan_id','version'), $options);
  720. // if ($order['detail'] == '') unset($order['detail']);
  721. // $order['notify_url']=Request::instance()->domain() . "/api/wechat/notify/" . $cid;
  722. $result = self::payment(false, $cid)->contract->query(
  723. $order
  724. );
  725. @file_put_contents("quanju.txt", json_encode($result) . "-查询签约状态返回结果\r\n", 8);
  726. return $result;
  727. }
  728. /**
  729. * 查询签约订单
  730. */
  731. public static function querySignOrder($order_id,$cid=0)
  732. {
  733. @file_put_contents("quanju2.txt", $order_id . "-查询的订单id\r\n", 8);
  734. @file_put_contents("quanju2.txt", $cid . "-cid\r\n", 8);
  735. $result = self::payment2(false, $cid)->order->queryByOutTradeNumber(
  736. $order_id
  737. );
  738. @file_put_contents("quanju2.txt", json_encode($result) . "-查询签约状态返回结果\r\n", 8);
  739. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] !='FAIL'){
  740. if (!empty($result['trade_state_desc'])){
  741. if ($result['trade_state_desc'] == '支付成功'){
  742. $time=strtotime($result['time_end']);
  743. LaveMonth::where(['order_id' => $order_id])->update(['paid' => 1,'paytime'=>$time]); //修改订单状态
  744. }
  745. }
  746. }
  747. return $result;
  748. }
  749. /**
  750. * 查询签约关系
  751. */
  752. public static function querycontract($mch_id,$contract_id,$version='1.0',$options=[],$cid=0)
  753. {
  754. $appid='wx5681205d1ef4d9d3';
  755. $order = array_merge(compact('appid','mch_id','contract_id','version'), $options);
  756. // if ($order['detail'] == '') unset($order['detail']);
  757. // $order['notify_url']=Request::instance()->domain() . "/api/wechat/notify/" . $cid;
  758. $result = self::payment(false, $cid)->contract->querycontract(
  759. $order
  760. );
  761. // @file_put_contents("quanju.txt", json_encode($result) . "-查询签约状态返回结果\r\n", 8);
  762. return $result;
  763. }
  764. }