app_id = $data['platform_appid']; self::$wechat_config->app_secret = $data['platform_key']; self::$wechat_config->mch_id = $data['platform_mch_id']; self::$wechat_config->pay_key = $data['platform_pay_key']; self::$wechat_config->cert_pem = $data['platform_apiclient_cert']; self::$wechat_config->key_pem = $data['platform_apiclient_key']; if (get_params('mini_type') === 'merchant' || \Yii::$app->prod_is_dandianpu()) { self::$wechat_config->app_id = $data['platform_mch_appid']; self::$wechat_config->app_secret = $data['platform_mch_key']; } }else{ $self_mini = Option::get('self_mini', $store_id, 'store', 0)['value']; if (\Yii::$app->prod_is_dandianpu() && !Store::hasIncoming($store_id) && !intval($self_mini)) { if ((int)$from === 3) { //app逻辑 $appid = Option::get('one_store_app_wechat_appid', 0, 'saas', ''); $secret = Option::get('one_store_app_wechat_secret', 0, 'saas', ''); $mch_id = Option::get('one_store_app_mch_id', 0, 'saas', ''); $pay_key = Option::get('one_store_app_pay_key', 0, 'saas', ''); $apiclient_cert = Option::get('one_store_app_apiclient_cert', 0, 'saas', ''); $apiclient_key = Option::get('one_store_app_apiclient_key', 0, 'saas', ''); } else { $appid = Option::get('one_store_wechat_appid', 0, 'saas', ''); $secret = Option::get('one_store_wechat_secret', 0, 'saas', ''); $mch_id = Option::get('one_store_mch_id', 0, 'saas', ''); $pay_key = Option::get('one_store_pay_key', 0, 'saas', ''); $apiclient_cert = Option::get('one_store_apiclient_cert', 0, 'saas', ''); $apiclient_key = Option::get('one_store_apiclient_key', 0, 'saas', ''); } self::$wechat_config = (object)[ 'app_id' => $appid['value'], 'app_secret' => $secret['value'], 'mch_id' => $mch_id['value'], 'pay_key' => $pay_key['value'], 'cert_pem' => $apiclient_cert['value'], 'key_pem' => $apiclient_key['value'], ]; } elseif (\Yii::$app->isSaas() && !Store::hasIncoming($store_id) && intval($from) !== WechatConfig::TYPE_CONFIG_H5) { $appid = Option::get('platform_appid', 0, 'saas', ''); $secret = Option::get('platform_key', 0, 'saas', ''); $mch_id = Option::get('platform_mch_id', 0, 'saas', ''); $pay_key = Option::get('platform_pay_key', 0, 'saas', ''); $apiclient_cert = Option::get('platform_apiclient_cert', 0, 'saas', ''); $apiclient_key = Option::get('platform_apiclient_key', 0, 'saas', ''); self::$wechat_config = (object)[ 'app_id' => $appid['value'], 'app_secret' => $secret['value'], 'mch_id' => $mch_id['value'], 'pay_key' => $pay_key['value'], 'cert_pem' => $apiclient_cert['value'], 'key_pem' => $apiclient_key['value'], ]; } else { //店铺微信配置 self::$wechat_config = WechatConfig::findOne(['store_id' => $store_id, 'type' => $from]); } if (\Yii::$app->prod_is_dandianpu() && intval($from) === WechatConfig::TYPE_CONFIG_H5) { if(Store::hasIncoming($store_id)){ self::$wechat_config = WechatConfig::findOne(['store_id' => $store_id, 'type' => $from]); }else{ $wechat_config = Option::getSaasWechat(); $sub_mch_id = Option::get('one_store_wechat_h5_sub_mch_id', 0, 'saas', '')['value']; self::$wechat_config = (object)[ 'app_id' => $wechat_config['sp_appid'], 'app_secret' => $wechat_config['sp_key'], 'key' => $wechat_config['sp_key'], 'mch_id' => $wechat_config['sp_mch_id'], 'sub_mch_id' => $sub_mch_id, 'pay_key' => $wechat_config['sp_key'], 'cert_pem' => $wechat_config['sp_apiclient_cert'], 'key_pem' => $wechat_config['sp_apiclient_key'], ]; } } if (\Yii::$app->prod_is_dandianpu() && intval($from) === WechatConfig::TYPE_CONFIG_MP) { $one_store_wechat_official_appid = Option::get('one_store_wechat_official_appid', 0, 'saas', '')['value']; $one_store_wechat_official_secret = Option::get('one_store_wechat_official_secret', 0, 'saas', '')['value']; $one_store_official_pay_key = Option::get('one_store_official_pay_key', 0, 'saas', '')['value']; $one_store_official_apiclient_cert = Option::get('one_store_official_apiclient_cert', 0, 'saas', '')['value']; $one_store_official_apiclient_key = Option::get('one_store_official_apiclient_key', 0, 'saas', '')['value']; $one_store_wechat_official_sub_mch_id = Option::get('one_store_wechat_official_sub_mch_id', 0, 'saas', '')['value']; self::$wechat_config = (object)[ 'app_id' => $one_store_wechat_official_appid, 'app_secret' => $one_store_wechat_official_secret, 'mch_id' => $one_store_wechat_official_sub_mch_id, 'pay_key' => $one_store_official_pay_key, 'cert_pem' => $one_store_official_apiclient_cert, 'key_pem' => $one_store_official_apiclient_key, ]; } } if (!self::$wechat_config) { throw new \Exception('Wechat App Is Null'); } $config = [ 'app_id' => self::$wechat_config->app_id, 'secret' => self::$wechat_config->app_secret, 'response_type' => 'array' ]; if ($type != self::WECHAT_KIND_PAY) { if ($type == self::WECHAT_KIND_MINI) { self::$wechat_mini = Factory::miniProgram($config); } else { self::$wechat_official = Factory::officialAccount($config); } } else { // 证书 if (!is_dir(\Yii::$app->runtimePath . '/pem')) { mkdir(\Yii::$app->runtimePath . '/pem'); file_put_contents(\Yii::$app->runtimePath . '/pem/index.html', ''); } $cert_pem_file = null; if (self::$wechat_config->cert_pem) { $cert_pem_file = \Yii::$app->runtimePath . '/pem/' . md5(self::$wechat_config->cert_pem); if (!file_exists($cert_pem_file)) { file_put_contents($cert_pem_file, self::$wechat_config->cert_pem); } } $key_pem_file = null; if (self::$wechat_config->key_pem) { $key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5(self::$wechat_config->key_pem); if (!file_exists($key_pem_file)) { file_put_contents($key_pem_file, self::$wechat_config->key_pem); } } $config['key'] = self::$wechat_config->pay_key; $config['mch_id'] = self::$wechat_config->mch_id; if(isset(self::$wechat_config->sub_mch_id)) { self::$wechat_config->sub_mch_id && $config['sub_mch_id'] = self::$wechat_config->sub_mch_id; } $config['cert_path'] = $cert_pem_file; $config['key_path'] = $key_pem_file; self::$wechat_pay = Factory::payment($config); if(\app\modules\admin\models\WastoreForm::$test && get_mini_id() == 88){ $config = [ 'app_id' => 'wx1ce41243db1e4373', 'secret' => '6563aceb2ef97e0d2f5b884f11e43544', 'response_type' => 'array' ]; // $cert_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_config['sp_apiclient_cert']); // if (!file_exists($cert_pem_file)) { // file_put_contents($cert_pem_file, $wechat_config['sp_apiclient_cert']); // } $config['key'] = 'b1f76e762183a45e10e74ba0e16b4234'; $config['mch_id'] = '1480763552'; $config['cert_path'] = $cert_pem_file; $config['key_path'] = $key_pem_file; self::$wechat_pay = \EasyWeChat\Factory::payment($config); } \Yii::error($config); } } /** * jsapi ticket */ public static function getTicketConfig (array $api_array = [], $url = null) { try { self::init(get_store_id(), self::WECHAT_KIND_OFFICIAL, WechatConfig::TYPE_CONFIG_MP); } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } $app = WechatMini::getWechatConfig(get_store_id(), 0, WechatMini::TYPE_OFFICIAL); if (!$app) { return [ 'code' => 1, 'msg' => '没有对应的配置项' ]; } $jsapi_params = $app->jssdk->getConfigArray($api_array, false, false, [], $url); \Yii::error($jsapi_params); return $jsapi_params; } }