RoutineService.php 6.9 KB

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