221 lines
7.8 KiB
PHP
221 lines
7.8 KiB
PHP
<?php
|
|
|
|
|
|
|
|
|
|
namespace app\common\repositories\user;
|
|
|
|
|
|
use app\common\dao\user\LabelRuleDao;
|
|
use app\common\dao\user\UserOrderDao;
|
|
use app\common\model\system\merchant\Merchant;
|
|
use app\common\model\user\User;
|
|
use app\common\repositories\BaseRepository;
|
|
use app\common\repositories\store\order\StoreOrderRepository;
|
|
use crmeb\jobs\SendSmsJob;
|
|
use crmeb\services\PayService;
|
|
use think\exception\ValidateException;
|
|
use think\facade\Db;
|
|
use think\facade\Log;
|
|
use think\facade\Queue;
|
|
|
|
/**
|
|
* Class LabelRuleRepository
|
|
* @package app\common\repositories\user
|
|
* @author xaboy
|
|
* @day 2020/10/20
|
|
* @mixin LabelRuleDao
|
|
*/
|
|
class UserOrderRepository extends BaseRepository
|
|
{
|
|
|
|
//付费会员
|
|
const TYPE_SVIP = 'S-';
|
|
|
|
/**
|
|
* LabelRuleRepository constructor.
|
|
* @param LabelRuleDao $dao
|
|
*/
|
|
public function __construct(UserOrderDao $dao)
|
|
{
|
|
$this->dao = $dao;
|
|
}
|
|
|
|
public function getList(array $where, $page, $limit)
|
|
{
|
|
$query = $this->dao->search($where);
|
|
$count = $query->count();
|
|
$list = $query->with([
|
|
'user' => function($query){
|
|
$query->field('uid,nickname,avatar,phone,is_svip,svip_endtime');
|
|
}
|
|
])->order('create_time DESC')->page($page, $limit)->select()->toArray();
|
|
return compact('count', 'list');
|
|
}
|
|
|
|
/**
|
|
* @param $data
|
|
* @return mixed
|
|
* @author xaboy
|
|
* @day 2020/10/21
|
|
*/
|
|
public function add($res, $user, $params){
|
|
$order_sn = app()->make(StoreOrderRepository::class)->getNewOrderId(StoreOrderRepository::TYPE_SN_USER_ORDER);
|
|
$data = [
|
|
'title' => $res['value']['svip_name'],
|
|
'link_id' => $res->group_data_id,
|
|
'order_sn' => $order_sn,
|
|
'pay_price' => $res['value']['price'],
|
|
'order_info' => json_encode($res['value'],JSON_UNESCAPED_UNICODE),
|
|
'uid' => $user->uid,
|
|
'order_type' => self::TYPE_SVIP.$res['value']['svip_type'],
|
|
'pay_type' => $res['value']['price'] == 0 ? 'free' : $params['pay_type'],
|
|
'status' => 1,
|
|
'other' => $user->is_svip == -1 ? 'first' : '',
|
|
'bind_mer_id' => $params['bind_mer_id'] ?? '',
|
|
'bind_staff_id' => $params['bind_staff_id'] ?? '',
|
|
'exchange_code' => $params['exchange_code'] ?? '',
|
|
'exchange_code_id' => $params['exchange_code_id'] ?? 0,
|
|
'source' => $params['source'] ?? 0,
|
|
];
|
|
// 判断:兑换不需要支付
|
|
if($data['source'] == 2){
|
|
$data['pay_price'] = 0;
|
|
$data['pay_type'] = 'free';
|
|
}
|
|
$body = [
|
|
'order_sn' => $order_sn,
|
|
'pay_price' => $data['pay_price'],
|
|
'attach' => 'user_order',
|
|
'body' =>'付费会员'
|
|
];
|
|
$type = $params['pay_type'];
|
|
if (in_array($type, ['weixin', 'alipay'], true) && $params['is_app']) {
|
|
$type .= 'App';
|
|
}
|
|
if ($params['return_url'] && $type === 'alipay') $body['return_url'] = $params['return_url'];
|
|
|
|
$info = $this->dao->create($data);
|
|
if ($data['pay_price']){
|
|
try {
|
|
|
|
// 余额支付
|
|
if ($type === 'balance') return $this->payBalance($user, $data, $info->order_id);
|
|
|
|
|
|
$service = new PayService($type,$body, 'user_order');
|
|
$config = $service->pay($user);
|
|
|
|
return app('json')->status($type, $config + ['order_id' => $info->order_id]);
|
|
} catch (\Exception $e) {
|
|
// 判断:兑换不需要支付
|
|
if($data['exchange_code_id'] > 0 && !empty($data['exchange_code'])){
|
|
throw new ValidateException($e->getMessage());
|
|
}
|
|
return app('json')->status('error', $e->getMessage(), ['order_id' => $info->order_id]);
|
|
}
|
|
} else {
|
|
$res = $this->paySuccess($data);
|
|
return app('json')->status('success', ['order_id' => $info->order_id]);
|
|
}
|
|
}
|
|
|
|
public function paySuccess($data)
|
|
{
|
|
/*
|
|
array (
|
|
'order_sn' => 'wxs167090166498470921',
|
|
'data' =>
|
|
EasyWeChat\Support\Collection::__set_state(array(
|
|
'items' =>
|
|
array (
|
|
'appid' => 'wx4409eaedbd62b213',
|
|
'attach' => 'user_order',
|
|
'bank_type' => 'OTHERS',
|
|
'cash_fee' => '1',
|
|
'fee_type' => 'CNY',
|
|
'is_subscribe' => 'N',
|
|
'mch_id' => '1288093001',
|
|
'nonce_str' => '6397efa100165',
|
|
'openid' => 'oOdvCvjvCG0FnCwcMdDD_xIODRO0',
|
|
'out_trade_no' => 'wxs167090166498470921',
|
|
'result_code' => 'SUCCESS',
|
|
'return_code' => 'SUCCESS',
|
|
'sign' => '125C56DE030A461E45D421E44C88BC30',
|
|
'time_end' => '20221213112118',
|
|
'total_fee' => '1',
|
|
'trade_type' => 'JSAPI',
|
|
'transaction_id' => '4200001656202212131458556229',
|
|
),
|
|
)),
|
|
*/
|
|
$res = $this->dao->getWhere(['order_sn' => $data['order_sn']]);
|
|
$type = explode('-',$res['order_type'])[0].'-';
|
|
// 付费会员充值
|
|
if ($type == self::TYPE_SVIP) {
|
|
$result = Db::transaction(function () use($data, $res) {
|
|
$res->paid = 1;
|
|
$res->pay_time = date('y_m-d H:i:s', time());
|
|
$res->save();
|
|
return $this->payAfter($res, $res);
|
|
});
|
|
|
|
event('user.vipPay', [$res]);
|
|
|
|
return $result;
|
|
}
|
|
}
|
|
|
|
public function payAfter($data, $ret)
|
|
{
|
|
$info = json_decode($data['order_info']);
|
|
$user = app()->make(UserRepository::class)->get($ret['uid']);
|
|
$day = $info->svip_type == 3 ? 0 : $info->svip_number;
|
|
$endtime = ($user['svip_endtime'] && $user['is_svip'] != 0) ? $user['svip_endtime'] : date('Y-m-d H:i:s',time());
|
|
$svip_endtime = date('Y-m-d H:i:s',strtotime("$endtime +$day day" ));
|
|
|
|
$user->is_svip = $info->svip_type;
|
|
$user->svip_endtime = $svip_endtime;
|
|
$user->save();
|
|
$ret->status = 1;
|
|
$ret->pay_time = date('Y-m-d H:i:s',time());
|
|
$ret->end_time = $svip_endtime;
|
|
$ret->save();
|
|
$date = $info->svip_type == 3 ? '终身会员' : $svip_endtime;
|
|
if ($user->phone) Queue::push(SendSmsJob::class,['tempId' => 'SVIP_PAY_SUCCESS','id' => ['phone' => $user->phone, 'date' => $date]]);
|
|
return ;
|
|
}
|
|
|
|
|
|
// 余额支付
|
|
public function payBalance(User $user, $data, $order_id){
|
|
// 判断:是否开启余额支付
|
|
if (!systemConfig('yue_pay_status')) throw new ValidateException('未开启余额支付');
|
|
$balance = $user['now_money'] ?? 0;
|
|
if ($balance < $data['pay_price']) throw new ValidateException('余额不足,请更换支付方式');
|
|
// 余额支付处理
|
|
Db::transaction(function () use ($user, $data, $balance, $order_id) {
|
|
// 扣除用户余额
|
|
$user->now_money = bcsub($user->now_money, $data['pay_price'], 2);
|
|
$user->save();
|
|
// 余额变更记录
|
|
$userBillData = [
|
|
'link_id' => $order_id,
|
|
'status' => 1,
|
|
'title' => '开通会员卡',
|
|
'number' => $data['pay_price'],
|
|
'mark' => '余额支付'.floatval($data['pay_price']).'元开通'.$data['title'],
|
|
'balance' => $user->now_money
|
|
];
|
|
app()->make(UserBillRepository::class)->decBill($user['uid'], 'now_money', 'pay_vip', $data);
|
|
|
|
$this->paySuccess($data);
|
|
});
|
|
|
|
return app('json')->status('success', '余额支付成功', ['order_id' => $order_id]);
|
|
}
|
|
|
|
|
|
|
|
}
|