'0'], ]; } /** * 存储配置 */ public function saveConfig() { if (!$this->validate()) { return [ 'code' => 1, 'msg' => '参数错误' ]; } // if (\Yii::$app->isSaas()) { // $this->model->app_id = $this->app_id; // $this->model->mch_id = $this->mch_id; // $this->model->store_id = $this->store_id; // $this->model->name = $this->name; // $this->model->gh_wechat_app_id = $this->gh_wechat_app_id; // if ($this->model->save()) { // return [ // 'code' => 0, // 'msg' => '保存成功', // ]; // } // return [ // 'code' => 1, // 'msg' => json_encode($this->model->errors), // ]; // } if (!is_dir(\Yii::$app->runtimePath . '/pem')) { mkdir(\Yii::$app->runtimePath . '/pem'); file_put_contents(\Yii::$app->runtimePath . '/pem/index.html', ''); } if (!empty($this->cert_pem_url)) { $this->cert_pem = file_get_contents(str_replace('https://' . \Yii::$app->request->hostName, \Yii::$app->basePath, $this->cert_pem_url)); } else { if (!empty($this->cert_pem)) { $this->cert_pem_url = $this->handlePemFile($this->cert_pem); } } if (!empty($this->key_pem_url)) { $this->key_pem = file_get_contents(str_replace('https://' . \Yii::$app->request->hostName, \Yii::$app->basePath, $this->key_pem_url)); } else { if (!empty($this->key_pem)) { $this->key_pem_url = $this->handlePemFile($this->key_pem); } } $config = [ 'app_id' => $this->app_id, 'secret' => $this->app_secret, 'key' => $this->pay_key, 'mch_id' => $this->mch_id, 'cert_path' => self::handlePemFile($this->cert_pem, 1), 'key_path' => self::handlePemFile($this->key_pem, 1) ]; // 验证appid appsecret 是否合法 if ($this->app_secret) { try { if ($this->type == WechatConfig::TYPE_CONFIG_MINI) { $this->app = Factory::miniProgram($config); } if ($this->type == WechatConfig::TYPE_CONFIG_MP) { $this->app = Factory::officialAccount($config); } if ($this->type == WechatConfig::TYPE_CONFIG_APP) { \Yii::error($config); $this->app = Factory::miniProgram($config); } $this->app->access_token->getToken(); } catch (HttpException $e) { if ($e->formattedResponse['errcode'] == 40013) { return [ 'code' => 1, 'msg' => 'AppId错误' ]; } if ($e->formattedResponse['errcode'] == 40125) { return [ 'code' => 1, 'msg' => 'AppSecret错误' ]; } } } //$app = Factory::payment($config); if (!empty($this->cert_pem) || !empty($this->cert_pem)) { if ($this->type == 2 && empty($this->mch_id)) { return [ 'code' => 1, 'msg' => '请填写商户号' ]; } if ($this->type == 2 && empty($this->pay_key)) { return [ 'code' => 1, 'msg' => '请填写微信支付Api支付密钥' ]; } // 验证证书 try { $app = Factory::payment($config); $app->security->getPublicKey(); } catch (\Exception $e) { return [ 'code' => 1, 'msg' => '证书验证失败' ]; } catch (RequestException $e) { if ($e->getHandlerContext()['errno'] == 58) { return [ 'code' => 1, 'msg' => '微信支付证书错误' ]; } } } if (($this->mch_id && $this->pay_key)) { if (empty($this->cert_pem) || empty($this->key_pem)) { return [ 'code' => 1, 'msg' => '请填写微信支付证书' ]; } $order_no = date('YmdHis') . mt_rand(1000, 9999); $res = $app->order->queryByOutTradeNumber($order_no); if ($res['return_code'] == "FAIL") { return [ 'code' => 1, 'msg' => '微信支付商户号或微信支付Api密钥错误--('.$res['return_msg'].')' ]; } } $ext_arr = []; if ($this->model->isNewRecord) { $this->model->store_id = $this->store_id; $this->model->type = $this->type; if ($this->type == WechatConfig::TYPE_CONFIG_APP && !empty($this->app_name)) { $ext_arr = [ 'app_name' => '' ]; if (!empty($this->app_name)) { $ext_arr['app_name'] = $this->app_name; } $this->model->ext = json_encode($ext_arr); } if ($this->type == WechatConfig::TYPE_CONFIG_MP) { $ext_arr = [ 'token' => '', 'auto_reply' => '' ]; if (!empty($this->token)) { $ext_arr['token'] = $this->token; } if (!empty($this->auto_reply)) { $ext_arr['auto_reply'] = $this->auto_reply; } $this->model->ext = json_encode($ext_arr); } $this->model->created_at = $this->model->updated_at = time(); } else { $ext_arr = json_decode($this->model->ext, true); if ($this->type == WechatConfig::TYPE_CONFIG_APP && !empty($this->app_name)) { if (!empty($this->app_name)) { $ext_arr['app_name'] = $this->app_name; } $this->model->ext = json_encode($ext_arr); } if ($this->type == WechatConfig::TYPE_CONFIG_MP) { if (!empty($this->token)) { $ext_arr['token'] = $this->token; } if (!empty($this->auto_reply)) { $ext_arr['auto_reply'] = $this->auto_reply; } $this->model->ext = json_encode($ext_arr); } $this->model->updated_at = time(); } empty($this->token) || $ext_arr['token'] = $this->token; empty($this->aes_key) || $ext_arr['aes_key'] = $this->aes_key; $this->model->ext = json_encode($ext_arr); $this->model->app_id = $this->app_id; $this->model->app_secret = $this->app_secret; $this->model->mch_id = $this->mch_id; $this->model->pay_key = $this->pay_key; $this->model->name = $this->name; $this->model->gh_wechat_app_id = $this->gh_wechat_app_id; if ($this->cert_pem) { $this->model->cert_pem = $this->cert_pem; } if ($this->key_pem) { $this->model->key_pem = $this->key_pem; } if ($this->cert_pem_url) { $this->model->cert_pem_url = $this->cert_pem_url; } if ($this->cert_pem_url) { $this->model->cert_pem_url = $this->cert_pem_url; } if ($this->model->save()) { return [ 'code' => 0, 'msg' => '保存成功', ]; } return [ 'code' => 1, 'msg' => implode(';', array_values($this->model->firstErrors)) ]; } /** * 获取配置 */ public function getConfig() { $returnArray = [ 'mini' => [], 'mp' => [], 'app' => [], 'self_mini' =>0, 'is_saas' => 0, 'store_id' => $this->store_id, ]; $wechatConfig = WechatConfig::find()->where(['store_id' => $this->store_id, 'is_delete' => 0]); if ($wechatConfig) { //是商盟和saas if(\Yii::$app->isSaas()){ // $wechatConfig->select(['app_id','app_secret','name','mch_id','ext','type', 'gh_wechat_app_id']); $returnArray['is_saas'] = 1; } $self_mini = \app\models\Option::get('self_mini', $this->store_id, 'store', 0)['value']; if($self_mini){ $returnArray['self_mini'] = 1; } foreach ($wechatConfig->asArray()->all() as $value) { if (empty($value['key_pem_url']) && !empty($value['key_pem'])) { $value['key_pem_url'] = self::handlePemFile($value['key_pem']); } if (empty($value['cert_pem_url']) && !empty($value['cert_pem'])) { $value['cert_pem_url'] = self::handlePemFile($value['cert_pem']); } $ext = json_decode($value['ext'], true); switch ($value['type']) { case WechatConfig::TYPE_CONFIG_MINI: $value['token'] = !empty($ext['token']) ? $ext['token'] : ''; $value['aes_key'] = !empty($ext['aes_key']) ? $ext['aes_key'] : ''; $returnArray['mini'] = $value; break; case WechatConfig::TYPE_CONFIG_MP: $ext = json_decode($value['ext'], true); $value['token'] = !empty($ext['token']) ? $ext['token'] : ''; $value['auto_reply'] = !empty($ext['auto_reply']) ? $ext['auto_reply'] : ''; // unset($value['ext']); $returnArray['mp'] = $value; break; case WechatConfig::TYPE_CONFIG_APP: $ext = json_decode($value['ext'], true); $value['app_name'] = !empty($ext['app_name']) ? $ext['app_name'] : ''; unset($value['ext']); $returnArray['app'] = $value; break; default: break; } } } return [ 'code' => 0, 'msg' => 'success', 'data' => $returnArray ]; } //处理上传证书 public static function handlePemFile($pem_content, $type = 0) { // 证书 if (!is_dir(\Yii::$app->runtimePath . '/pem')) { mkdir(\Yii::$app->runtimePath . '/pem'); file_put_contents(\Yii::$app->runtimePath . '/pem/index.html', ''); } $pem_file = null; $pem_url = ''; if ($pem_content) { $pem_file_name = md5($pem_content) . '.pem'; $pem_file = \Yii::$app->runtimePath . '/pem/' . $pem_file_name; if (!file_exists($pem_file)) { file_put_contents($pem_file, $pem_content); } $pem_url = 'https://' . \Yii::$app->request->hostName . '/runtime/pem/' . $pem_file_name; } if ($type) { return $pem_file; } return $pem_url; } }