WeChatServer.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop开源商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | gitee下载:https://gitee.com/likeshop_gitee
  7. // | github下载:https://github.com/likeshop-github
  8. // | 访问官网:https://www.likeshop.cn
  9. // | 访问社区:https://home.likeshop.cn
  10. // | 访问手册:http://doc.likeshop.cn
  11. // | 微信公众号:likeshop技术社区
  12. // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
  13. // | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
  14. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  15. // | likeshop团队版权所有并拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshop.cn.team
  18. // +----------------------------------------------------------------------
  19. namespace app\common\server;
  20. use app\common\model\Client_;
  21. use app\common\model\Pay;
  22. use think\Exception;
  23. /**
  24. * 微信服务 服务类
  25. * Class WeChatServer
  26. * @package app\common\server
  27. */
  28. class WeChatServer
  29. {
  30. /**
  31. * @notes 获取小程序配置
  32. * @return array
  33. * @author suny
  34. * @date 2021/7/13 6:35 下午
  35. */
  36. public static function getMnpConfig()
  37. {
  38. $epay = get_addon_config('epay');
  39. $config = [
  40. 'app_id' => $epay['wechat']['miniapp_id'],
  41. 'secret' => config('wxMiniProgram.secret'),
  42. 'mch_id' => $epay['wechat']['mch_id'],
  43. 'key' => $epay['wechat']['key'],
  44. 'response_type' => 'array',
  45. 'log' => [
  46. 'level' => 'debug',
  47. 'file' => 'wechat.log'
  48. ],
  49. ];
  50. return $config;
  51. }
  52. /**
  53. * @notes 获取微信公众号配置
  54. * @return array
  55. * @author suny
  56. * @date 2021/7/13 6:35 下午
  57. */
  58. public static function getOaConfig()
  59. {
  60. $config = [
  61. 'app_id' => ConfigServer::get('oa', 'app_id'),
  62. 'secret' => ConfigServer::get('oa', 'secret'),
  63. 'mch_id' => ConfigServer::get('oa', 'mch_id'),
  64. 'key' => ConfigServer::get('oa', 'key'),
  65. 'token' => ConfigServer::get('oa', 'token', ''),
  66. 'response_type' => 'array',
  67. 'log' => [
  68. 'level' => 'debug',
  69. 'file' => app()->getRootPath() . 'runtime/wechat/' . date('Ym') . '/' . date('d') . '.log'
  70. ],
  71. ];
  72. return $config;
  73. }
  74. //微信开放平台->web应用
  75. public static function getOpWebConfig()
  76. {
  77. $config = [
  78. 'app_id' => ConfigServer::get('op', 'web_app_id'),
  79. 'secret' => ConfigServer::get('op', 'web_secret'),
  80. ];
  81. return $config;
  82. }
  83. /**
  84. * @notes 获取url
  85. * @param $str
  86. * @return string
  87. * @author suny
  88. * @date 2021/7/13 6:35 下午
  89. */
  90. public static function getUrl($str)
  91. {
  92. return (string)url($str, [], false, true);
  93. }
  94. /**
  95. * @notes 根据不同来源获取支付配置
  96. * @param $order_source
  97. * @return array
  98. * @throws Exception
  99. * @author suny
  100. * @date 2021/7/13 6:36 下午
  101. */
  102. public static function getPayConfigBySource($order_source)
  103. {
  104. $notify_url = '';
  105. switch ($order_source) {
  106. case Client_::mnp:
  107. $notify_url = self::getUrl('pay/notifyMnp');
  108. break;
  109. case Client_::oa:
  110. case Client_::h5:
  111. case Client_::pc:
  112. $notify_url = self::getUrl('pay/notifyOa');
  113. break;
  114. case Client_::android:
  115. case Client_::ios:
  116. $notify_url = self::getUrl('pay/notifyApp');
  117. break;
  118. }
  119. $config = self::getPayConfig($order_source);
  120. if (empty($config) ||
  121. empty($config['key']) ||
  122. empty($config['mch_id']) ||
  123. empty($config['app_id']) ||
  124. empty($config['secret'])
  125. ) {
  126. throw new Exception('请在后台配置好微信支付!');
  127. }
  128. return [
  129. 'config' => $config,
  130. 'notify_url' => $notify_url,
  131. ];
  132. }
  133. //===================================支付配置=======================================================
  134. /**
  135. * @notes 微信支付设置 H5支付 appid 可以是公众号appid
  136. * @param $client
  137. * @return array
  138. * @throws \think\db\exception\DataNotFoundException
  139. * @throws \think\db\exception\DbException
  140. * @throws \think\db\exception\ModelNotFoundException
  141. * @author suny
  142. * @date 2021/7/13 6:36 下午
  143. */
  144. public static function getPayConfig($client)
  145. {
  146. switch ($client) {
  147. case Client_::mnp:
  148. $appid = ConfigServer::get('mnp', 'app_id');
  149. $secret = ConfigServer::get('mnp', 'secret');
  150. break;
  151. case Client_::oa:
  152. case Client_::h5:
  153. case Client_::pc:
  154. $appid = ConfigServer::get('oa', 'app_id');
  155. $secret = ConfigServer::get('oa', 'secret');
  156. break;
  157. case Client_::android:
  158. case Client_::ios:
  159. $appid = ConfigServer::get('op', 'app_id');
  160. $secret = ConfigServer::get('op', 'secret');
  161. break;
  162. default:
  163. $appid = '';
  164. $secret = '';
  165. }
  166. $pay = Pay::where(['code' => 'wechat'])->find()->toArray();
  167. $config = [
  168. 'app_id' => $appid,
  169. 'secret' => $secret,
  170. 'mch_id' => $pay['config']['mch_id'] ?? '',
  171. 'key' => $pay['config']['pay_sign_key'] ?? '',
  172. 'cert_path' => $pay['config']['apiclient_cert'] ?? '',
  173. 'key_path' => $pay['config']['apiclient_key'] ?? '',
  174. 'response_type' => 'array',
  175. 'log' => [
  176. 'level' => 'debug',
  177. 'file' => app()->getRootPath() . 'runtime/wechat/' . date('Ym') . '/' . date('d') . '.log'
  178. ],
  179. ];
  180. if (is_cli()) {
  181. if (!defined('ROOT_PATH')) {
  182. define('ROOT_PATH', __DIR__);
  183. }
  184. $config['cert_path'] = ROOT_PATH . '/public/' . $pay['config']['apiclient_cert'];
  185. $config['key_path'] = ROOT_PATH . '/public/' . $pay['config']['apiclient_key'];
  186. }
  187. return $config;
  188. }
  189. }