WeChatConfigService.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\common\service\wechat;
  15. use app\common\enum\PayEnum;
  16. use app\common\enum\user\UserTerminalEnum;
  17. use app\common\model\pay\PayConfig;
  18. use app\common\service\ConfigService;
  19. use think\facade\Log;
  20. /**
  21. * 微信配置类
  22. * Class WeChatConfigService
  23. * @package app\common\service
  24. */
  25. class WeChatConfigService
  26. {
  27. /**
  28. * @notes 获取小程序配置
  29. * @return array
  30. * @author 段誉
  31. * @date 2022/9/6 19:49
  32. */
  33. public static function getMnpConfig()
  34. {
  35. return [
  36. 'app_id' => ConfigService::get('mnp_setting', 'app_id'),
  37. 'secret' => ConfigService::get('mnp_setting', 'app_secret'),
  38. 'response_type' => 'array',
  39. 'log' => [
  40. 'level' => 'debug',
  41. 'file' => app()->getRootPath() . 'runtime/wechat/' . date('Ym') . '/' . date('d') . '.log'
  42. ],
  43. ];
  44. }
  45. /**
  46. * @notes 获取微信公众号配置
  47. * @return array
  48. * @author 段誉
  49. * @date 2022/9/6 19:49
  50. */
  51. public static function getOaConfig()
  52. {
  53. return [
  54. 'app_id' => ConfigService::get('oa_setting', 'app_id'),
  55. 'secret' => ConfigService::get('oa_setting', 'app_secret'),
  56. 'token' => ConfigService::get('oa_setting', 'token'),
  57. 'response_type' => 'array',
  58. 'log' => [
  59. 'level' => 'debug',
  60. 'file' => app()->getRootPath() . 'runtime/wechat/' . date('Ym') . '/' . date('d') . '.log'
  61. ],
  62. ];
  63. }
  64. /**
  65. * @notes 获取微信开放平台配置
  66. * @return array
  67. * @author 段誉
  68. * @date 2022/10/20 15:51
  69. */
  70. public static function getOpConfig()
  71. {
  72. return [
  73. 'app_id' => ConfigService::get('open_platform', 'app_id'),
  74. 'secret' => ConfigService::get('open_platform', 'app_secret'),
  75. 'response_type' => 'array',
  76. 'log' => [
  77. 'level' => 'debug',
  78. 'file' => app()->getRootPath() . 'runtime/wechat/' . date('Ym') . '/' . date('d') . '.log'
  79. ],
  80. ];
  81. }
  82. /**
  83. * @notes 根据终端获取支付配置
  84. * @param $terminal
  85. * @return array
  86. * @author 段誉
  87. * @date 2023/2/27 15:45
  88. */
  89. public static function getPayConfigByTerminal($terminal)
  90. {
  91. switch ($terminal) {
  92. case UserTerminalEnum::WECHAT_MMP:
  93. $notifyUrl = (string)url('pay/notifyMnp', [], false, true);
  94. break;
  95. case UserTerminalEnum::WECHAT_OA:
  96. case UserTerminalEnum::PC:
  97. case UserTerminalEnum::H5:
  98. $notifyUrl = (string)url('pay/notifyOa', [], false, true);
  99. break;
  100. case UserTerminalEnum::ANDROID:
  101. case UserTerminalEnum::IOS:
  102. $notifyUrl = (string)url('pay/notifyApp', [], false, true);
  103. break;
  104. }
  105. $pay = PayConfig::where(['pay_way' => PayEnum::WECHAT_PAY])->findOrEmpty()->toArray();
  106. //判断是否已经存在证书文件夹,不存在则新建
  107. if (!file_exists(app()->getRootPath() . 'runtime/cert')) {
  108. mkdir(app()->getRootPath() . 'runtime/cert', 0775, true);
  109. }
  110. //写入文件
  111. $apiclientCert = $pay['config']['apiclient_cert'] ?? '';
  112. $apiclientKey = $pay['config']['apiclient_key'] ?? '';
  113. $certPath = app()->getRootPath() . 'runtime/cert/' . md5($apiclientCert) . '.pem';
  114. $keyPath = app()->getRootPath() . 'runtime/cert/' . md5($apiclientKey) . '.pem';
  115. if (!empty($apiclientCert) && !file_exists($certPath)) {
  116. static::setCert($certPath, trim($apiclientCert));
  117. }
  118. if (!empty($apiclientKey) && !file_exists($keyPath)) {
  119. static::setCert($keyPath, trim($apiclientKey));
  120. }
  121. Log::info('微信支付配置:'.$notifyUrl.':'.formatLogData($pay));
  122. return [
  123. // 商户号
  124. 'mch_id' => $pay['config']['mch_id'] ?? '',
  125. // 商户证书
  126. 'private_key' => $keyPath,
  127. 'certificate' => $certPath,
  128. // v3 API 秘钥
  129. 'secret_key' => $pay['config']['pay_sign_key'] ?? '',
  130. 'notify_url' => $notifyUrl,
  131. 'http' => [
  132. 'throw' => true, // 状态码非 200、300 时是否抛出异常,默认为开启
  133. 'timeout' => 5.0,
  134. ]
  135. ];
  136. }
  137. /**
  138. * @notes 临时写入证书
  139. * @param $path
  140. * @param $cert
  141. * @author 段誉
  142. * @date 2023/2/27 15:48
  143. */
  144. public static function setCert($path, $cert)
  145. {
  146. $fopenPath = fopen($path, 'w');
  147. fwrite($fopenPath, $cert);
  148. fclose($fopenPath);
  149. }
  150. }