123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\index\model;
- use app\common\library\paypal\PayPal;
- use app\index\service\order\PaySuccess;
- use app\index\model\{Goods as GoodsModel, OrderRefund as OrderRefundModel, Setting as SettingModel};
- use app\index\service\{User as UserService, Payment as PaymentService};
- use app\index\service\order\{PaySuccess as OrderPaySuccesService, source\Factory as OrderSourceFactory};
- use app\index\model\User as UserModel;
- use app\common\model\Order as OrderModel;
- use app\common\service\{Order as OrderService, order\Complete as OrderCompleteService};
- use app\common\enum\{
- Setting as SettingEnum,
- OrderType as OrderTypeEnum,
- order\PayType as OrderPayTypeEnum,
- order\PayStatus as PayStatusEnum,
- order\OrderStatus as OrderStatusEnum,
- // order\DeliveryType as DeliveryTypeEnum,
- order\ReceiptStatus as ReceiptStatusEnum,
- order\DeliveryStatus as DeliveryStatusEnum
- };
- use app\common\library\helper;
- use cores\exception\BaseException;
- use think\facade\Log;
- /**
- * 订单模型
- * Class Order
- * @package app\api\model
- */
- class Order extends OrderModel
- {
- /**
- * 隐藏字段
- * @var array
- */
- protected $hidden = [
- 'store_id',
- 'update_time'
- ];
- // 信息提示
- private $message = '';
- /**
- * 待支付订单详情
- * @param string $orderNo 订单号
- * @return null|static
- */
- public static function getPayDetail(string $orderNo): ?Order
- {
- return self::detail(['order_no' => $orderNo, 'pay_status' => PayStatusEnum::PENDING, 'is_delete' => 0], ['goods', 'user']);
- }
- /**
- * 订单支付事件
- * @param int $payType
- * @return bool
- */
- public function onPay(int $payType = OrderPayTypeEnum::WECHAT): bool
- {
- // 判断订单状态
- $orderSource = OrderSourceFactory::getFactory($this['order_source']);
- if (!$orderSource->checkOrderStatusOnPay($this)) {
- $this->error = $orderSource->getError();
- return false;
- }
- // 余额支付
- if ($payType == OrderPayTypeEnum::BALANCE) {
- return $this->onPaymentByBalance($this['order_no']);
- }
- return true;
- }
- /**
- * 构建支付请求的参数
- * @param self $order 订单信息
- * @param int $payType 订单支付方式
- * @return array
- * @throws BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function onOrderPayment(self $order, int $payType): array
- {
- if ($payType == OrderPayTypeEnum::WECHAT) {
- return $this->onPaymentByWechat($order);
- }
- if ($payType == OrderPayTypeEnum::PAYPAL) {
- return $this->onPaymentByPaypal($order);
- }
- //积分兑换
- if ($payType == OrderPayTypeEnum::POINTS) {
- $userInfo = UserService::getCurrentLoginUser();
- $points = $userInfo['points'];
- $payPoints = intval(bcmul(strval($order['pay_price']), '100', 0));//订单所需积分
- if (intval($points) < $payPoints) {
- return ['flag' => false, 'message' => '积分不够'];
- } else {
- $orderModel = new PaySuccess($order['order_no']);
- $transId = 'VP' . date('YmdHis') . $userInfo['user_id'];
- $describe = "用户消费:{$order['order_no']}";
- UserModel::setIncPoints($userInfo['user_id'], -$payPoints, $describe);
- $status = $orderModel->onPaySuccess(OrderPayTypeEnum::POINTS, ['transaction_id' => $transId]);
- if ($status) {
- return ['flag' => true, 'message' => 'success'];
- } else {
- Log::error('orderPayment:积分兑换error,userId:' . $userInfo['user_id'] . ',orderNo:' . $order['order_no']);
- return ['flag' => false, 'message' => 'error'];
- }
- }
- }
- return [];
- }
- /**
- * 构建微信支付请求
- * @param self $order 订单详情
- * @return array
- * @throws BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- protected function onPaymentByWechat(self $order): array
- {
- return PaymentService::wechat(
- $order['order_id'],
- $order['order_no'],
- $order['pay_price'],
- OrderTypeEnum::ORDER
- );
- }
- protected function onPaymentByPaypal(self $order): array
- {
- /* $conf = [
- 'client_id' => 'AS0FH780ZGtSAdpT1NTjwkFzryCPf69rZb_FR9Rt_rZdasB80cmjqTQ6CQELWiFVh_MU9e31CSnyz7Ai', // ClientID
- 'secret' => 'EDqRQhgLNHCb5bxld98T8-JJJZKvMIeqxudO7lMwDFOxBfy138PjM5A21FnDNyb3q4yYUh8r7Qr2BnVi', // ClientSecret
- 'web_hook_id' => '3NP026061E6858914',
- ];*/
- $conf = config('paypal');
- $pp = new PayPal($conf);
- return $pp->unify($order['order_no'], $order['pay_price']);
- }
- /**
- * 立即购买:获取订单商品列表
- * @param int $goodsId 商品ID
- * @param string $goodsSkuId 商品SKU
- * @param int $goodsNum 购买数量
- * @return mixed
- * @throws BaseException
- */
- public function getOrderGoodsListByNow(int $goodsId, string $goodsSkuId, int $goodsNum)
- {
- // 获取商品列表
- $model = new GoodsModel;
- $goodsList = $model->isGoodsGradeMoney(false)->getListByIdsFromApi([$goodsId]);
- if ($goodsList->isEmpty()) {
- throwError('未找到商品信息');
- }
- // 隐藏冗余的属性
- $goodsList->hidden(array_merge($model->hidden, ['content', 'goods_images', 'images']));
- foreach ($goodsList as &$item) {
- // 商品sku信息
- $goodsInfo['skuInfo'] = GoodsModel::getSkuInfo($item, $goodsSkuId, false);
- // 商品单价
- $item['goods_price'] = $item['skuInfo']['goods_price'];
- // 商品购买数量
- $item['total_num'] = $goodsNum;
- // 商品SKU索引
- $item['goods_sku_id'] = $item['skuInfo']['goods_sku_id'];
- // 商品购买总金额
- $item['total_price'] = helper::bcmul($item['goods_price'], $goodsNum);
- }
- return $goodsList;
- }
- /**
- * 余额支付标记订单已支付
- * @param string $orderNo 订单号
- * @return bool
- */
- public function onPaymentByBalance(string $orderNo): bool
- {
- // 获取订单详情
- $service = new OrderPaySuccesService($orderNo);
- // 发起余额支付
- $status = $service->onPaySuccess(OrderPayTypeEnum::BALANCE);
- if (!$status) {
- $this->error = $service->getError();
- }
- return $status;
- }
- /**
- * 获取用户订单列表
- * @param string $type 订单类型 (all全部 payment待付款 delivery待发货 received待收货 comment待评价)
- * @return \think\Paginator
- * @throws \think\db\exception\DbException
- * @throws BaseException
- */
- public function getList(string $type = 'all'): \think\Paginator
- {
- // 筛选条件
- $filter = [];
- // 订单数据类型
- switch ($type) {
- case 'all':
- break;
- case 'payment':
- $filter['pay_status'] = PayStatusEnum::PENDING;
- $filter['order_status'] = OrderStatusEnum::NORMAL;
- break;
- case 'delivery':
- $filter['pay_status'] = PayStatusEnum::SUCCESS;
- $filter['delivery_status'] = DeliveryStatusEnum::NOT_DELIVERED;
- $filter['order_status'] = OrderStatusEnum::NORMAL;
- break;
- case 'received':
- $filter['pay_status'] = PayStatusEnum::SUCCESS;
- $filter['delivery_status'] = DeliveryStatusEnum::DELIVERED;
- $filter['receipt_status'] = ReceiptStatusEnum::NOT_RECEIVED;
- $filter['order_status'] = OrderStatusEnum::NORMAL;
- break;
- case 'comment':
- $filter['is_comment'] = 0;
- $filter['order_status'] = OrderStatusEnum::COMPLETED;
- break;
- }
- // 当前用户ID
- $userId = UserService::getCurrentLoginUserId();
- // 查询列表数据
- return $this->with(['goods.image'])
- ->where($filter)
- ->where('user_id', '=', $userId)
- ->where('is_delete', '=', 0)
- ->order(['create_time' => 'desc'])
- ->paginate(15);
- }
- /**
- * 取消订单
- * @return bool|mixed
- */
- public function cancel()
- {
- if ($this['delivery_status'] == DeliveryStatusEnum::DELIVERED) {
- $this->error = 'Unsupported action.';
- return false;
- }
- // 订单是否已支付
- $isPay = $this['pay_status'] == PayStatusEnum::SUCCESS;
- // 提示信息
- $this->message = $isPay ? '订单已申请取消,需等待后台审核' : '订单已取消成功';
- // 订单取消事件
- return $this->transaction(function () use ($isPay) {
- // 订单取消事件
- $isPay == false && OrderService::cancelEvent($this);
- // 更新订单状态: 已付款的订单设置为"待取消", 等待后台审核
- return $this->save(['order_status' => $isPay ? OrderStatusEnum::APPLY_CANCEL : OrderStatusEnum::CANCELLED]);
- });
- }
- /**
- * 确认收货
- * @return bool|mixed
- */
- public function receipt()
- {
- // 验证订单是否合法
- // 条件1: 订单必须已发货
- // 条件2: 订单必须未收货
- if ($this['delivery_status'] != 20 || $this['receipt_status'] != 10) {
- $this->error = 'Unsupported actions.';
- return false;
- }
- return $this->transaction(function () {
- // 更新订单状态
- $status = $this->save([
- 'receipt_status' => 20,
- 'receipt_time' => time(),
- 'order_status' => 30
- ]);
- // 执行订单完成后的操作
- $OrderCompleteService = new OrderCompleteService();
- $OrderCompleteService->complete([$this], static::$storeId);
- return $status;
- });
- }
- /**
- * 获取当前用户订单数量
- * @param string $type 订单类型 (all全部 payment待付款 received待发货 deliver待收货 comment待评价)
- * @return int
- * @throws BaseException
- */
- public function getCount(string $type = 'all'): int
- {
- // 筛选条件
- $filter = [];
- // 订单数据类型
- switch ($type) {
- case 'all':
- break;
- case 'payment':
- $filter['pay_status'] = PayStatusEnum::PENDING;
- break;
- case 'received':
- $filter['pay_status'] = PayStatusEnum::SUCCESS;
- $filter['delivery_status'] = DeliveryStatusEnum::DELIVERED;
- $filter['receipt_status'] = ReceiptStatusEnum::NOT_RECEIVED;
- break;
- case 'delivery':
- $filter['pay_status'] = PayStatusEnum::SUCCESS;
- $filter['delivery_status'] = DeliveryStatusEnum::NOT_DELIVERED;
- $filter['order_status'] = OrderStatusEnum::NORMAL;
- break;
- case 'comment':
- $filter['is_comment'] = 0;
- $filter['order_status'] = OrderStatusEnum::COMPLETED;
- break;
- }
- // 当前用户ID
- $userId = UserService::getCurrentLoginUserId();
- // 查询数据
- return $this->where('user_id', '=', $userId)
- ->where('order_status', '<>', 20)
- ->where($filter)
- ->where('is_delete', '=', 0)
- ->count();
- }
- /**
- * 获取用户订单详情(含关联数据)
- * @param int $orderId 订单ID
- * @return Order|array|null
- * @throws BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function getUserOrderDetail(int $orderId)
- {
- // 关联查询
- $with = [
- 'goods' => ['image', 'goods', 'refund'],
- 'address', 'express'
- ];
- // 查询订单记录
- $order = static::getDetail($orderId, $with);
- // 该订单是否允许申请售后
- $order['isAllowRefund'] = static::isAllowRefund($order);
- return $order;
- }
- /**
- * 获取用户订单详情(仅订单记录)
- * @param int $orderId
- * @param array $with
- * @return Order|array|null
- * @throws BaseException
- */
- public static function getDetail(int $orderId, array $with = [])
- {
- // 查询订单记录
- $order = static::detail([
- 'order_id' => $orderId,
- 'user_id' => UserService::getCurrentLoginUserId(),
- ], $with);
- empty($order) && throwError('The order does not exist');
- return $order;
- }
- /**
- * 获取当前用户待处理的订单数量
- * @return array
- * @throws BaseException
- */
- public function getTodoCounts(): array
- {
- return [
- 'payment' => $this->getCount('payment'), // 待付款的订单
- 'delivery' => $this->getCount('delivery'), // 待发货的订单
- 'received' => $this->getCount('received'), // 待收货的订单
- 'refund' => OrderRefundModel::getCountByUnderway(), // 进行中的售后单
- ];
- }
- // 返回提示信息
- public function getMessage(): string
- {
- return $this->message;
- }
- /**
- * 当前订单是否允许申请售后
- * @param Order $order
- * @return bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- private static function isAllowRefund(self $order): bool
- {
- // 必须是已发货的订单
- if ($order['delivery_status'] != DeliveryStatusEnum::DELIVERED) {
- return false;
- }
- // 允许申请售后期限(天)
- $refundDays = SettingModel::getItem(SettingEnum::TRADE)['order']['refund_days'];
- // 不允许售后
- if ($refundDays == 0) {
- return false;
- }
- // 当前时间超出允许申请售后期限
- if (
- $order['receipt_status'] == ReceiptStatusEnum::RECEIVED
- && time() > ($order->getData('receipt_time') + ((int)$refundDays * 86400))
- ) {
- return false;
- }
- return true;
- }
- }
|