chengkun
2025-05-22 3b321a2882db082c68aaf8771e0f55daa58a63d3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
 
 
 
//微信红包处理类
 
include(dirname(dirname(dirname(__FILE__)))."/data/api/wxpay/wxpay_data.php");
 
define("APIKEY",$wxpaydata['sy_wxpaykey']);  //微信现金红包api key
define("PATHCERT",$wxpaydata['sy_wxpem_cert']); //cert存放位置,不放在网站下
define("PATHKEY",$wxpaydata['sy_wxpem_key']); //key存放位置,不放在网站下
define("PATHCA",$wxpaydata['sy_wxpem_ca']); //ca存放位置,不放在网站下
define("APPID",$wxpaydata['sy_wxpayappid']);   //公众号
define('MCHID', $wxpaydata['sy_wxpaymchid']);  //商户号
 
class ApiWxHb{
    
    const APIKEY   = APIKEY;
    const PATHCERT = PATHCERT;
    const PATHKEY  = PATHKEY;
    const PATHCA   = PATHCA;
    const APPID    = APPID;
    const MCHID    = MCHID;
 
    var $para;
 
    function set_para($key,$value){
        $this->para[$key] = $value;
    }
    
    function create_noncestr( $length = 24 ) {  
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";  
        $str ="";  
        for ( $i = 0; $i < $length; $i++ )  {  
            $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);   
        }  
        return $str;  
    }
 
 
 
    function check_sign_para(){
        
        if(!$this->para["nonce_str"]   || 
            !$this->para["mch_billno"] || 
            !$this->para["mch_id"]     || 
            !$this->para["wxappid"]    || 
            !$this->para["send_name"]  ||
            !$this->para["re_openid"]  || 
            !$this->para["total_amount"] || 
            !$this->para["total_num"]  || 
            !$this->para["wishing"]    || 
            !$this->para["client_ip"]  || 
            !$this->para["act_name"]
            )
        {
            return false;
        }
        return true;
 
    }
 
    function create_sign(){
        if($this->check_sign_para() == false) {
            return 'FAIL';die;
        }
        ksort($this->para);
        $tempsign = "";
        foreach ($this->para as $k => $v){
            if (null != $v && "null" != $v && "sign" != $k) {
                $tempsign .= $k . "=" . $v . "&";
            }
        }
        $tempsign = substr($tempsign, 0, strlen($tempsign)-1); //去掉最后的&
        $tempsign .="&key=". APIKEY;  //拼接APIKEY
 
        return strtoupper(md5($tempsign));
    }
 
    function create_xml(){
        $return = $this->set_para('sign', $this->create_sign());
        if($return == 'FAIL'){
            return;
        }else{
            return $this->ArrayToXml($this->para);
        }
        
    }
 
    function ArrayToXml($arr)
    {
        $xml = "<xml>";
        foreach ($arr as $key=>$val)
        {
             if (is_numeric($val))
             {
                 $xml.="<".$key.">".$val."</".$key.">"; 
 
             }
             else
                 $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";  
        }
        $xml.= "</xml>";
        return $xml; 
    }
    
    function curl_post_ssl($url, $vars, $second=30)
    {
        $ch = curl_init();
        //超时时间
        curl_setopt($ch,CURLOPT_TIMEOUT,$second);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
        
        //以下两种方式需选择一种
        //第一种方法,cert 与 key 分别属于两个.pem文件
        curl_setopt($ch,CURLOPT_SSLCERT,PATHCERT);
         curl_setopt($ch,CURLOPT_SSLKEY,PATHKEY);
         
     
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
     
        curl_setopt($ch,CURLOPT_POST, 1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
        $data = curl_exec($ch);
        
        if($data){
            curl_close($ch);
            return $data;
        }
        else { 
            $error = curl_errno($ch);
        
            curl_close($ch);
            return false;
        }
    }
    //红包发送
    function sendHb($data){
        
        $this->set_para("nonce_str", $this->create_noncestr()); //随机字符串
        $this->set_para("mch_billno", $data['mch_billno']); //订单号
        $this->set_para("mch_id", MCHID); //商户号
        $this->set_para("wxappid", APPID);  //公众号
        $this->set_para("send_name", $data['send_name']); //红包发送者名称
        $this->set_para("re_openid", $data['openid']); //接受红包的用户 
        $this->set_para("total_amount", $data['total_amount']); //付款金额,单位分
        $this->set_para("total_num", 1); //红包发放总人数
        $this->set_para("wishing", $data['wishing']); //红包祝福语
        $this->set_para("client_ip", $data['client_ip']); //调用接口的机器 Ip 地址    
        $this->set_para("act_name", $data['act_name']); //活动名称
        // 发放红包使用场景,红包金额大于200或者小于1元时必传(即非‘其他’场景)
        if ($data['scene_id'] != 0){
            $this->set_para("scene_id", 'PRODUCT_'.$data['scene_id']); //场景id
        }
        //目前微信里暂无显示的地方
        
        $this->set_para("remark", $data['remark']);//备注信息
 
        $postxml = $this->create_xml(); 
        if(!$postxml){
            $responseObj = array('return_code'=>'FAIL','return_msg'=>'签名参数错误!');
        }else{
            $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
            $response = $this->curl_post_ssl($url, $postxml);
            $responseObj = simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA);
            $responseObj = (array)$responseObj;  
        }
        
        return $responseObj;
    }
 
    function senpay_create_sign(){
        
        ksort($this->para);
        $tempsign = "";
        foreach ($this->para as $k => $v){
            if (null != $v && "null" != $v && "sign" != $k) {
                $tempsign .= $k . "=" . $v . "&";
            }
        }
        $tempsign = substr($tempsign, 0, strlen($tempsign)-1);
        $tempsign .="&key=". APIKEY;
 
        return strtoupper(md5($tempsign));
    }
 
    function senpay_create_xml(){
        $return = $this->set_para('sign', $this->senpay_create_sign());
        if($return == 'FAIL'){
            return;
        }else{
            return $this->ArrayToXml($this->para);
        }
        
    }
    function sendPay($data){
 
 
        $this->set_para("mchid", MCHID); //商户号
        $this->set_para("mch_appid", APPID);  //公众号
        $this->set_para("nonce_str", $this->create_noncestr()); //随机字符串
        $this->set_para("partner_trade_no", $data['partner_trade_no']); //订单号
        $this->set_para("check_name", 'FORCE_CHECK'); 
        $this->set_para("re_user_name", $data['real_name']); //接受打款的用户 
        $this->set_para("openid", $data['openid']); //接受打款的用户 
        $this->set_para("amount", $data['amount']); //付款金额,单位分
        $this->set_para("desc", $data['desc']); //企业付款操作说明信息
        $this->set_para("spbill_create_ip", $data['spbill_create_ip']); //调用接口的机器 Ip 地址    
        
        $postxml = $this->senpay_create_xml();
        
        if(!$postxml){
            $responseObj = array('return_code'=>'FAIL','return_msg'=>'签名参数错误!');
        }else{
            $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
            $response = $this->curl_post_ssl($url, $postxml);
            
            $responseObj = simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA);
            
            $responseObj = (array)$responseObj;  
 
        }
        
        return $responseObj;
    }
 
 
}
 
?>