Payment.php 1.6 KB

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