WxPayDataBase.php 3.3 KB

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