Payment.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Http\Models;
  3. use Auth;
  4. use Eloquent;
  5. use Illuminate\Database\Eloquent\Builder;
  6. use Illuminate\Database\Eloquent\Model;
  7. use Illuminate\Support\Carbon;
  8. /**
  9. * 支付单
  10. * Class Payment
  11. *
  12. * @property int $id
  13. * @property string|null $sn
  14. * @property int $user_id 用户ID
  15. * @property int|null $oid 本地订单ID
  16. * @property int $amount 金额,单位分
  17. * @property string|null $qr_code 支付二维码
  18. * @property int $status 状态:-1-支付失败、0-等待支付、1-支付成功
  19. * @property Carbon $created_at
  20. * @property Carbon $updated_at
  21. * @property-read mixed $pay_way_label
  22. * @property-read mixed $status_label
  23. * @property-read Order|null $order
  24. * @property-read User $user
  25. * @method static Builder|Payment newModelQuery()
  26. * @method static Builder|Payment newQuery()
  27. * @method static Builder|Payment query()
  28. * @method static Builder|Payment uid()
  29. * @method static Builder|Payment whereAmount($value)
  30. * @method static Builder|Payment whereCreatedAt($value)
  31. * @method static Builder|Payment whereId($value)
  32. * @method static Builder|Payment whereOid($value)
  33. * @method static Builder|Payment whereQrCode($value)
  34. * @method static Builder|Payment whereSn($value)
  35. * @method static Builder|Payment whereStatus($value)
  36. * @method static Builder|Payment whereUpdatedAt($value)
  37. * @method static Builder|Payment whereUserId($value)
  38. * @mixin Eloquent
  39. */
  40. class Payment extends Model
  41. {
  42. protected $table = 'payment';
  43. protected $primaryKey = 'id';
  44. protected $appends = ['status_label'];
  45. function scopeUid($query)
  46. {
  47. return $query->where('user_id', Auth::user()->id);
  48. }
  49. function user()
  50. {
  51. return $this->belongsTo(User::class, 'user_id', 'id');
  52. }
  53. function order()
  54. {
  55. return $this->belongsTo(Order::class, 'oid', 'oid');
  56. }
  57. function getAmountAttribute($value)
  58. {
  59. return $value/100;
  60. }
  61. function setAmountAttribute($value)
  62. {
  63. return $this->attributes['amount'] = $value*100;
  64. }
  65. // 订单状态
  66. function getStatusLabelAttribute()
  67. {
  68. switch($this->attributes['status']){
  69. case -1:
  70. $status_label = '支付失败';
  71. break;
  72. case 1:
  73. $status_label = '支付成功';
  74. break;
  75. case 0:
  76. default:
  77. $status_label = '等待支付';
  78. }
  79. return $status_label;
  80. }
  81. // 支付方式
  82. function getPayWayLabelAttribute()
  83. {
  84. switch($this->attributes['pay_way']){
  85. case 1:
  86. $pay_way_label = '微信';
  87. break;
  88. case 2:
  89. default:
  90. $pay_way_label = '支付宝';
  91. }
  92. return $pay_way_label;
  93. }
  94. }