98 lines
2.5 KiB
PHP
98 lines
2.5 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 CommonPaymentConfigEditForm extends Model
|
||
{
|
||
public $name;
|
||
public $pay_type;
|
||
public $config;
|
||
public $status;
|
||
public $remark;
|
||
|
||
public $cx_mch_id;
|
||
public $model;
|
||
|
||
|
||
public function rules()
|
||
{
|
||
return [
|
||
[['name', 'remark', 'pay_type'], 'trim'],
|
||
[['name', 'remark', 'pay_type'], 'string'],
|
||
[['status', 'cx_mch_id'], 'integer'],
|
||
[['model', 'config'], 'safe'],
|
||
[['status'], 'default', 'value' => 0],
|
||
[['status'], 'in', 'range' => [0, 1]],
|
||
[['model', 'config', 'name', 'pay_type',], 'required'],
|
||
];
|
||
}
|
||
|
||
public function attributeLabels()
|
||
{
|
||
return [
|
||
'cx_mch_id' => '平台商户ID',
|
||
'name' => '名称',
|
||
'pay_type' => '支付类型',
|
||
'status' => '状态:0=停用,1=启用',
|
||
'config' => '配置',
|
||
'remark' => '备注',
|
||
];
|
||
}
|
||
|
||
public function save()
|
||
{
|
||
if(!$this->validate()){
|
||
return $this->getModelError();
|
||
}
|
||
if($this->model->isNewRecord){
|
||
$this->model->cx_mch_id = $this->cx_mch_id;
|
||
$this->model->status = $this->status;
|
||
}
|
||
$this->model->name = $this->name;
|
||
$pay_types = $this->getPayTypes();
|
||
if(!in_array($this->pay_type, array_column($pay_types, 'short_name'))){
|
||
return $this->apiReturnError('无效支付类型');
|
||
}
|
||
$this->model->pay_type = $this->pay_type;
|
||
$this->model->remark = $this->remark;
|
||
|
||
$form = new CommonPaymentConfigForm();
|
||
$form->scenario = $this->pay_type;
|
||
$form->attributes = $this->config;
|
||
$res = $form->save();
|
||
if($res['code'] != 0){
|
||
return $res;
|
||
}
|
||
$this->model->config = $res['data']['data'];
|
||
|
||
if(!$this->model->save()){
|
||
return $this->getModelError($this->model);
|
||
}
|
||
return $this->apiReturnSuccess('保存成功');
|
||
}
|
||
|
||
private function getPayTypes()
|
||
{
|
||
$form = new CommonPaymentSettingForm();
|
||
$form->cx_mch_id = $this->cx_mch_id;
|
||
$data = $form->search();
|
||
return $data['pay_types'];
|
||
}
|
||
|
||
}
|
||
|