init: import production code mss.nahaichuan.com (exclude Runtime/Uploads/backups/logs)
这个提交包含在:
@@ -0,0 +1,248 @@
|
||||
<?php
|
||||
|
||||
namespace Distribution\Model;
|
||||
|
||||
use Common\Model\MerRemitModel;
|
||||
|
||||
use Common\Model\RemitRecordModel;
|
||||
use Common\Model\RetailMessageModel;
|
||||
use Pay\Library\WeChat\WxRemitPay; // 微信企业付款类
|
||||
|
||||
/**
|
||||
* 分销提现表
|
||||
* Class WithdrawalModel
|
||||
* @package Distribution\Model
|
||||
*/
|
||||
class WithdrawalModel extends \Common\Model\BaseModel
|
||||
{
|
||||
protected $tableName = 'distribution_withdrawal';
|
||||
protected $fields = array(
|
||||
'id', //int(11) NOT NULL AUTO_INCREMENT,
|
||||
'merchant_id', //int(11) NOT NULL DEFAULT '0' COMMENT '商户id',
|
||||
'distributor_id', //int(11) NOT NULL DEFAULT '0' COMMENT '分销员ID',
|
||||
'remit_record_id', //int(11) NOT NULL DEFAULT '0' COMMENT '打款记录表ID',
|
||||
'add_time', //int(11) DEFAULT '0' NULL COMMENT '提现时间',
|
||||
'state', //tinyint(1) DEFAULT '0' COMMENT '到账状态 0未打款 1已到账 2处理中',
|
||||
'status', //tinyint(1) DEFAULT '0' COMMENT '审核状态 0未审核 1已审核 2无需审核',
|
||||
'withdrawal_way', //tinyint(1) DEFAULT '0' COMMENT '提现方式 0微信 1支付宝 2银行卡',
|
||||
'withdrawal_money', //decimal(10,2) DEFAULT '0.00' COMMENT '提现金额',
|
||||
'withdrawal_json', //varchar(255) DEFAULT '' COMMENT '提现信息',
|
||||
);
|
||||
|
||||
|
||||
// 微信打款到零钱
|
||||
public function remitPay($id, $merchant_id)
|
||||
{
|
||||
if(!$merchant_id || !$id){
|
||||
$result['status'] = 0;
|
||||
$result['msg'] = '关键信息缺失!';
|
||||
return $result;
|
||||
}
|
||||
|
||||
$MerRemitModel = new MerRemitModel();
|
||||
$DistributorModel = new DistributorModel();
|
||||
$RemitRecordModel = new RemitRecordModel();
|
||||
$MessageModel = new RetailMessageModel();
|
||||
|
||||
$dataMap = [
|
||||
'merchant_id' => $merchant_id
|
||||
];
|
||||
$config = $MerRemitModel->getFind($dataMap);
|
||||
if ($config && isset($config['id']) && $config['wx_state'] == 1) {
|
||||
//
|
||||
} else {
|
||||
$result['status'] = 0;
|
||||
$result['msg'] = '商家未配置企业付款,或者打款到零钱已关闭!';
|
||||
return $result;
|
||||
}
|
||||
|
||||
// 提现记录获取
|
||||
$withdrawalWhere['id'] = $id;
|
||||
$withdrawalWhere['state'] = ['NEQ', 1];
|
||||
$Withdrawal = $this->getFind($withdrawalWhere);
|
||||
if (!$Withdrawal) {
|
||||
$result['status'] = 0;
|
||||
$result['msg'] = '申请记录错误!';
|
||||
return $result;
|
||||
} else if ($Withdrawal['state'] == 1) {
|
||||
$result['status'] = 0;
|
||||
$result['msg'] = '提现申请已到账!';
|
||||
return $result;
|
||||
} else if ($Withdrawal['status'] == 0) {
|
||||
$result['status'] = 0;
|
||||
$result['msg'] = '提现请求审核中,请稍后!';
|
||||
return $result;
|
||||
}
|
||||
|
||||
if($Withdrawal['state'] == 2){ // 打款中,获取之前的订单号
|
||||
$remitRes = $RemitRecordModel->getFind(['user_id' => $Withdrawal['id']], 'id, out_trade_no');
|
||||
if(!$remitRes['id'] || !$remitRes['out_trade_no']){
|
||||
$result['status'] = 0;
|
||||
$result['msg'] = '提现记录错误!';
|
||||
return $result;
|
||||
}
|
||||
$remitRes = $remitRes['id'];
|
||||
$out_trade_no = $remitRes['out_trade_no'];
|
||||
} else {
|
||||
$out_trade_no = createOrderTradeNo($Withdrawal['id']);
|
||||
$RemitRecordData = [
|
||||
'record_id' => $Withdrawal['id'],
|
||||
'merchant_id' => $merchant_id,
|
||||
'user_id' => $Withdrawal['distributor_id'],
|
||||
'money' => $Withdrawal['withdrawal_money'],
|
||||
'remit_type' => 1,
|
||||
'out_trade_no' => $out_trade_no,
|
||||
];
|
||||
$remitRes = $RemitRecordModel->addOrder($RemitRecordData);
|
||||
}
|
||||
|
||||
// 申请人员uid获取
|
||||
$uidInfo = $DistributorModel->getFind(['id' => $Withdrawal['distributor_id'] ], 'uid');
|
||||
|
||||
// 申请人员的openid 获取
|
||||
$openid = $DistributorModel->getOpenidByDistributor($Withdrawal['distributor_id']);
|
||||
if(!$openid){
|
||||
$result['status'] = 0;
|
||||
$result['msg'] = '用户关键信息缺失!';
|
||||
return $result;
|
||||
}
|
||||
|
||||
if($remitRes){
|
||||
$id = $remitRes;
|
||||
$money = $Withdrawal['withdrawal_money'] * 100;
|
||||
$WxRemitPay = new WxRemitPay($config);
|
||||
$res = $WxRemitPay->remitPay($out_trade_no, $openid, $money, '分销员佣金');
|
||||
$errmsg = $WxRemitPay->getMsg();
|
||||
if ($res) {
|
||||
// 打款成功
|
||||
$wxReturnData = $WxRemitPay->getReturnData();
|
||||
|
||||
// 更新--申请信息/分销员可提现金额
|
||||
$this->updateOrder(['id' => $Withdrawal['id']], ['state' => 1, 'remit_record_id' => $id]);
|
||||
|
||||
// 更新订单
|
||||
$map = array(
|
||||
'id' => $id,
|
||||
'out_trade_no' => $out_trade_no,
|
||||
'merchant_id' => $merchant_id
|
||||
);
|
||||
$saveData = array(
|
||||
'add_time' => strtotime($wxReturnData['payment_time']),
|
||||
'trade_no' => $wxReturnData['payment_no'],
|
||||
'order_state' => 'SUCCESS',
|
||||
'other_info' => json_encode($wxReturnData)
|
||||
);
|
||||
$res = $RemitRecordModel->updateOrder($map, $saveData);
|
||||
|
||||
if ($res) {
|
||||
$result['status'] = 1;
|
||||
$result['msg'] = '微信打款到零钱成功!';
|
||||
|
||||
// 消息推送
|
||||
$MessageData = [
|
||||
'type' => 6,
|
||||
'uid' => $uidInfo['uid'],
|
||||
'title' => '微信打款到零钱成功',
|
||||
'content' => '恭喜您,微信打款到零钱成功.'.$Withdrawal['withdrawal_money'].'元',
|
||||
'add_time' => time()
|
||||
];
|
||||
} else {
|
||||
$result['status'] = 1;
|
||||
$result['msg'] = '微信打款到零钱成功,但打款流水更新失败,建议记账,流水单号:' . $out_trade_no;
|
||||
// 消息推送
|
||||
$MessageData = [
|
||||
'type' => 6,
|
||||
'uid' => $uidInfo['uid'],
|
||||
'title' => '微信打款到零钱成功',
|
||||
'content' => '恭喜您,微信打款到零钱成功.'.$Withdrawal['withdrawal_money'].'元',
|
||||
'add_time' => time()
|
||||
];
|
||||
}
|
||||
$MessageModel->addOrder($MessageData);
|
||||
return $result;
|
||||
} else if ($res === 0) {
|
||||
// 更新--申请信息/分销员可提现金额(冻结)
|
||||
$this->updateOrder(['id' => $Withdrawal['id']], ['state' => 2]);
|
||||
|
||||
// 打款处理中
|
||||
$map = array(
|
||||
'id' => $id,
|
||||
'out_trade_no' => $out_trade_no,
|
||||
'merchant_id' => $merchant_id
|
||||
);
|
||||
$saveData = array(
|
||||
'order_state' => 'PROCESSING'
|
||||
);
|
||||
$res = $RemitRecordModel->updateOrder($map, $saveData);
|
||||
if ($res) {
|
||||
$result['status'] = 0;
|
||||
$result['msg'] = $errmsg;
|
||||
return $result;
|
||||
} else {
|
||||
$result['status'] = 0;
|
||||
$result['msg'] = $errmsg . ' 打款处理中,但打款流水未正确修改状态,建议记账,流水单号:' . $out_trade_no;
|
||||
return $result;
|
||||
}
|
||||
} else {
|
||||
// 打款失败,用户资金返还
|
||||
// $DistributorModel->toSetInc(['id' => $Withdrawal['distributor_id']], 'settable_amount', $Withdrawal['withdrawal_money']);
|
||||
|
||||
$map = array(
|
||||
'id' => $id,
|
||||
'out_trade_no' => $out_trade_no,
|
||||
'merchant_id' => $merchant_id
|
||||
);
|
||||
$saveData = array(
|
||||
'order_state' => 'FAILED'
|
||||
);
|
||||
$res = $RemitRecordModel->updateOrder($map, $saveData);
|
||||
// 打款失败
|
||||
$result['status'] = 0;
|
||||
$result['msg'] = $errmsg;
|
||||
return $result;
|
||||
}
|
||||
|
||||
} else {
|
||||
$error = $RemitRecordModel->error;
|
||||
$error = $error ? $error : '打款流水添加失败';
|
||||
$result['status'] = 0;
|
||||
$result['msg'] = $error;
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
// 查询订单状态
|
||||
public function getOrderStatus($id)
|
||||
{
|
||||
$MerRemitModel = new MerRemitModel();
|
||||
|
||||
$record_id = $this->getFind(['id' =>$id], 'remit_record_id, merchant_id');
|
||||
$trade_no = $MerRemitModel->getFind(['id' =>$record_id['id']], 'trade_no');
|
||||
if(!$trade_no){
|
||||
return 0;
|
||||
}
|
||||
|
||||
$dataMap = [
|
||||
'merchant_id' => $record_id['merchant_id']
|
||||
];
|
||||
$config = $MerRemitModel->getFind($dataMap);
|
||||
if ($config && isset($config['id']) && $config['wx_state'] == 1) {
|
||||
//
|
||||
} else {
|
||||
$result['status'] = 0;
|
||||
$result['msg'] = '商家未配置企业付款,或者打款到零钱已关闭!';
|
||||
return $result;
|
||||
}
|
||||
$WxRemitPay = new WxRemitPay($config);
|
||||
|
||||
$res = $WxRemitPay->getPayState($trade_no['trade_no']);
|
||||
if($res){
|
||||
return 1;
|
||||
} else if($res === 0) {
|
||||
return 2;
|
||||
} else {
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户