cxfoot/models/common/CommonRechargeActionForm.php
2023-10-27 14:25:12 +08:00

216 lines
7.4 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2021年9月30日
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\models\common;
use app\models\Model;
use app\models\Balance;
use app\models\RechargeOrder;
use app\models\RechargeRule;
use app\models\UniqueOrderNo;
use app\models\PaymentTypes;
use app\models\common\CommonRechargeRuleListForm;
use app\models\common\CommonPaymentSettingForm;
use app\components\SysConst;
use app\models\common\payment\Payment;
use app\models\common\payment\PaymentOrder;
class CommonRechargeActionForm extends Model
{
public $type;
public $money;
public $remark;
public $receipt;
public $operator_name;
public $rule_id;
public $pay_type;
public $user_id;
public $scene;
public $cx_mch_id;
public function rules()
{
return [
[['remark', 'receipt', 'operator_name'], 'trim'],
[['remark', 'receipt', 'operator_name'], 'string'],
[['type', 'cx_mch_id', 'user_id', 'rule_id'], 'integer'],
[['money'], 'number'],
[['type'], 'in', 'range' => [1,2]],
[['cx_mch_id', 'user_id', 'scene'], 'required'],
[['money', 'type', 'remark', 'operator_name'], 'required', 'on' => 'recharge'],
[['money', 'pay_type',], 'required', 'on' => 'auto_recharge'],
];
}
public function attributeLabels(){
return [
'operator_name' => '操作用户',
'type' => '类型',
'money' => '金额',
'remark' => '备注',
'receipt' => '回执',
];
}
//后台充值
public function recharge()
{
if(!$this->validate()){
return $this->getModelError();
}
if($this->type == 1 && $this->money <= 0){
return $this->apiReturnError('充值金额不能小于0');
}
$res = Balance::initBalance($this->user_id, $this->scene, $this->cx_mch_id);
if($res['code'] != 0)
return $res;
$balance = Balance::findOne(['user_id' => $this->user_id, 'scene' => $this->scene, 'cx_mch_id' => $this->cx_mch_id, 'is_delete' => 0]);
if($balance == null){
return $this->apiReturnError('系统内部错误');
}
if($this->type == 2 && $this->money > $balance->account_balance){
return $this->apiReturnError('账户余额不足');
}
$type_cn = $this->type == 1 ? '充值' : '扣除';
$desc = $this->operator_name . "操作后台" . $type_cn . $this->money."";
$order_no = UniqueOrderNo::generate(UniqueOrderNo::ORDER_TYPE_MANUAL_RECHARGE_ORDER);
$order_type = UniqueOrderNo::getOrderTypeByOrderNo($order_no);
$ext = [
'remark' => $this->remark,
'receipt' => $this->receipt
];
$ext = json_encode($ext,JSON_UNESCAPED_UNICODE);
$t = \Yii::$app->db->beginTransaction();
$res = Balance::logger($this->user_id, $this->type, $this->money, $desc, $order_type, $order_no, $this->scene, $ext, $this->cx_mch_id);
if($res['code'] != 0){
$t->rollBack();
return $res;
}
$t->commit();
return $this->apiReturnSuccess("操作成功");
}
//充值设置
public function setting()
{
if(!$this->validate()){
return $this->getModelError();
}
$data = [];
//充值方案
$form = new CommonRechargeRuleListForm();
$form->cx_mch_id = $this->cx_mch_id;
$res = $form->search();
$recharge_rules = $res['code'] == 0 ? $res['data'] : [];
$data['recharge_rules'] = $recharge_rules;
//支持支付方式
$data['pay_types'] = $this->getPayType($this->cx_mch_id);
return $this->apiReturnSuccess('ok', $data);
}
private function getPayType($cx_mch_id, $platform = 'wxmp')
{
$form = new CommonPaymentSettingForm();
$form->cx_mch_id = $cx_mch_id;
$res = $form->search();
$pay_type_list = $res['pay_types'];
$pay_types = [];
foreach($pay_type_list as $index => $item){
if(!$item['is_show'] || !$item['is_open'])
continue;
//排除
if(in_array($item['short_name'], [SysConst::$cxPayTypeEnBalance, SysConst::$cxPayTypeEnBank]))
continue;
//@TODO 根据平台来源显示支付方式
$pay_types[] = $item;
}
return $pay_types;
}
//用户发起充值
public function autoRecharge()
{
if(!$this->validate()){
return $this->getModelError();
}
if($this->money <= 0 ){
return $this->apiReturnError('充值金额需大于0');
}
//支付方式检查
$pay_types = $this->getPayType($this->cx_mch_id);
if(!in_array($this->pay_type, array_column($pay_types, 'short_name'))){
return $this->apiReturnError('不支持此充值方式');
}
$this->money = sprintf('%.2f', $this->money);
$t = \Yii::$app->db->beginTransaction();
$order = new RechargeOrder();
$order->cx_mch_id = $this->cx_mch_id;
$order->order_no = UniqueOrderNo::generate(UniqueOrderNo::ORDER_TYPE_AUTO_RECHARGE_ORDER, "\\app\\models\\RechargeOrder");
$order->user_id = $this->user_id;
$order->pay_price = $this->money;
$order->pay_type = PaymentTypes::payTypeKeyToId($this->pay_type);
$order->is_pay = 0;
//充值方案
if($this->rule_id != 0){
$recharge_rule = RechargeRule::findOne(['id' => $this->rule_id, 'cx_mch_id' => $this->cx_mch_id, 'is_delete' => 0]);
if($recharge_rule == null){
return $this->apiReturnError('无效充值方案');
}
$order->send_price = $recharge_rule->send_price;
} else {
$recharge_rule = RechargeRule::find()
->where([
'cx_mch_id' => $this->cx_mch_id,
'is_delete' => 0
])
->andWhere([
'<=',
'pay_price',
$this->money
])
->orderBy(['pay_price' => SORT_DESC])->one();
if($recharge_rule != null){
$order->send_price = $recharge_rule->send_price;
}
}
if(!$order->save()){
return $this->getModelError($order);
}
$scene_cn = Balance::getBalanceScene($this->scene);
$paymentOrder = new PaymentOrder([
'orderNo' => $order->order_no,
'amount' => (float)$this->money,
'title' => $scene_cn.'余额充值',
'notifyClass' => '\\app\\models\\common\\notify\\payment\\BalanceRechargePaymentNotify',
'supportPayTypes' => array_column($pay_types, 'short_name'),
'payType' => PaymentTypes::payTypeKeyToId($this->pay_type)
]);
$payment = new Payment();
$paymentOrders[] = $paymentOrder;
$res = $payment->createOrder($paymentOrders);
if($res['code'] != 0){
$t->rollBack();
return $res;
}
$t->commit();
return $res;
}
}