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. class Payment extends Model
  9. {
  10. use HasFactory;
  11. public $incrementing = false;
  12. protected $primaryKey = 'id';
  13. /**
  14. * @var string[]
  15. */
  16. protected $fillable = [
  17. 'id',
  18. 'user_id',
  19. 'payment_id',
  20. 'payment_method',
  21. 'status',
  22. 'type',
  23. 'amount',
  24. 'price',
  25. 'tax_value',
  26. 'total_price',
  27. 'tax_percent',
  28. 'currency_code',
  29. 'shop_item_product_id',
  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. * @return float
  50. */
  51. public function formatToCurrency($value, $locale = 'en_US')
  52. {
  53. $formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
  54. return $formatter->formatCurrency($value, $this->currency_code);
  55. }
  56. }