Base.php 7.1 KB

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