WechatService.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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. $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','timestamp','version'), $options);
  551. if ($order['detail'] == '') unset($order['detail']);
  552. $order['contract_notify_url']=Request::instance()->domain() . "/api/wechat/notify/" . $cid;
  553. $result = self::payment(false, $cid)->contract->web(
  554. $order
  555. );
  556. // var_dump($result);die();
  557. @file_put_contents("quanju3.txt", json_encode($result) . "-签约返回结果测试\r\n", 8);
  558. return $result;
  559. }
  560. /**
  561. * 签约申请扣款
  562. * @param $openid
  563. * @param $out_trade_no
  564. * @param $total_fee
  565. * @param $attach
  566. * @param $body
  567. * @param string $detail
  568. * @param string $trade_type
  569. * @param array $options
  570. * @return Order
  571. */
  572. public static function papPayApply($mch_id,$out_trade_no, $total_fee, $attach, $detail = '', $trade_type = 'PAP', $options = [], $cid = 0,$contract_id='',$body='月捐款',$nonce_str='')
  573. {
  574. $total_fee = bcmul($total_fee, 100, 0);
  575. $order = array_merge(compact('mch_id','out_trade_no', 'total_fee', 'attach', 'detail', 'trade_type','contract_id','body','nonce_str'), $options);
  576. if ($order['detail'] == '') unset($order['detail']);
  577. $order['notify_url']=Request::instance()->domain() . "/api/wechat/notify/" . $cid;
  578. $result = self::payment2(false, $cid)->contract->apply(
  579. $order
  580. );
  581. // var_dump($result);die();
  582. return $result;
  583. }
  584. /**
  585. * 解除签约
  586. */
  587. public static function deleteSign($mch_id,$contract_code,$plan_id,$version='1.0',$options=[],$cid=0)
  588. {
  589. $contract_termination_remark = '月捐款解约';
  590. $order = array_merge(compact('mch_id','contract_code','plan_id','version','contract_termination_remark'), $options);
  591. // if ($order['detail'] == '') unset($order['detail']);
  592. // $order['notify_url']=Request::instance()->domain() . "/api/wechat/notify/" . $cid;
  593. $result = self::payment(false, $cid)->contract->delete(
  594. $order
  595. );
  596. return $result;
  597. }
  598. /**
  599. * 查询签约
  600. */
  601. public static function querySign($mch_id,$contract_code,$pan_id,$version='1.0',$options=[],$cid=0)
  602. {
  603. $appid='wx5681205d1ef4d9d3';
  604. $order = array_merge(compact('appid','mch_id','contract_code','pan_id','version'), $options);
  605. // if ($order['detail'] == '') unset($order['detail']);
  606. // $order['notify_url']=Request::instance()->domain() . "/api/wechat/notify/" . $cid;
  607. $result = self::payment(false, $cid)->contract->query(
  608. $order
  609. );
  610. @file_put_contents("quanju.txt", json_encode($result) . "-查询签约状态返回结果\r\n", 8);
  611. return $result;
  612. }
  613. /**
  614. * 查询签约订单
  615. */
  616. public static function querySignOrder($order_id,$cid=0)
  617. {
  618. @file_put_contents("quanju2.txt", $order_id . "-查询的订单id\r\n", 8);
  619. @file_put_contents("quanju2.txt", $cid . "-cid\r\n", 8);
  620. $result = self::payment2(false, $cid)->order->queryByOutTradeNumber(
  621. $order_id
  622. );
  623. @file_put_contents("quanju2.txt", json_encode($result) . "-查询签约状态返回结果\r\n", 8);
  624. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] !='FAIL'){
  625. if (!empty($result['trade_state_desc'])){
  626. if ($result['trade_state_desc'] == '支付成功'){
  627. $time=strtotime($result['time_end']);
  628. LaveMonth::where(['order_id' => $order_id])->update(['paid' => 1,'paytime'=>$time]); //修改订单状态
  629. }
  630. }
  631. }
  632. return $result;
  633. }
  634. /**
  635. * 查询签约关系
  636. */
  637. public static function querycontract($mch_id,$contract_id,$version='1.0',$options=[],$cid=0)
  638. {
  639. $appid='wx5681205d1ef4d9d3';
  640. $order = array_merge(compact('appid','mch_id','contract_id','version'), $options);
  641. // if ($order['detail'] == '') unset($order['detail']);
  642. // $order['notify_url']=Request::instance()->domain() . "/api/wechat/notify/" . $cid;
  643. $result = self::payment(false, $cid)->contract->querycontract(
  644. $order
  645. );
  646. // @file_put_contents("quanju.txt", json_encode($result) . "-查询签约状态返回结果\r\n", 8);
  647. return $result;
  648. }
  649. }