WeChatConfigService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. /**
  20. * 微信配置类
  21. * Class WeChatConfigService
  22. * @package app\common\service
  23. */
  24. class WeChatConfigService
  25. {
  26. /**
  27. * @notes 获取小程序配置
  28. * @return array
  29. * @author 段誉
  30. * @date 2022/9/6 19:49
  31. */
  32. public static function getMnpConfig()
  33. {
  34. return [
  35. 'app_id' => ConfigService::get('mnp_setting', 'app_id'),
  36. 'secret' => ConfigService::get('mnp_setting', 'app_secret'),
  37. 'response_type' => 'array',
  38. 'log' => [
  39. 'level' => 'debug',
  40. 'file' => app()->getRootPath() . 'runtime/wechat/' . date('Ym') . '/' . date('d') . '.log'
  41. ],
  42. ];
  43. }
  44. /**
  45. * @notes 获取微信公众号配置
  46. * @return array
  47. * @author 段誉
  48. * @date 2022/9/6 19:49
  49. */
  50. public static function getOaConfig()
  51. {
  52. return [
  53. 'app_id' => ConfigService::get('oa_setting', 'app_id'),
  54. 'secret' => ConfigService::get('oa_setting', 'app_secret'),
  55. 'token' => ConfigService::get('oa_setting', 'token'),
  56. 'response_type' => 'array',
  57. 'log' => [
  58. 'level' => 'debug',
  59. 'file' => app()->getRootPath() . 'runtime/wechat/' . date('Ym') . '/' . date('d') . '.log'
  60. ],
  61. ];
  62. }
  63. /**
  64. * @notes 获取微信开放平台配置
  65. * @return array
  66. * @author 段誉
  67. * @date 2022/10/20 15:51
  68. */
  69. public static function getOpConfig()
  70. {
  71. return [
  72. 'app_id' => ConfigService::get('open_platform', 'app_id'),
  73. 'secret' => ConfigService::get('open_platform', 'app_secret'),
  74. 'response_type' => 'array',
  75. 'log' => [
  76. 'level' => 'debug',
  77. 'file' => app()->getRootPath() . 'runtime/wechat/' . date('Ym') . '/' . date('d') . '.log'
  78. ],
  79. ];
  80. }
  81. /**
  82. * @notes 根据终端获取支付配置
  83. * @param $terminal
  84. * @return array
  85. * @author 段誉
  86. * @date 2023/2/27 15:45
  87. */
  88. public static function getPayConfigByTerminal($terminal)
  89. {
  90. switch ($terminal) {
  91. case UserTerminalEnum::WECHAT_MMP:
  92. $notifyUrl = (string)url('pay/notifyMnp', [], false, true);
  93. break;
  94. case UserTerminalEnum::WECHAT_OA:
  95. case UserTerminalEnum::PC:
  96. case UserTerminalEnum::H5:
  97. $notifyUrl = (string)url('pay/notifyOa', [], false, true);
  98. break;
  99. case UserTerminalEnum::ANDROID:
  100. case UserTerminalEnum::IOS:
  101. $notifyUrl = (string)url('pay/notifyApp', [], false, true);
  102. break;
  103. }
  104. $pay = PayConfig::where(['pay_way' => PayEnum::WECHAT_PAY])->findOrEmpty()->toArray();
  105. //判断是否已经存在证书文件夹,不存在则新建
  106. if (!file_exists(app()->getRootPath() . 'runtime/cert')) {
  107. mkdir(app()->getRootPath() . 'runtime/cert', 0775, true);
  108. }
  109. //写入文件
  110. $apiclientCert = $pay['config']['apiclient_cert'] ?? '';
  111. $apiclientKey = $pay['config']['apiclient_key'] ?? '';
  112. $certPath = app()->getRootPath() . 'runtime/cert/' . md5($apiclientCert) . '.pem';
  113. $keyPath = app()->getRootPath() . 'runtime/cert/' . md5($apiclientKey) . '.pem';
  114. if (!empty($apiclientCert) && !file_exists($certPath)) {
  115. static::setCert($certPath, trim($apiclientCert));
  116. }
  117. if (!empty($apiclientKey) && !file_exists($keyPath)) {
  118. static::setCert($keyPath, trim($apiclientKey));
  119. }
  120. return [
  121. // 商户号
  122. 'mch_id' => $pay['config']['mch_id'] ?? '',
  123. // 商户证书
  124. 'private_key' => $keyPath,
  125. 'certificate' => $certPath,
  126. // v3 API 秘钥
  127. 'secret_key' => $pay['config']['pay_sign_key'] ?? '',
  128. 'notify_url' => $notifyUrl,
  129. 'http' => [
  130. 'throw' => true, // 状态码非 200、300 时是否抛出异常,默认为开启
  131. 'timeout' => 5.0,
  132. ]
  133. ];
  134. }
  135. /**
  136. * @notes 临时写入证书
  137. * @param $path
  138. * @param $cert
  139. * @author 段誉
  140. * @date 2023/2/27 15:48
  141. */
  142. public static function setCert($path, $cert)
  143. {
  144. $fopenPath = fopen($path, 'w');
  145. fwrite($fopenPath, $cert);
  146. fclose($fopenPath);
  147. }
  148. }