微信支付查询订单V2 接口
//查询订单是否存在
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 = $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;
}
public function ToUrlParams_a( $params ){
$string = '';
if( !empty($params) ){
$array = array();
foreach( $params as $key => $value ){
$array[] = $key.'='.$value;
}
$string = implode("&",$array);
}
return $string;
}
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;
}
}
}
来源地址:https://blog.csdn.net/dangjunwei123/article/details/125652445
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341