Payment.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace App\Models;
  3. use Hidehalo\Nanoid\Client;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. use NumberFormatter;
  8. use Spatie\Activitylog\Traits\LogsActivity;
  9. class Payment extends Model
  10. {
  11. use HasFactory;
  12. use LogsActivity;
  13. public $incrementing = false;
  14. /**
  15. * @var string[]
  16. */
  17. protected $fillable = [
  18. 'id',
  19. 'user_id',
  20. 'payment_id',
  21. 'payer_id',
  22. 'payer',
  23. 'status',
  24. 'type',
  25. 'amount',
  26. 'price',
  27. 'currency_code',
  28. ];
  29. public static function boot()
  30. {
  31. parent::boot();
  32. static::creating(function (Payment $payment) {
  33. $client = new Client();
  34. $payment->{$payment->getKeyName()} = $client->generateId($size = 8);
  35. });
  36. }
  37. /**
  38. * @return BelongsTo
  39. */
  40. public function user()
  41. {
  42. return $this->belongsTo(User::class);
  43. }
  44. public function formatCurrency($locale = 'en_US')
  45. {
  46. $formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
  47. return $formatter->formatCurrency($this->price, $this->currency_code);
  48. }
  49. }