RoutineService.php 6.8 KB

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