Alipay.php 5.2 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\InvalidParamsException;
  12. use Yansongda\Pay\Exception\ServiceNotFoundException;
  13. use Yansongda\Pay\Pay;
  14. use Yansongda\Pay\Plugin\Alipay\CallbackPlugin;
  15. use Yansongda\Pay\Plugin\Alipay\LaunchPlugin;
  16. use Yansongda\Pay\Plugin\Alipay\PreparePlugin;
  17. use Yansongda\Pay\Plugin\Alipay\RadarSignPlugin;
  18. use Yansongda\Pay\Plugin\ParserPlugin;
  19. use Yansongda\Supports\Collection;
  20. use Yansongda\Supports\Str;
  21. /**
  22. * @method ResponseInterface app(array $order) APP 支付
  23. * @method Collection pos(array $order) 刷卡支付
  24. * @method Collection scan(array $order) 扫码支付
  25. * @method Collection transfer(array $order) 帐户转账
  26. * @method ResponseInterface wap(array $order) 手机网站支付
  27. * @method ResponseInterface web(array $order) 电脑支付
  28. * @method Collection mini(array $order) 小程序支付
  29. */
  30. class Alipay extends AbstractProvider
  31. {
  32. public const URL = [
  33. Pay::MODE_NORMAL => 'https://openapi.alipay.com/gateway.do?charset=utf-8',
  34. Pay::MODE_SANDBOX => 'https://openapi-sandbox.dl.alipaydev.com/gateway.do?charset=utf-8',
  35. Pay::MODE_SERVICE => 'https://openapi.alipay.com/gateway.do?charset=utf-8',
  36. ];
  37. /**
  38. * @return null|array|Collection|MessageInterface
  39. *
  40. * @throws ContainerException
  41. * @throws InvalidParamsException
  42. * @throws ServiceNotFoundException
  43. */
  44. public function __call(string $shortcut, array $params)
  45. {
  46. $plugin = '\\Yansongda\\Pay\\Plugin\\Alipay\\Shortcut\\'.
  47. Str::studly($shortcut).'Shortcut';
  48. return $this->call($plugin, ...$params);
  49. }
  50. /**
  51. * @param array|string $order
  52. *
  53. * @return array|Collection
  54. *
  55. * @throws ContainerException
  56. * @throws InvalidParamsException
  57. * @throws ServiceNotFoundException
  58. */
  59. public function find($order)
  60. {
  61. $order = is_array($order) ? $order : ['out_trade_no' => $order];
  62. Event::dispatch(new Event\MethodCalled('alipay', __METHOD__, $order, null));
  63. return $this->__call('query', [$order]);
  64. }
  65. /**
  66. * @param array|string $order
  67. *
  68. * @return array|Collection
  69. *
  70. * @throws ContainerException
  71. * @throws InvalidParamsException
  72. * @throws ServiceNotFoundException
  73. */
  74. public function cancel($order)
  75. {
  76. $order = is_array($order) ? $order : ['out_trade_no' => $order];
  77. Event::dispatch(new Event\MethodCalled('alipay', __METHOD__, $order, null));
  78. return $this->__call('cancel', [$order]);
  79. }
  80. /**
  81. * @param array|string $order
  82. *
  83. * @return array|Collection
  84. *
  85. * @throws ContainerException
  86. * @throws InvalidParamsException
  87. * @throws ServiceNotFoundException
  88. */
  89. public function close($order)
  90. {
  91. $order = is_array($order) ? $order : ['out_trade_no' => $order];
  92. Event::dispatch(new Event\MethodCalled('alipay', __METHOD__, $order, null));
  93. return $this->__call('close', [$order]);
  94. }
  95. /**
  96. * @return array|Collection
  97. *
  98. * @throws ContainerException
  99. * @throws InvalidParamsException
  100. * @throws ServiceNotFoundException
  101. */
  102. public function refund(array $order)
  103. {
  104. Event::dispatch(new Event\MethodCalled('alipay', __METHOD__, $order, null));
  105. return $this->__call('refund', [$order]);
  106. }
  107. /**
  108. * @param null|array|ServerRequestInterface $contents
  109. *
  110. * @throws ContainerException
  111. * @throws InvalidParamsException
  112. */
  113. public function callback($contents = null, ?array $params = null): Collection
  114. {
  115. $request = $this->getCallbackParams($contents);
  116. Event::dispatch(new Event\CallbackReceived('alipay', $request->all(), $params, null));
  117. return $this->pay(
  118. [CallbackPlugin::class],
  119. $request->merge($params)->all()
  120. );
  121. }
  122. public function success(): ResponseInterface
  123. {
  124. return new Response(200, [], 'success');
  125. }
  126. public function mergeCommonPlugins(array $plugins): array
  127. {
  128. return array_merge(
  129. [PreparePlugin::class],
  130. $plugins,
  131. [RadarSignPlugin::class],
  132. [LaunchPlugin::class, ParserPlugin::class],
  133. );
  134. }
  135. /**
  136. * @param null|array|ServerRequestInterface $contents
  137. */
  138. protected function getCallbackParams($contents = null): Collection
  139. {
  140. if (is_array($contents)) {
  141. return Collection::wrap($contents);
  142. }
  143. if ($contents instanceof ServerRequestInterface) {
  144. return Collection::wrap('GET' === $contents->getMethod() ? $contents->getQueryParams() :
  145. $contents->getParsedBody());
  146. }
  147. $request = ServerRequest::fromGlobals();
  148. return Collection::wrap(
  149. array_merge($request->getQueryParams(), $request->getParsedBody())
  150. );
  151. }
  152. }