WxPayDataBase.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: yunzhong
  5. * Date: 2019/10/5
  6. * Time: 16:06
  7. */
  8. namespace app\common\services\wechat\lib;
  9. /**
  10. * 2015-06-29 修复签名问题
  11. **/
  12. /**
  13. *
  14. * 数据对象基础类,该类中定义数据类最基本的行为,包括:
  15. * 计算/设置/获取签名、输出xml格式的参数、从xml读取数据对象等
  16. * @author widyhu
  17. *
  18. */
  19. class WxPayDataBase
  20. {
  21. protected $values = array();
  22. /**
  23. * 设置签名,详见签名生成算法类型
  24. * @param string $value
  25. **/
  26. public function SetSignType($sign_type)
  27. {
  28. $this->values['sign_type'] = $sign_type;
  29. return $sign_type;
  30. }
  31. /**
  32. * 设置签名,详见签名生成算法
  33. * @param string $value
  34. **/
  35. public function SetSign($config)
  36. {
  37. $sign = $this->MakeSign($config);
  38. $this->values['sign'] = $sign;
  39. return $sign;
  40. }
  41. /**
  42. * 获取签名,详见签名生成算法的值
  43. * @return 值
  44. **/
  45. public function GetSign()
  46. {
  47. return $this->values['sign'];
  48. }
  49. /**
  50. * 判断签名,详见签名生成算法是否存在
  51. * @return true 或 false
  52. **/
  53. public function IsSignSet()
  54. {
  55. return array_key_exists('sign', $this->values);
  56. }
  57. /**
  58. * 输出xml字符
  59. * @throws WxPayException
  60. **/
  61. public function ToXml()
  62. {
  63. if(!is_array($this->values) || count($this->values) <= 0)
  64. {
  65. throw new WxPayException("数组数据异常!");
  66. }
  67. $xml = "<xml>";
  68. foreach ($this->values as $key=>$val)
  69. {
  70. if (is_numeric($val)){
  71. $xml.="<".$key.">".$val."</".$key.">";
  72. }else{
  73. $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
  74. }
  75. }
  76. $xml.="</xml>";
  77. return $xml;
  78. }
  79. /**
  80. * 将xml转为array
  81. * @param string $xml
  82. * @throws WxPayException
  83. */
  84. public function FromXml($xml)
  85. {
  86. if(!$xml){
  87. throw new WxPayException("xml数据异常!");
  88. }
  89. //将XML转为array
  90. //禁止引用外部xml实体
  91. libxml_disable_entity_loader(true);
  92. $this->values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  93. return $this->values;
  94. }
  95. /**
  96. * 格式化参数格式化成url参数
  97. */
  98. public function ToUrlParams()
  99. {
  100. $buff = "";
  101. foreach ($this->values as $k => $v)
  102. {
  103. if($k != "sign" && $v != "" && !is_array($v)){
  104. $buff .= $k . "=" . $v . "&";
  105. }
  106. }
  107. $buff = trim($buff, "&");
  108. return $buff;
  109. }
  110. /**
  111. * 生成签名
  112. * @param WxPayConfigInterface $config 配置对象
  113. * @param bool $needSignType 是否需要补signtype
  114. * @return 签名,本函数不覆盖sign成员变量,如要设置签名需要调用SetSign方法赋值
  115. */
  116. public function MakeSign($config, $needSignType = true)
  117. {
  118. if($needSignType) {
  119. $this->SetSignType($config->GetSignType());
  120. }
  121. //签名步骤一:按字典序排序参数
  122. ksort($this->values);
  123. $string = $this->ToUrlParams();
  124. //签名步骤二:在string后加入KEY
  125. $string = $string . "&key=".$config->GetKey();
  126. //签名步骤三:MD5加密或者HMAC-SHA256
  127. if($config->GetSignType() == "MD5"){
  128. $string = md5($string);
  129. } else if($config->GetSignType() == "HMAC-SHA256") {
  130. $string = hash_hmac("sha256",$string ,$config->GetKey());
  131. } else {
  132. throw new WxPayException("签名类型不支持!");
  133. }
  134. //签名步骤四:所有字符转为大写
  135. $result = strtoupper($string);
  136. return $result;
  137. }
  138. /**
  139. * 获取设置的值
  140. */
  141. public function GetValues()
  142. {
  143. return $this->values;
  144. }
  145. }