Payment.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Models;
  3. use App\Components\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 order(): BelongsTo
  23. {
  24. return $this->belongsTo(Order::class);
  25. }
  26. public function close() // 关闭支付单
  27. {
  28. return $this->update(['status' => -1]);
  29. }
  30. public function failed() // 关闭支付单
  31. {
  32. return $this->close() && $this->order()->close();
  33. }
  34. public function complete() // 完成支付单
  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. switch ($this->attributes['status']) {
  54. case -1:
  55. $status_label = trans('common.failed_item', ['attribute' => trans('user.pay')]);
  56. break;
  57. case 1:
  58. $status_label = trans('common.success_item', ['attribute' => trans('user.pay')]);
  59. break;
  60. case 0:
  61. default:
  62. $status_label = trans('common.payment.status.wait');
  63. }
  64. return $status_label;
  65. }
  66. }