博主,之前做微信支付,在网上很少找到支付的文章,所以就抽空把微信支付流程给整理出来,方便各位刚刚接触微信支付的用户,参考,如有不好之处,欢迎评论指出
1、必须开通支付,并且有备案的域名 和 配置 https
2.微信小程序处理
.wxml
.js
3 后台处理部分(博主使用php为例子)【其他语言参照修改就可以了】
以下参数不懂,可通过这个查看,微信文档查看参数的名称
https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_1
下面是实现的代码:
//public function Pay(){ $fee = 0.01;//0.01 $appid = 'appid';//appid $body = ''; $mch_id = ''; $nonce_str = $this->nonce_str();// $notify_url = 'url'; $openid = 'openid'; $out_trade_no = $this->order_number();//商户订单号 $spbill_create_ip = 'ip'; $total_fee = $fee*100;//1 1*100 $trade_type = 'JSAPI';// // $post['appid'] = $appid; $post['body'] = $body; $post['mch_id'] = $mch_id; $post['nonce_str'] = $nonce_str;// $post['notify_url'] = $notify_url; $post['openid'] = $openid; $post['out_trade_no'] = $out_trade_no; $post['spbill_create_ip'] = $spbill_create_ip;//ip $post['total_fee'] = $total_fee;// $post['trade_type'] = $trade_type; $sign = $this->sign($post);// $post_xml = '<xml> <appid>'.$appid.'</appid> <body>'.$body.'</body> <mch_id>'.$mch_id.'</mch_id> <nonce_str>'.$nonce_str.'</nonce_str> <notify_url>'.$notify_url.'</notify_url> <openid>'.$openid.'</openid> <out_trade_no>'.$out_trade_no.'</out_trade_no> <spbill_create_ip>'.$spbill_create_ip.'</spbill_create_ip> <total_fee>'.$total_fee.'</total_fee> <trade_type>'.$trade_type.'</trade_type> <sign>'.$sign.'</sign> </xml> '; //prepay_id $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; $xml = $this->http_request($url,$post_xml); $array = $this->xml($xml);// if($array['RETURN_CODE'] == 'SUCCESS' && $array['RESULT_CODE'] == 'SUCCESS'){ $time = time(); $tmp='';// $tmp['appId'] = $appid; $tmp['nonceStr'] = $nonce_str; $tmp['package'] = 'prepay_id='.$array['PREPAY_ID']; $tmp['signType'] = 'MD5'; $tmp['timeStamp'] = "$time"; $data['state'] = 1; $data['timeStamp'] = "$time";// $data['nonceStr'] = $nonce_str;// $data['signType'] = 'MD5';// MD5 $data['package'] = 'prepay_id='.$array['PREPAY_ID'];// prepay_id prepay_id=* $data['paySign'] = $this->sign($tmp);//,; $data['out_trade_no'] = $out_trade_no; }else{ $data['state'] = 0; $data['text'] = ""; $data['RETURN_CODE'] = $array['RETURN_CODE']; $data['RETURN_MSG'] = $array['RETURN_MSG']; } echo json_encode($data); }
//32private function nonce_str(){ $result = ''; $str = 'QWERTYUIOPASDFGHJKLZXVBNMqwertyuioplkjhgfdsamnbvcxz'; for ($i=0;$i<32;$i++){ $result .= $str[rand(0,48)]; } return $result; }
//private function order_number($openid){ //date('Ymd',time()).time().rand(10,99);//18位 return md5($openid.time().rand(10,99));//32位 }
// $dataprivate function sign($data){ $stringA = ''; foreach ($data as $key=>$value){ if(!$value) continue; if($stringA) $stringA .= '&'.$key."=".$value; else $stringA = $key."=".$value; }
$wx_key = '';//key$stringSignTemp = $stringA.'&key='.$wx_key;//key return strtoupper(md5($stringSignTemp));}
//curlfunction http_request($url,$data = null,$headers=array()) { $curl = curl_init(); if( count($headers) >= 1 ){ curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); } curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); 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; }
//xml private function xml($xml){ $p = xml_parser_create(); xml_parse_into_struct($p, $xml, $vals, $index); xml_parser_free($p); $data = ""; foreach ($index as $key=>$value) { if($key == 'xml' || $key == 'XML') continue; $tag = $vals[$value[0]]['tag']; $value = $vals[$value[0]]['value']; $data[$tag] = $value; } return $data; }
这里就实现微信支付了,看效果,在真机上面测试,才行
文章来源: 微信小程序支付,微信支付 php