Blogger Information
Blog 55
fans 0
comment 0
visits 59322
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
小程序支付
南鸢离梦的博客
Original
901 people have browsed it
  1. 直接把里面的参数替换成你的就可以了,前提是你要开通的有微信支付功能,需要商户号,appidappsecret,openid
  2. openid的获取比较简单,我再写一篇博客做介绍,不懂得可以私聊我
  3. //小程序端代码:
  4. pay:function(){
  5. var that=this
  6. wx.getStorage({
  7. key: 'openid',
  8. success: function(res) {
  9. wx.request({
  10. //这里是后台的处理方法,url是自定义的,直接换成你自己的后台处理方法即可,Wx_Pay这个方法在下面写的有
  11. //后台用的php做处理,java的可以参考方法,道理都是一样的
  12. url: url + 'Wx_Pay',
  13. data: {
  14. //用户的openid
  15. openid:res.data,
  16. fee: that.data.totalPrice, //支付金额
  17. details: that.data.goodsList[0].goods_name,//支付商品的名称
  18. },
  19. success:function(result){
  20. if(result.data){
  21. //out_trade_no=res.data['out_trade_no'];
  22. wx.requestPayment({
  23. timeStamp: result.data['timeStamp'],
  24. nonceStr: result.data['nonceStr'],
  25. package: result.data['package'],
  26. signType: 'MD5',
  27. paySign: result.data['paySign'],
  28. 'success':function(successret){
  29. console.log('支付成功');
  30. //获取支付用户的信息
  31. wx.getStorage({
  32. key: 'userInfo',
  33. success: function (getuser) {
  34. //加入订单表做记录
  35. wx.request({
  36. url: url + 'Wx_AddOrder',
  37. data: {
  38. uname: getuser.data.nickName,
  39. goods: that.data.goodsList[0].goods_name,
  40. price: that.data.totalPrice,
  41. openid:res.data,
  42. },
  43. success: function (lastreturn) {
  44. console.log("存取成功");
  45. }
  46. })
  47. },
  48. })
  49. },'fail':function(res){
  50. }
  51. })
  52. }
  53. }
  54. })
  55. },
  56. })
  57. },
  58. //后台
  59. //微信支付
  60. public function Wx_Pay(){
  61. $request=Request::instance();
  62. $fee=$request->param('fee');
  63. $details=$request->param('details');//商品的详情,比如iPhone8,紫色
  64. // $fee = 0.01;//举例充值0.01
  65. $appid = 'appid';//appid
  66. $body = $details;// '金邦汇商城';//'【自己填写】'
  67. $mch_id = '1486742092';//'你的商户号【自己填写】'
  68. $nonce_str = $this->nonce_str();//随机字符串
  69. $notify_url = 'https://zys.jinbh.cn/admin/Api/Wx_Speech';//回调的url【自己填写】';
  70. $openid = $request->param('openid');//'用户的openid【自己填写】';
  71. $out_trade_no = $this->order_number($openid);//商户订单号
  72. $spbill_create_ip = '123.206.45.131';//'服务器的ip【自己填写】';
  73. $total_fee = $fee*100;//因为充值金额最小是1 而且单位为分 如果是充值1元所以这里需要*100
  74. $trade_type = 'JSAPI';//交易类型 默认
  75. //这里是按照顺序的 因为下面的签名是按照顺序 排序错误 肯定出错
  76. $post['appid'] = $appid;
  77. $post['body'] = $body;
  78. $post['mch_id'] = $mch_id;
  79. $post['nonce_str'] = $nonce_str;//随机字符串
  80. $post['notify_url'] = $notify_url;
  81. $post['openid'] = $openid;
  82. $post['out_trade_no'] = $out_trade_no;
  83. $post['spbill_create_ip'] = $spbill_create_ip;//终端的ip
  84. $post['total_fee'] = $total_fee;//总金额 最低为一块钱 必须是整数
  85. $post['trade_type'] = $trade_type;
  86. $sign = $this->sign($post);//签名
  87. $post_xml = '<xml>
  88. <appid>'.$appid.'</appid>
  89. <body>'.$body.'</body>
  90. <mch_id>'.$mch_id.'</mch_id>
  91. <nonce_str>'.$nonce_str.'</nonce_str>
  92. <notify_url>'.$notify_url.'</notify_url>
  93. <openid>'.$openid.'</openid>
  94. <out_trade_no>'.$out_trade_no.'</out_trade_no>
  95. <spbill_create_ip>'.$spbill_create_ip.'</spbill_create_ip>
  96. <total_fee>'.$total_fee.'</total_fee>
  97. <trade_type>'.$trade_type.'</trade_type>
  98. <sign>'.$sign.'</sign>
  99. </xml> ';
  100. //统一接口prepay_id
  101. $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
  102. $xml = $this->http_request($url,$post_xml);
  103. $array = $this->xml($xml);//全要大写
  104. if($array['RETURN_CODE'] == 'SUCCESS' && $array['RESULT_CODE'] == 'SUCCESS'){
  105. $time = time();
  106. $tmp='';//临时数组用于签名
  107. $tmp['appId'] = $appid;
  108. $tmp['nonceStr'] = $nonce_str;
  109. $tmp['package'] = 'prepay_id='.$array['PREPAY_ID'];
  110. $tmp['signType'] = 'MD5';
  111. $tmp['timeStamp'] = "$time";
  112. $data['state'] = 1;
  113. $data['timeStamp'] = "$time";//时间戳
  114. $data['nonceStr'] = $nonce_str;//随机字符串
  115. $data['signType'] = 'MD5';//签名算法,暂支持 MD5
  116. $data['package'] = 'prepay_id='.$array['PREPAY_ID'];//统一下单接口返回的 prepay_id 参数值,提交格式如:prepay_id=*
  117. $data['paySign'] = $this->sign($tmp);//签名,具体签名方案参见微信公众号支付帮助文档;
  118. $data['out_trade_no'] = $out_trade_no;
  119. }else{
  120. $data['state'] = 0;
  121. $data['text'] = "错误";
  122. $data['RETURN_CODE'] = $array['RETURN_CODE'];
  123. $data['RETURN_MSG'] = $array['RETURN_MSG'];
  124. }
  125. echo json_encode($data);
  126. }
  127. //随机32位字符串
  128. private function nonce_str(){
  129. $result = '';
  130. $str = 'QWERTYUIOPASDFGHJKLZXVBNMqwertyuioplkjhgfdsamnbvcxz';
  131. for ($i=0;$i<32;$i++){
  132. $result .= $str[rand(0,48)];
  133. }
  134. return $result;
  135. }
  136. //生成订单号
  137. private function order_number($openid){
  138. //date('Ymd',time()).time().rand(10,99);//18位
  139. return md5($openid.time().rand(10,99));//32位
  140. }
  141. //签名 $data要先排好顺序
  142. public function sign($data)
  143. {
  144. $stringA = '';
  145. foreach ($data as $key => $value) {
  146. if (!$value) continue;
  147. if ($stringA) $stringA .= '&' . $key . "=" . $value;
  148. else $stringA = $key . "=" . $value;
  149. }
  150. $wx_key = 'Zhangyusheng19810318015729366660';//申请支付后有给予一个商户账号和密码,登陆后自己设置key
  151. $stringSignTemp = $stringA . '&key=' . $wx_key;//申请支付后有给予一个商户账号和密码,登陆后自己设置key
  152. return strtoupper(md5($stringSignTemp));
  153. }
  154. //curl请求啊
  155. function http_request($url, $data = null, $headers = array())
  156. {
  157. $curl = curl_init();
  158. if (count($headers) >= 1) {
  159. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  160. }
  161. curl_setopt($curl, CURLOPT_URL, $url);
  162. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  163. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  164. if (!empty($data)) {
  165. curl_setopt($curl, CURLOPT_POST, 1);
  166. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  167. }
  168. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  169. $output = curl_exec($curl);
  170. curl_close($curl);
  171. return $output;
  172. }
  173. //获取xml
  174. private function xml($xml){
  175. $p = xml_parser_create();
  176. xml_parse_into_struct($p, $xml, $vals, $index);
  177. xml_parser_free($p);
  178. $data = "";
  179. foreach ($index as $key=>$value) {
  180. if($key == 'xml' || $key == 'XML') continue;
  181. $tag = $vals[$value[0]]['tag'];
  182. $value = $vals[$value[0]]['value'];
  183. $data[$tag] = $value;
  184. }
  185. return $data;
  186. }
  187. //微信支付结束
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post