WechatService.php 32 KB

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