123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <?php
- namespace service;
- use think\Url;
- class RoutineService{
- public static function options(){
- $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']);
- return $payment;
- }
-
- public static function payRoutine($openid,$out_trade_no,$fee,$attach,$body){
- $appid = self::options()['routine_appId'] ? self::options()['routine_appId'] : '';
- $mch_id = self::options()['pay_routine_mchid'] ? self::options()['pay_routine_mchid'] : '';
- $nonce_str = self::nonce_str();
- $spbill_create_ip = self::get_server_ip();
- $total_fee = $fee*100;
- $trade_type = 'JSAPI';
- $notify_url = SystemConfigService::get('site_url').Url::build('routine/Routine/notify');
- $post['appid'] = $appid;
- $post['attach'] = $attach;
- $post['body'] = $body;
- $post['mch_id'] = $mch_id;
- $post['nonce_str'] = $nonce_str;
- $post['notify_url'] = $notify_url;
- $post['openid'] = $openid;
- $post['out_trade_no'] = $out_trade_no;
- $post['spbill_create_ip'] = $spbill_create_ip;
- $post['total_fee'] = $total_fee;
- $post['trade_type'] = $trade_type;
- $sign = self::sign($post);
- $post_xml = '<xml>
- <appid>'.$appid.'</appid>
- <attach>'.$attach.'</attach>
- <body>'.$body.'</body>
- <mch_id>'.$mch_id.'</mch_id>
- <nonce_str>'.$nonce_str.'</nonce_str>
- <notify_url>'.$notify_url.'</notify_url>
- <openid>'.$openid.'</openid>
- <out_trade_no>'.$out_trade_no.'</out_trade_no>
- <spbill_create_ip>'.$spbill_create_ip.'</spbill_create_ip>
- <total_fee>'.$total_fee.'</total_fee>
- <trade_type>'.$trade_type.'</trade_type>
- <sign>'.$sign.'</sign>
- </xml> ';
-
- $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
- $xml = self::http_request($url,$post_xml);
- $array = self::xml($xml);
- if($array['RETURN_CODE'] == 'SUCCESS' && $array['RESULT_CODE'] == 'SUCCESS'){
- $time = time();
- $tmp='';
- $tmp['appId'] = $appid;
- $tmp['nonceStr'] = $nonce_str;
- $tmp['package'] = 'prepay_id='.$array['PREPAY_ID'];
- $tmp['signType'] = 'MD5';
- $tmp['timeStamp'] = "$time";
- $data['state'] = 1;
- $data['timeStamp'] = "$time";
- $data['nonceStr'] = $nonce_str;
- $data['signType'] = 'MD5';
- $data['package'] = 'prepay_id='.$array['PREPAY_ID'];
- $data['paySign'] = self::sign($tmp);
- $data['out_trade_no'] = $out_trade_no;
- }else{
- $data['state'] = 0;
- $data['text'] = "错误";
- $data['RETURN_CODE'] = $array['RETURN_CODE'];
- $data['RETURN_MSG'] = $array['RETURN_MSG'];
- }
- return $data;
- }
-
- public static function nonce_str(){
- $result = '';
- $str = 'QWERTYUIOPASDFGHJKLZXVBNMqwertyuioplkjhgfdsamnbvcxz';
- for ($i=0;$i<32;$i++){
- $result .= $str[rand(0,48)];
- }
- return $result;
- }
- public static function order_number($openid){
- return md5($openid.time().rand(10,99));
- }
-
- public static function sign($data){
- $stringA = '';
- foreach ($data as $key=>$value){
- if(!$value) continue;
- if($stringA) $stringA .= '&'.$key."=".$value;
- else $stringA = $key."=".$value;
- }
- $wx_key = self::options()['pay_routine_key'] ? self::options()['pay_routine_key'] : '';
- $stringSignTemp = $stringA.'&key='.$wx_key;
- return strtoupper(md5($stringSignTemp));
- }
-
- public static function http_request($url,$data = null,$headers=array())
- {
- $curl = curl_init();
- if( count($headers) >= 1 ){
- curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
- }
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
- if (!empty($data)){
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- }
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- $output = curl_exec($curl);
- curl_close($curl);
- return $output;
- }
-
- public static function xml($xml){
- $p = xml_parser_create();
- xml_parse_into_struct($p, $xml, $vals, $index);
- xml_parser_free($p);
- $data = "";
- foreach ($index as $key=>$value) {
- if($key == 'xml' || $key == 'XML') continue;
- $tag = $vals[$value[0]]['tag'];
- $value = $vals[$value[0]]['value'];
- $data[$tag] = $value;
- }
- return $data;
- }
- public static function get_server_ip() {
- if (isset($_SERVER)) {
- if($_SERVER['SERVER_ADDR']) {
- $server_ip = $_SERVER['SERVER_ADDR'];
- }
- else {
- $server_ip = $_SERVER['LOCAL_ADDR'];
- }
- }
- else {
- $server_ip = getenv('SERVER_ADDR');
- }
- return $server_ip;
- }
- }
|