WxPayResults.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: yunzhong
  5. * Date: 2019/11/18
  6. * Time: 10:49
  7. */
  8. namespace app\common\services\wechat\lib;
  9. /**
  10. *
  11. * 接口调用结果类
  12. * @author widyhu
  13. *
  14. */
  15. class WxPayResults extends WxPayDataBase
  16. {
  17. /**
  18. * 生成签名 - 重写该方法
  19. * @param WxPayConfigInterface $config 配置对象
  20. * @param bool $needSignType 是否需要补signtype
  21. * @return 签名,本函数不覆盖sign成员变量,如要设置签名需要调用SetSign方法赋值
  22. */
  23. public function MakeSign($config, $needSignType = false)
  24. {
  25. //签名步骤一:按字典序排序参数
  26. ksort($this->values);
  27. $string = $this->ToUrlParams();
  28. //签名步骤二:在string后加入KEY
  29. $string = $string . "&key=".$config->GetKey();
  30. //签名步骤三:MD5加密或者HMAC-SHA256
  31. if(strlen($this->GetSign()) <= 32){
  32. //如果签名小于等于32个,则使用md5验证
  33. $string = md5($string);
  34. } else {
  35. //是用sha256校验
  36. $string = hash_hmac("sha256",$string ,$config->GetKey());
  37. }
  38. //签名步骤四:所有字符转为大写
  39. $result = strtoupper($string);
  40. return $result;
  41. }
  42. /**
  43. * 检测签名
  44. * @param WxPayConfigInterface $config 配置对象
  45. * @return bool
  46. * @throws WxPayException
  47. */
  48. public function CheckSign($config)
  49. {
  50. if(!$this->IsSignSet()){
  51. throw new WxPayException("签名错误!");
  52. }
  53. $sign = $this->MakeSign($config, false);
  54. if($this->GetSign() == $sign){
  55. //签名正确
  56. return true;
  57. }
  58. throw new WxPayException("签名错误!");
  59. }
  60. /**
  61. *
  62. * 使用数组初始化
  63. * @param array $array
  64. */
  65. public function FromArray($array)
  66. {
  67. $this->values = $array;
  68. }
  69. /**
  70. *
  71. * 使用数组初始化对象
  72. * @param array $array
  73. * @param $config
  74. * @param bool $noCheckSign 是否检测签名
  75. * @return WxPayResults
  76. * @throws WxPayException
  77. */
  78. public static function InitFromArray($config, $array, $noCheckSign = false)
  79. {
  80. $obj = new self();
  81. $obj->FromArray($array);
  82. if($noCheckSign == false){
  83. $obj->CheckSign($config);
  84. }
  85. return $obj;
  86. }
  87. /**
  88. *
  89. * 设置参数
  90. * @param string $key
  91. * @param string $value
  92. */
  93. public function SetData($key, $value)
  94. {
  95. $this->values[$key] = $value;
  96. }
  97. /**
  98. * 将xml转为array
  99. * @param WxPayConfigInterface $config 配置对象
  100. * @param $xml
  101. * @return array|bool
  102. * @throws WxPayException
  103. */
  104. public static function Init($config, $xml)
  105. {
  106. $obj = new self();
  107. $obj->FromXml($xml);
  108. //失败则直接返回失败
  109. if($obj->values['return_code'] != 'SUCCESS') {
  110. foreach ($obj->values as $key => $value) {
  111. #除了return_code和return_msg之外其他的参数存在,则报错
  112. if($key != "return_code" && $key != "return_msg"){
  113. throw new WxPayException("输入数据存在异常!");
  114. return false;
  115. }
  116. }
  117. return $obj->GetValues();
  118. }
  119. $obj->CheckSign($config);
  120. return $obj->GetValues();
  121. }
  122. }