WxPayResults.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * example目录下为简单的支付样例,仅能用于搭建快速体验微信支付使用
  4. * 样例的作用仅限于指导如何使用sdk,在安全上面仅做了简单处理, 复制使用样例代码时请慎重
  5. * 请勿直接直接使用样例对外提供服务
  6. **/
  7. namespace addons\qingdongams\library\wx;
  8. /**
  9. * 接口调用结果类
  10. * @author widyhu
  11. */
  12. class WxPayResults extends WxPayDataBase
  13. {
  14. /**
  15. * 生成签名 - 重写该方法
  16. * @param WxPayConfig $config 配置对象
  17. * @param bool $needSignType 是否需要补signtype
  18. * @return 签名,本函数不覆盖sign成员变量,如要设置签名需要调用SetSign方法赋值
  19. */
  20. public function MakeSign($config, $needSignType = false)
  21. {
  22. //签名步骤一:按字典序排序参数
  23. ksort($this->values);
  24. $string = $this->ToUrlParams();
  25. //签名步骤二:在string后加入KEY
  26. $string = $string . "&key=" . $config->GetKey();
  27. //签名步骤三:MD5加密或者HMAC-SHA256
  28. if (strlen($this->GetSign()) <= 32) {
  29. //如果签名小于等于32个,则使用md5验证
  30. $string = md5($string);
  31. } else {
  32. //是用sha256校验
  33. $string = hash_hmac("sha256", $string, $config->GetKey());
  34. }
  35. //签名步骤四:所有字符转为大写
  36. $result = strtoupper($string);
  37. return $result;
  38. }
  39. /**
  40. * @param WxPayConfigInterface $config 配置对象
  41. * 检测签名
  42. */
  43. public function CheckSign($config)
  44. {
  45. if (!$this->IsSignSet()) {
  46. throw new Exception("签名错误!");
  47. }
  48. $sign = $this->MakeSign($config, false);
  49. if ($this->GetSign() == $sign) {
  50. //签名正确
  51. return true;
  52. }
  53. throw new Exception("签名错误!");
  54. }
  55. /**
  56. * 使用数组初始化
  57. * @param array $array
  58. */
  59. public function FromArray($array)
  60. {
  61. $this->values = $array;
  62. }
  63. /**
  64. * 使用数组初始化对象
  65. * @param array $array
  66. * @param 是否检测签名 $noCheckSign
  67. */
  68. public static function InitFromArray($config, $array, $noCheckSign = false)
  69. {
  70. $obj = new self();
  71. $obj->FromArray($array);
  72. if ($noCheckSign == false) {
  73. $obj->CheckSign($config);
  74. }
  75. return $obj;
  76. }
  77. /**
  78. * 设置参数
  79. * @param string $key
  80. * @param string $value
  81. */
  82. public function SetData($key, $value)
  83. {
  84. $this->values[$key] = $value;
  85. }
  86. /**
  87. * 将xml转为array
  88. * @param WxPayConfigInterface $config 配置对象
  89. * @param string $xml
  90. * @throws WxPayException
  91. */
  92. public static function Init($config, $xml)
  93. {
  94. $obj = new self();
  95. $obj->FromXml($xml);
  96. //失败则直接返回失败
  97. if ($obj->values['return_code'] != 'SUCCESS') {
  98. foreach ($obj->values as $key => $value) {
  99. #除了return_code和return_msg之外其他的参数存在,则报错
  100. if ($key != "return_code" && $key != "return_msg") {
  101. throw new Exception("输入数据存在异常!");
  102. return false;
  103. }
  104. }
  105. return $obj->GetValues();
  106. }
  107. $obj->CheckSign($config);
  108. return $obj->GetValues();
  109. }
  110. }