Unifiedorder.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. namespace App\Servers\weixin;
  3. use App\Servers\CommonServer;
  4. /**
  5. * 统一下单
  6. * Created by PhpStorm.
  7. * User: Administrator
  8. * Date: 2016/10/24
  9. * Time: 11:28
  10. */
  11. class Unifiedorder
  12. {
  13. private $conf=array(
  14. 'appid'=>'',
  15. 'mch_id'=>'',
  16. 'notify_url'=>'',
  17. 'out_trade_no'=>'',
  18. 'total_fee'=>'',
  19. 'trade_type'=>'',
  20. 'body'=>''
  21. //'appsecret'=>''
  22. );
  23. private $file_conf=[
  24. 'cert_file'=>'',
  25. 'key_file'=>''
  26. ];
  27. private $url='https://api.mch.weixin.qq.com/pay/unifiedorder';
  28. function __construct($conf=null,$file_conf=[])
  29. {
  30. if($conf)$this->conf=$conf;
  31. if(!empty($file_conf['cert_file'])){
  32. $this->file_conf['cert_file']=$file_conf['cert_file'];
  33. $this->file_conf['key_file']=empty($file_conf['key_file'])?'':$file_conf['key_file'];
  34. }
  35. }
  36. /**
  37. * 微信签名
  38. */
  39. public function wxSign($data,$keys)
  40. {
  41. ksort($data);
  42. $str='';
  43. foreach($data as $key=>$val)
  44. {
  45. if($val)
  46. {
  47. if($str!='')$str.='&';
  48. $str.="{$key}={$val}";
  49. }
  50. }
  51. $str.='&key='.$keys;
  52. $sign=strtoupper(MD5($str));
  53. return $sign;
  54. }
  55. /**
  56. * 获取客户端ip
  57. * @return string
  58. */
  59. private function getIp()
  60. {
  61. $cip=CommonServer::creatServer()->getClientIp();
  62. // if ($_SERVER['REMOTE_ADDR']) {
  63. // $cip = $_SERVER['REMOTE_ADDR'];
  64. // } elseif (getenv("REMOTE_ADDR")) {
  65. // $cip = getenv("REMOTE_ADDR");
  66. // } elseif (getenv("HTTP_CLIENT_IP")) {
  67. // $cip = getenv("HTTP_CLIENT_IP");
  68. // } else {
  69. // $cip = "unknown";
  70. // }
  71. return $cip;
  72. }
  73. /**
  74. *
  75. * 产生随机字符串,不长于32位
  76. * @param int $length
  77. * @return 产生的随机字符串
  78. */
  79. public function getNonceStr($length = 32)
  80. {
  81. $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  82. $str ="";
  83. for ( $i = 0; $i < $length; $i++ ) {
  84. $str .= substr($chars, mt_rand(0, strlen($chars)-1), 1);
  85. }
  86. return $str;
  87. }
  88. /**
  89. * 微信退款
  90. * @param $private_key
  91. * @return array
  92. */
  93. function wxPayRefund($private_key){
  94. $data=$this->conf;
  95. $data['nonce_str']=$this->getNonceStr();
  96. $data['sign']=$this->wxSign($data,$private_key);
  97. $xml = "<xml>";
  98. foreach ($data as $key=>$val)
  99. {
  100. if (is_numeric($val))
  101. {
  102. $xml.="<".$key.">".$val."</".$key.">";
  103. }
  104. else
  105. $xml.="<".$key.">".$val."</".$key.">";
  106. }
  107. $xml.="</xml>";
  108. $url='https://api.mch.weixin.qq.com/secapi/pay/refund';
  109. // $url='https://api.mch.weixin.qq.com/pay/orderquery';
  110. $key_file=trim($this->file_conf['key_file'],'.');
  111. $cert_file=trim($this->file_conf['cert_file'],'.');
  112. if(!is_file($key_file) || !is_file($cert_file)){
  113. return array('code'=>0,'data'=>'证书文件不存在');
  114. }
  115. $ch=curl_init();
  116. curl_setopt($ch, CURLOPT_URL, $url); //设置访问路径
  117. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //设置可以返回字符串
  118. curl_setopt($ch, CURLOPT_POST,TRUE);//post请求
  119. curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
  120. curl_setopt($ch,CURLOPT_SSLCERT, $cert_file);
  121. curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
  122. curl_setopt($ch,CURLOPT_SSLKEY, $key_file);
  123. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
  124. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
  125. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);//设置传递的参数
  126. $request=curl_exec($ch);
  127. curl_close($ch);
  128. $request=json_decode(json_encode(simplexml_load_string($request, 'SimpleXMLElement', LIBXML_NOCDATA)),true);
  129. if($request['return_code']=='SUCCESS'&&$request['result_code']=='SUCCESS')
  130. {
  131. return array('code'=>1,'data'=>$request);
  132. }
  133. else
  134. {
  135. return array('code'=>0,'data'=>$request);
  136. }
  137. }
  138. function getPayOrder($private_key){
  139. $data=$this->conf;
  140. $data['nonce_str']=$this->getNonceStr();
  141. // $data['spbill_create_ip']=$this->getIp();
  142. $data['sign']=$this->wxSign($data,$private_key);
  143. $xml = "<xml>";
  144. foreach ($data as $key=>$val)
  145. {
  146. if (is_numeric($val))
  147. {
  148. $xml.="<".$key.">".$val."</".$key.">";
  149. }
  150. else
  151. $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
  152. }
  153. $xml.="</xml>";
  154. $key_file=trim($this->file_conf['key_file'],'.');
  155. $cert_file=trim($this->file_conf['cert_file'],'.');
  156. if(!is_file($key_file) || !is_file($cert_file)){
  157. return array('code'=>0,'msg'=>'证书文件不存在');
  158. }
  159. $url='https://api.mch.weixin.qq.com/pay/orderquery';
  160. $ch=curl_init();
  161. curl_setopt($ch, CURLOPT_URL, $url); //设置访问路径
  162. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //设置可以返回字符串
  163. curl_setopt($ch, CURLOPT_POST,TRUE);//post请求
  164. curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
  165. curl_setopt($ch,CURLOPT_SSLCERT, $cert_file);
  166. curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
  167. curl_setopt($ch,CURLOPT_SSLKEY, $key_file);
  168. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
  169. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
  170. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);//设置传递的参数
  171. $request=curl_exec($ch);
  172. curl_close($ch);
  173. $request=json_decode(json_encode(simplexml_load_string($request, 'SimpleXMLElement', LIBXML_NOCDATA)),true);
  174. if($request['return_code']=='SUCCESS'&&$request['result_code']=='SUCCESS')
  175. {
  176. return array('code'=>1,'msg'=>'订单信息','data'=>$request);
  177. }
  178. else
  179. {
  180. return array('code'=>0,'msg'=>empty($request['return_msg'])?'订单信息':($request['return_msg'].','.(empty($request['err_code_des'])?'':$request['err_code_des'])),'data'=>$request);
  181. }
  182. }
  183. /**
  184. *获取统一支付信息
  185. * @param $private_key 支付秘钥
  186. * @return array
  187. */
  188. public function getOrder($private_key)
  189. {
  190. $data=$this->conf;
  191. $data['nonce_str']=$this->getNonceStr();
  192. $data['spbill_create_ip']=$this->getIp();
  193. $data['sign']=$this->wxSign($data,$private_key);
  194. $xml = "<xml>";
  195. foreach ($data as $key=>$val)
  196. {
  197. if (is_numeric($val))
  198. {
  199. $xml.="<".$key.">".$val."</".$key.">";
  200. }
  201. else
  202. $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
  203. }
  204. $xml.="</xml>";
  205. $ch=curl_init();
  206. curl_setopt($ch, CURLOPT_URL, $this->url); //设置访问路径
  207. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //设置可以返回字符串
  208. curl_setopt($ch, CURLOPT_POST,TRUE);//post请求
  209. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
  210. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
  211. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);//设置传递的参数
  212. $request=curl_exec($ch);
  213. curl_close($ch);
  214. $request=json_decode(json_encode(simplexml_load_string($request, 'SimpleXMLElement', LIBXML_NOCDATA)),true);
  215. var_dump($request);
  216. if($request['return_code']=='SUCCESS'&&$request['result_code']=='SUCCESS')
  217. {
  218. return array('code'=>1,'data'=>$request);
  219. }
  220. else
  221. {
  222. return array('code'=>0,'data'=>$request['return_msg']);
  223. }
  224. }
  225. /**
  226. * @param $url
  227. * @param string $type 请求方式
  228. * @param string $postdata post数据 数组格式
  229. * @return mixed
  230. */
  231. private function send_request($url,$type='get',$postdata='')
  232. {
  233. $ch=curl_init();
  234. curl_setopt($ch, CURLOPT_URL, $url); //设置访问路径
  235. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //设置可以返回字符串
  236. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
  237. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
  238. $head=array('User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36');
  239. curl_setopt($ch, CURLOPT_HTTPHEADER,$head);
  240. if($type=='post')
  241. {
  242. curl_setopt($ch, CURLOPT_POST,TRUE);//post请求
  243. curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);//设置传递的参数
  244. }
  245. $request=curl_exec($ch);
  246. curl_close($ch);
  247. return $request;
  248. }
  249. /**
  250. * 企业付款到领钱
  251. * @param $private_key
  252. * @return array
  253. */
  254. function setVerified($private_key){
  255. $data=$this->conf;
  256. $data['nonce_str']=$this->getNonceStr();
  257. // $data['spbill_create_ip']=$this->getIp();
  258. $data['spbill_create_ip']='119.23.242.246';
  259. $data['sign']=$this->wxSign($data,$private_key);
  260. $xml = "<xml>";
  261. foreach ($data as $key=>$val)
  262. {
  263. if (is_numeric($val))
  264. {
  265. $xml.="<".$key.">".$val."</".$key.">";
  266. }
  267. else
  268. $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
  269. }
  270. $xml.="</xml>";
  271. $key_file=trim($this->file_conf['key_file'],'.');
  272. $cert_file=trim($this->file_conf['cert_file'],'.');
  273. // dd($key_file,$cert_file);
  274. if(!is_file($key_file) || !is_file($cert_file)){
  275. return array('code'=>0,'msg'=>'证书文件不存在');
  276. }
  277. $url='https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
  278. $ch=curl_init();
  279. curl_setopt($ch, CURLOPT_URL, $url); //设置访问路径
  280. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //设置可以返回字符串
  281. curl_setopt($ch, CURLOPT_POST,TRUE);//post请求
  282. curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
  283. curl_setopt($ch,CURLOPT_SSLCERT, $cert_file);
  284. curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
  285. curl_setopt($ch,CURLOPT_SSLKEY, $key_file);
  286. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
  287. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
  288. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);//设置传递的参数
  289. $request=curl_exec($ch);
  290. curl_close($ch);
  291. $request=json_decode(json_encode(simplexml_load_string($request, 'SimpleXMLElement', LIBXML_NOCDATA)),true);
  292. if($request['return_code']=='SUCCESS'&&$request['result_code']=='SUCCESS')
  293. {
  294. return array('code'=>1,'msg'=>'提现成功','data'=>$request);
  295. }
  296. else
  297. {
  298. return array('code'=>0,'msg'=>empty($request['return_msg'])?'提现失败':($request['return_msg'].','.(empty($request['err_code_des'])?'':$request['err_code_des'])),'data'=>$request);
  299. }
  300. }
  301. }