Wechat.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. declare(strict_types=1);
  3. namespace Yansongda\Pay\Provider;
  4. use GuzzleHttp\Psr7\Response;
  5. use GuzzleHttp\Psr7\ServerRequest;
  6. use Psr\Http\Message\MessageInterface;
  7. use Psr\Http\Message\ResponseInterface;
  8. use Psr\Http\Message\ServerRequestInterface;
  9. use Yansongda\Pay\Event;
  10. use Yansongda\Pay\Exception\ContainerException;
  11. use Yansongda\Pay\Exception\Exception;
  12. use Yansongda\Pay\Exception\InvalidParamsException;
  13. use Yansongda\Pay\Exception\ServiceNotFoundException;
  14. use Yansongda\Pay\Pay;
  15. use Yansongda\Pay\Plugin\ParserPlugin;
  16. use Yansongda\Pay\Plugin\Wechat\CallbackPlugin;
  17. use Yansongda\Pay\Plugin\Wechat\LaunchPlugin;
  18. use Yansongda\Pay\Plugin\Wechat\PreparePlugin;
  19. use Yansongda\Pay\Plugin\Wechat\RadarSignPlugin;
  20. use Yansongda\Supports\Collection;
  21. use Yansongda\Supports\Str;
  22. /**
  23. * @method Collection app(array $order) APP 支付
  24. * @method Collection mini(array $order) 小程序支付
  25. * @method Collection mp(array $order) 公众号支付
  26. * @method Collection scan(array $order) 扫码支付
  27. * @method Collection wap(array $order) H5 支付
  28. * @method Collection transfer(array $order) 帐户转账
  29. * @method Collection papay(array $order) 支付时签约(委托代扣)
  30. * @method Collection papayApply(array $order) 申请代扣(委托代扣)
  31. * @method Collection papayContract(array $order) 申请代扣(委托代扣)
  32. */
  33. class Wechat extends AbstractProvider
  34. {
  35. public const AUTH_TAG_LENGTH_BYTE = 16;
  36. public const MCH_SECRET_KEY_LENGTH_BYTE = 32;
  37. public const URL = [
  38. Pay::MODE_NORMAL => 'https://api.mch.weixin.qq.com/',
  39. Pay::MODE_SANDBOX => 'https://api.mch.weixin.qq.com/sandboxnew/',
  40. Pay::MODE_SERVICE => 'https://api.mch.weixin.qq.com/',
  41. ];
  42. /**
  43. * @return null|array|Collection|MessageInterface
  44. *
  45. * @throws ContainerException
  46. * @throws InvalidParamsException
  47. * @throws ServiceNotFoundException
  48. */
  49. public function __call(string $shortcut, array $params)
  50. {
  51. $plugin = '\\Yansongda\\Pay\\Plugin\\Wechat\\Shortcut\\'.
  52. Str::studly($shortcut).'Shortcut';
  53. return $this->call($plugin, ...$params);
  54. }
  55. /**
  56. * @param array|string $order
  57. *
  58. * @return array|Collection
  59. *
  60. * @throws ContainerException
  61. * @throws InvalidParamsException
  62. * @throws ServiceNotFoundException
  63. */
  64. public function find($order)
  65. {
  66. $order = is_array($order) ? $order : ['transaction_id' => $order];
  67. Event::dispatch(new Event\MethodCalled('wechat', __METHOD__, $order, null));
  68. return $this->__call('query', [$order]);
  69. }
  70. /**
  71. * @param array|string $order
  72. *
  73. * @throws InvalidParamsException
  74. */
  75. public function cancel($order): void
  76. {
  77. throw new InvalidParamsException(Exception::METHOD_NOT_SUPPORTED, 'Wechat does not support cancel api');
  78. }
  79. /**
  80. * @param array|string $order
  81. *
  82. * @throws ContainerException
  83. * @throws InvalidParamsException
  84. * @throws ServiceNotFoundException
  85. */
  86. public function close($order): void
  87. {
  88. $order = is_array($order) ? $order : ['out_trade_no' => $order];
  89. Event::dispatch(new Event\MethodCalled('wechat', __METHOD__, $order, null));
  90. $this->__call('close', [$order]);
  91. }
  92. /**
  93. * @return array|Collection
  94. *
  95. * @throws ContainerException
  96. * @throws InvalidParamsException
  97. * @throws ServiceNotFoundException
  98. */
  99. public function refund(array $order)
  100. {
  101. Event::dispatch(new Event\MethodCalled('wechat', __METHOD__, $order, null));
  102. return $this->__call('refund', [$order]);
  103. }
  104. /**
  105. * @param null|array|ServerRequestInterface $contents
  106. *
  107. * @throws ContainerException
  108. * @throws InvalidParamsException
  109. */
  110. public function callback($contents = null, ?array $params = null): Collection
  111. {
  112. $request = $this->getCallbackParams($contents);
  113. Event::dispatch(new Event\CallbackReceived('wechat', clone $request, $params, null));
  114. return $this->pay(
  115. [CallbackPlugin::class],
  116. ['request' => $request, 'params' => $params]
  117. );
  118. }
  119. public function success(): ResponseInterface
  120. {
  121. return new Response(
  122. 200,
  123. ['Content-Type' => 'application/json'],
  124. json_encode(['code' => 'SUCCESS', 'message' => '成功']),
  125. );
  126. }
  127. public function mergeCommonPlugins(array $plugins): array
  128. {
  129. return array_merge(
  130. [PreparePlugin::class],
  131. $plugins,
  132. [RadarSignPlugin::class],
  133. [LaunchPlugin::class, ParserPlugin::class],
  134. );
  135. }
  136. /**
  137. * @param null|array|ServerRequestInterface $contents
  138. */
  139. protected function getCallbackParams($contents = null): ServerRequestInterface
  140. {
  141. if (is_array($contents) && isset($contents['body'], $contents['headers'])) {
  142. return new ServerRequest('POST', 'http://localhost', $contents['headers'], $contents['body']);
  143. }
  144. if (is_array($contents)) {
  145. return new ServerRequest('POST', 'http://localhost', [], json_encode($contents));
  146. }
  147. if ($contents instanceof ServerRequestInterface) {
  148. return $contents;
  149. }
  150. return ServerRequest::fromGlobals();
  151. }
  152. }