Test.php 8.5 KB

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