小程序端

  1. /**
  2. * 微信支付接口
  3. */
  4. wxPaymoney:function (out_trade_no, true_money){ //out_trade_no 后台统一下单接口需要用
  5. var that = this
  6. wx.hideToast() //隐藏toast
  7. wx.request({
  8. method: \'POST\',
  9. data: {
  10. openid: \'************\', //调用人的openid
  11. out_trade_no: out_trade_no,
  12. body: \'答题奖金\',
  13. no****rl: \'https://www.q**************.php\',//后台接口自动修改订单状态
  14. t*****e: true_money //支付金额
  15. },
  16. url: \'https://b***************gn.php\', //服务器接口(微信统一下单接口)
  17. header: {
  18. \'Content-Type\': \'application/x-www-form-urlencoded\'
  19. },
  20. success: function (res) {
  21. console.log(res.data)
  22. console.log(\'调起支付\')
  23. wx.requestPayment({ //成功之后,调用小程序微信支付
  24. \'timeStamp\': res.data.timeStamp,
  25. \'nonceStr\': res.data.nonceStr,
  26. \'package\': res.data.package,
  27. \'signType\': \'MD5\',
  28. \'paySign\': res.data.paySign,
  29. success: function (res) {
  30. console.log(res)
  31. wx.showToast({
  32. title: \'支付成功\',
  33. icon: \'success\',
  34. duration: 3000
  35. })
  36. },
  37. fail: function (res) {
  38. console.log(\'付款失败\');
  39. wx.showModal({
  40. title: \'提醒\',
  41. content: \'付款失败,如要出题请重新编辑题目\',
  42. showCancel: false,
  43. success:function(){
  44. wx.redirectTo({
  45. url: \'/pa*******ndex\',
  46. })
  47. }
  48. })
  49. return
  50. },
  51. })
  52. },
  53. fail: function (res) {
  54. console.log(res.data)
  55. }
  56. })
  57. }

服务器端

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

 

版权声明:本文为yszr原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/yszr/p/10422059.html