Payment.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. 'payment_method',
  22. 'status',
  23. 'type',
  24. 'amount',
  25. 'price',
  26. 'tax_value',
  27. 'total_price',
  28. 'tax_percent',
  29. 'currency_code',
  30. ];
  31. public static function boot()
  32. {
  33. parent::boot();
  34. static::creating(function (Payment $payment) {
  35. $client = new Client();
  36. $payment->{$payment->getKeyName()} = $client->generateId($size = 8);
  37. });
  38. }
  39. /**
  40. * @return BelongsTo
  41. */
  42. public function user()
  43. {
  44. return $this->belongsTo(User::class);
  45. }
  46. /**
  47. * @param mixed $value
  48. * @param string $locale
  49. *
  50. * @return float
  51. */
  52. public function formatToCurrency($value,$locale = 'en_US')
  53. {
  54. $formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
  55. return $formatter->formatCurrency($value, $this->currency_code);
  56. }
  57. }