Douyin.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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\AddRadarPlugin;
  16. use Yansongda\Artful\Plugin\ParserPlugin;
  17. use Yansongda\Artful\Plugin\StartPlugin;
  18. use Yansongda\Artful\Rocket;
  19. use Yansongda\Pay\Contract\ProviderInterface;
  20. use Yansongda\Pay\Event\CallbackReceived;
  21. use Yansongda\Pay\Event\MethodCalled;
  22. use Yansongda\Pay\Exception\Exception;
  23. use Yansongda\Pay\Pay;
  24. use Yansongda\Pay\Plugin\Douyin\V1\Pay\AddPayloadSignaturePlugin;
  25. use Yansongda\Pay\Plugin\Douyin\V1\Pay\CallbackPlugin;
  26. use Yansongda\Pay\Plugin\Douyin\V1\Pay\ResponsePlugin;
  27. use Yansongda\Supports\Collection;
  28. use Yansongda\Supports\Str;
  29. /**
  30. * @method Collection|Rocket mini(array $order) 小程序支付
  31. */
  32. class Douyin implements ProviderInterface
  33. {
  34. public const URL = [
  35. Pay::MODE_NORMAL => 'https://developer.toutiao.com/',
  36. Pay::MODE_SANDBOX => 'https://open-sandbox.douyin.com/',
  37. Pay::MODE_SERVICE => 'https://developer.toutiao.com/',
  38. ];
  39. /**
  40. * @throws ContainerException
  41. * @throws InvalidParamsException
  42. * @throws ServiceNotFoundException
  43. */
  44. public function __call(string $shortcut, array $params): null|Collection|MessageInterface|Rocket
  45. {
  46. $plugin = '\Yansongda\Pay\Shortcut\Douyin\\'.Str::studly($shortcut).'Shortcut';
  47. return Artful::shortcut($plugin, ...$params);
  48. }
  49. /**
  50. * @throws ContainerException
  51. * @throws InvalidParamsException
  52. */
  53. public function pay(array $plugins, array $params): null|Collection|MessageInterface|Rocket
  54. {
  55. return Artful::artful($plugins, $params);
  56. }
  57. /**
  58. * @throws ContainerException
  59. * @throws InvalidParamsException
  60. * @throws ServiceNotFoundException
  61. */
  62. public function query(array $order): Collection|Rocket
  63. {
  64. Event::dispatch(new MethodCalled('douyin', __METHOD__, $order, null));
  65. return $this->__call('query', [$order]);
  66. }
  67. /**
  68. * @throws InvalidParamsException
  69. */
  70. public function cancel(array $order): Collection|Rocket
  71. {
  72. throw new InvalidParamsException(Exception::PARAMS_METHOD_NOT_SUPPORTED, '参数异常: 抖音不支持 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, '参数异常: 抖音不支持 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('douyin', __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('douyin', $request->all(), $params, null));
  99. return $this->pay([CallbackPlugin::class], $request->merge($params)->all());
  100. }
  101. public function success(): ResponseInterface
  102. {
  103. return new Response(
  104. 200,
  105. ['Content-Type' => 'application/json'],
  106. json_encode(['err_no' => 0, 'err_tips' => 'success']),
  107. );
  108. }
  109. public function mergeCommonPlugins(array $plugins): array
  110. {
  111. return array_merge(
  112. [StartPlugin::class],
  113. $plugins,
  114. [AddPayloadSignaturePlugin::class, AddPayloadBodyPlugin::class, AddRadarPlugin::class, ResponsePlugin::class, ParserPlugin::class],
  115. );
  116. }
  117. protected function getCallbackParams(null|array|ServerRequestInterface $contents = null): Collection
  118. {
  119. if (is_array($contents)) {
  120. return Collection::wrap($contents);
  121. }
  122. if (!$contents instanceof ServerRequestInterface) {
  123. $contents = ServerRequest::fromGlobals();
  124. }
  125. $body = Collection::wrap($contents->getParsedBody());
  126. if ($body->isNotEmpty()) {
  127. return $body;
  128. }
  129. return Collection::wrapJson((string) $contents->getBody());
  130. }
  131. }