Unipay.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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\Unipay\CallbackPlugin;
  17. use Yansongda\Pay\Plugin\Unipay\LaunchPlugin;
  18. use Yansongda\Pay\Plugin\Unipay\PreparePlugin;
  19. use Yansongda\Pay\Plugin\Unipay\RadarSignPlugin;
  20. use Yansongda\Supports\Collection;
  21. use Yansongda\Supports\Str;
  22. /**
  23. * @method ResponseInterface web(array $order) 电脑支付
  24. */
  25. class Unipay extends AbstractProvider
  26. {
  27. public const URL = [
  28. Pay::MODE_NORMAL => 'https://gateway.95516.com/',
  29. Pay::MODE_SANDBOX => 'https://gateway.test.95516.com/',
  30. Pay::MODE_SERVICE => 'https://gateway.95516.com',
  31. ];
  32. /**
  33. * @return null|array|Collection|MessageInterface
  34. *
  35. * @throws ContainerException
  36. * @throws InvalidParamsException
  37. * @throws ServiceNotFoundException
  38. */
  39. public function __call(string $shortcut, array $params)
  40. {
  41. $plugin = '\\Yansongda\\Pay\\Plugin\\Unipay\\Shortcut\\'.
  42. Str::studly($shortcut).'Shortcut';
  43. return $this->call($plugin, ...$params);
  44. }
  45. /**
  46. * @param array|string $order
  47. *
  48. * @return array|Collection
  49. *
  50. * @throws ContainerException
  51. * @throws InvalidParamsException
  52. * @throws ServiceNotFoundException
  53. */
  54. public function find($order)
  55. {
  56. if (!is_array($order)) {
  57. throw new InvalidParamsException(Exception::UNIPAY_FIND_STRING_NOT_SUPPORTED);
  58. }
  59. Event::dispatch(new Event\MethodCalled('unipay', __METHOD__, $order, null));
  60. return $this->__call('query', [$order]);
  61. }
  62. /**
  63. * @param array|string $order
  64. *
  65. * @return array|Collection
  66. *
  67. * @throws ContainerException
  68. * @throws InvalidParamsException
  69. * @throws ServiceNotFoundException
  70. */
  71. public function cancel($order)
  72. {
  73. if (!is_array($order)) {
  74. throw new InvalidParamsException(Exception::UNIPAY_CANCEL_STRING_NOT_SUPPORTED);
  75. }
  76. Event::dispatch(new Event\MethodCalled('unipay', __METHOD__, $order, null));
  77. return $this->__call('cancel', [$order]);
  78. }
  79. /**
  80. * @param array|string $order
  81. *
  82. * @return array|Collection
  83. *
  84. * @throws InvalidParamsException
  85. */
  86. public function close($order)
  87. {
  88. throw new InvalidParamsException(Exception::METHOD_NOT_SUPPORTED, 'Unipay does not support close api');
  89. }
  90. /**
  91. * @return array|Collection
  92. *
  93. * @throws ContainerException
  94. * @throws InvalidParamsException
  95. * @throws ServiceNotFoundException
  96. */
  97. public function refund(array $order)
  98. {
  99. Event::dispatch(new Event\MethodCalled('unipay', __METHOD__, $order, null));
  100. return $this->__call('refund', [$order]);
  101. }
  102. /**
  103. * @param null|array|ServerRequestInterface $contents
  104. *
  105. * @throws ContainerException
  106. * @throws InvalidParamsException
  107. */
  108. public function callback($contents = null, ?array $params = null): Collection
  109. {
  110. $request = $this->getCallbackParams($contents);
  111. Event::dispatch(new Event\CallbackReceived('unipay', $request->all(), $params, null));
  112. return $this->pay(
  113. [CallbackPlugin::class],
  114. $request->merge($params)->all()
  115. );
  116. }
  117. public function success(): ResponseInterface
  118. {
  119. return new Response(200, [], 'success');
  120. }
  121. public function mergeCommonPlugins(array $plugins): array
  122. {
  123. return array_merge(
  124. [PreparePlugin::class],
  125. $plugins,
  126. [RadarSignPlugin::class],
  127. [LaunchPlugin::class, ParserPlugin::class],
  128. );
  129. }
  130. /**
  131. * @param null|array|ServerRequestInterface $contents
  132. */
  133. protected function getCallbackParams($contents = null): Collection
  134. {
  135. if (is_array($contents)) {
  136. return Collection::wrap($contents);
  137. }
  138. if ($contents instanceof ServerRequestInterface) {
  139. return Collection::wrap($contents->getParsedBody());
  140. }
  141. $request = ServerRequest::fromGlobals();
  142. return Collection::wrap($request->getParsedBody());
  143. }
  144. }