PayService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace app\common\Service\Pay;
  3. use think\Log;
  4. use think\Exception;
  5. use app\common\Enum\ChannelEnum;
  6. use app\common\Service\Pay\Provider\Base;
  7. use app\common\exception\BusinessException;
  8. use app\common\model\pay\index as PayModel;
  9. /**
  10. * 支付服务类 - 工厂模式
  11. * 配合 yansongda 支付库
  12. */
  13. class PayService
  14. {
  15. protected $payment;
  16. protected $platform;
  17. protected $channel;
  18. /**
  19. * 构造函数
  20. * @param string $payment 支付方式 (wechat|alipay)
  21. * @param string $platform 平台标识 (可选)
  22. * @param string $channel 渠道标识 (可选)
  23. */
  24. public function __construct($payment, $platform = null, $channel = null)
  25. {
  26. $this->payment = $payment;
  27. $this->platform = $platform ?: $this->getPlatformFromRequest();
  28. $this->channel = $channel ?: $this->getChannelFromRequest();
  29. // 验证平台参数
  30. if (!$this->platform) {
  31. throw new BusinessException('缺少用户端平台参数');
  32. }
  33. // 验证渠道参数
  34. if ($this->channel && !ChannelEnum::isValidChannel($this->channel)) {
  35. throw new BusinessException('无效的渠道参数: ' . $this->channel);
  36. }
  37. // 验证支付方式
  38. if (!in_array($payment, ['wechat', 'alipay','douyin'])) {
  39. throw new BusinessException('不支持的支付方式: ' . $payment);
  40. }
  41. }
  42. /**
  43. * 获取支付提供器
  44. * @param string $payment 支付方式
  45. * @return Base
  46. * @throws Exception
  47. */
  48. public function provider($payment = null)
  49. {
  50. try {
  51. $payment = $payment ?: $this->payment;
  52. $className = "\\app\\common\\Service\\Pay\\Provider\\" . ucfirst(strtolower($payment));
  53. if (!class_exists($className)) {
  54. throw new Exception("支付提供器不存在: {$className}");
  55. }
  56. return new $className($this, $this->platform, $this->channel);
  57. } catch (\Exception $e) {
  58. Log::error('PayService provider error: ' . $e->getMessage());
  59. throw new Exception('支付类型不支持: ' . $payment);
  60. }
  61. }
  62. /**
  63. * 获取当前平台
  64. * @return string
  65. */
  66. public function getPlatform()
  67. {
  68. return $this->platform;
  69. }
  70. /**
  71. * 获取当前渠道
  72. * @return string
  73. */
  74. public function getChannel()
  75. {
  76. return $this->channel;
  77. }
  78. /**
  79. * 获取当前支付方式
  80. * @return string
  81. */
  82. public function getPayment()
  83. {
  84. return $this->payment;
  85. }
  86. /**
  87. * 从请求中获取平台参数
  88. * @return string|null
  89. */
  90. protected function getPlatformFromRequest()
  91. {
  92. $request = request();
  93. return $request->header('platform') ?: $request->param('platform');
  94. }
  95. /**
  96. * 从请求中获取渠道参数
  97. * @return string|null
  98. */
  99. protected function getChannelFromRequest()
  100. {
  101. $request = request();
  102. $channel = $request->header('channel') ?: $request->param('channel');
  103. // 如果没有明确的渠道参数,尝试从平台推断
  104. if (!$channel && $this->platform) {
  105. $channel = $this->inferChannelFromPlatform($this->platform);
  106. }
  107. return $channel;
  108. }
  109. /**
  110. * 从平台推断渠道
  111. * @param string $platform
  112. * @return string|null
  113. */
  114. protected function inferChannelFromPlatform($platform)
  115. {
  116. $platformToChannel = [
  117. 'WechatOfficialAccount' => ChannelEnum::CHANNEL_WECHAT_OFFICIAL_ACCOUNT,
  118. 'WechatMiniProgram' => ChannelEnum::CHANNEL_WECHAT_MINI_PROGRAM,
  119. 'H5' => ChannelEnum::CHANNEL_H5,
  120. 'App' => ChannelEnum::CHANNEL_ANDROID_APP, // 默认Android,可根据具体情况调整
  121. 'PC' => ChannelEnum::CHANNEL_PC,
  122. ];
  123. return $platformToChannel[$platform] ?? null;
  124. }
  125. /**
  126. * 验证支付环境
  127. * @param string $payment 支付方式
  128. * @param string $channel 渠道
  129. * @return bool
  130. * @throws Exception
  131. */
  132. public function validatePaymentEnvironment($payment, $channel = null)
  133. {
  134. $channel = $channel ?: $this->channel;
  135. if (!$channel) {
  136. return true; // 如果没有渠道信息,跳过验证
  137. }
  138. // 微信支付环境验证
  139. if ($payment === 'wechat') {
  140. if (!ChannelEnum::isWechatChannel($channel) && $channel !== ChannelEnum::CHANNEL_H5) {
  141. throw new BusinessException('当前渠道不支持微信支付');
  142. }
  143. }
  144. // 支付功能验证
  145. if (!ChannelEnum::channelSupportsFeature($channel, 'payment')) {
  146. throw new BusinessException('当前渠道不支持支付功能');
  147. }
  148. return true;
  149. }
  150. /**
  151. * 魔术方法,代理到支付提供器
  152. * @param string $funcname
  153. * @param array $arguments
  154. * @return mixed
  155. */
  156. public function __call($funcname, $arguments)
  157. {
  158. return $this->provider()->{$funcname}(...$arguments);
  159. }
  160. /**
  161. * 获取支付信息
  162. * @param string $orderId
  163. * @return array
  164. */
  165. public static function getPayInfo($orderId = 0)
  166. {
  167. $payInfo = PayModel::where('id', $orderId)->select();
  168. return $payInfo;
  169. }
  170. }