Base.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. *
  98. * @param string $platform
  99. * @return string
  100. */
  101. protected function convertPlatformName($platform)
  102. {
  103. // 将驼峰命名转换为下划线格式
  104. return strtolower(preg_replace('/([A-Z])/', '_$1', lcfirst($platform)));
  105. }
  106. /**
  107. * 获取平台配置参数
  108. *
  109. * @return array
  110. */
  111. protected function getPlatformConfig()
  112. {
  113. // 需要转换 前段穿 DouyinMiniProgram 后端配置是 douyin_mini_program
  114. $platformKey = $this->convertPlatformName($this->platform);
  115. $platformConfig = ShopConfigService::getConfigs('shop.platform.' . $platformKey);
  116. $paymentConfig = $platformConfig['payment'] ?? [];
  117. $app_id = $platformConfig['app_id'] ?? '';
  118. return compact('paymentConfig', 'app_id');
  119. }
  120. /**
  121. * 根据平台以及支付方式 获取支付配置表的配置参数
  122. *
  123. * @param string $payment
  124. * @return array
  125. */
  126. protected function getPayConfig($payment, $paymentConfig)
  127. {
  128. $methods = $paymentConfig['methods'];
  129. $payment_config = $paymentConfig[$payment] ?? 0;
  130. if (!in_array($payment, $methods)) {
  131. throw new BusinessException('当前平台未开启该支付方式');
  132. }
  133. if ($payment_config) {
  134. $payConfig = PayConfig::where('type', $payment)->find($payment_config);
  135. }
  136. if (!isset($payConfig) || !$payConfig) {
  137. throw new BusinessException('支付配置参数不存在');
  138. }
  139. return $payConfig->params;
  140. }
  141. /**
  142. * 获取对应的支付方法名
  143. *
  144. * @param strign $payment
  145. * @return string
  146. */
  147. protected function getMethod($payment)
  148. {
  149. $method = [
  150. 'wechat' => [
  151. 'WechatOfficialAccount' => 'mp', //公众号支付 Collection
  152. 'WechatMiniProgram' => 'mini', //小程序支付 Collection
  153. 'H5' => 'wap', //手机网站支付 Response
  154. 'App' => 'app' // APP 支付 JsonResponse
  155. ],
  156. 'alipay' => [
  157. 'WechatOfficialAccount' => 'wap', //手机网站支付 Response
  158. 'WechatMiniProgram' => 'wap', //小程序支付
  159. 'H5' => 'wap', //手机网站支付 Response
  160. 'App' => 'app' //APP 支付 JsonResponse
  161. ],
  162. 'douyin' => [
  163. 'DouyinMiniProgram' => 'mini', //小程序支付 Collection
  164. ],
  165. ];
  166. return $method[$payment][$this->platform];
  167. }
  168. /**
  169. * yansongda 基础配置
  170. *
  171. * @return void
  172. */
  173. protected function baseConfig()
  174. {
  175. $log_path = RUNTIME_PATH . 'log/pay/';
  176. if (!is_dir($log_path)) {
  177. @mkdir($log_path, 0755, true);
  178. }
  179. return [
  180. 'logger' => [ // optional
  181. 'enable' => true,
  182. 'file' => $log_path . 'pay.log',
  183. 'level' => config('app_debug') ? 'debug' : 'info', // 建议生产环境等级调整为 info,开发环境为 debug
  184. 'type' => 'daily', // optional, 可选 daily.
  185. 'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
  186. ],
  187. 'http' => [ // optional
  188. 'timeout' => 5.0,
  189. 'connect_timeout' => 5.0,
  190. // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
  191. ],
  192. ];
  193. }
  194. /**
  195. * 格式化支付参数 (抽象方法,由子类实现)
  196. * @param array $config
  197. * @param array $data
  198. * @param string $type
  199. * @return array
  200. */
  201. protected function formatConfig($config, $data = [], $type = 'normal')
  202. {
  203. return $config;
  204. }
  205. /**
  206. * 记录支付日志
  207. * @param string $message
  208. * @param array $context
  209. * @param string $level
  210. */
  211. protected function logPayment($message, $context = [], $level = 'info')
  212. {
  213. Log::record($message . ' ' . json_encode($context, JSON_UNESCAPED_UNICODE), $level);
  214. }
  215. }