'小程序名称', 'app_id' => '小程序AppId', 'app_secret' => '小程序AppSecret', 'mch_id' => '微信支付商户号', 'key' => '微信支付Apiv2密钥', 'cx_mch_id' => '平台商户ID', 'key_three' => '微信支付Apiv3密钥' ]; } public function save() { if(!$this->validate()){ return $this->getModelError(); } if($this->model->isNewRecord){ $this->model->cx_mch_id = $this->cx_mch_id; } else { $this->cx_mch_id = $this->model->cx_mch_id; } $this->model->attributes = $this->attributes; 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 ($this->cert_pem) { $cert_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($this->cert_pem); if (!file_exists($cert_pem_file)) file_put_contents($cert_pem_file, $this->cert_pem); if(!file_exists($cert_pem_file)){ return [ 'code'=>1, 'msg'=>'证书读取不到' ]; } } $key_pem_file = null; if ($this->key_pem) { $key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($this->key_pem); if (!file_exists($key_pem_file)) file_put_contents($key_pem_file, $this->key_pem); if(!file_exists($key_pem_file)){ return [ 'code'=>1, 'msg'=>'证书读取不到' ]; } } //验证配置是否有效 $res = $this->check_config($cert_pem_file, $key_pem_file); if($res['code'] != 0){ return $res; } if(!$this->model->save()){ return $this->getModelError($this->model); } return [ 'code' => 0, 'msg' => '保存成功', ]; } private function check_config($cert_pem_file, $key_pem_file) { $wechat = new Wechat([ 'appId' => $this->app_id, 'appSecret' => $this->app_secret, 'mchId' => $this->mch_id, 'apiKey' => $this->key, 'certPem' => $cert_pem_file, 'keyPem' => $key_pem_file, ]); $res = $wechat->getAccessToken(true); if ($wechat->errCode == 40013) { return [ 'code' => 1, 'msg' => '小程序AppId错误' ]; } if ($wechat->errCode == 40125) { return [ 'code' => 1, 'msg' => '小程序AppSecret错误' ]; } if ($wechat->curl->error_code == 58) { return [ 'code' => 1, 'msg' => '微信支付证书错误' ]; } if (!($this->mch_id == 0 && $this->key == 0)) { $order_no = date('YmdHis') . rand(1000, 9999); $res = $wechat->pay->orderQuery($order_no); if ($res['return_code'] == "FAIL") { return [ 'code' => 1, 'msg' => '微信支付商户号或微信支付Api密钥错误' ]; } } return [ 'code' => 0, 'msg' => 'ok' ]; } }