RefundNotify.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. namespace EasyWeChat\Payment;
  11. use EasyWeChat\Core\Exceptions\FaultException;
  12. use EasyWeChat\Support\Collection;
  13. use EasyWeChat\Support\XML;
  14. use Symfony\Component\HttpFoundation\Request;
  15. class RefundNotify
  16. {
  17. /**
  18. * Merchant instance.
  19. *
  20. * @var \EasyWeChat\Payment\Merchant
  21. */
  22. protected $merchant;
  23. /**
  24. * Request instance.
  25. *
  26. * @var \Symfony\Component\HttpFoundation\Request
  27. */
  28. protected $request;
  29. /**
  30. * Payment notify (extract from XML).
  31. *
  32. * @var Collection
  33. */
  34. protected $notify;
  35. /**
  36. * Constructor.
  37. *
  38. * @param Merchant $merchant
  39. * @param Request $request
  40. */
  41. public function __construct(Merchant $merchant, Request $request = null)
  42. {
  43. $this->merchant = $merchant;
  44. $this->request = $request ?: Request::createFromGlobals();
  45. }
  46. /**
  47. * Return the notify body from request.
  48. *
  49. * @return \EasyWeChat\Support\Collection
  50. *
  51. * @throws \EasyWeChat\Core\Exceptions\FaultException
  52. */
  53. public function getNotify()
  54. {
  55. if (!empty($this->notify)) {
  56. return $this->notify;
  57. }
  58. try {
  59. $xml = XML::parse(strval($this->request->getContent()));
  60. } catch (\Throwable $t) {
  61. throw new FaultException('Invalid request XML: '.$t->getMessage(), 400);
  62. } catch (\Exception $e) {
  63. throw new FaultException('Invalid request XML: '.$e->getMessage(), 400);
  64. }
  65. if (!is_array($xml) || empty($xml)) {
  66. throw new FaultException('Invalid request XML.', 400);
  67. }
  68. $xml['req_info'] = $this->decode($xml['req_info']);
  69. return $this->notify = new Collection($xml);
  70. }
  71. public function decode($reqInfo)
  72. {
  73. $ciphertext = base64_decode($reqInfo, true);
  74. $key = md5($this->merchant->key);
  75. $decrypted = openssl_decrypt($ciphertext, 'aes-256-ecb', $key, OPENSSL_RAW_DATA);
  76. return XML::parse($decrypted);
  77. }
  78. }