{"errcode":40163,"errmsg":"code been used, hints: [ req_id: zd0638th44 ]"}
微信支付获取用户openid时,报出错误。原因是apache中同时配置了http和https,微信网页oauth认证通知了两次
$openId = $tools->GetOpenid();
public function GetOpenid()
{
//通过code获得openid
if (!isset($_GET[‘code‘])) {
//触发微信返回code码
$baseUrl = urlencode(‘https://‘ . $_SERVER[‘HTTP_HOST‘] . $_SERVER[‘REQUEST_URI‘]);
$url = $this->__CreateOauthUrlForCode($baseUrl);
Log::write(‘$url:‘ .$url);
Header("Location: $url");
exit();
} else {
//获取code码,以获取openid
$code = $_GET[‘code‘];
Log::write(‘code:‘ .$code);
$openid = $this->getOpenidFromMp($code);
return $openid;
}
}
错误消息提示code己经被使用过了,微信支付只调用一次GetOpenid()怎么会出现这种情况呢。
[ log ] GetOpenidFromMp: {"access_token":"DwiMImR_Cc8NmHsi4IZ4jAb8mrs6v5fEbiHDkS3Zv_UpiGF-LxaLWdC8S9YQ1yQAhHQ2IQpa6AvXNnFOUYbjP6KhxMwHZCeqhAi12rXtN3o","expires_in":7200,"refresh_token":"5Xn4mKIc7aRq08LFjf1Aw1EZS7v_KF10jeZP1Yd1-eFzlmdK-Tjeuw7SQ1wpFyI-44vEW7pQOlfy69FLvQuFXTXhFHK00lPWr-Uv1uPQXaw","openid":"o6DPDtwo9grbSux1UT9aD8mgyFvU","scope":"snsapi_base"}
[ log ] GetOpenidFromMp: {"errcode":40163,"errmsg":"code been used, hints: [ req_id: zd0638th44 ]"}
上面的两条日志是在一次支付中出现的,证明code确实被使用了两次。
bug是在配置https以后出现的,这个提示让我最终找到了原因。
我删除apache配置文件中http保留https,原因嘛就是微信通知2017-12-31日前微信开发必须使用https。