Test.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2022 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // | 免费声明 ( https://thinkadmin.top/disclaimer )
  11. // +----------------------------------------------------------------------
  12. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  13. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  14. // +----------------------------------------------------------------------
  15. namespace app\wechat\controller\api;
  16. use app\wechat\service\WechatService;
  17. use think\admin\Controller;
  18. use think\admin\extend\CodeExtend;
  19. use think\Response;
  20. use WeChat\Contracts\Tools;
  21. /**
  22. * 微信测试工具
  23. * Class Test
  24. * @package app\wechat\controller\api
  25. */
  26. class Test extends Controller
  27. {
  28. /**
  29. * 微信JSAPI支付二维码
  30. * @return Response
  31. * @throws \Endroid\QrCode\Exceptions\ImageFunctionFailedException
  32. * @throws \Endroid\QrCode\Exceptions\ImageFunctionUnknownException
  33. * @throws \Endroid\QrCode\Exceptions\ImageTypeInvalidException
  34. */
  35. public function jsapiQrc(): Response
  36. {
  37. $this->url = sysuri('wechat/api.test/jsapi', [], false, true);
  38. return $this->_buildQrcResponse($this->url);
  39. }
  40. /**
  41. * 显示网页授权二维码
  42. * @return Response
  43. * @throws \Endroid\QrCode\Exceptions\ImageFunctionFailedException
  44. * @throws \Endroid\QrCode\Exceptions\ImageFunctionUnknownException
  45. * @throws \Endroid\QrCode\Exceptions\ImageTypeInvalidException
  46. */
  47. public function oauthQrc(): Response
  48. {
  49. $this->url = sysuri('wechat/api.test/oauth', [], false, true);
  50. return $this->_buildQrcResponse($this->url);
  51. }
  52. /**
  53. * 显示网页授权二维码
  54. * @return Response
  55. * @throws \Endroid\QrCode\Exceptions\ImageFunctionFailedException
  56. * @throws \Endroid\QrCode\Exceptions\ImageFunctionUnknownException
  57. * @throws \Endroid\QrCode\Exceptions\ImageTypeInvalidException
  58. */
  59. public function jssdkQrc(): Response
  60. {
  61. $this->url = sysuri('wechat/api.test/jssdk', [], false, true);
  62. return $this->_buildQrcResponse($this->url);
  63. }
  64. /**
  65. * 微信扫码支付模式一二维码显示
  66. * @return Response
  67. * @throws \Endroid\QrCode\Exceptions\ImageFunctionFailedException
  68. * @throws \Endroid\QrCode\Exceptions\ImageFunctionUnknownException
  69. * @throws \Endroid\QrCode\Exceptions\ImageTypeInvalidException
  70. */
  71. public function scanOneQrc(): Response
  72. {
  73. $pay = WechatService::WePayOrder();
  74. return $this->_buildQrcResponse($pay->qrcParams('8888888'));
  75. }
  76. /**
  77. * 扫码支付模式二测试二维码
  78. * @return Response
  79. * @throws \Endroid\QrCode\Exceptions\ImageFunctionFailedException
  80. * @throws \Endroid\QrCode\Exceptions\ImageFunctionUnknownException
  81. * @throws \Endroid\QrCode\Exceptions\ImageTypeInvalidException
  82. * @throws \WeChat\Exceptions\InvalidResponseException
  83. * @throws \WeChat\Exceptions\LocalCacheException
  84. */
  85. public function scanTwoQrc(): Response
  86. {
  87. $result = WechatService::WePayOrder()->create([
  88. 'body' => '测试商品',
  89. 'total_fee' => '1',
  90. 'trade_type' => 'NATIVE',
  91. 'notify_url' => sysuri('wechat/api.test/notify', [], false, true),
  92. 'out_trade_no' => CodeExtend::uniqidNumber(18),
  93. 'spbill_create_ip' => $this->request->ip(),
  94. ]);
  95. return $this->_buildQrcResponse($result['code_url']);
  96. }
  97. /**
  98. * 网页授权测试
  99. * @throws \WeChat\Exceptions\InvalidResponseException
  100. * @throws \WeChat\Exceptions\LocalCacheException
  101. * @throws \think\admin\Exception
  102. * @throws \think\db\exception\DataNotFoundException
  103. * @throws \think\db\exception\DbException
  104. * @throws \think\db\exception\ModelNotFoundException
  105. */
  106. public function oauth()
  107. {
  108. $this->url = $this->request->url(true);
  109. $this->fans = WechatService::instance()->getWebOauthInfo($this->url, 1);
  110. $this->fetch();
  111. }
  112. /**
  113. * JSSDK测试
  114. * @throws \WeChat\Exceptions\InvalidResponseException
  115. * @throws \WeChat\Exceptions\LocalCacheException
  116. * @throws \think\admin\Exception
  117. * @throws \think\db\exception\DataNotFoundException
  118. * @throws \think\db\exception\DbException
  119. * @throws \think\db\exception\ModelNotFoundException
  120. */
  121. public function jssdk()
  122. {
  123. $this->options = WechatService::instance()->getWebJssdkSign();
  124. $this->fetch();
  125. }
  126. /**
  127. * 微信扫码支付模式一通知处理
  128. * -- 注意,需要在微信商户配置支付通知地址
  129. * @return string
  130. * @throws \WeChat\Exceptions\InvalidResponseException
  131. * @throws \WeChat\Exceptions\LocalCacheException
  132. */
  133. public function scanOneNotify(): string
  134. {
  135. $pay = WechatService::WePayOrder();
  136. $notify = $pay->getNotify();
  137. p('======= 来自扫码支付1的数据 ======');
  138. p($notify);
  139. // 微信统一下单处理
  140. $options = [
  141. 'body' => "测试商品,产品ID:{$notify['product_id']}",
  142. 'total_fee' => '1',
  143. 'trade_type' => 'NATIVE',
  144. 'notify_url' => sysuri('wechat/api.test/notify', [], false, true),
  145. 'out_trade_no' => CodeExtend::uniqidDate(18),
  146. 'spbill_create_ip' => $this->request->ip(),
  147. ];
  148. p('======= 来自扫码支付1统一下单结果 ======');
  149. p($order = $pay->create($options));
  150. // 回复XML文本
  151. $result = [
  152. 'return_code' => 'SUCCESS',
  153. 'return_msg' => '处理成功',
  154. 'appid' => $notify['appid'],
  155. 'mch_id' => $notify['mch_id'],
  156. 'nonce_str' => Tools::createNoncestr(),
  157. 'prepay_id' => $order['prepay_id'],
  158. 'result_code' => 'SUCCESS',
  159. ];
  160. $result['sign'] = $pay->getPaySign($result);
  161. p('======= 来自扫码支付1返回的结果 ======');
  162. p($result);
  163. return Tools::arr2xml($result);
  164. }
  165. /**
  166. * 微信JSAPI支付测试
  167. * @return string
  168. * @throws \WeChat\Exceptions\LocalCacheException
  169. * @throws \think\admin\Exception
  170. * @throws \think\db\exception\DataNotFoundException
  171. * @throws \think\db\exception\DbException
  172. * @throws \think\db\exception\ModelNotFoundException
  173. * @throws \WeChat\Exceptions\InvalidResponseException
  174. */
  175. public function jsapi(): string
  176. {
  177. $this->url = $this->request->url(true);
  178. $this->pay = WechatService::WePayOrder();
  179. $user = WechatService::instance()->getWebOauthInfo($this->url);
  180. if (empty($user['openid'])) return '<h3>网页授权获取OPENID失败!</h3>';
  181. // 生成预支付码
  182. $result = $this->pay->create([
  183. 'body' => '测试商品',
  184. 'openid' => $user['openid'],
  185. 'total_fee' => '1',
  186. 'trade_type' => 'JSAPI',
  187. 'notify_url' => sysuri('wechat/api.test/notify', [], false, true),
  188. 'out_trade_no' => CodeExtend::uniqidDate(18),
  189. 'spbill_create_ip' => $this->request->ip(),
  190. ]);
  191. // 数据参数格式化
  192. $resultJson = var_export($result, true);
  193. $optionJson = json_encode($this->pay->jsapiParams($result['prepay_id']), JSON_UNESCAPED_UNICODE);
  194. $configJson = json_encode(WechatService::instance()->getWebJssdkSign(), JSON_UNESCAPED_UNICODE);
  195. return <<<HTML
  196. <pre>
  197. 当前用户OPENID: {$user['openid']}
  198. \n\n--- 创建微信预支付码结果 ---\n {$resultJson}
  199. \n\n--- JSAPI 及 H5 支付参数 ---\n {$optionJson}
  200. </pre>
  201. <button id='paytest' type='button'>JSAPI支付测试</button>
  202. <script src='//res.wx.qq.com/open/js/jweixin-1.6.0.js'></script>
  203. <script>
  204. wx.config({$configJson});
  205. document.getElementById('paytest').onclick = function(){
  206. var options = {$optionJson};
  207. options.success = function(){
  208. alert('支付成功');
  209. }
  210. wx.chooseWXPay(options);
  211. }
  212. </script>
  213. HTML;
  214. }
  215. /**
  216. * 支付通知接收处理
  217. * @return string
  218. * @throws \WeChat\Exceptions\InvalidResponseException
  219. */
  220. public function notify(): string
  221. {
  222. $wechat = WechatService::WePayOrder();
  223. p($wechat->getNotify());
  224. return 'SUCCESS';
  225. }
  226. /**
  227. * 创建二维码响应对应
  228. * @param string $url 二维码内容
  229. * @return Response
  230. * @throws \Endroid\QrCode\Exceptions\ImageFunctionFailedException
  231. * @throws \Endroid\QrCode\Exceptions\ImageFunctionUnknownException
  232. * @throws \Endroid\QrCode\Exceptions\ImageTypeInvalidException
  233. */
  234. private function _buildQrcResponse(string $url): Response
  235. {
  236. $qrCode = new \Endroid\QrCode\QrCode();
  237. $qrCode->setText($url)->setSize(300)->setPadding(20)->setImageType('png');
  238. return response($qrCode->get(), 200, ['Content-Type' => 'image/png']);
  239. }
  240. }