You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

107 lines
2.3 KiB

  1. <?php
  2. /*
  3. * This file is part of the overtrue/wechat.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. /**
  11. * Notify.php.
  12. *
  13. * @author overtrue <i@overtrue.me>
  14. * @copyright 2015 overtrue <i@overtrue.me>
  15. *
  16. * @see https://github.com/overtrue
  17. * @see http://overtrue.me
  18. */
  19. namespace EasyWeChat\Payment;
  20. use EasyWeChat\Core\Exceptions\FaultException;
  21. use EasyWeChat\Support\Collection;
  22. use EasyWeChat\Support\XML;
  23. use Symfony\Component\HttpFoundation\Request;
  24. /**
  25. * Class Notify.
  26. */
  27. class Notify
  28. {
  29. /**
  30. * Merchant instance.
  31. *
  32. * @var \EasyWeChat\Payment\Merchant
  33. */
  34. protected $merchant;
  35. /**
  36. * Request instance.
  37. *
  38. * @var \Symfony\Component\HttpFoundation\Request
  39. */
  40. protected $request;
  41. /**
  42. * Payment notify (extract from XML).
  43. *
  44. * @var Collection
  45. */
  46. protected $notify;
  47. /**
  48. * Constructor.
  49. *
  50. * @param Merchant $merchant
  51. * @param Request $request
  52. */
  53. public function __construct(Merchant $merchant, Request $request = null)
  54. {
  55. $this->merchant = $merchant;
  56. $this->request = $request ?: Request::createFromGlobals();
  57. }
  58. /**
  59. * Validate the request params.
  60. *
  61. * @return bool
  62. */
  63. public function isValid($signkey)
  64. {
  65. $localSign = generate_sign($this->getNotify()->except('sign')->all(), $signkey, 'md5');
  66. return $localSign === $this->getNotify()->get('sign');
  67. }
  68. /**
  69. * Return the notify body from request.
  70. *
  71. * @return \EasyWeChat\Support\Collection
  72. *
  73. * @throws \EasyWeChat\Core\Exceptions\FaultException
  74. */
  75. public function getNotify()
  76. {
  77. if (!empty($this->notify)) {
  78. return $this->notify;
  79. }
  80. try {
  81. $xml = XML::parse(strval($this->request->getContent()));
  82. } catch (\Throwable $t) {
  83. throw new FaultException('Invalid request XML: '.$t->getMessage(), 400);
  84. } catch (\Exception $e) {
  85. throw new FaultException('Invalid request XML: '.$e->getMessage(), 400);
  86. }
  87. if (!is_array($xml) || empty($xml)) {
  88. throw new FaultException('Invalid request XML.', 400);
  89. }
  90. return $this->notify = new Collection($xml);
  91. }
  92. }