Payment.php 1.3 KB

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