176 lines
5.7 KiB
PHP
176 lines
5.7 KiB
PHP
<?php
|
||
|
||
namespace app\models;
|
||
|
||
use Yii;
|
||
use app\components\SiteHelper;
|
||
use app\components\SysConst;
|
||
|
||
/**
|
||
* This is the model class for table "{{%payment_config}}".
|
||
*
|
||
* @property int $id ID
|
||
* @property int $cx_mch_id 平台商户ID
|
||
* @property string $name 名称
|
||
* @property string $pay_type 支付类型
|
||
* @property int $status 状态:0=停用,1=启用
|
||
* @property string $config 配置
|
||
* @property int $is_delete 是否删除:0=否
|
||
* @property int $created_at 添加时间
|
||
* @property int $updated_at 更新时间
|
||
* @property int $deleted_at 删除时间
|
||
* @property string $remark 备注
|
||
*/
|
||
class PaymentConfig extends \yii\db\ActiveRecord
|
||
{
|
||
const STATUS_ON = 1; //启用
|
||
const STATUS_OFF = 0; //停用
|
||
|
||
public $app_id;
|
||
public $mch_id;
|
||
public $private_key_pem;
|
||
public $public_key_pem;
|
||
public $private_key_pem_file;
|
||
public $public_key_pem_file;
|
||
public $private_key_password;
|
||
public $api_url;
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public static function tableName()
|
||
{
|
||
return '{{%payment_config}}';
|
||
}
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public function rules()
|
||
{
|
||
return [
|
||
[['name', 'remark'], 'trim'],
|
||
[['cx_mch_id', 'status', 'is_delete', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
|
||
[['name', 'pay_type', 'config'], 'required'],
|
||
[['config'], 'string'],
|
||
[['name'], 'string', 'max' => 50],
|
||
[['remark'], 'string', 'max' => 255],
|
||
[['pay_type'], 'string', 'max' => 24],
|
||
];
|
||
}
|
||
|
||
/**
|
||
* {@inheritdoc}
|
||
*/
|
||
public function attributeLabels()
|
||
{
|
||
return [
|
||
'id' => 'ID',
|
||
'cx_mch_id' => '平台商户ID',
|
||
'name' => '名称',
|
||
'pay_type' => '支付类型',
|
||
'status' => '状态:0=停用,1=启用',
|
||
'config' => '配置',
|
||
'is_delete' => '是否删除:0=否',
|
||
'created_at' => '添加时间',
|
||
'updated_at' => '更新时间',
|
||
'deleted_at' => '删除时间',
|
||
'remark' => '备注',
|
||
];
|
||
}
|
||
|
||
public function beforeSave($insert) {
|
||
if(parent::beforeSave($insert)){
|
||
if($this->isNewRecord){
|
||
$this->created_at = time();
|
||
}
|
||
$this->updated_at = time();
|
||
if($this->is_delete == 1){
|
||
$this->deleted_at = time();
|
||
}
|
||
$this->htmlTagFilter();
|
||
return true;
|
||
} else {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
public function htmlTagFilter()
|
||
{
|
||
$this->name = Model::htmlTagFilter($this->name);
|
||
$this->remark = Model::htmlTagFilter($this->remark);
|
||
}
|
||
|
||
public static function getStatus($status)
|
||
{
|
||
$labels = self::statusLabels();
|
||
return isset($labels[$status]) ? $labels[$status] : "未知";
|
||
}
|
||
|
||
public static function statusLabels()
|
||
{
|
||
return [
|
||
'0' => '停用',
|
||
'1' => '开启'
|
||
];
|
||
}
|
||
|
||
public static function getPayType($key)
|
||
{
|
||
$labels = PaymentTypes::payTypeLabels();
|
||
return isset($labels[$key]) ? $labels[$key] : "未知";
|
||
}
|
||
|
||
public function initConfig()
|
||
{
|
||
if(!empty($this->config)){
|
||
$is = SiteHelper::isBase64Encode($this->config);
|
||
if(!$is)return;
|
||
$config = base64_decode($this->config);
|
||
$config = json_decode($config,true);
|
||
$this->app_id = isset($config['app_id']) ? $config['app_id'] : '';
|
||
$this->mch_id = isset($config['mch_id']) ? $config['mch_id'] : '';
|
||
$this->private_key_pem = isset($config['private_key_pem']) ? $config['private_key_pem'] : '';
|
||
$this->public_key_pem = isset($config['public_key_pem']) ? $config['public_key_pem'] : '';
|
||
$this->private_key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($this->private_key_pem);
|
||
$this->public_key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($this->public_key_pem);
|
||
$this->private_key_password = isset($config['private_key_password']) ? $config['private_key_password'] : '';
|
||
$this->api_url = isset($config['api_url']) ? $config['api_url'] : '';
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取支付配置
|
||
*/
|
||
public static function getPaymentConfig($pay_type, $cx_mch_id = 0)
|
||
{
|
||
$model = PaymentConfig::findOne([
|
||
'cx_mch_id' => $cx_mch_id,
|
||
'pay_type' => $pay_type,
|
||
'status' => PaymentConfig::STATUS_ON,
|
||
'is_delete' => 0
|
||
]);
|
||
if($model == null){
|
||
$pay_type_cn = PaymentTypes::getPayType(PaymentTypes::payTypeKeyToId($pay_type));
|
||
return Model::asReturnError("系统尚未配置{$pay_type_cn}支付信息");
|
||
}
|
||
$model->initConfig();
|
||
if($pay_type == SysConst::$cxPayTypeEnUnionpay){
|
||
$data = [
|
||
'app_id' => $model->app_id,
|
||
'mch_id' => $model->mch_id,
|
||
'private_key_password' => $model->private_key_password,
|
||
'private_key_pem' => $model->private_key_pem,
|
||
'private_key_pem_file' => $model->private_key_pem_file,
|
||
'public_key_pem' => $model->public_key_pem,
|
||
'public_key_pem_file' => $model->public_key_pem_file,
|
||
'api_url' => $model->api_url,
|
||
];
|
||
}
|
||
$data['name'] = $model->name;
|
||
$data['pay_type'] = $pay_type;
|
||
return Model::asReturnSuccess('ok', $data);
|
||
}
|
||
}
|