博客列表 >app微信支付服务端

app微信支付服务端

南鸢离梦的博客
南鸢离梦的博客原创
2022年09月27日 15:49:55570浏览

<?php
namespace app\api\controller;

  1. use think\Db;
  2. class WxPay{
  3. public function pay($body_name='',$order_number='',$fee='') {
  4. $wxdata = Db::name('wxpay')->find();
  5. $appid = $wxdata['appid']; //应用的appid
  6. $mch_id = $wxdata['mch_id']; // 您的商户账号
  7. $nonce_str = $this -> nonce_str(); //随机字符串
  8. $body = $body_name; // 举例: 服务预约
  9. $out_trade_no = $order_number; //商户订单号
  10. $total_fee = $fee*100;
  11. $spbill_create_ip = '122.114.62.70'; // IP白名单
  12. $notify_url = 'http://app.ggg.com/api/Notify/Wx_notify'; // 回调的url【自己填写,如若回调不成功请注意查看服务器是否开启防盗链,回调地址用http】
  13. $trade_type = 'APP'; //交易类型 默认
  14. //这里是按照顺序的 因为下面的签名是按照顺序 排序错误 肯定出错
  15. $post['appid'] = $appid;
  16. $post['body'] = $body;
  17. $post['mch_id'] = $mch_id;
  18. $post['nonce_str'] = $nonce_str; //随机字符串
  19. $post['notify_url'] = $notify_url;
  20. $post['out_trade_no'] = $out_trade_no;
  21. $post['spbill_create_ip'] = $spbill_create_ip; //终端的ip
  22. $post['total_fee'] = $total_fee; //总金额 最低为一块钱 必须是整数
  23. $post['trade_type'] = $trade_type;
  24. $sign = $this -> sign($post,$wxdata['key']); //签名
  25. $post_xml = "<xml>
  26. <appid><![CDATA[$appid]]></appid>
  27. <body><![CDATA[$body]]></body>
  28. <mch_id><![CDATA[$mch_id]]></mch_id>
  29. <nonce_str><![CDATA[$nonce_str]]></nonce_str>
  30. <notify_url><![CDATA[$notify_url]]></notify_url>
  31. <out_trade_no><![CDATA[$out_trade_no]]></out_trade_no>
  32. <spbill_create_ip><![CDATA[$spbill_create_ip]]></spbill_create_ip>
  33. <total_fee><![CDATA[$total_fee]]></total_fee>
  34. <trade_type><![CDATA[$trade_type]]></trade_type>
  35. <sign><![CDATA[$sign]]></sign>
  36. </xml>";
  37. //统一接口prepay_id
  38. $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
  39. $xml = $this -> http_request($url, $post_xml);
  40. $array = $this -> xml($xml); //全要大写
  41. // dump($array);die;
  42. if ($array['RETURN_CODE'] == 'SUCCESS' && $array['RESULT_CODE'] == 'SUCCESS') {
  43. $time = time();
  44. $tmp = []; //临时数组用于签名
  45. $tmp['appid'] = $appid;
  46. $tmp['noncestr'] = $nonce_str;
  47. $tmp['package'] = 'Sign=WXPay';
  48. $tmp["partnerid"] = $mch_id;
  49. $tmp['prepayid'] = $array['PREPAY_ID'];
  50. $tmp['timestamp'] = "$time";
  51. $data['appid'] = $appid;
  52. $data['noncestr'] = $nonce_str; //随机字符串
  53. // $data['package'] = 'prepay_id='.$array['PREPAY_ID']; //统一接口返回的 prepay_id 参数值,提交格式如:prepay_id=*
  54. $data['package'] = "Sign=WXPay";
  55. $data['partnerid']= $mch_id;
  56. $data['prepayid'] = $array['PREPAY_ID'];
  57. $data['timestamp'] = "$time"; //时间戳
  58. $data['sign'] = $this ->sign($tmp,$wxdata['key']);//签名
  59. } else {
  60. $data['status'] = 0;
  61. $data['text'] = "错误";
  62. $data['RETURN_CODE'] = $array['RETURN_CODE'];
  63. $data['RETURN_MSG'] = $array['RETURN_MSG'];
  64. }
  65. return json_encode($data);
  66. }
  67. public function sign($data,$wx_key)
  68. {
  69. //签名 $data要先排好顺序
  70. $stringA = '';
  71. ksort($data);
  72. foreach($data as $key => $value) {
  73. if (!$value) continue;
  74. if ($stringA)
  75. $stringA.= '&'.$key."=".$value;
  76. else
  77. $stringA = $key."=".$value;
  78. }
  79. $stringSignTemp = $stringA.'&key='.$wx_key; //申请支付后有给予一个商户账号和密码,登陆后自己设置key
  80. return strtoupper(md5($stringSignTemp));
  81. }
  82. //随机32位字符串
  83. private function nonce_str() {
  84. $result = '';
  85. $str = 'QWERTYUIOPASDFGHJKLZXVBNMqwertyuioplkjhgfdsamnbvcxz';
  86. for ($i = 0; $i < 32; $i++) {
  87. $result.= $str[rand(0, 48)];
  88. }
  89. return $result;
  90. }
  91. //curl请求啊
  92. public function http_request($url, $data = null, $headers = array()) {
  93. $curl = curl_init();
  94. if (count($headers) >= 1) {
  95. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  96. }
  97. curl_setopt($curl, CURLOPT_URL, $url);
  98. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  99. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  100. if (!empty($data)) {
  101. curl_setopt($curl, CURLOPT_POST, 1);
  102. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  103. }
  104. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  105. $output = curl_exec($curl);
  106. curl_close($curl);
  107. return $output;
  108. }
  109. //获取xml
  110. private function xml($xml) {
  111. $p = xml_parser_create();
  112. xml_parse_into_struct($p, $xml, $vals, $index);
  113. xml_parser_free($p);
  114. $data = "";
  115. foreach($index as $key => $value) {
  116. if ($key == 'xml' || $key == 'XML') continue;
  117. $tag = $vals[$value[0]]['tag'];
  118. $value = $vals[$value[0]]['value'];
  119. $data[$tag] = $value;
  120. }
  121. return $data;
  122. }

}

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议