Update Test.php

This commit is contained in:
邹景立 2021-03-01 13:12:59 +08:00
parent 8dffe539af
commit 3dba53a719

View File

@ -176,7 +176,6 @@ class Test extends Controller
/** /**
* 微信JSAPI支付测试 * 微信JSAPI支付测试
* @return string * @return string
* @return void|string
* @throws \WeChat\Exceptions\LocalCacheException * @throws \WeChat\Exceptions\LocalCacheException
* @throws \think\admin\Exception * @throws \think\admin\Exception
* @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DataNotFoundException
@ -184,11 +183,11 @@ class Test extends Controller
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
* @throws \WeChat\Exceptions\InvalidResponseException * @throws \WeChat\Exceptions\InvalidResponseException
*/ */
public function jsapi() public function jsapi(): string
{ {
$this->url = $this->request->url(true); $this->url = $this->request->url(true);
$this->pay = WechatService::WePayOrder(); $this->pay = WechatService::WePayOrder();
$user = WechatService::instance()->getWebOauthInfo($this->url, 0); $user = WechatService::instance()->getWebOauthInfo($this->url);
if (empty($user['openid'])) return '<h3>网页授权获取OPENID失败</h3>'; if (empty($user['openid'])) return '<h3>网页授权获取OPENID失败</h3>';
// 生成预支付码 // 生成预支付码
$result = $this->pay->create([ $result = $this->pay->create([
@ -200,32 +199,29 @@ class Test extends Controller
'out_trade_no' => CodeExtend::uniqidDate(18), 'out_trade_no' => CodeExtend::uniqidDate(18),
'spbill_create_ip' => $this->request->ip(), 'spbill_create_ip' => $this->request->ip(),
]); ]);
// 创建JSAPI参数签名 // 数据参数格式化
$options = $this->pay->jsapiParams($result['prepay_id']); $resultJson = var_export($result, true);
// JSSDK 签名配置 $optionJson = json_encode($this->pay->jsapiParams($result['prepay_id']), JSON_UNESCAPED_UNICODE);
$optionJSON = json_encode($options, JSON_UNESCAPED_UNICODE); $configJson = json_encode(WechatService::instance()->getWebJssdkSign(), JSON_UNESCAPED_UNICODE);
$configJSON = json_encode(WechatService::instance()->getWebJssdkSign(), JSON_UNESCAPED_UNICODE); return <<<HTML
<pre>
echo '<pre>'; 当前用户OPENID: {$user['openid']}
echo "当前用户OPENID: {$user['openid']}"; \n\n--- 创建微信预支付码结果 ---\n {$resultJson}
echo "\n--- 创建预支付码 ---\n"; \n\n--- JSAPI H5 支付参数 ---\n {$optionJson}
var_export($result); </pre>
echo "\n\n--- JSAPI 及 H5 参数 ---\n"; <button id='paytest' type='button'>JSAPI支付测试</button>
var_export($options); <script src='//res.wx.qq.com/open/js/jweixin-1.6.0.js'></script>
echo '</pre>'; <script>
echo "<button id='paytest' type='button'>JSAPI支付测试</button>"; wx.config({$configJson});
echo " document.getElementById('paytest').onclick = function(){
<script src='//res.wx.qq.com/open/js/jweixin-1.2.0.js'></script> var options = {$optionJson};
<script> options.success = function(){
wx.config({$configJSON}); alert('支付成功');
document.getElementById('paytest').onclick = function(){ }
var options = {$optionJSON}; wx.chooseWXPay(options);
options.success = function(){ }
alert('支付成功'); </script>
} HTML;
wx.chooseWXPay(options);
}
</script>";
} }
/** /**