function arrayToXml($arr) { $xml = "<xml>"; foreach ($arr as $key=>$val) { if (is_numeric($val)){ $xml.="<".$key.">".$val."</".$key.">"; }else{ $xml.="<".$key."><![CDATA[".$val."]]></".$key.">"; } } $xml.="</xml>"; return $xml; } function xmlToArray($xml) { //禁止引用外部xml实体 libxml_disable_entity_loader(true); $values = json_decode(json_encode(simplexml_load_string($xml, ‘SimpleXMLElement‘, LIBXML_NOCDATA)), true); return $values; }
$appid = C(‘APPID‘); //appid $mch_id = C(‘MCH_ID‘); //商户号 $key = C(‘KEY‘); //商户秘钥key $nonce_str = md5(time().mt_rand(0,1000)); //随机字符串 $total_fee = $order_list[‘goods_amount‘]*100; //订单金额 $refund_fee = $order_list[‘goods_amount‘]*100; //退款金额 $sign = md5("appid=$appid&mch_id=$mch_id&nonce_str=$nonce_str&out_refund_no=$out_refund_no&out_trade_no=$out_trade_no&refund_fee=$refund_fee&total_fee=$total_fee&key=$key"); $array = array( ‘appid‘ => $appid, ‘mch_id‘ => $mch_id, ‘nonce_str‘ => $nonce_str, ‘out_refund_no‘ => $out_refund_no, ‘out_trade_no‘ => $out_trade_no, ‘refund_fee‘ => $refund_fee, ‘total_fee‘ => $total_fee, ‘sign‘ => $sign, ); $url = ‘https://api.mch.weixin.qq.com/secapi/pay/refund‘; $xml = arrayToXml($array); $ch=curl_init(); curl_setopt($ch,CURLOPT_URL,$url); // curl_setopt($ch,CURLOPT_HEADER,1); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,1);//证书检查 curl_setopt($ch,CURLOPT_SSLCERTTYPE,‘pem‘); curl_setopt($ch,CURLOPT_SSLCERT,dirname(dirname(dirname(__FILE__))).‘/cert/apiclient_cert.pem‘); curl_setopt($ch,CURLOPT_SSLCERTTYPE,‘pem‘); curl_setopt($ch,CURLOPT_SSLKEY,dirname(dirname(dirname(__FILE__))).‘/cert/apiclient_key.pem‘); curl_setopt($ch,CURLOPT_SSLCERTTYPE,‘pem‘); curl_setopt($ch,CURLOPT_CAINFO,dirname(dirname(dirname(__FILE__))).‘/cert/rootca.pem‘); curl_setopt($ch,CURLOPT_POST,1); curl_setopt($ch,CURLOPT_POSTFIELDS,$xml); $data=curl_exec($ch); $data = xmlToArray($data); } if($data[‘return_code‘] == ‘SUCCESS‘ && $data[‘result_code‘] == ‘SUCCESS‘){ //返回来的是xml格式需要转换成数组再提取值,用来做更新 curl_close($ch); }else{
$ajax[‘status‘] = ‘error‘;
$ajax[‘content‘] = $data[‘err_code_des‘];
curl_close($ch);
}