Results.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace lc\wechat\wxpay\database;
  3. use lc\helpers\Helper;
  4. use lc\wechat\wxpay\base\WxPayConfig;
  5. use lc\wechat\wxpay\base\WxPayException;
  6. /**
  7. * @desc 接口调用结果类 Results
  8. */
  9. class Results extends Base
  10. {
  11. /**
  12. * @desc 检测签名
  13. *
  14. * @return bool
  15. *
  16. * @throws WxPayException
  17. */
  18. public function checkSign()
  19. {
  20. if (!$this->isSignSet()) {
  21. throw new WxPayException('签名错误.');
  22. }
  23. $sign = Helper::makeSign($this->getValues(), WxPayConfig::$key);
  24. if ($this->getSign() == $sign) {
  25. return true;
  26. }
  27. throw new WxPayException('签名错误.');
  28. }
  29. /**
  30. * 使用数组初始化.
  31. *
  32. * @param array $array
  33. */
  34. public function fromArray($array)
  35. {
  36. $this->values = $array;
  37. }
  38. /**
  39. * @desc 设置参数
  40. *
  41. * @param string $key
  42. * @param string $value
  43. */
  44. public function setData($key, $value)
  45. {
  46. $this->values[$key] = $value;
  47. }
  48. /*
  49. * @desc 将xml转为array
  50. * @param $xml
  51. * @param bool $check
  52. * @return array
  53. */
  54. public static function init($xml, $check = true)
  55. {
  56. $obj = new self();
  57. $obj->values = Helper::xmlToArray($xml);
  58. if ('SUCCESS' != $obj->values['return_code']) {
  59. return $obj->getValues();
  60. }
  61. if ($check) {
  62. $obj->checkSign();
  63. }
  64. return $obj->getValues();
  65. }
  66. }