• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

微信支付查询订单V2 接口

武飞扬头像
欢乐程序员
帮助1

//查询订单是否存在
public function userOrderCheck(){
    
        $out_trade_no=  input("out_trade_no");   //订单号
        $type=  input("type"); //类型 1 充值,2 商城订单

        $appid  = 'XXXXXX';//小程序openid
 
        $secret = 'XXX';  //小程序secret
        $mch_id = 'XXXXX'; //商户id
        $paykey = 'XXXX';  //V2 密钥
        $access_token = $this->get_access_token_a();
        $url = 'https://api.mch.weixin.qq.com/pay/orderquery';
        //这里是按照顺序的 因为下面的签名是按照(字典序)顺序 排序错误 肯定出错
        $post['appid'] = $appid;
        $post['mch_id'] = $mch_id;
        $post['nonce_str'] = md5(mt_rand(0,1000));//随机字符串
        $post['out_trade_no'] = $out_trade_no;//微信支付记录订单号
        $sign = $this->MakeSign_a($post,$paykey);              //签名
        $post_xml = '<xml>
                       <appid>'.$appid.'</appid>
                       <mch_id>'.$mch_id.'</mch_id>
                       <nonce_str>'.$post['nonce_str'].'</nonce_str>
                       <out_trade_no>'.$post['out_trade_no'].'</out_trade_no>
                       <sign>'.$sign.'</sign>
                    </xml>';
        $xml = $this->https_request_a($url,$post_xml);
        $result = $this->xml2array_a($xml); 
     
        if($result["return_code"] =="SUCCESS" && $result["result_code"]=="SUCCESS"){
           //自己的业务逻辑

}   
        
}


    //获取token
    public function get_access_token_a(){

        $appid  = '';   //小程序openid
 
        $secret = 'xxxx';
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
        $res = $this->https_request_a($url,'');
        $access_token1=json_decode($res,true);
        $access_token = $access_token1['access_token'];
        return $access_token;
    }
    
       /**
     * 将参数拼接为url: key=value&key=value
     * @param $params
     * @return string
     */
    public function ToUrlParams_a( $params ){
        $string = '';
        if( !empty($params) ){
            $array = array();
            foreach( $params as $key => $value ){
                $array[] = $key.'='.$value;
            }
            $string = implode("&",$array);
        }
        return $string;
    }
    
      /**
     * 生成签名, $KEY就是支付key
     * @return 签名
     */
    public function MakeSign_a( $params,$KEY){
        //签名步骤一:按字典序排序数组参数
        ksort($params);
        $string = $this->ToUrlParams_a($params);  //参数进行拼接key=value&k=v
        //签名步骤二:在string后加入KEY
        $string = $string . "&key=".$KEY;
        //签名步骤三:MD5加密
        $string = md5($string);
        //签名步骤四:所有字符转为大写
        $result = strtoupper($string);
        return $result;

    }
    
    
    //获取xml里面数据,转换成array
    private function xml2array_a($xml) {
        if(!$xml) {
            throw new RuntimeException('xml 数据异常转换失败!!');
        }
        $map = array();
        libxml_disable_entity_loader(true);
        $map = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        // var_dump($map);
        return $map;
    }
    
    
    //post请求
    public function https_request_a($url,$data = null){
        if(function_exists('curl_init')){
          $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
            if (!empty($data)){
                curl_setopt($curl, CURLOPT_POST, 1);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            }
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($curl);
            curl_close($curl);
            return $output;
        }else{
          return false;
        }
    }
    
    

}

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfefkg
系列文章
更多 icon
同类精品
更多 icon
继续加载