支付凭证优化

This commit is contained in:
Dev
2026-10-27 18:52:26 +08:00
parent 25804d500e
commit d6e67ef8ed
2 changed files with 47 additions and 5 deletions
@@ -31,6 +31,8 @@ class EasyWeChatService
$this->app = new MiniAppApplication([
'app_id' => $this->config['app_id'],
'secret' => $this->config['secret'],
// 稳定版凭证重复获取不会把上一张作废。旧接口并发刷新会返回 40001
'use_stable_access_token' => true,
'http' => ['throw' => false, 'timeout' => 10.0],
]);
@@ -46,6 +48,24 @@ class EasyWeChatService
]);
}
/**
* 刷新小程序 access_token。普通刷新不会使有效凭证失效;force 只在微信返回凭证无效时使用
* @param bool $force
* @return string
*/
public function refreshMiniAccessToken($force = false)
{
$accessToken = $this->app->getAccessToken();
if ($force && method_exists($accessToken, 'getStableAccessToken')) {
return $accessToken->getStableAccessToken(true);
}
if ($accessToken instanceof \EasyWeChat\Kernel\Contracts\RefreshableAccessToken) {
return $accessToken->refresh();
}
return '';
}
/**
* 小程序登录(code2session)
* @param string $code wx.login 获取的 code