Client.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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\Refund;
  11. use EasyWeChat\Payment\Kernel\BaseClient;
  12. class Client extends BaseClient
  13. {
  14. /**
  15. * Refund by out trade number.
  16. *
  17. * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
  18. *
  19. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  20. */
  21. public function byOutTradeNumber(string $number, string $refundNumber, int $totalFee, int $refundFee, array $optional = [])
  22. {
  23. return $this->refund($refundNumber, $totalFee, $refundFee, array_merge($optional, ['out_trade_no' => $number]));
  24. }
  25. /**
  26. * Refund by transaction id.
  27. *
  28. * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
  29. *
  30. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  31. */
  32. public function byTransactionId(string $transactionId, string $refundNumber, int $totalFee, int $refundFee, array $optional = [])
  33. {
  34. return $this->refund($refundNumber, $totalFee, $refundFee, array_merge($optional, ['transaction_id' => $transactionId]));
  35. }
  36. /**
  37. * Query refund by transaction id.
  38. *
  39. * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
  40. *
  41. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  42. */
  43. public function queryByTransactionId(string $transactionId)
  44. {
  45. return $this->query($transactionId, 'transaction_id');
  46. }
  47. /**
  48. * Query refund by out trade number.
  49. *
  50. * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
  51. *
  52. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  53. */
  54. public function queryByOutTradeNumber(string $outTradeNumber)
  55. {
  56. return $this->query($outTradeNumber, 'out_trade_no');
  57. }
  58. /**
  59. * Query refund by out refund number.
  60. *
  61. * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
  62. *
  63. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  64. */
  65. public function queryByOutRefundNumber(string $outRefundNumber)
  66. {
  67. return $this->query($outRefundNumber, 'out_refund_no');
  68. }
  69. /**
  70. * Query refund by refund id.
  71. *
  72. * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
  73. *
  74. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  75. */
  76. public function queryByRefundId(string $refundId)
  77. {
  78. return $this->query($refundId, 'refund_id');
  79. }
  80. /**
  81. * Refund.
  82. *
  83. * @param array $optional
  84. *
  85. * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
  86. *
  87. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  88. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  89. * @throws \GuzzleHttp\Exception\GuzzleException
  90. */
  91. protected function refund(string $refundNumber, int $totalFee, int $refundFee, $optional = [])
  92. {
  93. $params = array_merge([
  94. 'out_refund_no' => $refundNumber,
  95. 'total_fee' => $totalFee,
  96. 'refund_fee' => $refundFee,
  97. 'appid' => $this->app['config']->app_id,
  98. ], $optional);
  99. return $this->safeRequest($this->wrap(
  100. $this->app->inSandbox() ? 'pay/refund' : 'secapi/pay/refund'
  101. ), $params);
  102. }
  103. /**
  104. * Query refund.
  105. *
  106. * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
  107. *
  108. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  109. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  110. * @throws \GuzzleHttp\Exception\GuzzleException
  111. */
  112. protected function query(string $number, string $type)
  113. {
  114. $params = [
  115. 'appid' => $this->app['config']->app_id,
  116. $type => $number,
  117. ];
  118. return $this->request($this->wrap('pay/refundquery'), $params);
  119. }
  120. }