|
|
@@ -0,0 +1,845 @@ |
|
|
|
<?php |
|
|
|
|
|
|
|
namespace app\controller\store\project\logic\order; |
|
|
|
|
|
|
|
use app\model\coin\CoinLog; |
|
|
|
use app\model\store\project\MemberOnceCardItemModel; |
|
|
|
use app\model\store\project\MemberOnceCardModel; |
|
|
|
use app\model\store\project\MemberProjectModel; |
|
|
|
use app\model\store\project\OnceCardItemModel; |
|
|
|
use app\model\store\project\OnceCardModel; |
|
|
|
use app\model\store\project\order\OnceCardOrderItemModel; |
|
|
|
use app\model\store\project\order\OnceCardOrderModel; |
|
|
|
use app\model\store\project\ProjectModel; |
|
|
|
use app\services\activity\coupon\StoreCouponUserServices; |
|
|
|
use app\services\pay\OrderPayServices; |
|
|
|
use app\services\pay\YuePayServices; |
|
|
|
use app\services\pay\PayServices; |
|
|
|
use app\services\store\logic\ApplyServiceLogic; |
|
|
|
use think\facade\Cache; |
|
|
|
|
|
|
|
class OrderOnceCardLogic |
|
|
|
{ |
|
|
|
|
|
|
|
use OrderTrait; |
|
|
|
|
|
|
|
protected $once_card; |
|
|
|
protected $order; |
|
|
|
|
|
|
|
// public function wxPay($params) |
|
|
|
// { |
|
|
|
// $this->checkOrderPayStatus($params); |
|
|
|
// $this->completePayAct('weixin', $params['payinfo']); |
|
|
|
// $MemberWalletLogic = new MemberWalletLogic($this->order->shop_id, $this->order->member_id); |
|
|
|
// $MemberWalletLogic->addIntegralByPayment($this->order->need_pay); //更新支付返的积分 |
|
|
|
// //计算业绩 |
|
|
|
// $PartnerLogic = new PartnerLogic($this->order->shop_id); |
|
|
|
// $PartnerLogic->commission($this->order->member_id, $this->order->need_pay); |
|
|
|
// //写入GMV |
|
|
|
// $shop_gmv_logs = new ShopGmvLogsModel(); |
|
|
|
// $shop_gmv_logs->shop_id = $this->order->shop_id; |
|
|
|
// $shop_gmv_logs->balance = $this->order->need_pay; |
|
|
|
// $shop_gmv_logs->save(); |
|
|
|
// } |
|
|
|
// |
|
|
|
// public function moneyPay($params) |
|
|
|
// { |
|
|
|
// //判断订单支付的状态与数据初始化 |
|
|
|
// $this->checkOrderPayStatus($params); |
|
|
|
// |
|
|
|
// //下面是独属于余额支付 |
|
|
|
// $MemberWalletLogic = new MemberWalletLogic($this->shopId, $this->member->uid); |
|
|
|
// $res = $MemberWalletLogic->useBalance($this->order->need_pay, $this->order->total_price, 15); //商城购物使用 |
|
|
|
// if ($res == false) $this->error($MemberWalletLogic->getError()); |
|
|
|
// |
|
|
|
// //完成支付后的操作 |
|
|
|
// $this->completePayAct('money'); |
|
|
|
// } |
|
|
|
|
|
|
|
public function createOrder($params) |
|
|
|
{ |
|
|
|
//判断用户 |
|
|
|
empty($params['member_id']) && $this->error('请先登录后再操作'); |
|
|
|
$this->checkMember($params['member_id'], false); |
|
|
|
|
|
|
|
$once_card_id = (int)$params['once_card_id']; |
|
|
|
if ($once_card_id == 0) $this->error('次卡不存在'); |
|
|
|
//判断次卡 |
|
|
|
$this->checkOnceCard($once_card_id, true); |
|
|
|
if ($this->once_card->is_delete != 0) $this->error('次卡不存在'); |
|
|
|
if ($this->once_card->status != 1) $this->error('次卡已下架'); |
|
|
|
|
|
|
|
//秒杀活动时间内判断数量 |
|
|
|
if ($flash_status = $this->checkFlashStatus()) { |
|
|
|
if ($this->once_card->flash_num < 1) $this->error('秒杀数量不足够'); |
|
|
|
} |
|
|
|
|
|
|
|
//判断总价 |
|
|
|
$total_price = $price = $flash_status ? $this->once_card->flash_price : $this->once_card->price; |
|
|
|
|
|
|
|
$integral = 0; |
|
|
|
$balance = 0; |
|
|
|
|
|
|
|
//判断优惠卷 |
|
|
|
$coupon_id = $params['coupon_id']; |
|
|
|
$couponBalance = 0; |
|
|
|
if (!empty($coupon_id)) { |
|
|
|
/** @var StoreCouponUserServices $couponServices */ |
|
|
|
$couponServices = app()->make(StoreCouponUserServices::class); |
|
|
|
$couponInfo = $couponServices->getOne([['id', '=', $coupon_id], ['uid', '=', $params['member_id']], ['is_fail', '=', 0], ['status', '=', 0], ['start_time', '<=', $_SERVER['REQUEST_TIME']], ['end_time', '>=', $_SERVER['REQUEST_TIME']]], '*', ['issue']); |
|
|
|
if (!$couponInfo) { |
|
|
|
$this->error('优惠券不存在'); |
|
|
|
} |
|
|
|
$type = $couponInfo['applicable_type'] ?? 0; |
|
|
|
if ($couponInfo['use_min_price'] > $total_price || $type != 0) {//只能是通用券 |
|
|
|
$this->error('不满足优惠劵的使用条件!'); |
|
|
|
} |
|
|
|
//满减券 |
|
|
|
if ($couponInfo['coupon_type'] == 1) { |
|
|
|
$couponPrice = $couponInfo['coupon_price']; |
|
|
|
} else { |
|
|
|
if ($couponInfo['coupon_price'] <= 0) {//0折 |
|
|
|
$couponPrice = $total_price; |
|
|
|
} else if ($couponInfo['coupon_price'] >= 100) { |
|
|
|
$couponPrice = 0; |
|
|
|
} else { |
|
|
|
$truePrice = (float)bcmul((string)$total_price, bcdiv((string)$couponInfo['coupon_price'], '100', 2), 2); |
|
|
|
$couponPrice = (float)bcsub((string)$total_price, (string)$truePrice, 2); |
|
|
|
} |
|
|
|
} |
|
|
|
if ($couponPrice < $total_price) { |
|
|
|
$need_pay = (float)bcsub((string)$total_price, (string)$couponPrice, 2); |
|
|
|
} else { |
|
|
|
$couponPrice = $total_price; |
|
|
|
$need_pay = 0; |
|
|
|
} |
|
|
|
$couponBalance = $couponPrice; |
|
|
|
} else { |
|
|
|
//获取需要支付的金额 |
|
|
|
$need_pay = round((($total_price) * 100 - $couponBalance * 100 - $balance * 100) / 100, 2); |
|
|
|
} |
|
|
|
|
|
|
|
if ($params['payType'] == 'weixin' && $params['is_coin'] == 1) { |
|
|
|
//判断币 |
|
|
|
$Coin = new \app\model\coin\Coin(); |
|
|
|
$coin = $Coin->where('uid', $params['member_id'])->value('coin'); |
|
|
|
if (!empty($coin)) { |
|
|
|
if ($this->once_card->coin > $coin) { |
|
|
|
$this->error('币不足'); |
|
|
|
} |
|
|
|
} else { |
|
|
|
$this->error('币不足'); |
|
|
|
} |
|
|
|
if ($need_pay - $this->once_card->coin < 0) { |
|
|
|
$need_pay = 0; |
|
|
|
} else { |
|
|
|
$need_pay = $need_pay - $this->once_card->coin; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//处理次卡 |
|
|
|
$order_model = new OnceCardOrderModel(); |
|
|
|
$OnceCardOrder_data = [ |
|
|
|
'member_id' => $this->member->uid, |
|
|
|
'once_card_id' => $this->once_card->once_card_id, |
|
|
|
'name' => $this->once_card->name, |
|
|
|
'valid_time' => $this->once_card->valid_time, |
|
|
|
'valid_unit' => $this->once_card->valid_unit, |
|
|
|
'is_flash_sale' => $flash_status ? 1 : 0, |
|
|
|
'cover_img' => $this->once_card->cover_img, |
|
|
|
'total_price' => $total_price, |
|
|
|
'member_coupon_id' => $params['coupon_id'], |
|
|
|
'coupon_money' => $couponBalance, |
|
|
|
'use_integral' => $integral, |
|
|
|
'integral_balance' => $balance, |
|
|
|
'need_pay' => $need_pay, |
|
|
|
'pay_type' => $params['payType'], |
|
|
|
'add_time' => $_SERVER['REQUEST_TIME'], |
|
|
|
'add_ip' => $params['ip'], |
|
|
|
'order_id' => $this->getNewOrderId(), |
|
|
|
'coin' => $this->once_card->coin, |
|
|
|
'mark' => $params['mark'], |
|
|
|
'unique' => md5($this->getNewOrderId() . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8)), |
|
|
|
'verify_code' => $this->getStoreCode($order_model), |
|
|
|
'channel_type' => 'routine' |
|
|
|
]; |
|
|
|
if ($need_pay == '0' || $need_pay == '0.00') { |
|
|
|
$OnceCardOrder_data['is_paid'] = 1; |
|
|
|
$OnceCardOrder_data['pay_time'] = $_SERVER['REQUEST_TIME']; |
|
|
|
$OnceCardOrder_data['pay_info'] = '抵扣币或优惠券后,金额为0'; |
|
|
|
$OnceCardOrder_data['pay_price'] = 0.00; |
|
|
|
$OnceCardOrder_data['trade_no'] = ''; |
|
|
|
} |
|
|
|
|
|
|
|
$order_model->save($OnceCardOrder_data); |
|
|
|
|
|
|
|
//如果不是秒杀这时候要去添加销量 |
|
|
|
if ($flash_status) { |
|
|
|
$this->once_card->flash_num = $this->once_card->flash_num - 1; |
|
|
|
$this->once_card->fictitious_take_count = $this->once_card->fictitious_take_count + 1; |
|
|
|
$this->once_card->take_count = $this->once_card->take_count + 1; |
|
|
|
$this->once_card->save(); |
|
|
|
} else { |
|
|
|
$this->once_card->fictitious_take_count = $this->once_card->fictitious_take_count + 1; |
|
|
|
$this->once_card->take_count = $this->once_card->take_count + 1; |
|
|
|
$this->once_card->save(); |
|
|
|
} |
|
|
|
|
|
|
|
//处理次卡项目 |
|
|
|
$items = OnceCardItemModel::where([ |
|
|
|
['once_card_id', '=', $this->once_card->once_card_id], |
|
|
|
['is_delete', '=', 0] |
|
|
|
])->select()->toArray(); |
|
|
|
if (empty($items)) $this->error('次卡错误'); |
|
|
|
|
|
|
|
$item_data = []; |
|
|
|
foreach ($items as $k => $item) { |
|
|
|
if (null === $project = ProjectModel::find($item['project_id'])) $this->error('项目不存在'); |
|
|
|
$item_data[] = [ |
|
|
|
'member_id' => $this->member->uid, |
|
|
|
'once_card_order_id' => $order_model->once_card_order_id, |
|
|
|
'once_card_item_id' => $item['once_card_item_id'], |
|
|
|
'project_id' => $item['project_id'], |
|
|
|
'full_name' => $project->full_name, |
|
|
|
'abbreviation' => $project->abbreviation, |
|
|
|
'market_price' => $project->market_price, |
|
|
|
'cover_img' => $project->cover_img, |
|
|
|
'total_num' => $item['num'], |
|
|
|
'add_ip' => $params['ip'], |
|
|
|
'add_time' => $_SERVER['REQUEST_TIME'], |
|
|
|
]; |
|
|
|
} |
|
|
|
|
|
|
|
$order_item_model = new OnceCardOrderItemModel(); |
|
|
|
$order_item_model->saveAll($item_data); |
|
|
|
|
|
|
|
if ($params['is_coin'] == 1) { |
|
|
|
//扣除币 |
|
|
|
//增加币记录 |
|
|
|
$CoinLog = new CoinLog(); |
|
|
|
$CoinLog->operate($params['member_id'], $this->once_card->coin, 8, $order_model->once_card_order_id, 0); |
|
|
|
} |
|
|
|
|
|
|
|
//券记录 |
|
|
|
if (!empty($coupon_id)) { |
|
|
|
$res1 = $couponServices->useCoupon($coupon_id); |
|
|
|
if (!$res1) { |
|
|
|
$this->error('使用优惠劵失败!'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if ($need_pay == '0' || $need_pay == '0.00') { |
|
|
|
//支付金额为0,成功后,办理卡 |
|
|
|
$this->completePayAct($order_model->once_card_order_id, $params['ip']); |
|
|
|
return $order_model->once_card_order_id; |
|
|
|
} |
|
|
|
|
|
|
|
$orderId = $order_model->once_card_order_id; |
|
|
|
$order_model = new OnceCardOrderModel(); |
|
|
|
$order = $order_model->where('once_card_order_id', $orderId)->find(); |
|
|
|
$payType = $params['payType']; |
|
|
|
$uid = $params['member_id']; |
|
|
|
if ($orderId) { |
|
|
|
$orderInfo = $order->toArray(); |
|
|
|
$info = compact('orderId'); |
|
|
|
switch ($payType) { |
|
|
|
case PayServices::WEIXIN_PAY: |
|
|
|
/** @var OrderPayServices $payServices */ |
|
|
|
$payServices = app()->make(OrderPayServices::class); |
|
|
|
$from = 'weixin'; |
|
|
|
$info['jsConfig'] = $payServices->OnceCardOrderPay($orderInfo, $from); |
|
|
|
return $info; |
|
|
|
break; |
|
|
|
case PayServices::YUE_PAY: |
|
|
|
/** @var YuePayServices $yueServices */ |
|
|
|
$yueServices = app()->make(YuePayServices::class); |
|
|
|
$pay = $yueServices->OnceCardYueOrderPay($orderInfo, $uid); |
|
|
|
if ($pay['status'] === true) { |
|
|
|
//支付成功后,办理卡 |
|
|
|
$this->completePayAct($order_model->once_card_order_id, $params['ip']); |
|
|
|
return $order_model->once_card_order_id; |
|
|
|
} else { |
|
|
|
$this->error('余额支付失败'); |
|
|
|
} |
|
|
|
break; |
|
|
|
default: |
|
|
|
$this->error('payType参数错误'); |
|
|
|
break; |
|
|
|
} |
|
|
|
} else { |
|
|
|
$this->error('订单生成失败!'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 核销订单生成核销码 |
|
|
|
* @return false|string |
|
|
|
*/ |
|
|
|
public function getStoreCode($order_model) |
|
|
|
{ |
|
|
|
mt_srand(); |
|
|
|
list($msec, $sec) = explode(' ', microtime()); |
|
|
|
$num = $_SERVER['REQUEST_TIME'] + mt_rand(10, 999999) . '' . substr($msec, 2, 3);//生成随机数 |
|
|
|
if (strlen($num) < 12) |
|
|
|
$num = str_pad((string)$num, 12, 0, STR_PAD_RIGHT); |
|
|
|
else |
|
|
|
$num = substr($num, 0, 12); |
|
|
|
if ($order_model->where(['verify_code' => $num])->value('once_card_order_id')) { |
|
|
|
return $this->getStoreCode($order_model); |
|
|
|
} |
|
|
|
return $num; |
|
|
|
} |
|
|
|
|
|
|
|
public function getNewOrderId(string $prefix = 'wx') |
|
|
|
{ |
|
|
|
$snowflake = new \Godruoyi\Snowflake\Snowflake(); |
|
|
|
$is_callable = function ($currentTime) { |
|
|
|
// if (is_win()) { |
|
|
|
$redis = Cache::store('redis'); |
|
|
|
$swooleSequenceResolver = new \Godruoyi\Snowflake\RedisSequenceResolver($redis->handler()); |
|
|
|
return $swooleSequenceResolver->sequence($currentTime); |
|
|
|
// } else { |
|
|
|
// $swooleSequenceResolver = new \Godruoyi\Snowflake\SwooleSequenceResolver(); |
|
|
|
// return $swooleSequenceResolver->sequence($currentTime); |
|
|
|
// } |
|
|
|
}; |
|
|
|
//32位 |
|
|
|
if (PHP_INT_SIZE == 4) { |
|
|
|
$id = abs($snowflake->setSequenceResolver($is_callable)->id()); |
|
|
|
} else { |
|
|
|
$id = $snowflake->setStartTimeStamp(strtotime('2020-06-05') * 1000)->setSequenceResolver($is_callable)->id(); |
|
|
|
} |
|
|
|
return $prefix . $id; |
|
|
|
} |
|
|
|
|
|
|
|
public function completePayAct($once_card_order_id, $ip, $notify = []) |
|
|
|
{ |
|
|
|
if (!empty($notify)) { |
|
|
|
//判断传来的订单 |
|
|
|
$this->checkOrder($once_card_order_id, true, $notify['out_trade_no']); |
|
|
|
// $once_card_order_id = $this->order->once_card_order_id; |
|
|
|
} else { |
|
|
|
//判断传来的订单 |
|
|
|
$this->checkOrder($once_card_order_id, true); |
|
|
|
} |
|
|
|
if (!empty($notify)) { |
|
|
|
//支付回调成功 |
|
|
|
$this->order->is_paid = 1; |
|
|
|
$this->order->pay_time = $_SERVER['REQUEST_TIME']; |
|
|
|
$this->order->trade_no = $notify['transaction_id']; |
|
|
|
// $this->order->pay_price = $notify[''];//??? |
|
|
|
$this->order->pay_info = json_encode($notify); |
|
|
|
$this->order->save(); |
|
|
|
} |
|
|
|
//计算有有效时间 |
|
|
|
if ($this->order->valid_unit == 'day') $valid_unit = 86400; |
|
|
|
if ($this->order->valid_unit == 'month') $valid_unit = 86400 * 30; |
|
|
|
if ($this->order->valid_unit == 'year') $valid_unit = 86400 * 365; |
|
|
|
$valid_end_time = date('Y-m-d', $_SERVER['REQUEST_TIME'] + ($valid_unit * $this->order->valid_time)); |
|
|
|
|
|
|
|
//处理我的次卡 |
|
|
|
$member_once_data_data = [ |
|
|
|
'member_id' => $this->member->uid, |
|
|
|
'once_card_order_id' => $this->order->once_card_order_id, |
|
|
|
'once_card_id' => $this->once_card->once_card_id, |
|
|
|
'name' => $this->order->name, |
|
|
|
'cover_img' => $this->order->cover_img, |
|
|
|
'valid_end_time' => $valid_end_time, |
|
|
|
'total_price' => $this->order->total_price, |
|
|
|
'member_coupon_id' => $this->order->member_coupon_id, |
|
|
|
'coupon_money' => $this->order->coupon_money, |
|
|
|
'use_integral' => $this->order->use_integral, |
|
|
|
'integral_balance' => $this->order->integral_balance, |
|
|
|
'youhui_balance' => $this->order->youhui_balance, |
|
|
|
'need_pay' => $this->order->need_pay, |
|
|
|
'pay_type' => $this->order->pay_type, |
|
|
|
'pay_time' => $this->order->pay_time, |
|
|
|
'pay_info' => $this->order->pay_info, |
|
|
|
'add_time' => $_SERVER['REQUEST_TIME'], |
|
|
|
'add_ip' => $ip, |
|
|
|
'is_paid' => 1, |
|
|
|
]; |
|
|
|
$member_once_card_model = new MemberOnceCardModel(); |
|
|
|
$member_once_card_model->save($member_once_data_data); |
|
|
|
|
|
|
|
//处理我的次卡项目 |
|
|
|
$items = OnceCardOrderItemModel::where([ |
|
|
|
['once_card_order_id', '=', $this->order->once_card_order_id], |
|
|
|
])->select()->toArray(); |
|
|
|
if (empty($items)) $this->error('订单出错啦'); |
|
|
|
|
|
|
|
foreach ($items as $k => $item) { |
|
|
|
//处理次卡项目 |
|
|
|
$item_data = [ |
|
|
|
'member_id' => $this->member->uid, |
|
|
|
'member_once_card_id' => $member_once_card_model->member_once_card_id, |
|
|
|
'project_id' => $item['project_id'], |
|
|
|
'full_name' => $item['full_name'], |
|
|
|
'abbreviation' => $item['abbreviation'], |
|
|
|
'market_price' => $item['market_price'], |
|
|
|
'cover_img' => $item['cover_img'], |
|
|
|
'total_num' => $item['total_num'], |
|
|
|
'remain_num' => $item['total_num'], |
|
|
|
'add_time' => $_SERVER['REQUEST_TIME'], |
|
|
|
'add_ip' => $ip, |
|
|
|
]; |
|
|
|
$member_item_model = new MemberOnceCardItemModel(); |
|
|
|
$member_item_model->save($item_data); |
|
|
|
|
|
|
|
//添加项目销量 |
|
|
|
if (null === $project = ProjectModel::find($item['project_id'])) $this->error('项目不存在'); |
|
|
|
$project->take_count = $project->take_count + $item['total_num']; |
|
|
|
$project->save(); |
|
|
|
|
|
|
|
//处理我的项目 |
|
|
|
$member_project_data = [ |
|
|
|
'member_id' => $this->member->uid, |
|
|
|
'source' => 'once_card', |
|
|
|
'project_id' => $project->project_id, |
|
|
|
'member_once_card_item_id' => $member_item_model->member_once_card_item_id, |
|
|
|
'total_num' => $item['total_num'], |
|
|
|
'remain_num' => $item['total_num'], |
|
|
|
'valid_end_time' => $valid_end_time, |
|
|
|
'add_time' => $_SERVER['REQUEST_TIME'], |
|
|
|
'add_ip' => $ip, |
|
|
|
]; |
|
|
|
$member_project_model = new MemberProjectModel(); |
|
|
|
$member_project_model->save($member_project_data); |
|
|
|
} |
|
|
|
|
|
|
|
//添加办卡记录 |
|
|
|
$res = ApplyServiceLogic::apply($this->member->uid, 'once_card', $this->order->name, $member_once_card_model->member_once_card_id, $this->order->need_pay, $this->order->total_price, 0, 0); |
|
|
|
if (!$res) $this->error('支付失败'); |
|
|
|
} |
|
|
|
|
|
|
|
public function cancelOrder($params) |
|
|
|
{ |
|
|
|
//判断用户 |
|
|
|
empty($params['member_id']) && $this->error('请先登录后再操作'); |
|
|
|
$this->checkMember($params['member_id'], false); |
|
|
|
|
|
|
|
//判断传来的订单 |
|
|
|
$once_card_order_id = (int)$params['once_card_order_id']; |
|
|
|
$this->checkOrder($once_card_order_id, true); |
|
|
|
if ($this->order->member_id != $this->member->uid) $this->error('订单不存在'); |
|
|
|
if ($this->order->is_paid != 0) $this->error('订单不存在'); |
|
|
|
if ($this->order->is_delete == 1) $this->error('订单不在未支付状态'); |
|
|
|
|
|
|
|
//判断次卡 |
|
|
|
$this->checkOnceCard($this->order->once_card_id, true); |
|
|
|
|
|
|
|
//处理订单 |
|
|
|
$this->order->is_delete = 1; |
|
|
|
$this->order->save(); |
|
|
|
|
|
|
|
//如果是秒杀取消就释放库存 |
|
|
|
if ($this->order->is_flash_sale == 1) { |
|
|
|
$this->once_card->flash_num = $this->once_card->flash_num + 1; |
|
|
|
$this->once_card->fictitious_take_count = $this->once_card->fictitious_take_count - 1 > 0 ? $this->once_card->fictitious_take_count - 1 : 0; |
|
|
|
$this->once_card->take_count = $this->once_card->take_count - 1; |
|
|
|
$this->once_card->save(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public function checkOnceCard($once_card_id, $lock) |
|
|
|
{ |
|
|
|
if (null === $this->once_card = OnceCardModel::lock($lock)->find($once_card_id)) $this->error('次卡不存在'); |
|
|
|
} |
|
|
|
|
|
|
|
public function checkOrder($once_card_order_id, $lock, $out_trade_no = '') |
|
|
|
{ |
|
|
|
if (empty($out_trade_no)) { |
|
|
|
if (null === $this->order = OnceCardOrderModel::lock($lock)->find($once_card_order_id)) $this->error('订单不存在'); |
|
|
|
} else { |
|
|
|
if (null === $this->order = OnceCardOrderModel::where('order_id', $out_trade_no)::lock($lock)->find()) $this->error('订单不存在'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public function checkFlashStatus() |
|
|
|
{ |
|
|
|
return ($this->once_card->is_flash_sale == 1 && $this->once_card->is_flash_sale_expire == 0); |
|
|
|
} |
|
|
|
|
|
|
|
// public function apply($params) |
|
|
|
// { |
|
|
|
// //判断其合法性 |
|
|
|
// $operate_type = (int)$params['operate_type']; |
|
|
|
// $operate_id = (int)$params['operate_id']; |
|
|
|
// if (!in_array($operate_type, [1, 2])) $this->error('参数错误'); |
|
|
|
// |
|
|
|
// //判断用户 |
|
|
|
// $this->checkMember($params['member_id'], false); |
|
|
|
// |
|
|
|
// $once_card_id = (int)$params['once_card_id']; |
|
|
|
// if ($once_card_id == 0) $this->error('次卡不存在'); |
|
|
|
// //判断抢购 |
|
|
|
// $this->checkOnceCard($once_card_id, true); |
|
|
|
// if ($this->once_card->is_delete != 0) $this->error('次卡不存在'); |
|
|
|
// if ($this->once_card->status != 1) $this->error('次卡已下架'); |
|
|
|
// |
|
|
|
// //秒杀活动时间内判断数量 |
|
|
|
// if ($flash_status = $this->checkFlashStatus()) { |
|
|
|
// if ($this->once_card->flash_num < 1) $this->error('秒杀数量不足够'); |
|
|
|
// } |
|
|
|
// |
|
|
|
// //判断总价 |
|
|
|
// $total_price = $price = $flash_status ? $this->once_card->flash_price : $this->once_card->price; |
|
|
|
// if (empty($params['total_price'])) $this->error('参数错误#1'); |
|
|
|
// if ((float)$params['total_price'] != $total_price) $this->error('参数错误#2'); |
|
|
|
// |
|
|
|
// //得到实际支付的金额 |
|
|
|
// $need_pay = (float)$params['need_pay']; |
|
|
|
// |
|
|
|
// //获取优惠金额 |
|
|
|
// $youhui_balance = round((($total_price) * 100 - $need_pay * 100) / 100, 2); |
|
|
|
// $youhui_balance = $youhui_balance > 0 ? $youhui_balance : 0; |
|
|
|
// |
|
|
|
// |
|
|
|
// //计算有有效时间 |
|
|
|
// if ($this->once_card->valid_unit == 'day') $valid_unit = 86400; |
|
|
|
// if ($this->once_card->valid_unit == 'month') $valid_unit = 86400 * 30; |
|
|
|
// if ($this->once_card->valid_unit == 'year') $valid_unit = 86400 * 365; |
|
|
|
// $valid_end_time = date('Y-m-d', time() + ($valid_unit * $this->once_card->valid_time)); |
|
|
|
// |
|
|
|
// //处理次卡 |
|
|
|
// $member_once_card_model = new MemberOnceCardModel(); |
|
|
|
// $member_once_card_model->save([ |
|
|
|
// 'shop_id' => $this->shopId, |
|
|
|
// 'member_id' => $this->member->uid, |
|
|
|
// 'once_card_id' => $this->once_card->once_card_id, |
|
|
|
// 'name' => $this->once_card->name, |
|
|
|
// 'cover_img' => $this->once_card->cover_img, |
|
|
|
// 'valid_end_time' => $valid_end_time, |
|
|
|
// 'total_price' => $total_price, |
|
|
|
// 'need_pay' => $need_pay, |
|
|
|
// 'youhui_balance' => $youhui_balance, |
|
|
|
// 'pay_type' => 'offline', |
|
|
|
// 'pay_time' => time(), |
|
|
|
// ]); |
|
|
|
// |
|
|
|
// if ($flash_status) { |
|
|
|
// $this->once_card->flash_num = $this->once_card->flash_num - 1; |
|
|
|
// $this->once_card->fictitious_take_count = $this->once_card->fictitious_take_count + 1; |
|
|
|
// $this->once_card->take_count = $this->once_card->take_count + 1; |
|
|
|
// $this->once_card->save(); |
|
|
|
// } |
|
|
|
// |
|
|
|
// //处理次卡项目 |
|
|
|
// $items = OnceCardItemModel::where([ |
|
|
|
// ['shop_id', '=', $this->shopId], |
|
|
|
// ['once_card_id', '=', $this->once_card->once_card_id], |
|
|
|
// ['is_delete', '=', 0] |
|
|
|
// ])->select()->toArray(); |
|
|
|
// if (empty($items)) $this->error('次卡错误'); |
|
|
|
// |
|
|
|
// |
|
|
|
// foreach ($items as $k => $item) { |
|
|
|
// //添加项目销量 |
|
|
|
// if (null === $project = ProjectModel::find($item['project_id'])) $this->error('项目不存在'); |
|
|
|
// if ($project->shop_id != $this->shopId) $this->error('项目不存在'); |
|
|
|
// $project->take_count = $project->take_count + $item['num']; |
|
|
|
// $project->save(); |
|
|
|
// |
|
|
|
// $item_data = [ |
|
|
|
// 'shop_id' => $this->shopId, |
|
|
|
// 'member_id' => $this->member->uid, |
|
|
|
// 'member_once_card_id' => $member_once_card_model->member_once_card_id, |
|
|
|
// 'project_id' => $item['project_id'], |
|
|
|
// 'full_name' => $project->full_name, |
|
|
|
// 'abbreviation' => $project->abbreviation, |
|
|
|
// 'market_price' => $project->market_price, |
|
|
|
// 'cover_img' => $project->cover_img, |
|
|
|
// 'total_num' => $item['num'], |
|
|
|
// 'remain_num' => $item['num'], |
|
|
|
// ]; |
|
|
|
// $member_item_model = new MemberOnceCardItemModel(); |
|
|
|
// $member_item_model->save($item_data); |
|
|
|
// |
|
|
|
// //处理我的项目 |
|
|
|
// $member_project_data = [ |
|
|
|
// 'shop_id' => $this->shopId, |
|
|
|
// 'member_id' => $this->member->uid, |
|
|
|
// 'source' => 'once_card', |
|
|
|
// 'project_id' => $project->project_id, |
|
|
|
// 'member_once_card_item_id' => $member_item_model->member_once_card_item_id, |
|
|
|
// 'total_num' => $item['num'], |
|
|
|
// 'remain_num' => $item['num'], |
|
|
|
// 'valid_end_time' => $valid_end_time, |
|
|
|
// ]; |
|
|
|
// $member_project_model = new MemberProjectModel(); |
|
|
|
// $member_project_model->save($member_project_data); |
|
|
|
// } |
|
|
|
// |
|
|
|
// //添加办卡记录 |
|
|
|
// $res = ApplyServiceLogic::apply($this->shopId, $this->member->uid, 'once_card', $this->once_card->name, $member_once_card_model->member_once_card_id, $need_pay, $total_price, $operate_type, $operate_id); |
|
|
|
// if (!$res) $this->error('办理失败'); |
|
|
|
// |
|
|
|
// } |
|
|
|
// |
|
|
|
// public function sendOnceCard($params) |
|
|
|
// { |
|
|
|
// //判断用户 |
|
|
|
// $this->checkMember($params['member_id'], false); |
|
|
|
// |
|
|
|
// $once_card_id = (int)$params['once_card_id']; |
|
|
|
// if ($once_card_id == 0) $this->error('次卡不存在'); |
|
|
|
// //判断抢购 |
|
|
|
// $this->checkOnceCard($once_card_id, true); |
|
|
|
// if ($this->once_card->is_delete != 0) $this->error('次卡不存在'); |
|
|
|
// if ($this->once_card->status != 1) $this->error('次卡已下架'); |
|
|
|
// |
|
|
|
// //秒杀活动时间内判断数量 |
|
|
|
// $flash_status = $this->checkFlashStatus(); |
|
|
|
// |
|
|
|
// //判断总价 |
|
|
|
// $total_price = $price = $flash_status ? $this->once_card->flash_price : $this->once_card->price; |
|
|
|
// |
|
|
|
// //得到实际支付的金额 |
|
|
|
// $need_pay = 0; |
|
|
|
// |
|
|
|
// //获取优惠金额 |
|
|
|
// $youhui_balance = round((($total_price) * 100 - $need_pay * 100) / 100, 2); |
|
|
|
// $youhui_balance = $youhui_balance > 0 ? $youhui_balance : 0; |
|
|
|
// |
|
|
|
// |
|
|
|
// //计算有有效时间 |
|
|
|
// if ($this->once_card->valid_unit == 'day') $valid_unit = 86400; |
|
|
|
// if ($this->once_card->valid_unit == 'month') $valid_unit = 86400 * 30; |
|
|
|
// if ($this->once_card->valid_unit == 'year') $valid_unit = 86400 * 365; |
|
|
|
// $valid_end_time = date('Y-m-d', time() + ($valid_unit * $this->once_card->valid_time)); |
|
|
|
// |
|
|
|
// //处理次卡 |
|
|
|
// $member_once_card_model = new MemberOnceCardModel(); |
|
|
|
// $member_once_card_model->save([ |
|
|
|
// 'shop_id' => $this->shopId, |
|
|
|
// 'member_id' => $this->member->uid, |
|
|
|
// 'once_card_id' => $this->once_card->once_card_id, |
|
|
|
// 'name' => $this->once_card->name, |
|
|
|
// 'cover_img' => $this->once_card->cover_img, |
|
|
|
// 'valid_end_time' => $valid_end_time, |
|
|
|
// 'total_price' => $total_price, |
|
|
|
// 'need_pay' => $need_pay, |
|
|
|
// 'youhui_balance' => $youhui_balance, |
|
|
|
// 'pay_type' => 'offline', |
|
|
|
// 'pay_time' => time(), |
|
|
|
// ]); |
|
|
|
// |
|
|
|
// //处理次卡项目 |
|
|
|
// $items = OnceCardItemModel::where([ |
|
|
|
// ['shop_id', '=', $this->shopId], |
|
|
|
// ['once_card_id', '=', $this->once_card->once_card_id], |
|
|
|
// ['is_delete', '=', 0] |
|
|
|
// ])->select()->toArray(); |
|
|
|
// if (empty($items)) $this->error('次卡错误'); |
|
|
|
// |
|
|
|
// |
|
|
|
// foreach ($items as $k => $item) { |
|
|
|
// //添加项目销量 |
|
|
|
// if (null === $project = ProjectModel::find($item['project_id'])) $this->error('项目不存在'); |
|
|
|
// if ($project->shop_id != $this->shopId) $this->error('项目不存在'); |
|
|
|
// |
|
|
|
// $item_data = [ |
|
|
|
// 'shop_id' => $this->shopId, |
|
|
|
// 'member_id' => $this->member->uid, |
|
|
|
// 'member_once_card_id' => $member_once_card_model->member_once_card_id, |
|
|
|
// 'project_id' => $item['project_id'], |
|
|
|
// 'full_name' => $project->full_name, |
|
|
|
// 'abbreviation' => $project->abbreviation, |
|
|
|
// 'market_price' => $project->market_price, |
|
|
|
// 'cover_img' => $project->cover_img, |
|
|
|
// 'total_num' => $item['num'], |
|
|
|
// 'remain_num' => $item['num'], |
|
|
|
// ]; |
|
|
|
// $member_item_model = new MemberOnceCardItemModel(); |
|
|
|
// $member_item_model->save($item_data); |
|
|
|
// |
|
|
|
// //处理我的项目 |
|
|
|
// $member_project_data = [ |
|
|
|
// 'shop_id' => $this->shopId, |
|
|
|
// 'member_id' => $this->member->uid, |
|
|
|
// 'source' => 'once_card', |
|
|
|
// 'project_id' => $project->project_id, |
|
|
|
// 'member_once_card_item_id' => $member_item_model->member_once_card_item_id, |
|
|
|
// 'total_num' => $item['num'], |
|
|
|
// 'remain_num' => $item['num'], |
|
|
|
// 'valid_end_time' => $valid_end_time, |
|
|
|
// ]; |
|
|
|
// $member_project_model = new MemberProjectModel(); |
|
|
|
// $member_project_model->save($member_project_data); |
|
|
|
// } |
|
|
|
// |
|
|
|
// //添加办卡记录 |
|
|
|
// $res = ApplyServiceLogic::apply($this->shopId, $this->member->uid, 'once_card', $this->once_card->name, $member_once_card_model->member_once_card_id, $need_pay, $total_price, 0, 0); |
|
|
|
// if (!$res) $this->error('办理失败'); |
|
|
|
// } |
|
|
|
// |
|
|
|
// protected function completePayAct($type, $payinfo = []) |
|
|
|
// { |
|
|
|
// $this->order->is_paid = 1; |
|
|
|
// $this->order->is_delete = 1; |
|
|
|
// $this->order->pay_time = time(); |
|
|
|
// $this->order->pay_type = $type; |
|
|
|
// $this->order->pay_info = $payinfo; |
|
|
|
// $this->order->save(); |
|
|
|
// |
|
|
|
// //如果不是秒杀这时候要去添加销量 |
|
|
|
// if ($this->order->is_flash_sale == 0) { |
|
|
|
// $this->once_card->fictitious_take_count = $this->once_card->fictitious_take_count + 1; |
|
|
|
// $this->once_card->take_count = $this->once_card->take_count + 1; |
|
|
|
// $this->once_card->save(); |
|
|
|
// } |
|
|
|
// |
|
|
|
// //计算有有效时间 |
|
|
|
// if ($this->order->valid_unit == 'day') $valid_unit = 86400; |
|
|
|
// if ($this->order->valid_unit == 'month') $valid_unit = 86400 * 30; |
|
|
|
// if ($this->order->valid_unit == 'year') $valid_unit = 86400 * 365; |
|
|
|
// $valid_end_time = date('Y-m-d', time() + ($valid_unit * $this->order->valid_time)); |
|
|
|
// |
|
|
|
// |
|
|
|
// //处理我的次卡 |
|
|
|
// $member_once_data_data = [ |
|
|
|
// 'shop_id' => $this->shopId, |
|
|
|
// 'member_id' => $this->member->uid, |
|
|
|
// 'once_card_order_id' => $this->order->once_card_order_id, |
|
|
|
// 'once_card_id' => $this->once_card->once_card_id, |
|
|
|
// 'name' => $this->order->name, |
|
|
|
// 'cover_img' => $this->order->cover_img, |
|
|
|
// 'valid_end_time' => $valid_end_time, |
|
|
|
// 'market_price' => $this->order->market_price, |
|
|
|
// 'total_price' => $this->order->total_price, |
|
|
|
// 'member_coupon_id' => $this->order->member_coupon_id, |
|
|
|
// 'coupon_money' => $this->order->coupon_money, |
|
|
|
// 'use_integral' => $this->order->use_integral, |
|
|
|
// 'integral_balance' => $this->order->integral_balance, |
|
|
|
// 'youhui_balance' => $this->order->youhui_balance, |
|
|
|
// 'need_pay' => $this->order->need_pay, |
|
|
|
// 'pay_type' => $this->order->pay_type, |
|
|
|
// 'pay_time' => $this->order->pay_time, |
|
|
|
// 'pay_info' => $this->order->pay_info, |
|
|
|
// ]; |
|
|
|
// $member_once_card_model = new MemberOnceCardModel(); |
|
|
|
// $member_once_card_model->save($member_once_data_data); |
|
|
|
// |
|
|
|
// //处理我的次卡项目 |
|
|
|
// $items = OnceCardOrderItemModel::where([ |
|
|
|
// ['shop_id', '=', $this->shopId], |
|
|
|
// ['once_card_order_id', '=', $this->order->once_card_order_id], |
|
|
|
// ])->select()->toArray(); |
|
|
|
// if (empty($items)) $this->error('订单出错啦'); |
|
|
|
// |
|
|
|
// foreach ($items as $k => $item) { |
|
|
|
// //处理次卡项目 |
|
|
|
// $item_data = [ |
|
|
|
// 'shop_id' => $this->shopId, |
|
|
|
// 'member_id' => $this->member->uid, |
|
|
|
// 'member_once_card_id' => $member_once_card_model->member_once_card_id, |
|
|
|
// 'project_id' => $item['project_id'], |
|
|
|
// 'full_name' => $item['full_name'], |
|
|
|
// 'abbreviation' => $item['abbreviation'], |
|
|
|
// 'market_price' => $item['market_price'], |
|
|
|
// 'cover_img' => $item['cover_img'], |
|
|
|
// 'total_num' => $item['total_num'], |
|
|
|
// 'remain_num' => $item['total_num'], |
|
|
|
// ]; |
|
|
|
// $member_item_model = new MemberOnceCardItemModel(); |
|
|
|
// $member_item_model->save($item_data); |
|
|
|
// |
|
|
|
// |
|
|
|
// //添加项目销量 |
|
|
|
// if (null === $project = ProjectModel::find($item['project_id'])) $this->error('项目不存在'); |
|
|
|
// if ($project->shop_id != $this->shopId) $this->error('项目不存在'); |
|
|
|
// $project->take_count = $project->take_count + $item['total_num']; |
|
|
|
// $project->save(); |
|
|
|
// |
|
|
|
// //处理我的项目 |
|
|
|
// $member_project_data = [ |
|
|
|
// 'shop_id' => $this->shopId, |
|
|
|
// 'member_id' => $this->member->uid, |
|
|
|
// 'source' => 'once_card', |
|
|
|
// 'project_id' => $project->project_id, |
|
|
|
// 'member_once_card_item_id' => $member_item_model->member_once_card_item_id, |
|
|
|
// 'total_num' => $item['total_num'], |
|
|
|
// 'remain_num' => $item['total_num'], |
|
|
|
// 'valid_end_time' => $valid_end_time, |
|
|
|
// ]; |
|
|
|
// $member_project_model = new MemberProjectModel(); |
|
|
|
// $member_project_model->save($member_project_data); |
|
|
|
// } |
|
|
|
// |
|
|
|
// //添加办卡记录 |
|
|
|
// $res = ApplyServiceLogic::apply($this->shopId, $this->member->uid, 'once_card', $this->order->name, $member_once_card_model->member_once_card_id, $this->order->need_pay, $this->order->total_price, 0, 0); |
|
|
|
// if (!$res) $this->error('支付失败'); |
|
|
|
// |
|
|
|
// |
|
|
|
// } |
|
|
|
// |
|
|
|
// protected function checkOrderPayStatus($params) |
|
|
|
// { |
|
|
|
// //判断用户 |
|
|
|
// empty($params['member_id']) && $this->error('请先登录后再操作'); |
|
|
|
// $this->checkMember($params['member_id'], false); |
|
|
|
// |
|
|
|
// //判断传来的订单 |
|
|
|
// $once_card_order_id = (int)$params['once_card_order_id']; |
|
|
|
// $this->checkOrder($once_card_order_id, true); |
|
|
|
// if ($this->order->member_id != $this->member->uid) $this->error('订单不存在'); |
|
|
|
// if ($this->order->is_paid != 0) $this->error('订单不存在'); |
|
|
|
// if ($this->order->is_delete == 1) $this->error('订单不在未支付状态'); |
|
|
|
// if ($this->order->add_time < (time() - 900)) $this->error('订单支付已经超时了,不能继续支付'); |
|
|
|
// |
|
|
|
// //判断抢购 |
|
|
|
// $this->checkOnceCard($this->order->once_card_id, true); |
|
|
|
// |
|
|
|
// |
|
|
|
// //处理数据 |
|
|
|
// if (!empty($this->order->member_coupon_id)) { |
|
|
|
// if (null === $myCoupon = MemberCouponModel::find($this->order->member_coupon_id)) $this->error('优惠券不存在'); |
|
|
|
// if ($myCoupon->status == 1) $this->error('优惠券不存在'); |
|
|
|
// |
|
|
|
// $myCoupon->status = 1; |
|
|
|
// $myCoupon->save(); |
|
|
|
// LogsLogic::coupon($this->shopId, $this->member->uid, -1, 15, $this->order->member_coupon_id, '', 0); |
|
|
|
// } |
|
|
|
// $MemberWalletLogic = new MemberWalletLogic($this->shopId, $this->member->uid); |
|
|
|
// if (!empty($order->use_integral)) { //使用积分 |
|
|
|
// $res = $MemberWalletLogic->useIntegral($order->use_integral, 12); |
|
|
|
// if ($res == false) $this->error($MemberWalletLogic->getError()); |
|
|
|
// } |
|
|
|
// $needPay = round(($this->order->total_price * 100 - $this->order->coupon_money * 100 - $this->order->integral_balance * 100) / 100, 2); |
|
|
|
// if ($needPay <= 0 or $needPay != $this->order->need_pay) $this->error('支付金额不正确'); |
|
|
|
// } |
|
|
|
// |
|
|
|
// public function getIntegralBalance($total_price) |
|
|
|
// { |
|
|
|
// $SettingBalance = SettingBalanceModel::where([['shop_id', '=', $this->shopId]])->find(); |
|
|
|
// if (null === $SettingBalance) { |
|
|
|
// $integral_deduction_balance_limit_rate = 0; |
|
|
|
// } else { |
|
|
|
// $integral_deduction_balance_limit_rate = $SettingBalance->integral_deduction_balance_limit_rate; //积分抵扣的比率 百分比 |
|
|
|
// } |
|
|
|
// $integralSetting = SettingIntegralModel::where([['shop_id', '=', $this->shopId]])->find(); |
|
|
|
// if ($integralSetting === null) { //如果是空 代表不使用积分抵扣余额 |
|
|
|
// $integral_exchange_balance = 0; |
|
|
|
// } else { |
|
|
|
// $integral_exchange_balance = $integralSetting->integral_exchange_balance;//汇率 |
|
|
|
// } |
|
|
|
// |
|
|
|
// $pre_can_exchange_balance = round(($total_price * ($integral_deduction_balance_limit_rate * 100)) / 10000, 2); |
|
|
|
// $pre_can_exchange_integral = $integral_exchange_balance == 0 ? 0 : round(($pre_can_exchange_balance * 10000) / ($integral_exchange_balance * 100), 0); |
|
|
|
// $member_integral = $this->member->integral; |
|
|
|
// if ($member_integral >= $pre_can_exchange_integral) { |
|
|
|
// $return = [ |
|
|
|
// 'integral' => $pre_can_exchange_integral, |
|
|
|
// 'balance' => $pre_can_exchange_balance |
|
|
|
// ]; |
|
|
|
// } else { |
|
|
|
// $return = [ |
|
|
|
// 'integral' => $member_integral, |
|
|
|
// 'balance' => round(($member_integral * ($integral_exchange_balance * 100)) / 10000, 2), |
|
|
|
// ]; |
|
|
|
// } |
|
|
|
// return $return; |
|
|
|
// |
|
|
|
// } |
|
|
|
|
|
|
|
} |