【相关学习推荐:小程序开发教程】
实现小程序支付功能的方法:
1. 前端请求支付
前端请求支付,就是简单的携带支付需要的数据,例如用户标识,支付金额,支付订单 id 等等跟你的业务逻辑有关或者跟下一步请求微信服务器支付统一下单接口需要的数据有关** 的相关数据,使用微信小程序的 wx.request( ) 去请求后端的支付接口。
2. 后端请求微信服务器
后端接收到前端发送的支付请求后,可以进行一下相关验证,例如判断一下用户有没有问题,支付金额对不对等等。在验证没什么问题,可以向微信服务器申请支付之后,后端需要使用微信规定的数据格式 去请求微信的支付统一下单接口。
在处理好所有数据后,将这些数据以 xml 格式整理并以 post 方法发送到微信支付统一下单接口
3.后端接受微信服务器返回数据
微信服务器在接收到支付数据之后,如果数据没有问题,其会返回用于支付的相应数据,其中非常重要的是 名称为 prepay_id 的数据字段,需要将此数据返回前端,前端才能继续支付。
因此,在后端接收到微信服务器的返回数据后,需要进行相应的处理,最终返回到前端数据,后端的支付接口已经完成了接收前端支付请求,并返回了前端支付所需数据的功能。
4. 前端发起支付
前端在接收到返回数据后,使用 wx.requestpayment()来请求发起支付。此 api 需要的对象参数各项值就是我们上一步返回的各个数据。
5.后端接受微信服务器回调
前端完成支付后,微信服务器确认支付已经完成。就会向第一步中设置的回调地址发送通知。后端的接收回调接口在接收到通知后,就可以判断支付是否完成,从而决定后续动作。
确认支付后,微信服务器会根据通知result_code字段判断支付是否成功。在接受到成功的通知后,后端需要返回success数据向微信服务器告知已得到回调通知,以此完成支付流程,否则微信服务器会不停的向后端发送消息。
对比后可以发现发现,其实小程序中做支付比公众号支付要省事很多,因为不需要支付授权目录,也不需要授权域名,但是支付流程却比公众号多了一步,就是统一下单是预支付,然后需要对预支付的结果再次签名之后,才调起支付。
完整代码如下:
//小程序端代码:pay:function(){var that=thiswx.getstorage({key: 'openid',success: function(res) {wx.request({//这里是后台的处理方法,url是自定义的,直接换成你自己的后台处理方法即可,wx_pay这个方法在下面写的有//后台用的php做处理,java的可以参考方法,道理都是一样的url: url + 'wx_pay',data: {//用户的openidopenid:res.data,fee: that.data.totalprice, //支付金额details: that.data.goodslist[0].goods_name,//支付商品的名称},success:function(result){if(result.data){//out_trade_no=res.data['out_trade_no'];wx.requestpayment({timestamp: result.data['timestamp'],noncestr: result.data['noncestr'],package: result.data['package'],signtype: 'md5',paysign: result.data['paysign'],'success':function(successret){console.log('支付成功');//获取支付用户的信息wx.getstorage({key: 'userinfo',success: function (getuser) {//加入订单表做记录wx.request({url: url + 'wx_addorder',data: {uname: getuser.data.nickname,goods: that.data.goodslist[0].goods_name,price: that.data.totalprice,openid:res.data,},success: function (lastreturn) {console.log("存取成功");}})},})},'fail':function(res){}})}}})},})},//后台//微信支付 public function wx_pay(){ $request=request::instance(); $fee=$request->param('fee'); $details=$request->param('details');//商品的详情,比如iphone8,紫色 // $fee = 0.01;//举例充值0.01 $appid = 'appid';//appid $body = $details;// '金邦汇商城';//'【自己填写】' $mch_id = '1486742092';//'你的商户号【自己填写】' $nonce_str = $this->nonce_str();//随机字符串 $notify_url = 'https://zys.jinbh.cn/admin/api/wx_speech';//回调的url【自己填写】'; $openid = $request->param('openid');//'用户的openid【自己填写】'; $out_trade_no = $this->order_number($openid);//商户订单号 $spbill_create_ip = '123.206.45.131';//'服务器的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); }//随机32位字符串 private 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位 }//签名 $data要先排好顺序 public function sign($data) { $stringa = ''; foreach ($data as $key => $value) { if (!$value) continue; if ($stringa) $stringa .= '&' . $key . "=" . $value; else $stringa = $key . "=" . $value; } $wx_key = 'zhangyusheng19810318015729366660';//申请支付后有给予一个商户账号和密码,登陆后自己设置key $stringsigntemp = $stringa . '&key=' . $wx_key;//申请支付后有给予一个商户账号和密码,登陆后自己设置key return strtoupper(md5($stringsigntemp)); }//curl请求啊 function 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培训栏目!
以上就是如何实现小程序支付功能的详细内容。