Wechat.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\library\payment\gateway\driver;
  13. use app\common\library\payment\gateway\Driver;
  14. use app\common\library\payment\gateway\driver\wechat\V2;
  15. use app\common\library\payment\gateway\driver\wechat\V3;
  16. use cores\exception\BaseException;
  17. /**
  18. * 微信支付驱动
  19. * Class Wechat
  20. * @package app\common\library\payment\gateway\driver
  21. */
  22. class Wechat extends Driver
  23. {
  24. // 当前支付应用
  25. /* @var $app V2|V3 */
  26. private $app;
  27. /**
  28. * 微信支付驱动 [区分版本v2和v3]
  29. * @var string[]
  30. */
  31. private array $provider = [
  32. 'v2' => V2::class,
  33. 'v3' => V3::class
  34. ];
  35. /**
  36. * 获取微信支付应用
  37. * @return V2|V3
  38. */
  39. private function getApp()
  40. {
  41. if (!$this->app) {
  42. $this->app = new $this->provider[$this->options['version']];
  43. $this->app->setOptions($this->options, $this->client);
  44. }
  45. return $this->app;
  46. }
  47. /**
  48. * 统一下单API
  49. * @param string $outTradeNo 交易订单号
  50. * @param string $totalFee 实际付款金额
  51. * @param array $extra 附加的数据 (需要携带openid)
  52. * @return bool
  53. * @throws BaseException
  54. */
  55. public function unify(string $outTradeNo, string $totalFee, array $extra = []): bool
  56. {
  57. if (!$this->getApp()->setOptions($this->options, $this->client)->unify($outTradeNo, $totalFee, $extra)) {
  58. $this->setError($this->getApp()->getError());
  59. return false;
  60. }
  61. return true;
  62. }
  63. /**
  64. * 交易查询 (主动查询订单支付状态)
  65. * @param string $outTradeNo 交易订单号
  66. * @return array|null
  67. * @throws BaseException
  68. */
  69. public function tradeQuery(string $outTradeNo): ?array
  70. {
  71. return $this->getApp()->tradeQuery($outTradeNo);
  72. }
  73. /**
  74. * 支付成功后的异步通知
  75. * @return bool
  76. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  77. */
  78. public function notify(): bool
  79. {
  80. if (!$this->getApp()->notify()) {
  81. $this->setError($this->getApp()->getError());
  82. return false;
  83. }
  84. return true;
  85. }
  86. /**
  87. * 微信支付退款API
  88. * @param string $outTradeNo 第三方交易单号
  89. * @param string $refundAmount 退款金额
  90. * @param array $extra 附加数据 (需要携带订单付款总金额)
  91. * @return bool
  92. * @throws BaseException
  93. */
  94. public function refund(string $outTradeNo, string $refundAmount, array $extra = []): bool
  95. {
  96. if (!$this->getApp()->refund($outTradeNo, $refundAmount, $extra)) {
  97. $this->setError($this->getApp()->getError());
  98. return false;
  99. }
  100. return true;
  101. }
  102. /**
  103. * 商家转账到零钱API
  104. * @param string $outTradeNo 交易订单号
  105. * @param string $totalFee 实际付款金额
  106. * @param array $extra 附加的数据 (需要携带openid、desc)
  107. * @return bool
  108. * @throws BaseException
  109. */
  110. public function transfers(string $outTradeNo, string $totalFee, array $extra = []): bool
  111. {
  112. if (!$this->getApp()->transfers($outTradeNo, $totalFee, $extra)) {
  113. $this->setError($this->getApp()->getError());
  114. return false;
  115. }
  116. return true;
  117. }
  118. /**
  119. * 获取异步回调的请求参数
  120. * @return array
  121. */
  122. public function getNotifyParams(): array
  123. {
  124. return $this->getApp()->getNotifyParams();
  125. }
  126. /**
  127. * 返回异步通知结果的输出内容
  128. * @return string
  129. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  130. */
  131. public function getNotifyResponse(): string
  132. {
  133. return $this->getApp()->getNotifyResponse();
  134. }
  135. /**
  136. * 返回统一下单API的结果 (用于前端)
  137. * @return array
  138. * @throws BaseException
  139. */
  140. public function getUnifyResult(): array
  141. {
  142. return $this->getApp()->getUnifyResult();
  143. }
  144. }