Hongbao.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace common\library;
  3. //load()->classs('hongbao.common');
  4. use Yii;
  5. class Hongbao extends HongbaoCommon
  6. {
  7. public $nick_name;
  8. public $send_name;
  9. public $wishing;
  10. public $act_name;
  11. public $remark;
  12. public $key;
  13. public $mchid;
  14. public $wxappid;
  15. public $parameters;
  16. public $openid;
  17. public $money;
  18. public $url;
  19. public $fissionurl;
  20. public $hbinfo;
  21. public $curl_timeout;
  22. public $token;
  23. public $info;
  24. public $weixin;
  25. public $total_num;
  26. public $mch_billno;
  27. public function __construct($config)
  28. {
  29. $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']);
  30. $this->key = trim(Yii::$app->params['wechat']['key']);
  31. $this->mchid = trim(Yii::$app->params['wechat']['mchid']);
  32. $this->wxappid = trim(Yii::$app->params['wechat']['appID']);
  33. $this->openid = $config['openid'];
  34. $this->money = (double) $config['money'];
  35. //$this->total_num = $config['total_num'] ? (int) $config['total_num'] : 1;
  36. $this->total_num = 1;
  37. $this->nick_name = !empty($config['nick_name']) ? $config['nick_name'] : '红包';
  38. $this->send_name = !empty($config['send_name']) ? $config['send_name'] : '红包';
  39. $this->wishing = !empty($config['wishing']) ? $config['wishing'] : '感谢您预约工地';
  40. $this->act_name = !empty($config['act_name']) ? $config['act_name'] : '红包';
  41. $this->remark = !empty($config['remark']) ? $config['remark'] : '红包';
  42. //$this->mch_billno = $config['mch_billno'];
  43. $this->url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
  44. $this->fissionurl = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack';
  45. $this->hbinfo = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo';
  46. $this->curl_timeout = 60;
  47. }
  48. public function send()
  49. {
  50. if ( empty($this->openid) || empty($this->money)) {
  51. return array('status' => 'FAIL', 'msg' => '用户openid,金额不能为空');
  52. }
  53. if (empty($this->info)) {
  54. return array('status' => 'FAIL', 'msg' => '未获取到微信配置信息');
  55. }
  56. if (empty(Yii::$app->params['wechat']['key']) || empty(Yii::$app->params['wechat']['mchid']) || empty(Yii::$app->params['wechat']['appID'])) {
  57. return array('status' => 'FAIL', 'msg' => '密钥或商户号或公众号不能为空');
  58. }
  59. $this->parameters = array();
  60. $this->setParameter('nonce_str', $this->createNoncestr());
  61. $this->setParameter('mch_id', $this->mchid);
  62. $this->setParameter('wxappid', $this->wxappid);
  63. $this->setParameter('nick_name', $this->nick_name);
  64. $this->setParameter('send_name', $this->send_name);
  65. $this->setParameter('total_num', 1);
  66. $this->setParameter('wishing', $this->wishing);
  67. $this->setParameter('act_name', $this->act_name);
  68. $this->setParameter('remark', $this->remark);
  69. $this->setParameter('mch_billno', (string) $this->mchid . date('YmdHis', time()) . rand(1000, 9999));
  70. $this->setParameter('min_value', $this->money * 100);
  71. $this->setParameter('max_value', $this->money * 100);
  72. $this->setParameter('re_openid', (string) $this->openid);
  73. $this->setParameter('total_amount', $this->money * 100);
  74. $this->setParameter('client_ip',$this->getip());
  75. $this->setParameter('sign', $this->getSign($this->parameters));
  76. $xml = $this->createXml(1);
  77. // if($xml['status']=='FAIL')
  78. // {
  79. // return $xml;
  80. // }
  81. $response_xml = $this->postXmlSSLCurl($xml, $this->url, $this->curl_timeout);
  82. // if($response_xml['status']=='FAIL')
  83. // {
  84. // return $xml;
  85. // }
  86. $response_xml =$this->xmlToarray( $response_xml);
  87. if ($response_xml['return_code'] == 'FAIL') {
  88. return array('status' => 'FAIL', 'msg' => '发送失败');
  89. }
  90. else {
  91. $respon_array = $response_xml;
  92. if ($respon_array['result_code'] == 'FAIL') {
  93. return array('status' => 'FAIL', 'msg' => $respon_array['return_msg']);
  94. }
  95. else if($respon_array['result_code']=='SUCCESS') {
  96. return array('status' => 'SUCCESS', 'msg' => '发送成功', 'mch_billno' => $this->parameters['mch_billno']);
  97. }
  98. }
  99. }
  100. public function getip() {
  101. static $ip = '';
  102. $ip = $_SERVER['REMOTE_ADDR'];
  103. if(isset($_SERVER['HTTP_CDN_SRC_IP'])) {
  104. $ip = $_SERVER['HTTP_CDN_SRC_IP'];
  105. } elseif (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
  106. $ip = $_SERVER['HTTP_CLIENT_IP'];
  107. } 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)) {
  108. foreach ($matches[0] AS $xip) {
  109. if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {
  110. $ip = $xip;
  111. break;
  112. }
  113. }
  114. }
  115. return $ip;
  116. }
  117. public function createXml($type = 1)
  118. {
  119. $msg = '';
  120. if ((200 < $this->money) || ($this->money < 1)) {
  121. //$msg = '单个红包金额介于[1.00元,200.00元]之间';
  122. }else if ($this->parameters['mch_id'] == NULL) {
  123. $msg = '发红包接口中,缺少必填参数mch_id';
  124. }
  125. else if ($this->parameters['wxappid'] == NULL) {
  126. $msg = '发红包接口中,缺少必填参数wxappid';
  127. }
  128. else {
  129. if (($this->parameters['nick_name'] == NULL) && ($type == 1)) {
  130. $msg = '发红包接口中,缺少必填参数nick_name';
  131. }
  132. else if ($this->parameters['send_name'] == NULL) {
  133. $msg = '发红包接口中,缺少必填参数send_name';
  134. }
  135. else if ($this->parameters['wishing'] == NULL) {
  136. $msg = '发红包接口中,缺少必填参数wishing';
  137. }
  138. else if ($this->parameters['total_num'] == NULL) {
  139. $msg = '发红包接口中,缺少必填参数total_num';
  140. }
  141. else if ($this->parameters['act_name'] == NULL) {
  142. $msg = '发红包接口中,缺少必填参数act_name';
  143. }
  144. else if ($this->parameters['remark'] == NULL) {
  145. $msg = '发红包接口中,缺少必填参数remark';
  146. }
  147. }
  148. if ($msg != '') {
  149. return array('status' => 'FAIL', 'msg' => $msg);
  150. }
  151. return $this->arrayToXml($this->parameters);
  152. }
  153. public function setParameter($parameter, $parameterValue)
  154. {
  155. $this->parameters[$this->trimString($parameter)] = $this->trimString($parameterValue);
  156. }
  157. public function getSign($array)
  158. {
  159. $parames = array();
  160. foreach ((array) $array as $k => $v) {
  161. if (!empty($v)) {
  162. $parames[$k] = $v;
  163. }
  164. }
  165. ksort($parames);
  166. $temp_s = '';
  167. foreach ((array) $parames as $key => $val) {
  168. $temp_s .= $key . '=' . $val . '&';
  169. }
  170. if (0 < strlen($temp_s)) {
  171. $reqPar = substr($temp_s, 0, strlen($temp_s) - 1);
  172. }
  173. $string = $reqPar . '&key=' . $this->key;
  174. $signValue = strtoupper(md5($string));
  175. return $signValue;
  176. }
  177. public function postXmlSSLCurl($xml, $url, $second = 30)
  178. {
  179. if (empty($this->info['apiclient_cert']) || empty($this->info['apiclient_key']) || empty($this->info['rootca'])) {
  180. return array('status' => 'FAIL', 'msg' => '商户未上传证书文件');
  181. }
  182. else {
  183. $apiclient_cert = str_replace(array(Yii::$app->request->getHostInfo()), '', $this->info['apiclient_cert']);
  184. $apiclient_key = str_replace(array(Yii::$app->request->getHostInfo()), '', $this->info['apiclient_key']);
  185. $rootca = str_replace(array(Yii::$app->request->getHostInfo()), '', $this->info['rootca']);
  186. }
  187. $ch = curl_init();
  188. curl_setopt($ch, CURLOPT_TIMEOUT, $second);
  189. curl_setopt($ch, CURLOPT_URL, $url);
  190. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  191. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  192. curl_setopt($ch, CURLOPT_HEADER, false);
  193. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  194. curl_setopt($ch, CURLOPT_SSLCERT, $apiclient_cert);
  195. curl_setopt($ch, CURLOPT_SSLKEY, $apiclient_key);
  196. curl_setopt($ch, CURLOPT_CAINFO, $rootca);
  197. curl_setopt($ch, CURLOPT_POST, true);
  198. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  199. $data = curl_exec($ch);
  200. if ($data) {
  201. curl_close($ch);
  202. return $data;
  203. }
  204. else {
  205. $error = curl_error($ch);
  206. curl_close($ch);
  207. return array('status' => 'FAIL', 'msg' => $error);
  208. }
  209. }
  210. }
  211. ?>