Base.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace app\common\Service\Pay\Provider;
  3. use think\Log;
  4. use Yansongda\Pay\Pay;
  5. use Yansongda\Pay\Contract\HttpClientInterface;
  6. use app\common\model\pay\Config as PayConfig;
  7. use app\common\facade\HttpClient;
  8. use app\common\exception\BusinessException;
  9. use app\common\Service\ShopConfigService;
  10. /**
  11. * 支付提供器基类
  12. * 封装 yansongda 支付库的基础功能
  13. */
  14. class Base
  15. {
  16. /**
  17. * yansongda 支付示例
  18. * @var Yansongda\Pay\Pay
  19. */
  20. protected $pay = null;
  21. /**
  22. * 支付参数
  23. * @var array
  24. */
  25. public $config = null;
  26. /**
  27. * 支付服务实例
  28. * @var mixed
  29. */
  30. protected $payService = null;
  31. /**
  32. * 平台标识
  33. * @var string
  34. */
  35. protected $platform = null;
  36. /**
  37. * 渠道标识
  38. * @var string
  39. */
  40. protected $channel = null;
  41. /**
  42. * 构造函数
  43. * @param mixed $payService
  44. * @param string $platform
  45. * @param string $channel
  46. */
  47. public function __construct($payService = null, $platform = null, $channel = null)
  48. {
  49. $this->payService = $payService;
  50. $this->platform = $platform;
  51. $this->channel = $channel;
  52. }
  53. /**
  54. * yansongda 支付初始化
  55. *
  56. * @param string $payment
  57. * @param array $config
  58. * @return Yansongda\Pay\Pay
  59. */
  60. public function init($payment, $config = [], $type = 'normal')
  61. {
  62. $this->config = $this->getConfig($payment, $config, $type);
  63. $this->pay = Pay::config($this->config);
  64. Pay::set(HttpClientInterface::class, HttpClient::instance()); // 使用自定义 client (也继承至 GuzzleHttp\Client)
  65. }
  66. /**
  67. * 获取支付的所有参数
  68. *
  69. * @param string $payment
  70. * @return array
  71. */
  72. protected function getConfig($payment, $config = [], $type = 'normal')
  73. {
  74. // 获取平台配置
  75. $platformConfig = $this->getPlatformConfig();
  76. extract($platformConfig);
  77. $params = $this->getPayConfig($payment, $paymentConfig);
  78. // 格式化支付参数
  79. $params['mode'] = (int)($params['mode'] ?? 0);
  80. if($payment == 'douyin'){
  81. $params = $this->formatConfig($params, ['mini_app_id' => $app_id], $type);
  82. }else{
  83. $params = $this->formatConfig($params, ['app_id' => $app_id], $type);
  84. }
  85. // 合并传入的参数
  86. $params = array_merge($params, $config);
  87. // 合并参数
  88. $config = $this->baseConfig();
  89. $config = array_merge($config, [$payment => ['default' => $params]]);
  90. return $config;
  91. }
  92. /**
  93. * 转换平台名称格式
  94. * 将前端驼峰命名转换为后端下划线格式
  95. *
  96. * @param string $platform
  97. * @return string
  98. */
  99. protected function convertPlatformName($platform)
  100. {
  101. // 将驼峰命名转换为下划线格式
  102. return strtolower(preg_replace('/([A-Z])/', '_$1', lcfirst($platform)));
  103. }
  104. /**
  105. * 获取平台配置参数
  106. *
  107. * @return array
  108. */
  109. protected function getPlatformConfig()
  110. {
  111. // 需要转换 前段穿 DouyinMiniProgram 后端配置是 douyin_mini_program
  112. $platformKey = $this->convertPlatformName($this->platform);
  113. $platformConfig = ShopConfigService::getConfigs('shop.platform.' . $platformKey);
  114. $paymentConfig = $platformConfig['payment'] ?? [];
  115. $app_id = $platformConfig['app_id'] ?? '';
  116. return compact('paymentConfig', 'app_id');
  117. }
  118. /**
  119. * 根据平台以及支付方式 获取支付配置表的配置参数
  120. *
  121. * @param string $payment
  122. * @return array
  123. */
  124. protected function getPayConfig($payment, $paymentConfig)
  125. {
  126. $methods = $paymentConfig['methods'];
  127. $payment_config = $paymentConfig[$payment] ?? 0;
  128. if (!in_array($payment, $methods)) {
  129. throw new BusinessException('当前平台未开启该支付方式');
  130. }
  131. if ($payment_config) {
  132. $payConfig = PayConfig::where('type', $payment)->find($payment_config);
  133. }
  134. if (!isset($payConfig) || !$payConfig) {
  135. throw new BusinessException('支付配置参数不存在');
  136. }
  137. return $payConfig->params;
  138. }
  139. /**
  140. * 获取对应的支付方法名
  141. *
  142. * @param strign $payment
  143. * @return string
  144. */
  145. protected function getMethod($payment)
  146. {
  147. $method = [
  148. 'wechat' => [
  149. 'WechatOfficialAccount' => 'mp', //公众号支付 Collection
  150. 'WechatMiniProgram' => 'mini', //小程序支付 Collection
  151. 'H5' => 'wap', //手机网站支付 Response
  152. 'IOSApp' => 'app', // APP 支付 JsonResponse
  153. 'AndroidApp' => 'app', // APP 支付 JsonResponse
  154. ],
  155. 'alipay' => [
  156. 'WechatOfficialAccount' => 'wap', //手机网站支付 Response
  157. 'WechatMiniProgram' => 'wap', //小程序支付
  158. 'H5' => 'wap', //手机网站支付 Response
  159. 'IOSApp' => 'app', // APP 支付 JsonResponse
  160. 'AndroidApp' => '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. }