RoutineService.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace service;
  3. use think\Url;
  4. /**
  5. * 小程序支付
  6. * Created by PhpStorm.
  7. * User: Administrator
  8. * Date: 2018/5/29 0029
  9. * Time: 上午 10:10
  10. */
  11. class RoutineService{
  12. public static function options(){
  13. $payment = SystemConfigService::more(['site_url','routine_appId','routine_appsecret','pay_routine_mchid','pay_routine_client_cert','pay_routine_client_key','pay_routine_key','pay_weixin_open']);
  14. return $payment;
  15. }
  16. /**
  17. * @param $openid $openid 用户openid
  18. * @param $fee $fee 金额
  19. * @param $out_trade_no $out_trade_no 订单号
  20. * @param $body $body 提示
  21. * @return mixed
  22. */
  23. public static function payRoutine($openid,$out_trade_no,$fee,$attach,$body){
  24. $appid = self::options()['routine_appId'] ? self::options()['routine_appId'] : '';//如果是公众号 就是公众号的appid
  25. $mch_id = self::options()['pay_routine_mchid'] ? self::options()['pay_routine_mchid'] : '';
  26. $nonce_str = self::nonce_str();//随机字符串
  27. $spbill_create_ip = self::get_server_ip();
  28. $total_fee = $fee*100;//因为充值金额最小是1 而且单位为分 如果是充值1元所以这里需要*100
  29. $trade_type = 'JSAPI';//交易类型 默认
  30. $notify_url = SystemConfigService::get('site_url').Url::build('routine/Routine/notify');
  31. $post['appid'] = $appid;
  32. $post['attach'] = $attach;
  33. $post['body'] = $body;
  34. $post['mch_id'] = $mch_id;
  35. $post['nonce_str'] = $nonce_str;//随机字符串
  36. $post['notify_url'] = $notify_url;
  37. $post['openid'] = $openid;
  38. $post['out_trade_no'] = $out_trade_no;
  39. $post['spbill_create_ip'] = $spbill_create_ip;//终端的ip
  40. $post['total_fee'] = $total_fee;//总金额
  41. $post['trade_type'] = $trade_type;
  42. $sign = self::sign($post);//签名
  43. $post_xml = '<xml>
  44. <appid>'.$appid.'</appid>
  45. <attach>'.$attach.'</attach>
  46. <body>'.$body.'</body>
  47. <mch_id>'.$mch_id.'</mch_id>
  48. <nonce_str>'.$nonce_str.'</nonce_str>
  49. <notify_url>'.$notify_url.'</notify_url>
  50. <openid>'.$openid.'</openid>
  51. <out_trade_no>'.$out_trade_no.'</out_trade_no>
  52. <spbill_create_ip>'.$spbill_create_ip.'</spbill_create_ip>
  53. <total_fee>'.$total_fee.'</total_fee>
  54. <trade_type>'.$trade_type.'</trade_type>
  55. <sign>'.$sign.'</sign>
  56. </xml> ';
  57. // dump($post_xml);
  58. //统一接口prepay_id
  59. $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
  60. $xml = self::http_request($url,$post_xml);
  61. $array = self::xml($xml);//全要大写
  62. if($array['RETURN_CODE'] == 'SUCCESS' && $array['RESULT_CODE'] == 'SUCCESS'){
  63. $time = time();
  64. $tmp=array();//临时数组用于签名
  65. $tmp['appId'] = $appid;
  66. $tmp['nonceStr'] = $nonce_str;
  67. $tmp['package'] = 'prepay_id='.$array['PREPAY_ID'];
  68. $tmp['signType'] = 'MD5';
  69. $tmp['timeStamp'] = "$time";
  70. $data=array();
  71. $data['state'] = 1;
  72. $data['timeStamp'] = "$time";//时间戳
  73. $data['nonceStr'] = $nonce_str;//随机字符串
  74. $data['signType'] = 'MD5';//签名算法,暂支持 MD5
  75. $data['package'] = 'prepay_id='.$array['PREPAY_ID'];//统一下单接口返回的 prepay_id 参数值,提交格式如:prepay_id=*
  76. $data['paySign'] = self::sign($tmp);//签名,具体签名方案参见微信公众号支付帮助文档;
  77. $data['out_trade_no'] = $out_trade_no;
  78. }else{
  79. $data=array();
  80. $data['state'] = 0;
  81. $data['text'] = "错误";
  82. $data['RETURN_CODE'] = $array['RETURN_CODE'];
  83. $data['RETURN_MSG'] = $array['RETURN_MSG'];
  84. }
  85. return $data;
  86. }
  87. //随机32位字符串
  88. public static function nonce_str(){
  89. $result = '';
  90. $str = 'QWERTYUIOPASDFGHJKLZXVBNMqwertyuioplkjhgfdsamnbvcxz';
  91. for ($i=0;$i<32;$i++){
  92. $result .= $str[rand(0,48)];
  93. }
  94. return $result;
  95. }
  96. public static function order_number($openid){
  97. return md5($openid.time().rand(10,99));//32位
  98. }
  99. //签名 $data要先排好顺序
  100. public static function sign($data){
  101. $stringA = '';
  102. foreach ($data as $key=>$value){
  103. if(!$value) continue;
  104. if($stringA) $stringA .= '&'.$key."=".$value;
  105. else $stringA = $key."=".$value;
  106. }
  107. $wx_key = self::options()['pay_routine_key'] ? self::options()['pay_routine_key'] : '';//申请支付后有给予一个商户账号和密码,登陆后自己设置key
  108. $stringSignTemp = $stringA.'&key='.$wx_key;//申请支付后有给予一个商户账号和密码,登陆后自己设置key
  109. // dump($stringSignTemp);
  110. return strtoupper(md5($stringSignTemp));
  111. }
  112. //curl请求
  113. public static function http_request($url,$data = null,$headers=array())
  114. {
  115. $curl = curl_init();
  116. if( count($headers) >= 1 ){
  117. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  118. }
  119. curl_setopt($curl, CURLOPT_URL, $url);
  120. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  121. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  122. if (!empty($data)){
  123. curl_setopt($curl, CURLOPT_POST, 1);
  124. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  125. }
  126. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  127. $output = curl_exec($curl);
  128. curl_close($curl);
  129. return $output;
  130. }
  131. //获取xml
  132. public static function xml($xml){
  133. $p = xml_parser_create();
  134. xml_parse_into_struct($p, $xml, $vals, $index);
  135. xml_parser_free($p);
  136. $data = "";
  137. foreach ($index as $key=>$value) {
  138. if($key == 'xml' || $key == 'XML') continue;
  139. $tag = $vals[$value[0]]['tag'];
  140. $value = $vals[$value[0]]['value'];
  141. $data[$tag] = $value;
  142. }
  143. return $data;
  144. }
  145. public static function get_server_ip() {
  146. if (isset($_SERVER)) {
  147. if($_SERVER['SERVER_ADDR']) {
  148. $server_ip = $_SERVER['SERVER_ADDR'];
  149. }
  150. else {
  151. $server_ip = $_SERVER['LOCAL_ADDR'];
  152. }
  153. }
  154. else {
  155. $server_ip = getenv('SERVER_ADDR');
  156. }
  157. return $server_ip;
  158. }
  159. }