Wechat.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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\WechatConfig;
  9. use app\models\Option;
  10. use EasyWeChat\Factory;
  11. use yii\base\Exception;
  12. use app\models\Store;
  13. class Wechat
  14. {
  15. /**
  16. * 具体类型
  17. */
  18. const WECHAT_KIND_MINI = 'mini';
  19. const WECHAT_KIND_OFFICIAL = 'official';
  20. const WECHAT_KIND_PAY = 'pay';
  21. /**
  22. * @var array
  23. * 合法类型
  24. */
  25. protected static $validRequest = [
  26. self::WECHAT_KIND_MINI,
  27. self::WECHAT_KIND_OFFICIAL,
  28. self::WECHAT_KIND_PAY
  29. ];
  30. /**
  31. * @var wechat对象配置
  32. */
  33. public static $wechat_config;
  34. /**
  35. * @var \EasyWeChat\MiniProgram\Application
  36. */
  37. public static $wechat_mini;
  38. /**
  39. * @var \EasyWeChat\OfficialAccount\Application
  40. */
  41. public static $wechat_official;
  42. /**
  43. * @var \EasyWeChat\Payment\Application
  44. */
  45. public static $wechat_pay;
  46. /**
  47. * 初始化
  48. * @param string $type
  49. * @param integer $from
  50. * @throws \Exception
  51. */
  52. public static function init($store_id, $type = self::WECHAT_KIND_MINI, $from = WechatConfig::TYPE_CONFIG_MINI) {
  53. if (!in_array($type, self::$validRequest)) {
  54. throw new \Exception('Input object type not available');
  55. }
  56. // 获取微信配置进行初始化
  57. if($store_id == -1){ //商盟微信配置
  58. self::$wechat_config = new WechatConfig();
  59. $keys = [
  60. 'platform_appid',
  61. 'platform_mch_id',
  62. 'platform_key',
  63. 'platform_pay_key',
  64. 'platform_apiclient_cert',
  65. 'platform_apiclient_key',
  66. 'platform_api_key',
  67. 'platform_mch_appid',
  68. 'platform_mch_key'
  69. ];
  70. $data = Option::get($keys, 0, 'saas');
  71. $data = array_column($data, 'value','name');
  72. self::$wechat_config->app_id = $data['platform_appid'];
  73. self::$wechat_config->app_secret = $data['platform_key'];
  74. self::$wechat_config->mch_id = $data['platform_mch_id'];
  75. self::$wechat_config->pay_key = $data['platform_pay_key'];
  76. self::$wechat_config->cert_pem = $data['platform_apiclient_cert'];
  77. self::$wechat_config->key_pem = $data['platform_apiclient_key'];
  78. if (get_params('mini_type') === 'merchant' || \Yii::$app->prod_is_dandianpu()) {
  79. self::$wechat_config->app_id = $data['platform_mch_appid'];
  80. self::$wechat_config->app_secret = $data['platform_mch_key'];
  81. }
  82. }else{
  83. $self_mini = Option::get('self_mini', $store_id, 'store', 0)['value'];
  84. if (\Yii::$app->prod_is_dandianpu() && !Store::hasIncoming($store_id) && !intval($self_mini)) {
  85. if ((int)$from === 3) { //app逻辑
  86. $appid = Option::get('one_store_app_wechat_appid', 0, 'saas', '');
  87. $secret = Option::get('one_store_app_wechat_secret', 0, 'saas', '');
  88. $mch_id = Option::get('one_store_app_mch_id', 0, 'saas', '');
  89. $pay_key = Option::get('one_store_app_pay_key', 0, 'saas', '');
  90. $apiclient_cert = Option::get('one_store_app_apiclient_cert', 0, 'saas', '');
  91. $apiclient_key = Option::get('one_store_app_apiclient_key', 0, 'saas', '');
  92. } else {
  93. $appid = Option::get('one_store_wechat_appid', 0, 'saas', '');
  94. $secret = Option::get('one_store_wechat_secret', 0, 'saas', '');
  95. $mch_id = Option::get('one_store_mch_id', 0, 'saas', '');
  96. $pay_key = Option::get('one_store_pay_key', 0, 'saas', '');
  97. $apiclient_cert = Option::get('one_store_apiclient_cert', 0, 'saas', '');
  98. $apiclient_key = Option::get('one_store_apiclient_key', 0, 'saas', '');
  99. }
  100. self::$wechat_config = (object)[
  101. 'app_id' => $appid['value'],
  102. 'app_secret' => $secret['value'],
  103. 'mch_id' => $mch_id['value'],
  104. 'pay_key' => $pay_key['value'],
  105. 'cert_pem' => $apiclient_cert['value'],
  106. 'key_pem' => $apiclient_key['value'],
  107. ];
  108. } elseif (\Yii::$app->isSaas() && !Store::hasIncoming($store_id) && intval($from) !== WechatConfig::TYPE_CONFIG_H5) {
  109. $appid = Option::get('platform_appid', 0, 'saas', '');
  110. $secret = Option::get('platform_key', 0, 'saas', '');
  111. $mch_id = Option::get('platform_mch_id', 0, 'saas', '');
  112. $pay_key = Option::get('platform_pay_key', 0, 'saas', '');
  113. $apiclient_cert = Option::get('platform_apiclient_cert', 0, 'saas', '');
  114. $apiclient_key = Option::get('platform_apiclient_key', 0, 'saas', '');
  115. self::$wechat_config = (object)[
  116. 'app_id' => $appid['value'],
  117. 'app_secret' => $secret['value'],
  118. 'mch_id' => $mch_id['value'],
  119. 'pay_key' => $pay_key['value'],
  120. 'cert_pem' => $apiclient_cert['value'],
  121. 'key_pem' => $apiclient_key['value'],
  122. ];
  123. } else {
  124. //店铺微信配置
  125. self::$wechat_config = WechatConfig::findOne(['store_id' => $store_id, 'type' => $from]);
  126. }
  127. if (\Yii::$app->prod_is_dandianpu() && intval($from) === WechatConfig::TYPE_CONFIG_H5) {
  128. if(Store::hasIncoming($store_id)){
  129. self::$wechat_config = WechatConfig::findOne(['store_id' => $store_id, 'type' => $from]);
  130. }else{
  131. $wechat_config = Option::getSaasWechat();
  132. $sub_mch_id = Option::get('one_store_wechat_h5_sub_mch_id', 0, 'saas', '')['value'];
  133. self::$wechat_config = (object)[
  134. 'app_id' => $wechat_config['sp_appid'],
  135. 'app_secret' => $wechat_config['sp_key'],
  136. 'key' => $wechat_config['sp_key'],
  137. 'mch_id' => $wechat_config['sp_mch_id'],
  138. 'sub_mch_id' => $sub_mch_id,
  139. 'pay_key' => $wechat_config['sp_key'],
  140. 'cert_pem' => $wechat_config['sp_apiclient_cert'],
  141. 'key_pem' => $wechat_config['sp_apiclient_key'],
  142. ];
  143. }
  144. }
  145. if (\Yii::$app->prod_is_dandianpu() && intval($from) === WechatConfig::TYPE_CONFIG_MP) {
  146. $one_store_wechat_official_appid = Option::get('one_store_wechat_official_appid', 0, 'saas', '')['value'];
  147. $one_store_wechat_official_secret = Option::get('one_store_wechat_official_secret', 0, 'saas', '')['value'];
  148. $one_store_official_pay_key = Option::get('one_store_official_pay_key', 0, 'saas', '')['value'];
  149. $one_store_official_apiclient_cert = Option::get('one_store_official_apiclient_cert', 0, 'saas', '')['value'];
  150. $one_store_official_apiclient_key = Option::get('one_store_official_apiclient_key', 0, 'saas', '')['value'];
  151. $one_store_wechat_official_sub_mch_id = Option::get('one_store_wechat_official_sub_mch_id', 0, 'saas', '')['value'];
  152. self::$wechat_config = (object)[
  153. 'app_id' => $one_store_wechat_official_appid,
  154. 'app_secret' => $one_store_wechat_official_secret,
  155. 'mch_id' => $one_store_wechat_official_sub_mch_id,
  156. 'pay_key' => $one_store_official_pay_key,
  157. 'cert_pem' => $one_store_official_apiclient_cert,
  158. 'key_pem' => $one_store_official_apiclient_key,
  159. ];
  160. }
  161. }
  162. if (!self::$wechat_config) {
  163. throw new \Exception('Wechat App Is Null');
  164. }
  165. $config = [
  166. 'app_id' => self::$wechat_config->app_id,
  167. 'secret' => self::$wechat_config->app_secret,
  168. 'response_type' => 'array'
  169. ];
  170. if ($type != self::WECHAT_KIND_PAY) {
  171. if ($type == self::WECHAT_KIND_MINI) {
  172. self::$wechat_mini = Factory::miniProgram($config);
  173. } else {
  174. self::$wechat_official = Factory::officialAccount($config);
  175. }
  176. } else {
  177. // 证书
  178. if (!is_dir(\Yii::$app->runtimePath . '/pem')) {
  179. mkdir(\Yii::$app->runtimePath . '/pem');
  180. file_put_contents(\Yii::$app->runtimePath . '/pem/index.html', '');
  181. }
  182. $cert_pem_file = null;
  183. if (self::$wechat_config->cert_pem) {
  184. $cert_pem_file = \Yii::$app->runtimePath . '/pem/' . md5(self::$wechat_config->cert_pem);
  185. if (!file_exists($cert_pem_file)) {
  186. file_put_contents($cert_pem_file, self::$wechat_config->cert_pem);
  187. }
  188. }
  189. $key_pem_file = null;
  190. if (self::$wechat_config->key_pem) {
  191. $key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5(self::$wechat_config->key_pem);
  192. if (!file_exists($key_pem_file)) {
  193. file_put_contents($key_pem_file, self::$wechat_config->key_pem);
  194. }
  195. }
  196. $config['key'] = self::$wechat_config->pay_key;
  197. $config['mch_id'] = self::$wechat_config->mch_id;
  198. if(isset(self::$wechat_config->sub_mch_id)) {
  199. self::$wechat_config->sub_mch_id && $config['sub_mch_id'] = self::$wechat_config->sub_mch_id;
  200. }
  201. $config['cert_path'] = $cert_pem_file;
  202. $config['key_path'] = $key_pem_file;
  203. self::$wechat_pay = Factory::payment($config);
  204. if(\app\modules\admin\models\WastoreForm::$test && get_mini_id() == 88){
  205. $config = [
  206. 'app_id' => 'wx1ce41243db1e4373',
  207. 'secret' => '6563aceb2ef97e0d2f5b884f11e43544',
  208. 'response_type' => 'array'
  209. ];
  210. // $cert_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_config['sp_apiclient_cert']);
  211. // if (!file_exists($cert_pem_file)) {
  212. // file_put_contents($cert_pem_file, $wechat_config['sp_apiclient_cert']);
  213. // }
  214. $config['key'] = 'b1f76e762183a45e10e74ba0e16b4234';
  215. $config['mch_id'] = '1480763552';
  216. $config['cert_path'] = $cert_pem_file;
  217. $config['key_path'] = $key_pem_file;
  218. self::$wechat_pay = \EasyWeChat\Factory::payment($config);
  219. }
  220. \Yii::error($config);
  221. }
  222. }
  223. /**
  224. * jsapi ticket
  225. */
  226. public static function getTicketConfig (array $api_array = [], $url = null) {
  227. try {
  228. self::init(get_store_id(), self::WECHAT_KIND_OFFICIAL, WechatConfig::TYPE_CONFIG_MP);
  229. } catch (\Exception $e) {
  230. return [
  231. 'code' => 1,
  232. 'msg' => $e->getMessage()
  233. ];
  234. }
  235. $app = WechatMini::getWechatConfig(get_store_id(), 0, WechatMini::TYPE_OFFICIAL);
  236. if (!$app) {
  237. return [
  238. 'code' => 1,
  239. 'msg' => '没有对应的配置项'
  240. ];
  241. }
  242. $jsapi_params = $app->jssdk->getConfigArray($api_array, false, false, [], $url);
  243. \Yii::error($jsapi_params);
  244. return $jsapi_params;
  245. }
  246. }