Payment.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace App\Models;
  3. use App\Casts\money;
  4. use App\Utils\Helpers;
  5. use Auth;
  6. use Illuminate\Database\Eloquent\Builder;
  7. use Illuminate\Database\Eloquent\Casts\Attribute;
  8. use Illuminate\Database\Eloquent\Model;
  9. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  10. /**
  11. * 支付单.
  12. */
  13. class Payment extends Model
  14. {
  15. protected $table = 'payment';
  16. protected $guarded = [];
  17. protected $casts = ['amount' => money::class];
  18. public function scopeUid(Builder $query): Builder
  19. {
  20. return $query->whereUserId(Auth::id());
  21. }
  22. public function user(): BelongsTo
  23. {
  24. return $this->belongsTo(User::class);
  25. }
  26. public function failed(): bool
  27. { // 关闭支付单
  28. return $this->close() && $this->order()->close();
  29. }
  30. public function close(): bool
  31. { // 关闭支付单
  32. return $this->update(['status' => -1]);
  33. }
  34. public function order(): BelongsTo
  35. {
  36. return $this->belongsTo(Order::class);
  37. }
  38. public function complete(): bool
  39. { // 完成支付单
  40. return $this->update(['status' => 1]);
  41. }
  42. protected function amountTag(): Attribute
  43. {
  44. return Attribute::make(
  45. get: fn () => Helpers::getPriceTag($this->amount),
  46. );
  47. }
  48. protected function statusLabel(): Attribute
  49. { // 订单状态
  50. return Attribute::make(
  51. get: fn () => match ($this->status) {
  52. -1 => trans('common.failed_item', ['attribute' => trans('user.pay')]),
  53. 1 => trans('common.success_item', ['attribute' => trans('user.pay')]),
  54. default => trans('common.payment.status.wait'),
  55. },
  56. );
  57. }
  58. }