用户
 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,登录网站

微信小程序开发联盟 首页 教程 查看内容

小程序与php 实现微信支付

Rolan 2017-6-18 00:04

小程序访问地址:payfee.PHP: view plain copyinclude 'WeixinPay.php'; $appid=''; $openid= $_GET; $mch_id=''; $key=''; $out_trade_no = $mch_id. time(); $total_fee = $_GET; if(empty($total_fee)) //押金 { ...

小程序访问地址: 
payfee.PHP:

  1. [php] view plain copy
  2. include 'WeixinPay.php';
  3. $appid='';
  4. $openid= $_GET['id'];
  5. $mch_id='';
  6. $key='';
  7. $out_trade_no = $mch_id. time();
  8. $total_fee = $_GET['fee'];
  9. if(empty($total_fee)) //押金
  10. {
  11. $body = "充值押金";
  12. $total_fee = floatval(99*100);
  13. }
  14. else {
  15. $body = "充值余额";
  16. $total_fee = floatval($total_fee*100);
  17. }
  18. $weixinpay = new WeixinPay($appid,$openid,$mch_id,$key,$out_trade_no,$body,$total_fee);
  19. $return=$weixinpay->pay();
  20. echo json_encode($return);

WeixinPay.php:

  1. [php] view plain copy
  2. <?php
  3. /*
  4. * 小程序微信支付
  5. */
  6. class WeixinPay {
  7. protected $appid;
  8. protected $mch_id;
  9. protected $key;
  10. protected $openid;
  11. protected $out_trade_no;
  12. protected $body;
  13. protected $total_fee;
  14. function __construct($appid, $openid, $mch_id, $key,$out_trade_no,$body,$total_fee) {
  15. $this->appid = $appid;
  16. $this->openid = $openid;
  17. $this->mch_id = $mch_id;
  18. $this->key = $key;
  19. $this->out_trade_no = $out_trade_no;
  20. $this->body = $body;
  21. $this->total_fee = $total_fee;
  22. }
  23. public function pay() {
  24. //统一下单接口
  25. $return = $this->weixinapp();
  26. return $return;
  27. }
  28. //统一下单接口
  29. private function unifiedorder() {
  30. $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
  31. $parameters = array(
  32. 'appid' => $this->appid, //小程序ID
  33. 'mch_id' => $this->mch_id, //商户号
  34. 'nonce_str' => $this->createNoncestr(), //随机字符串
  35. // 'body' => 'test', //商品描述
  36. 'body' => $this->body,
  37. // 'out_trade_no' => '2015450806125348', //商户订单号
  38. 'out_trade_no'=> $this->out_trade_no,
  39. // 'total_fee' => floatval(0.01 * 100), //总金额 单位 分
  40. 'total_fee' => $this->total_fee,
  41. // 'spbill_create_ip' => $_SERVER['REMOTE_ADDR'], //终端IP
  42. 'spbill_create_ip' => '192.168.0.161', //终端IP
  43. 'notify_url' => 'http://www.weixin.qq.com/wxpay/pay.php', //通知地址 确保外网能正常访问
  44. 'openid' => $this->openid, //用户id
  45. 'trade_type' => 'JSAPI'//交易类型
  46. );
  47. //统一下单签名
  48. $parameters['sign'] = $this->getSign($parameters);
  49. $xmlData = $this->arrayToXml($parameters);
  50. $return = $this->xmlToArray($this->postXmlCurl($xmlData, $url, 60));
  51. return $return;
  52. }
  53. private static function postXmlCurl($xml, $url, $second = 30)
  54. {
  55. $ch = curl_init();
  56. //设置超时
  57. curl_setopt($ch, CURLOPT_TIMEOUT, $second);
  58. curl_setopt($ch, CURLOPT_URL, $url);
  59. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  60. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); //严格校验
  61. //设置header
  62. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  63. //要求结果为字符串且输出到屏幕上
  64. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  65. //post提交方式
  66. curl_setopt($ch, CURLOPT_POST, TRUE);
  67. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  68. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
  69. curl_setopt($ch, CURLOPT_TIMEOUT, 40);
  70. set_time_limit(0);
  71. //运行curl
  72. $data = curl_exec($ch);
  73. //返回结果
  74. if ($data) {
  75. curl_close($ch);
  76. return $data;
  77. } else {
  78. $error = curl_errno($ch);
  79. curl_close($ch);
  80. throw new WxPayException("curl出错,错误码:$error");
  81. }
  82. }
  83. //数组转换成xml
  84. private function arrayToXml($arr) {
  85. $xml = "<root>";
  86. foreach ($arr as $key => $val) {
  87. if (is_array($val)) {
  88. $xml .= "<" . $key . ">" . arrayToXml($val) . "</" . $key . ">";
  89. } else {
  90. $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
  91. }
  92. }
  93. $xml .= "</root>";
  94. return $xml;
  95. }
  96. //xml转换成数组
  97. private function xmlToArray($xml) {
  98. //禁止引用外部xml实体
  99. libxml_disable_entity_loader(true);
  100. $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  101. $val = json_decode(json_encode($xmlstring), true);
  102. return $val;
  103. }
  104. //微信小程序接口
  105. private function weixinapp() {
  106. //统一下单接口
  107. $unifiedorder = $this->unifiedorder();
  108. // print_r($unifiedorder);
  109. $parameters = array(
  110. 'appId' => $this->appid, //小程序ID
  111. 'timeStamp' => '' . time() . '', //时间戳
  112. 'nonceStr' => $this->createNoncestr(), //随机串
  113. 'package' => 'prepay_id=' . $unifiedorder['prepay_id'], //数据包
  114. 'signType' => 'MD5'//签名方式
  115. );
  116. //签名
  117. $parameters['paySign'] = $this->getSign($parameters);
  118. return $parameters;
  119. }
  120. //作用:产生随机字符串,不长于32位
  121. private function createNoncestr($length = 32) {
  122. $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  123. $str = "";
  124. for ($i = 0; $i < $length; $i++) {
  125. $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  126. }
  127. return $str;
  128. }
  129. //作用:生成签名
  130. private function getSign($Obj) {
  131. foreach ($Obj as $k => $v) {
  132. $Parameters[$k] = $v;
  133. }
  134. //签名步骤一:按字典序排序参数
  135. ksort($Parameters);
  136. $String = $this->formatBizQueryParaMap($Parameters, false);
  137. //签名步骤二:在string后加入KEY
  138. $String = $String . "&key=" . $this->key;
  139. //签名步骤三:MD5加密
  140. $String = md5($String);
  141. //签名步骤四:所有字符转为大写
  142. $result_ = strtoupper($String);
  143. return $result_;
  144. }
  145. ///作用:格式化参数,签名过程需要使用
  146. private function formatBizQueryParaMap($paraMap, $urlencode) {
  147. $buff = "";
  148. ksort($paraMap);
  149. foreach ($paraMap as $k => $v) {
  150. if ($urlencode) {
  151. $v = urlencode($v);
  152. }
  153. $buff .= $k . "=" . $v . "&";
  154. }
  155. $reqPar;
  156. if (strlen($buff) > 0) {
  157. $reqPar = substr($buff, 0, strlen($buff) - 1);
  158. }
  159. return $reqPar;
  160. }
  161. }

小程序页面处理:

  1. [html] view plain copy
  2. wx.request({
  3. url:'https://www.yourhost.com/weixin/WeiActivity/payfee.php',//改成你自己的链接
  4. header:{
  5. 'Content-Type':'application/x-www-form-urlencoded'
  6. },
  7. method:'POST',
  8. success:function(res){
  9. console.log(res.data);
  10. console.log('调起支付');
  11. wx.requestPayment({
  12. 'timeStamp': res.data.timeStamp,
  13. 'nonceStr': res.data.nonceStr,
  14. 'package': res.data.package,
  15. 'signType':'MD5',
  16. 'paySign': res.data.paySign,
  17. 'success':function(res){
  18. console.log('success');
  19. wx.showToast({
  20. title:'支付成功',
  21. icon:'success',
  22. duration:3000
  23. });
  24. },
  25. 'fail':function(res){
  26. console.log('fail');
  27. },
  28. 'complete':function(res){
  29. console.log('complete');
  30. }
  31. });
  32. },
  33. fail:function(res){
  34. console.log(res.data)
  35. }
  36. });
  37. 回调url:notify.php
  38. [php] view plain copy
  39. $postXml = $GLOBALS["HTTP_RAW_POST_DATA"]; //接收微信参数
  40. if (empty($postXml)) {
  41. return false;
  42. }
  43. //将xml格式转换成数组
  44. function xmlToArray($xml) {
  45. //禁止引用外部xml实体
  46. libxml_disable_entity_loader(true);
  47. $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  48. $val = json_decode(json_encode($xmlstring), true);
  49. return $val;
  50. }
  51. $attr = xmlToArray($postXml);
  52. $total_fee = $attr[total_fee];
  53. $open_id = $attr[openid];
  54. $out_trade_no = $attr[out_trade_no];
  55. $time = $attr[time_end];

以上几步你就可以完成小程序的微信支付与php完美的结合起来

鲜花
鲜花 (1)
鸡蛋
鸡蛋

刚表态过的朋友 (1 人)

分享至 : QQ空间
收藏
原作者: 千堆雪惹尘埃 来自: csdn博客

相关阅读