收藏
回答

虚拟支付 mp_sig签名一直抱错?




   
   function request_post($url , $post_data )
   {
       $data_string = json_encode($post_data);
       $ch = curl_init ();
       curl_setopt ( $ch, CURLOPT_URL, $url );
       curl_setopt ( $ch, CURLOPT_POST, 1 );
       curl_setopt($ch, CURLOPT_HEADER, false);
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
       // curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
       curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
       curl_setopt($ch, CURLOPT_HTTPHEADER, array(
           'Content-Type: application/json',
           'Content-Length: ' . strlen($data_string)
       ));
       $result = curl_exec($ch);
       if (curl_errno($ch)) {
           print curl_error($ch);
       }
       curl_close($ch);
       return $result ;
   }
   //创建签名  
   function create_sig( $post_data ,$appKey)
   {
       ksort($post_data);
        $o = "";
       foreach ( $post_data as $k => $v )
       {
           $o.= "$k=" . rawurlencode( $v ). "&" ;
       }
       $stringA = substr($o,0,-1);
//       stringA+"&org_loc=/cgi-bin/midas/getbalance&method=POST&secret=zNLgAGgqsEWJOg1nFVaO5r7fAlIQxr1u"
       $stringSignTemp=$stringA."&org_loc=/cgi-bin/midas/getbalance&method=POST&secret=".$appKey;
               // file_put_contents ( 'create_sig.log', $stringSignTemp . PHP_EOL, FILE_APPEND | LOCK_EX );
       $s = hash_hmac('sha256', $stringSignTemp, $appKey, false);
       // $s = hash_hmac('sha256', $stringSignTemp, strtr($appKey, '-_', '+/'), true);
       // $sig = base64_encode($s);
       $sig = $s ;
       return $sig;
   }
   function create_mp_sig( $post_data,$session_key)
   {
       
       ksort($post_data);
        $o = "";
       foreach ( $post_data as $k => $v )
       {
           $o.= "$k=" . rawurlencode( $v ). "&" ;
       }
       $stringA = substr($o,0,-1);
       //$stringSignTemp=stringA+"&org_loc=/cgi-bin/midas/getbalance&method=POST&session_key=V7Q38/i2KXaqrQyl2Yx9Hg=="
       $stringSignTemp=$stringA."&org_loc=/cgi-bin/midas/getbalance&method=POST&session_key=".$session_key;
           file_put_contents ( 'create_mp_sig.log', $stringSignTemp . PHP_EOL, FILE_APPEND | LOCK_EX );
       $s = hash_hmac('sha256', $stringSignTemp, $session_key, false);
       // $s = hash_hmac('sha256', $stringSignTemp, strtr($session_key, '-_', '+/'), true);
       // $sig = base64_encode($s);
        $sig = $s ;
       return $sig;
   }
   $datastr = file_get_contents('php://input');
   $data = json_decode($datastr,true);
   // file_put_contents ( 'debug2222.log', var_export($data,true) . PHP_EOL, FILE_APPEND | LOCK_EX );
   
   $appSecret = "修改xxx";
   $appid = "修改";
   $offer_id = "修改"; //米大师分配的offer_id
   
   $openid = $data["openid"]; //
   $access_token = $data["access_token"];
   $session_key = $data["session_key"];
   $ts = time();
   $zone_id = "1";
   $pf = "android";
   $test = true;
   //正式
   $api = "https://api.weixin.qq.com/cgi-bin/midas/getbalance?access_token=$access_token";
   $offerSecret = "修改";
   //测试
   if($test)
   {
       $api = "https://api.weixin.qq.com/cgi-bin/midas/sandbox/getbalance?access_token=$access_token";
       $offerSecret = "修改";
   }
   if(isset($openid))
   {
       $dataArr = array("openid"=>$openid,"appid"=>$appid,"offer_id"=>$offer_id,"ts"=>$ts,"zone_id"=>$zone_id,"pf"=>$pf);
       $testSign1 = create_sig($dataArr,$offerSecret);
       $dataArr2 = array("openid"=>$openid,"appid"=>$appid,"offer_id"=>$offer_id,"ts"=>$ts,"zone_id"=>$zone_id,"pf"=>$pf,"access_token"=>$access_token,"sig"=>$testSign1);
       $mp_sig = create_mp_sig($dataArr2,$session_key);
       $dataArr3 = array("openid"=>$openid,"appid"=>$appid,"offer_id"=>$offer_id,"ts"=>$ts,"zone_id"=>$zone_id,"pf"=>$pf,"access_token"=>$access_token,"sig"=>$testSign1,"mp_sig"=>$mp_sig);
       $result = request_post($api,$dataArr3);
               file_put_contents ( 'debug333.log', var_export($dataArr3,true) . PHP_EOL, FILE_APPEND | LOCK_EX );
       file_put_contents ( 'debug2222.log', $result . PHP_EOL, FILE_APPEND | LOCK_EX );
       print_r($result);
       // $dataArrTest = array("openid"=>"odkx20ENSNa2w5y3g_qOkOvBNM1g","appid"=>"wx1234567","offer_id"=>"12345678","ts"=>"1507530737","zone_id"=>pf,"sig"=>"1ad64e8dcb2ec1dc486b7fdf01f4a15159fc623dc3422470e51cf6870734726b","access_token"=>"ACCESSTOKEN");
       // $sigTest = create_mp_sig($dataArrTest,"V7Q38/i2KXaqrQyl2Yx9Hg==");
       // file_put_contents ( 'test.log', $sigTest . PHP_EOL, FILE_APPEND | LOCK_EX );
   }
?>



在接内购 ,一直卡在  mp_sig 抱错 :{"errcode":90009,"errmsg":"mp_sig error hint: [r16KCA0903e257]"}


可是这个算法 用上微信官方的例子给出的参数  是和文档一模一样的 。为什么实际测试就会出现mp_sig

这个算法我已经参照文档反复比对。还是抱这个错,哪位大佬能指正下到底是哪里出问题了。文档参照的:

https://developers.weixin.qq.com/minigame/dev/tutorial/open-ability/midas-signature.html


最后一次编辑于  2018-06-05
回答关注问题邀请回答
收藏

3 个回答

  • 贾彦东👿
    贾彦东👿
    2018-09-27

    这种情况大多是需要你检查你的sessionKey的有效性

    2018-09-27
    赞同
    回复
  • 志涛
    志涛
    2018-08-07

    我今天接支付也一直报这个错,你解决了吗

    2018-08-07
    赞同
    回复
  • yefeng
    yefeng
    2018-06-06

    来个官方解答下啊,我测试按照文档的模拟数据都是和文档结果一致的,切成正式数据就签名错误,求解啊。也没有个固定的人,游戏就卡这了,等着上线呢

    2018-06-06
    赞同
    回复