123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?php
- namespace common\library;
- //load()->classs('hongbao.common');
- use Yii;
- class Hongbao extends HongbaoCommon
- {
- public $nick_name;
- public $send_name;
- public $wishing;
- public $act_name;
- public $remark;
- public $key;
- public $mchid;
- public $wxappid;
- public $parameters;
- public $openid;
- public $money;
- public $url;
- public $fissionurl;
- public $hbinfo;
- public $curl_timeout;
- public $token;
- public $info;
- public $weixin;
- public $total_num;
- public $mch_billno;
- public function __construct($config)
- {
- $this->info = array('apiclient_cert'=>Yii::getAlias('@frontend').Yii::$app->params['wechat']['apiclient_cert'],'apiclient_key'=>Yii::getAlias('@frontend').Yii::$app->params['wechat']['apiclient_key'],'rootca'=>Yii::getAlias('@frontend').Yii::$app->params['wechat']['rootca']);
- $this->key = trim(Yii::$app->params['wechat']['key']);
- $this->mchid = trim(Yii::$app->params['wechat']['mchid']);
- $this->wxappid = trim(Yii::$app->params['wechat']['appID']);
- $this->openid = $config['openid'];
- $this->money = (double) $config['money'];
- //$this->total_num = $config['total_num'] ? (int) $config['total_num'] : 1;
- $this->total_num = 1;
- $this->nick_name = !empty($config['nick_name']) ? $config['nick_name'] : '红包';
- $this->send_name = !empty($config['send_name']) ? $config['send_name'] : '红包';
- $this->wishing = !empty($config['wishing']) ? $config['wishing'] : '感谢您预约工地';
- $this->act_name = !empty($config['act_name']) ? $config['act_name'] : '红包';
- $this->remark = !empty($config['remark']) ? $config['remark'] : '红包';
- //$this->mch_billno = $config['mch_billno'];
- $this->url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
- $this->fissionurl = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack';
- $this->hbinfo = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo';
- $this->curl_timeout = 60;
- }
- public function send()
- {
- if ( empty($this->openid) || empty($this->money)) {
- return array('status' => 'FAIL', 'msg' => '用户openid,金额不能为空');
- }
- if (empty($this->info)) {
- return array('status' => 'FAIL', 'msg' => '未获取到微信配置信息');
- }
- if (empty(Yii::$app->params['wechat']['key']) || empty(Yii::$app->params['wechat']['mchid']) || empty(Yii::$app->params['wechat']['appID'])) {
- return array('status' => 'FAIL', 'msg' => '密钥或商户号或公众号不能为空');
- }
- $this->parameters = array();
- $this->setParameter('nonce_str', $this->createNoncestr());
- $this->setParameter('mch_id', $this->mchid);
- $this->setParameter('wxappid', $this->wxappid);
- $this->setParameter('nick_name', $this->nick_name);
- $this->setParameter('send_name', $this->send_name);
- $this->setParameter('total_num', 1);
- $this->setParameter('wishing', $this->wishing);
- $this->setParameter('act_name', $this->act_name);
- $this->setParameter('remark', $this->remark);
- $this->setParameter('mch_billno', (string) $this->mchid . date('YmdHis', time()) . rand(1000, 9999));
- $this->setParameter('min_value', $this->money * 100);
- $this->setParameter('max_value', $this->money * 100);
- $this->setParameter('re_openid', (string) $this->openid);
- $this->setParameter('total_amount', $this->money * 100);
- $this->setParameter('client_ip',$this->getip());
- $this->setParameter('sign', $this->getSign($this->parameters));
- $xml = $this->createXml(1);
- // if($xml['status']=='FAIL')
- // {
- // return $xml;
- // }
- $response_xml = $this->postXmlSSLCurl($xml, $this->url, $this->curl_timeout);
- // if($response_xml['status']=='FAIL')
- // {
- // return $xml;
- // }
- $response_xml =$this->xmlToarray( $response_xml);
- if ($response_xml['return_code'] == 'FAIL') {
- return array('status' => 'FAIL', 'msg' => '发送失败');
- }
- else {
- $respon_array = $response_xml;
-
- if ($respon_array['result_code'] == 'FAIL') {
- return array('status' => 'FAIL', 'msg' => $respon_array['return_msg']);
- }
- else if($respon_array['result_code']=='SUCCESS') {
- return array('status' => 'SUCCESS', 'msg' => '发送成功', 'mch_billno' => $this->parameters['mch_billno']);
- }
- }
- }
- public function getip() {
- static $ip = '';
- $ip = $_SERVER['REMOTE_ADDR'];
- if(isset($_SERVER['HTTP_CDN_SRC_IP'])) {
- $ip = $_SERVER['HTTP_CDN_SRC_IP'];
- } elseif (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
- $ip = $_SERVER['HTTP_CLIENT_IP'];
- } elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
- foreach ($matches[0] AS $xip) {
- if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {
- $ip = $xip;
- break;
- }
- }
- }
- return $ip;
- }
- public function createXml($type = 1)
- {
- $msg = '';
- if ((200 < $this->money) || ($this->money < 1)) {
- //$msg = '单个红包金额介于[1.00元,200.00元]之间';
- }else if ($this->parameters['mch_id'] == NULL) {
- $msg = '发红包接口中,缺少必填参数mch_id';
- }
- else if ($this->parameters['wxappid'] == NULL) {
- $msg = '发红包接口中,缺少必填参数wxappid';
- }
- else {
- if (($this->parameters['nick_name'] == NULL) && ($type == 1)) {
- $msg = '发红包接口中,缺少必填参数nick_name';
- }
- else if ($this->parameters['send_name'] == NULL) {
- $msg = '发红包接口中,缺少必填参数send_name';
- }
- else if ($this->parameters['wishing'] == NULL) {
- $msg = '发红包接口中,缺少必填参数wishing';
- }
- else if ($this->parameters['total_num'] == NULL) {
- $msg = '发红包接口中,缺少必填参数total_num';
- }
- else if ($this->parameters['act_name'] == NULL) {
- $msg = '发红包接口中,缺少必填参数act_name';
- }
- else if ($this->parameters['remark'] == NULL) {
- $msg = '发红包接口中,缺少必填参数remark';
- }
- }
- if ($msg != '') {
- return array('status' => 'FAIL', 'msg' => $msg);
- }
- return $this->arrayToXml($this->parameters);
- }
- public function setParameter($parameter, $parameterValue)
- {
- $this->parameters[$this->trimString($parameter)] = $this->trimString($parameterValue);
- }
- public function getSign($array)
- {
- $parames = array();
- foreach ((array) $array as $k => $v) {
- if (!empty($v)) {
- $parames[$k] = $v;
- }
- }
- ksort($parames);
- $temp_s = '';
- foreach ((array) $parames as $key => $val) {
- $temp_s .= $key . '=' . $val . '&';
- }
- if (0 < strlen($temp_s)) {
- $reqPar = substr($temp_s, 0, strlen($temp_s) - 1);
- }
- $string = $reqPar . '&key=' . $this->key;
- $signValue = strtoupper(md5($string));
- return $signValue;
- }
- public function postXmlSSLCurl($xml, $url, $second = 30)
- {
- if (empty($this->info['apiclient_cert']) || empty($this->info['apiclient_key']) || empty($this->info['rootca'])) {
- return array('status' => 'FAIL', 'msg' => '商户未上传证书文件');
- }
- else {
- $apiclient_cert = str_replace(array(Yii::$app->request->getHostInfo()), '', $this->info['apiclient_cert']);
- $apiclient_key = str_replace(array(Yii::$app->request->getHostInfo()), '', $this->info['apiclient_key']);
- $rootca = str_replace(array(Yii::$app->request->getHostInfo()), '', $this->info['rootca']);
- }
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_TIMEOUT, $second);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($ch, CURLOPT_HEADER, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_SSLCERT, $apiclient_cert);
- curl_setopt($ch, CURLOPT_SSLKEY, $apiclient_key);
- curl_setopt($ch, CURLOPT_CAINFO, $rootca);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
- $data = curl_exec($ch);
- if ($data) {
- curl_close($ch);
-
- return $data;
- }
- else {
- $error = curl_error($ch);
- curl_close($ch);
- return array('status' => 'FAIL', 'msg' => $error);
- }
- }
- }
- ?>
|