Base.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace app\common\Service\Pay\Provider;
  3. use think\Log;
  4. use think\Exception;
  5. use Yansongda\Pay\Pay;
  6. use Yansongda\Pay\Contract\HttpClientInterface;
  7. use app\common\Enum\ChannelEnum;
  8. use app\common\model\pay\Config as PayConfig;
  9. use app\common\Service\Pay\HttpClient;
  10. use app\common\exception\BusinessException;
  11. use app\common\Service\ShopConfigService;
  12. /**
  13. * 支付提供器基类
  14. * 封装 yansongda 支付库的基础功能
  15. */
  16. class Base
  17. {
  18. /**
  19. * yansongda 支付示例
  20. * @var Yansongda\Pay\Pay
  21. */
  22. protected $pay = null;
  23. /**
  24. * 支付参数
  25. * @var array
  26. */
  27. public $config = null;
  28. /**
  29. * 支付服务实例
  30. * @var mixed
  31. */
  32. protected $payService = null;
  33. /**
  34. * 平台标识
  35. * @var string
  36. */
  37. protected $platform = null;
  38. /**
  39. * 渠道标识
  40. * @var string
  41. */
  42. protected $channel = null;
  43. /**
  44. * 构造函数
  45. * @param mixed $payService
  46. * @param string $platform
  47. * @param string $channel
  48. */
  49. public function __construct($payService = null, $platform = null, $channel = null)
  50. {
  51. $this->payService = $payService;
  52. $this->platform = $platform;
  53. $this->channel = $channel;
  54. }
  55. /**
  56. * yansongda 支付初始化
  57. *
  58. * @param string $payment
  59. * @param array $config
  60. * @return Yansongda\Pay\Pay
  61. */
  62. public function init($payment, $config = [], $type = 'normal')
  63. {
  64. $this->config = $this->getConfig($payment, $config, $type);
  65. $this->pay = Pay::config($this->config);
  66. Pay::set(HttpClientInterface::class, HttpClient::instance()); // 使用自定义 client (也继承至 GuzzleHttp\Client)
  67. }
  68. /**
  69. * 获取支付的所有参数
  70. *
  71. * @param string $payment
  72. * @return array
  73. */
  74. protected function getConfig($payment, $config = [], $type = 'normal')
  75. {
  76. // 获取平台配置
  77. $platformConfig = $this->getPlatformConfig();
  78. extract($platformConfig);
  79. $params = $this->getPayConfig($payment, $paymentConfig);
  80. // 格式化支付参数
  81. $params['mode'] = $params['mode'] ?? 0;
  82. if($payment == 'douyin'){
  83. $params = $this->formatConfig($params, ['mini_app_id' => $app_id], $type);
  84. }else{
  85. $params = $this->formatConfig($params, ['app_id' => $app_id], $type);
  86. }
  87. // 合并传入的参数
  88. $params = array_merge($params, $config);
  89. // 合并参数
  90. $config = $this->baseConfig();
  91. $config = array_merge($config, [$payment => ['default' => $params]]);
  92. return $config;
  93. }
  94. /**
  95. * 获取平台配置参数
  96. *
  97. * @return array
  98. */
  99. protected function getPlatformConfig()
  100. {
  101. $platformConfig = ShopConfigService::getConfigs('shop.platform.' . $this->platform);
  102. $paymentConfig = $platformConfig['payment'] ?? [];
  103. $app_id = $platformConfig['app_id'] ?? '';
  104. return compact('paymentConfig', 'app_id');
  105. }
  106. /**
  107. * 根据平台以及支付方式 获取支付配置表的配置参数
  108. *
  109. * @param string $payment
  110. * @return array
  111. */
  112. protected function getPayConfig($payment, $paymentConfig)
  113. {
  114. $methods = $paymentConfig['methods'];
  115. $payment_config = $paymentConfig[$payment] ?? 0;
  116. if (!in_array($payment, $methods)) {
  117. throw new BusinessException('当前平台未开启该支付方式');
  118. }
  119. if ($payment_config) {
  120. $payConfig = PayConfig::where('type', $payment)->find($payment_config);
  121. }
  122. if (!isset($payConfig) || !$payConfig) {
  123. throw new BusinessException('支付配置参数不存在');
  124. }
  125. return $payConfig->params;
  126. }
  127. /**
  128. * 获取对应的支付方法名
  129. *
  130. * @param strign $payment
  131. * @return string
  132. */
  133. protected function getMethod($payment)
  134. {
  135. $method = [
  136. 'wechat' => [
  137. 'WechatOfficialAccount' => 'mp', //公众号支付 Collection
  138. 'WechatMiniProgram' => 'mini', //小程序支付 Collection
  139. 'H5' => 'wap', //手机网站支付 Response
  140. 'App' => 'app' // APP 支付 JsonResponse
  141. ],
  142. 'alipay' => [
  143. 'WechatOfficialAccount' => 'wap', //手机网站支付 Response
  144. 'WechatMiniProgram' => 'wap', //小程序支付
  145. 'H5' => 'wap', //手机网站支付 Response
  146. 'App' => 'app' //APP 支付 JsonResponse
  147. ],
  148. 'douyin' => [
  149. 'douyin_mini_program' => 'mini', //小程序支付 Collection
  150. ],
  151. ];
  152. return $method[$payment][$this->platform];
  153. }
  154. /**
  155. * yansongda 基础配置
  156. *
  157. * @return void
  158. */
  159. protected function baseConfig()
  160. {
  161. $log_path = RUNTIME_PATH . 'log/pay/';
  162. if (!is_dir($log_path)) {
  163. @mkdir($log_path, 0755, true);
  164. }
  165. return [
  166. 'logger' => [ // optional
  167. 'enable' => true,
  168. 'file' => $log_path . 'pay.log',
  169. 'level' => config('app_debug') ? 'debug' : 'info', // 建议生产环境等级调整为 info,开发环境为 debug
  170. 'type' => 'daily', // optional, 可选 daily.
  171. 'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
  172. ],
  173. 'http' => [ // optional
  174. 'timeout' => 5.0,
  175. 'connect_timeout' => 5.0,
  176. // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
  177. ],
  178. ];
  179. }
  180. /**
  181. * 格式化支付参数 (抽象方法,由子类实现)
  182. * @param array $config
  183. * @param array $data
  184. * @param string $type
  185. * @return array
  186. */
  187. protected function formatConfig($config, $data = [], $type = 'normal')
  188. {
  189. return $config;
  190. }
  191. /**
  192. * 记录支付日志
  193. * @param string $message
  194. * @param array $context
  195. * @param string $level
  196. */
  197. protected function logPayment($message, $context = [], $level = 'info')
  198. {
  199. Log::record($message . ' ' . json_encode($context, JSON_UNESCAPED_UNICODE), $level);
  200. }
  201. }