WechatProfit.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\utils\Wechat;
  8. use app\models\Option;
  9. use app\models\SaasUser;
  10. use app\models\Store;
  11. use app\models\WechatConfig;
  12. use EasyWeChat\Factory;
  13. use yii\helpers\Json;
  14. class WechatProfit
  15. {
  16. /**
  17. * 具体类型
  18. */
  19. const WECHAT_KIND_MINI = 'mini';
  20. const WECHAT_KIND_OFFICIAL = 'official';
  21. const WECHAT_KIND_PAY = 'pay';
  22. /**
  23. * @var array
  24. * 合法类型
  25. */
  26. protected static $validRequest = [
  27. self::WECHAT_KIND_MINI,
  28. self::WECHAT_KIND_OFFICIAL,
  29. self::WECHAT_KIND_PAY
  30. ];
  31. /**
  32. * @var \EasyWeChat\MiniProgram\Application
  33. */
  34. public static $wechat_mini;
  35. /**
  36. * @var \EasyWeChat\OfficialAccount\Application
  37. */
  38. public static $wechat_official;
  39. /**
  40. * @var \EasyWeChat\Payment\Application
  41. */
  42. public static $wechat_pay;
  43. /**
  44. * 初始化
  45. * @param string $type
  46. * @param integer $from
  47. * @throws \Exception
  48. */
  49. public static function init($store_id = null, $is_platform = 0, $navite = 0, $from = WechatConfig::TYPE_CONFIG_MINI) {
  50. if (!$store_id) {
  51. $store_id = get_store_id();
  52. }
  53. // 获取微信配置进行初始化
  54. // if ($navite == 1) {
  55. // $wechat_config = Option::getSaasWechat2();
  56. // } else {
  57. $wechat_config = Option::getSaasWechat();
  58. // }
  59. if (empty($wechat_config)) {
  60. throw new \Exception('Wechat App Is Null');
  61. }
  62. if ($navite == 1) {
  63. $store_id = 0;
  64. }
  65. $store_wechat = WechatConfig::findOne(['store_id' => $store_id, 'type' => $from]);
  66. $config = [
  67. 'app_id' => $wechat_config['sp_appid'],
  68. 'sub_appid' => $store_wechat->app_id,
  69. 'sub_mch_id' => $store_wechat->mch_id,
  70. 'response_type' => 'array'
  71. ];
  72. // if ($navite == 1) {
  73. // $config['mch_id'] = $wechat_config['sp_mch_id'];
  74. // $config['sub_mch_id'] = $wechat_config['mch_id'];
  75. // $config['sub_appid'] = $wechat_config['sub_appid'];
  76. // }
  77. if ($is_platform) {
  78. $config['sub_appid'] = Option::get('platform_appid', 0, 'saas')['value'];
  79. }
  80. $self_mini = null;
  81. if ($store_id > 0) {
  82. $self_mini = \app\models\Option::get('self_mini', $store_id, 'store', 0)['value'];
  83. }
  84. if (\Yii::$app->prod_is_dandianpu() && !$self_mini && intval($from) === WechatConfig::TYPE_CONFIG_MINI) {
  85. $config['sub_appid'] = Option::get('one_store_wechat_appid', 0, 'saas', '')['value'];
  86. }
  87. if (\Yii::$app->prod_is_dandianpu() && intval($from) === WechatConfig::TYPE_CONFIG_MP) {
  88. $config['sub_appid'] = Option::get('one_store_wechat_official_appid', 0, 'saas', '')['value'];
  89. }
  90. // //
  91. // if($store_id>0){
  92. // $storeInfo = Store::findOne($store_id);
  93. // if($storeInfo->business_model != 1){
  94. // $config = Option::getBusinessWechat();
  95. // $config['sub_mch_id'] = $store_wechat->mch_id;
  96. // }
  97. // }
  98. $storeInfo = Store::findOne($store_id);
  99. if($storeInfo->business_model != 1){
  100. unset($config['sub_appid']);
  101. if ($from == WechatConfig::TYPE_CONFIG_APP) {
  102. $platform_appid = Option::get('platform_app_appid', 0, 'saas')['value'];
  103. } else {
  104. $platform_appid = Option::get('platform_appid', 0, 'saas')['value'];
  105. }
  106. $config['app_id'] = $platform_appid;
  107. $config['mch_id'] = $wechat_config['sp_mch_id'];
  108. }
  109. // 证书
  110. if (!is_dir(\Yii::$app->runtimePath . '/pem')) {
  111. mkdir(\Yii::$app->runtimePath . '/pem');
  112. file_put_contents(\Yii::$app->runtimePath . '/pem/index.html', '');
  113. }
  114. $cert_pem_file = null;
  115. if ($wechat_config['sp_apiclient_cert']) {
  116. $cert_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_config['sp_apiclient_cert']);
  117. if (!file_exists($cert_pem_file)) {
  118. file_put_contents($cert_pem_file, $wechat_config['sp_apiclient_cert']);
  119. }
  120. }
  121. $key_pem_file = null;
  122. if ($wechat_config['sp_apiclient_key']) {
  123. $key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_config['sp_apiclient_key']);
  124. if (!file_exists($key_pem_file)) {
  125. file_put_contents($key_pem_file, $wechat_config['sp_apiclient_key']);
  126. }
  127. }
  128. $config['key'] = $wechat_config['sp_key'];
  129. $config['mch_id'] = $wechat_config['sp_mch_id'];
  130. $config['cert_path'] = $cert_pem_file;
  131. $config['key_path'] = $key_pem_file;
  132. if(\Yii::$app->isSaas() && intval($from) === WechatConfig::TYPE_CONFIG_MP){
  133. if (!\Yii::$app->prod_is_dandianpu()) {
  134. $config['sub_appid'] = $store_wechat->app_id;
  135. $config['sub_mch_id'] = $store_wechat->mch_id;
  136. }
  137. }
  138. if (\Yii::$app->prod_is_dandianpu() && intval($from) === WechatConfig::TYPE_CONFIG_H5){
  139. unset($config['sub_appid']);
  140. }
  141. \Yii::error($config);
  142. self::$wechat_pay = Factory::payment($config);
  143. }
  144. /**
  145. * 初始化
  146. * @param string $type
  147. * @param integer $from
  148. * @throws \Exception
  149. */
  150. public static function initSaas($type = self::WECHAT_KIND_MINI) {
  151. if (!in_array($type, self::$validRequest)) {
  152. throw new \Exception('Input object type not available');
  153. }
  154. $wechat_config = [
  155. 'platform_appid' => Option::get('platform_appid', 0, 'saas')['value'],
  156. 'platform_mch_id' => Option::get('platform_mch_id', 0, 'saas')['value'],
  157. 'platform_key' => Option::get('platform_key', 0, 'saas')['value'],
  158. 'platform_apiclient_cert' => Option::get('platform_apiclient_cert', 0, 'saas')['value'],
  159. 'platform_apiclient_key' => Option::get('platform_apiclient_key', 0, 'saas')['value'],
  160. ];
  161. $config = [
  162. 'app_id' => $wechat_config['platform_appid'],
  163. 'secret' => $wechat_config['platform_key'],
  164. 'response_type' => 'array'
  165. ];
  166. if ($type != self::WECHAT_KIND_PAY) {
  167. if ($type == self::WECHAT_KIND_MINI) {
  168. self::$wechat_mini = Factory::miniProgram($config);
  169. } else {
  170. self::$wechat_official = Factory::officialAccount($config);
  171. }
  172. } else {
  173. // 证书
  174. if (!is_dir(\Yii::$app->runtimePath . '/saas/wechat/pem')) {
  175. mkdir(\Yii::$app->runtimePath . '/saas/wechat/pem');
  176. file_put_contents(\Yii::$app->runtimePath . '/saas/wechat/pem/index.html', '');
  177. }
  178. $cert_pem_file = null;
  179. if (!empty($wechat_config['platform_apiclient_cert'])) {
  180. $cert_pem_file = \Yii::$app->runtimePath . '/saas/wechat/pem/' . md5($wechat_config['platform_apiclient_cert']);
  181. if (!file_exists($cert_pem_file)) {
  182. file_put_contents($cert_pem_file, $wechat_config['platform_apiclient_cert']);
  183. }
  184. }
  185. $key_pem_file = null;
  186. if (!empty($wechat_config['platform_apiclient_key'])) {
  187. $key_pem_file = \Yii::$app->runtimePath . '/saas/wechat/pem/' . md5($wechat_config['platform_apiclient_key']);
  188. if (!file_exists($key_pem_file)) {
  189. file_put_contents($key_pem_file, $wechat_config['platform_apiclient_key']);
  190. }
  191. }
  192. $config['key'] = $wechat_config['platform_key'];
  193. $config['mch_id'] = $wechat_config['platform_mch_id'];
  194. $config['cert_path'] = $cert_pem_file;
  195. $config['key_path'] = $key_pem_file;
  196. \Yii::error($config);
  197. self::$wechat_pay = Factory::payment($config);
  198. }
  199. }
  200. /**
  201. * Undocumented function
  202. *
  203. * @Author LGL 24963@qq.com
  204. * @DateTime 2021-02-03
  205. * @desc: 实例化支付类
  206. * @return void
  207. */
  208. public static function initBusiness ($_from = WechatConfig::TYPE_CONFIG_MINI)
  209. {
  210. $wechat_config = new WechatConfig();
  211. $keys = [
  212. 'sp_name',
  213. 'sp_appid',
  214. 'sp_mch_id',
  215. 'sp_key',
  216. 'sp_apiclient_cert',
  217. 'sp_apiclient_key',
  218. 'platform_api_key',
  219. 'platform_serial_no',
  220. 'platform_appid',
  221. 'platform_app_appid'
  222. ];
  223. $data = Option::get($keys, 0, 'saas');
  224. $data = array_column($data, 'value','name');
  225. $wechat_config->app_id = $data['platform_appid'];
  226. $wechat_config->app_secret = $data['platform_serial_no'];
  227. $wechat_config->mch_id = $data['sp_mch_id'];
  228. $wechat_config->pay_key = $data['sp_key'];
  229. $wechat_config->cert_pem = $data['sp_apiclient_cert'];
  230. $wechat_config->key_pem = $data['sp_apiclient_key'];
  231. $config = [
  232. 'app_id' => $data['sp_appid'],
  233. //'secret' => $wechat_config->app_secret,
  234. 'key' => $wechat_config->pay_key,
  235. 'mch_id' => $wechat_config->mch_id,
  236. 'response_type' => 'array'
  237. ];
  238. if ($_from === WechatConfig::TYPE_CONFIG_APP) {
  239. $config['app_id'] = $data['platform_app_appid'];
  240. $config['app_secret'] = 'ec4cdee90d78680516eeef25f86b23a1';
  241. }
  242. // 证书
  243. if (!is_dir(\Yii::$app->runtimePath . '/pem')) {
  244. mkdir(\Yii::$app->runtimePath . '/pem');
  245. file_put_contents(\Yii::$app->runtimePath . '/pem/index.html', '');
  246. }
  247. $cert_pem_file = null;
  248. if ($wechat_config->cert_pem) {
  249. $cert_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_config->cert_pem);
  250. if (!file_exists($cert_pem_file)) {
  251. file_put_contents($cert_pem_file, $wechat_config->cert_pem);
  252. }
  253. }
  254. $key_pem_file = null;
  255. if ($wechat_config->key_pem) {
  256. $key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_config->key_pem);
  257. if (!file_exists($key_pem_file)) {
  258. file_put_contents($key_pem_file, $wechat_config->key_pem);
  259. }
  260. }
  261. $config['cert_path'] = $cert_pem_file;
  262. $config['key_path'] = $key_pem_file;
  263. $config['sub_appid'] = $wechat_config->app_id;
  264. self::$wechat_pay = Factory::payment($config);
  265. }
  266. }