Jsb.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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\ParserPlugin;
  15. use Yansongda\Artful\Rocket;
  16. use Yansongda\Pay\Contract\ProviderInterface;
  17. use Yansongda\Pay\Event\CallbackReceived;
  18. use Yansongda\Pay\Event\MethodCalled;
  19. use Yansongda\Pay\Exception\Exception;
  20. use Yansongda\Pay\Pay;
  21. use Yansongda\Pay\Plugin\Jsb\AddPayloadSignPlugin;
  22. use Yansongda\Pay\Plugin\Jsb\AddRadarPlugin;
  23. use Yansongda\Pay\Plugin\Jsb\CallbackPlugin;
  24. use Yansongda\Pay\Plugin\Jsb\ResponsePlugin;
  25. use Yansongda\Pay\Plugin\Jsb\StartPlugin;
  26. use Yansongda\Pay\Plugin\Jsb\VerifySignaturePlugin;
  27. use Yansongda\Supports\Collection;
  28. use Yansongda\Supports\Str;
  29. /**
  30. * @method Collection|Rocket scan(array $order) 扫码支付[微信支付宝都可扫描]
  31. */
  32. class Jsb implements ProviderInterface
  33. {
  34. public const URL = [
  35. Pay::MODE_NORMAL => 'https://mybank.jsbchina.cn:577/eis/merchant/merchantServices.htm',
  36. Pay::MODE_SANDBOX => 'https://epaytest.jsbchina.cn:9999/eis/merchant/merchantServices.htm',
  37. ];
  38. /**
  39. * @param mixed $name
  40. * @param mixed $params
  41. *
  42. * @throws ContainerException
  43. * @throws InvalidParamsException
  44. * @throws ServiceNotFoundException
  45. */
  46. public function __call($name, $params): null|Collection|MessageInterface|Rocket
  47. {
  48. $plugin = '\Yansongda\Pay\Shortcut\Jsb\\'.Str::studly($name).'Shortcut';
  49. return Artful::shortcut($plugin, ...$params);
  50. }
  51. /**
  52. * @throws ContainerException
  53. * @throws InvalidParamsException
  54. */
  55. public function pay(array $plugins, array $params): null|Collection|MessageInterface|Rocket
  56. {
  57. return Artful::artful($plugins, $params);
  58. }
  59. public function mergeCommonPlugins(array $plugins): array
  60. {
  61. return array_merge(
  62. [StartPlugin::class],
  63. $plugins,
  64. [AddPayloadSignPlugin::class, AddRadarPlugin::class, VerifySignaturePlugin::class, ResponsePlugin::class, ParserPlugin::class],
  65. );
  66. }
  67. /**
  68. * @throws InvalidParamsException
  69. */
  70. public function cancel(array $order): Collection|Rocket
  71. {
  72. throw new InvalidParamsException(Exception::PARAMS_METHOD_NOT_SUPPORTED, 'Jsb does not support cancel api');
  73. }
  74. /**
  75. * @throws InvalidParamsException
  76. */
  77. public function close(array $order): Collection|Rocket
  78. {
  79. throw new InvalidParamsException(Exception::PARAMS_METHOD_NOT_SUPPORTED, 'Jsb does not support close api');
  80. }
  81. /**
  82. * @throws ContainerException
  83. * @throws InvalidParamsException
  84. * @throws ServiceNotFoundException
  85. */
  86. public function refund(array $order): Collection|Rocket
  87. {
  88. Event::dispatch(new MethodCalled('jsb', __METHOD__, $order, null));
  89. return $this->__call('refund', [$order]);
  90. }
  91. /**
  92. * @throws ContainerException
  93. * @throws InvalidParamsException
  94. */
  95. public function callback(null|array|ServerRequestInterface $contents = null, ?array $params = null): Collection|Rocket
  96. {
  97. $request = $this->getCallbackParams($contents);
  98. Event::dispatch(new CallbackReceived('jsb', $request->all(), $params, null));
  99. return $this->pay(
  100. [CallbackPlugin::class],
  101. ['request' => $request, 'params' => $params]
  102. );
  103. }
  104. public function success(): ResponseInterface
  105. {
  106. return new Response(
  107. 200,
  108. ['Content-Type' => 'text/html'],
  109. 'success',
  110. );
  111. }
  112. /**
  113. * @throws ContainerException
  114. * @throws InvalidParamsException
  115. * @throws ServiceNotFoundException
  116. */
  117. public function query(array $order): Collection|Rocket
  118. {
  119. Event::dispatch(new MethodCalled('jsb', __METHOD__, $order, null));
  120. return $this->__call('query', [$order]);
  121. }
  122. protected function getCallbackParams($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. }