Unipay.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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\Artful\Artful;
  10. use Yansongda\Artful\Event;
  11. use Yansongda\Artful\Exception\ContainerException;
  12. use Yansongda\Artful\Exception\InvalidParamsException;
  13. use Yansongda\Artful\Exception\ServiceNotFoundException;
  14. use Yansongda\Artful\Plugin\AddPayloadBodyPlugin;
  15. use Yansongda\Artful\Plugin\ParserPlugin;
  16. use Yansongda\Artful\Rocket;
  17. use Yansongda\Pay\Contract\ProviderInterface;
  18. use Yansongda\Pay\Event\CallbackReceived;
  19. use Yansongda\Pay\Event\MethodCalled;
  20. use Yansongda\Pay\Exception\Exception;
  21. use Yansongda\Pay\Pay;
  22. use Yansongda\Pay\Plugin\Unipay\AddRadarPlugin;
  23. use Yansongda\Pay\Plugin\Unipay\Open\AddPayloadSignaturePlugin;
  24. use Yansongda\Pay\Plugin\Unipay\Open\CallbackPlugin;
  25. use Yansongda\Pay\Plugin\Unipay\Open\StartPlugin;
  26. use Yansongda\Pay\Plugin\Unipay\Open\VerifySignaturePlugin;
  27. use Yansongda\Supports\Collection;
  28. use Yansongda\Supports\Str;
  29. /**
  30. * @method ResponseInterface|Rocket web(array $order) 电脑支付
  31. * @method ResponseInterface|Rocket h5(array $order) H5支付
  32. * @method Collection|Rocket pos(array $order) 刷卡支付(付款码,被扫码)
  33. * @method Collection|Rocket scan(array $order) 扫码支付(摄像头,主动扫)
  34. */
  35. class Unipay implements ProviderInterface
  36. {
  37. public const URL = [
  38. Pay::MODE_NORMAL => 'https://gateway.95516.com/',
  39. Pay::MODE_SANDBOX => 'https://gateway.test.95516.com/',
  40. Pay::MODE_SERVICE => 'https://gateway.95516.com/',
  41. ];
  42. /**
  43. * @throws ContainerException
  44. * @throws InvalidParamsException
  45. * @throws ServiceNotFoundException
  46. */
  47. public function __call(string $shortcut, array $params): null|Collection|MessageInterface|Rocket
  48. {
  49. $plugin = '\Yansongda\Pay\Shortcut\Unipay\\'.Str::studly($shortcut).'Shortcut';
  50. return Artful::shortcut($plugin, ...$params);
  51. }
  52. /**
  53. * @throws ContainerException
  54. * @throws InvalidParamsException
  55. */
  56. public function pay(array $plugins, array $params): null|Collection|MessageInterface|Rocket
  57. {
  58. return Artful::artful($plugins, $params);
  59. }
  60. /**
  61. * @throws ContainerException
  62. * @throws InvalidParamsException
  63. * @throws ServiceNotFoundException
  64. */
  65. public function query(array $order): Collection|Rocket
  66. {
  67. Event::dispatch(new MethodCalled('unipay', __METHOD__, $order, null));
  68. return $this->__call('query', [$order]);
  69. }
  70. /**
  71. * @throws ContainerException
  72. * @throws InvalidParamsException
  73. * @throws ServiceNotFoundException
  74. */
  75. public function cancel(array $order): Collection|Rocket
  76. {
  77. Event::dispatch(new MethodCalled('unipay', __METHOD__, $order, null));
  78. return $this->__call('cancel', [$order]);
  79. }
  80. /**
  81. * @throws InvalidParamsException
  82. */
  83. public function close(array $order): Collection|Rocket
  84. {
  85. throw new InvalidParamsException(Exception::PARAMS_METHOD_NOT_SUPPORTED, '参数异常: 银联不支持 close API');
  86. }
  87. /**
  88. * @throws ContainerException
  89. * @throws InvalidParamsException
  90. * @throws ServiceNotFoundException
  91. */
  92. public function refund(array $order): Collection|Rocket
  93. {
  94. Event::dispatch(new MethodCalled('unipay', __METHOD__, $order, null));
  95. return $this->__call('refund', [$order]);
  96. }
  97. /**
  98. * @throws ContainerException
  99. * @throws InvalidParamsException
  100. */
  101. public function callback(null|array|ServerRequestInterface $contents = null, ?array $params = null): Collection|Rocket
  102. {
  103. $request = $this->getCallbackParams($contents);
  104. Event::dispatch(new CallbackReceived('unipay', $request->all(), $params, null));
  105. return $this->pay(
  106. [CallbackPlugin::class],
  107. $request->merge($params)->all()
  108. );
  109. }
  110. public function success(): ResponseInterface
  111. {
  112. return new Response(200, [], 'success');
  113. }
  114. public function mergeCommonPlugins(array $plugins): array
  115. {
  116. return array_merge(
  117. [StartPlugin::class],
  118. $plugins,
  119. [AddPayloadSignaturePlugin::class, AddPayloadBodyPlugin::class, AddRadarPlugin::class, VerifySignaturePlugin::class, ParserPlugin::class],
  120. );
  121. }
  122. protected function getCallbackParams(null|array|ServerRequestInterface $contents = null): Collection
  123. {
  124. if (is_array($contents)) {
  125. return Collection::wrap($contents);
  126. }
  127. if ($contents instanceof ServerRequestInterface) {
  128. return Collection::wrap($contents->getParsedBody());
  129. }
  130. $request = ServerRequest::fromGlobals();
  131. return Collection::wrap($request->getParsedBody());
  132. }
  133. }