public function MakeSign($data) { //签名步骤一:按字典序排序参数 ksort($data); $string = $this->ToUrlParams($data); //print_r($string); //签名步骤二:在string后加入KEY $string = $string . "&key=tianxunhuliantianxunhuliantianxu"; //print_r($string); //签名步骤三:MD5加密 $string = md5($string); //print_r($string); //签名步骤四:所有字符转为大写 $result = strtoupper($string); //print_r($result); return $result; }
/** * 格式化参数格式化成url参数 */ public function ToUrlParams($data) { $buff = ""; foreach ($data as $k => $v) { if($k != "sign" && $v != "" && !is_array($v)){ $buff .= $k . "=" . $v . "&"; } } $buff = trim($buff, "&"); return $buff; }
/** * * 产生随机字符串,不长于32位 * @param int $length * @return 产生的随机字符串 */ public static function getNonceStr($length = 32) { $chars = "abcdefghijklmnopqrstuvwxyz0123456789"; $str =""; for ( $i = 0; $i < $length; $i++ ) { $str .= substr($chars, mt_rand(0, strlen($chars)-1), 1); } return $str; }
function arrayToXml($arr){ $xml = "<xml>"; foreach ($arr as $key=>$val){ if(is_array($val)){ $xml.="<".$key.">".arrayToXml($val)."</".$key.">"; }else{ $xml.="<".$key.">".$val."</".$key.">"; } } $xml.="</xml>"; return $xml; }
// 微信post数组,订单号,商品金额 public function getWxPay($post,$order_sn){ $client_ip = $_SERVER["REMOTE_ADDR"]; //客户端ip $data[‘appid‘] = "";//appid $data[‘mch_id‘] = "";//商户号 $data[‘nonce_str‘] = $this->getNonceStr(); //随机32位字符串 $data[‘body‘] = ‘xmm-‘.$order_sn; $data[‘out_trade_no‘] = $order_sn; $data[‘total_fee‘] = $post[‘goods_price‘]*100; //注意 单位是分 $data[‘spbill_create_ip‘] = $client_ip; $data[‘notify_url‘] = "http://域名/api/register/notify"; $data[‘trade_type‘] = "APP"; //交易类型 $data[‘attach‘]="app_flash"; //print_r($data); $data[‘sign‘] = $this->MakeSign($data); $xml = $this->arrayToXml($data); //print_r($xml); $url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; $request = $this->postXmlCurl($xml,$url); //print_r($request); return $this->FromXml($request); }
时间: 2024-10-12 10:46:34