Base.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace addons\shopro\library\pay\provider;
  3. use think\Log;
  4. use Yansongda\Pay\Pay;
  5. use Yansongda\Pay\Contract\HttpClientInterface;
  6. use addons\shopro\facade\HttpClient;
  7. use app\admin\model\shopro\PayConfig;
  8. class Base
  9. {
  10. /**
  11. * yansongda 支付示例
  12. *
  13. * @var Yansongda\Pay\Pay
  14. */
  15. protected $pay = null; // yansongda 支付实例
  16. public $config = null; // 支付参数
  17. /**
  18. * yansongda 支付初始化
  19. *
  20. * @param string $payment
  21. * @param array $config
  22. * @return Yansongda\Pay\Pay
  23. */
  24. public function init($payment, $config = [], $type = 'normal')
  25. {
  26. $this->config = $this->getConfig($payment, $config, $type);
  27. $this->pay = Pay::config($this->config);
  28. Pay::set(HttpClientInterface::class, HttpClient::instance()); // 使用自定义 client (也继承至 GuzzleHttp\Client)
  29. }
  30. /**
  31. * 获取支付的所有参数
  32. *
  33. * @param string $payment
  34. * @return array
  35. */
  36. protected function getConfig($payment, $config = [], $type = 'normal')
  37. {
  38. // 获取平台配置
  39. $platformConfig = $this->getPlatformConfig();
  40. extract($platformConfig);
  41. $params = $this->getPayConfig($payment, $paymentConfig);
  42. // 格式化支付参数
  43. $params['mode'] = (int)($params['mode'] ?? 0);
  44. $params = $this->formatConfig($params, ['app_id' => $app_id], $type);
  45. // 合并传入的参数
  46. $params = array_merge($params, $config);
  47. // 合并参数
  48. $config = $this->baseConfig();
  49. $config = array_merge($config, [$payment => ['default' => $params]]);
  50. return $config;
  51. }
  52. /**
  53. * 获取平台配置参数
  54. *
  55. * @return array
  56. */
  57. protected function getPlatformConfig()
  58. {
  59. $platformConfig = sheep_config('shop.platform.' . $this->platform);
  60. $paymentConfig = $platformConfig['payment'] ?? [];
  61. $app_id = $platformConfig['app_id'] ?? '';
  62. return compact('paymentConfig', 'app_id');
  63. }
  64. /**
  65. * 根据平台以及支付方式 获取支付配置表的配置参数
  66. *
  67. * @param string $payment
  68. * @return array
  69. */
  70. protected function getPayConfig($payment, $paymentConfig)
  71. {
  72. $methods = $paymentConfig['methods'];
  73. $payment_config = $paymentConfig[$payment] ?? 0;
  74. if (!in_array($payment, $methods)) {
  75. error_stop('当前平台未开启该支付方式');
  76. }
  77. if ($payment_config) {
  78. $payConfig = PayConfig::normal()->where('type', $payment)->find($payment_config);
  79. }
  80. if (!isset($payConfig) || !$payConfig) {
  81. error_stop('支付配置参数不存在');
  82. }
  83. return $payConfig->params;
  84. }
  85. /**
  86. * 获取对应的支付方法名
  87. *
  88. * @param strign $payment
  89. * @return string
  90. */
  91. protected function getMethod($payment)
  92. {
  93. $method = [
  94. 'wechat' => [
  95. 'WechatOfficialAccount' => 'mp', //公众号支付 Collection
  96. 'WechatMiniProgram' => 'mini', //小程序支付 Collection
  97. 'H5' => 'wap', //手机网站支付 Response
  98. 'App' => 'app' // APP 支付 JsonResponse
  99. ],
  100. 'alipay' => [
  101. 'WechatOfficialAccount' => 'wap', //手机网站支付 Response
  102. 'WechatMiniProgram' => 'wap', //小程序支付
  103. 'H5' => 'wap', //手机网站支付 Response
  104. 'App' => 'app' //APP 支付 JsonResponse
  105. ],
  106. ];
  107. return $method[$payment][$this->platform];
  108. }
  109. /**
  110. * yansongda 基础配置
  111. *
  112. * @return void
  113. */
  114. protected function baseConfig()
  115. {
  116. $log_path = RUNTIME_PATH . 'log/pay/';
  117. if (!is_dir($log_path)) {
  118. @mkdir($log_path, 0755, true);
  119. }
  120. return [
  121. 'logger' => [ // optional
  122. 'enable' => true,
  123. 'file' => $log_path . 'pay.log',
  124. 'level' => config('app_debug') ? 'debug' : 'info', // 建议生产环境等级调整为 info,开发环境为 debug
  125. 'type' => 'daily', // optional, 可选 daily.
  126. 'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
  127. ],
  128. 'http' => [ // optional
  129. 'timeout' => 5.0,
  130. 'connect_timeout' => 5.0,
  131. // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
  132. ],
  133. ];
  134. }
  135. }