Payment.php 1.4 KB

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