[PHP] WeChat payment JsApi 40163 error

little bottle
Release: 2023-04-05 21:40:02
forward
4128 people have browsed it

This article will share with you the WeChat payment JsApi 40163 error and solution code.
Error:
Undefined array index: openid.
After inspection, it was found that: WeChat payment authorization obtains openId {"errcode":40163,"errmsg":"code been used",}
The reason is: WeChat payment code can only be used once, and it will be repeated the second time This error occurs when using.
Solving related reference: https://www.e-learn.cn/content/php/1102683
I tried the method in the reference to no avail, so I rewrote it according to the reason:
After troubleshooting, the problem was found Appears in: GetOpenid method in
WxPay.JsApiPay.php, the source code is:

/**
 * 
 * 通过跳转获取用户的openid,跳转流程如下:
 * 1、设置自己需要调回的url及其其他参数,跳转到微信服务器https://open.weixin.qq.com/connect/oauth2/authorize
 * 2、微信服务处理完成之后会跳转回用户redirect_uri地址,此时会带上一些参数,如:code
 * 
 * @return 用户的openid
 */
public function GetOpenid()
{

   //通过code获得openid
   if (!isset($_GET['code']) ){
      //触发微信返回code码
      $baseUrl = urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
      $url = $this->__CreateOauthUrlForCode($baseUrl);
      Header("Location: $url");
      exit();
   } else {
      //获取code码,以获取openid
       $code = $_GET['code'];
       $openid = $this->getOpenidFromMp($code);
    
       return $openid;
   }
}
Copy after login

The modified code is:

/**
 * 
 * 通过跳转获取用户的openid,跳转流程如下:
 * 1、设置自己需要调回的url及其其他参数,跳转到微信服务器https://open.weixin.qq.com/connect/oauth2/authorize
 * 2、微信服务处理完成之后会跳转回用户redirect_uri地址,此时会带上一些参数,如:code
 * 
 * @return 用户的openid
 */
public function GetOpenid()
{

   //通过code获得openid
   if (!isset($_GET['code']) ){
      //触发微信返回code码
      $baseUrl = urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
      $url = $this->__CreateOauthUrlForCode($baseUrl);
      Header("Location: $url");
      exit();
   } else {
      //获取code码,以获取openid
       $code = $_GET['code'];
           if(session("?$code")){
               $openid = $this->getOpenidFromMp($code);
           }else{
               $openid= session($code);
           }
           session($code, $openid);// ######  2019.03.01  加  为解决code been used

      return $openid;
   }
}
Copy after login

The logic is to convert the obtained openid into code Save the session with the name; when requesting again, check whether the session with the name code in the request exists to prevent the code from being used twice.

Related tutorials: PHP video tutorial

The above is the detailed content of [PHP] WeChat payment JsApi 40163 error. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template