cxgj/models/common/CommonPaymentConfigForm.php
2023-11-27 09:45:13 +08:00

80 lines
2.7 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2022年7月12日
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\models\common;
use app\models\PaymentConfig;
use app\models\PaymentTypes;
use app\models\Model;
class CommonPaymentConfigForm extends Model
{
public $app_id;
public $mch_id;
public $private_key_pem;
public $public_key_pem;
public $private_key_password;
public $api_url;
public function rules()
{
return [
[['app_id', 'mch_id', 'private_key_password', 'api_url'], 'trim'],
[['app_id', 'mch_id', 'private_key_password', 'api_url', 'private_key_pem', 'public_key_pem'], 'string'],
[['app_id', 'mch_id', 'private_key_password', 'api_url', 'private_key_pem', 'public_key_pem'], 'required', 'on' => 'unionpay'],
];
}
public function save()
{
if(!$this->validate()){
return $this->getModelError();
}
if($this->scenario == 'unionpay'){
if (!is_dir(\Yii::$app->runtimePath . '/pem')) {
mkdir(\Yii::$app->runtimePath . '/pem');
file_put_contents(\Yii::$app->runtimePath . '/pem/index.html', '');
}
$private_key_pem_file = null;
if ($this->private_key_pem) {
$private_key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($this->private_key_pem);
if (!file_exists($private_key_pem_file))
file_put_contents($private_key_pem_file, $this->private_key_pem);
if(!file_exists($private_key_pem_file)){
return $this->apiReturnError('证书读取不到');
}
}
$public_key_pem_file = null;
if ($this->public_key_pem) {
$public_key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($this->public_key_pem);
if (!file_exists($public_key_pem_file))
file_put_contents($public_key_pem_file, $this->public_key_pem);
if(!file_exists($public_key_pem_file)){
return $this->apiReturnError('证书读取不到');
}
}
}
$data = [
'app_id' => $this->app_id,
'mch_id' => $this->mch_id,
'private_key_pem' => $this->private_key_pem,
'private_key_password' => $this->private_key_password,
'public_key_pem' => $this->public_key_pem,
'api_url' => $this->api_url,
];
$data = base64_encode(json_encode($data));
return $this->apiReturnSuccess('ok', ['data' => $data]);
}
}