cxfoot/modules/admin/models/wechat/WechatAppForm.php
2023-11-24 09:50:42 +08:00

160 lines
4.6 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2020-11-23
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\modules\admin\models\wechat;
use app\models\Option;
use app\modules\admin\models\AdminModel;
use Wechat\DataConversion;
use Wechat\Wechat;
class WechatAppForm extends AdminModel
{
/** @var WechatApp $model */
public $model;
public $name;
public $app_id;
public $app_secret;
public $mch_id;
public $key;
public $cert_pem;
public $key_pem;
public $key_three;
public $cx_mch_id;
public function rules()
{
return [
[['name','app_id', 'app_secret', 'mch_id', 'key', 'cert_pem', 'key_pem','key_three'], 'trim'],
// [['name','app_id', 'app_secret', 'mch_id', 'key', 'model', 'cx_mch_id','key_three'], 'required'],
[['cx_mch_id'], 'integer'],
];
}
public function attributeLabels()
{
return [
'name' => '小程序名称',
'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'
];
}
}