0, 'status' => self::STATUS_PENDING, ]; /** * @var array */ protected $casts = [ 'completed_at' => 'datetime', 'status' => 'int', ]; /** * @var array */ protected $fillable = [ 'user_id', 'sn', 'user_coupon_id', 'coupon_discount_amount', 'vip_discount_amount', 'reduced_amount', 'shipping_fee', 'products_total_amount', 'total_amount', 'note', 'remark', 'pay_sn', 'pay_way', 'pay_at', 'consignee_name', 'consignee_telephone', 'consignee_zone', 'consignee_address', 'shipping_state', 'status', 'completed_at', ]; /** * @var array */ public static $payWayTexts = [ self::PAY_WAY_ALIPAY => '支付宝', self::PAY_WAY_WXPAY => '微信支付', self::PAY_WAY_BALANCE => '余额', ]; /** * 仅查询支付过期的订单 */ public function scopeExpired() { return $this->where('status', static::STATUS_PENDING)->where('created_at', '<=', now()->subSeconds(1800)); } /** * 属于此订单的商品 */ public function products() { return $this->hasMany(OrderProduct::class); } /** * 确认此订单是否可以被确认 * * @return bool */ public function isConfirmable(): bool { return $this->isPaid(); } /** * 确认此订单是否可以被取消 * * @return bool */ public function isCancelable(): bool { if ($this->isPaid()) { return $this->shipping_state === static::SHIPPING_STATE_PENDING; } return $this->isPending(); } /** * 确认此订单是否是待付款 * * @return bool */ public function isPending(): bool { return $this->status === static::STATUS_PENDING; } /** * 确认此订单是否是已付款 * * @return bool */ public function isPaid(): bool { return $this->status === static::STATUS_PENDING; } /** * 获取订单券优惠金额 * * @return string */ public function getCouponDiscountAmountFormatAttribute() { return Numeric::trimTrailingZero(bcdiv($this->attributes['coupon_discount_amount'], 100, 2)); } /** * 获取订单会员折扣金额 * * @return string */ public function getVipDiscountAmountFormatAttribute() { return Numeric::trimTrailingZero(bcdiv($this->attributes['vip_discount_amount'], 100, 2)); } /** * 获取订单减免金额 * * @return string */ public function getReducedAmountFormatAttribute() { return Numeric::trimTrailingZero(bcdiv($this->attributes['reduced_amount'], 100, 2)); } /** * 获取订单邮费 * * @return string */ public function getShippingFeeFormatAttribute() { return Numeric::trimTrailingZero(bcdiv($this->attributes['shipping_fee'], 100, 2)); } /** * 获取订单支付金额 * * @return string */ public function getTotalAmountFormatAttribute() { return Numeric::trimTrailingZero(bcdiv($this->attributes['total_amount'], 100, 2)); } /** * 获取订单商品总额 * * @return string */ public function getProductsTotalAmountFormatAttribute() { return Numeric::trimTrailingZero(bcdiv($this->attributes['products_total_amount'], 100, 2)); } /** * 待支付订单过期时间 * * @return int */ public function getExpiresAtAttribute() { if (! $this->isPending()) { return 0; } return now()->diffInSeconds( $this->created_at->addSeconds(1800), false ); } }