PaymentMethod.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use App\Models\BaseModel;
  6. class PaymentMethod extends BaseModel
  7. {
  8. use HasFactory;
  9. protected $table = 'payment_method';
  10. public function user(){
  11. return $this->hasOne(User::class, 'id', 'user_id');
  12. }
  13. public function displayName(){
  14. if($this->paypal_order_id) return 'PayPal';
  15. if($this->card_type){
  16. return "<small>{$this->card_type} ****-****-{$this->card_last_four}</small><small class='text-muted d-block'>Expires: {$this->expiration_month} / {$this->expiration_year}</small>";
  17. }
  18. $parsed = json_decode($this->stripe_detail_json ?? $this->braintree_detail_json);
  19. if(@$parsed->tenant){
  20. return "<small class='text-uppercase'>{$parsed->tenant} - {$parsed->email}</small>";
  21. }
  22. if(@$parsed->card) {
  23. return "<small class='text-uppercase'>{$parsed->card->brand} ****-****-{$parsed->card->last4}</small><small class='text-muted d-block'>Expires: {$parsed->card->exp_month} / {$parsed->card->exp_year}</small>";
  24. }
  25. return 'Non-card Payment Method';
  26. }
  27. public function displayNameShort(){
  28. if ($this->card_type) {
  29. return "****-****-{$this->card_last_four}";
  30. }
  31. $parsed = json_decode($this->stripe_detail_json);
  32. if(!@$parsed->card) return 'Non-card Payment Method';
  33. return "****-****-{$parsed->card->last4}";
  34. }
  35. public function brand(){
  36. if($this->card_type) return $this->card_type;
  37. $parsed = json_decode($this->stripe_detail_json);
  38. if(!@$parsed->card) return;
  39. return $parsed->card->brand;
  40. }
  41. public function cardLast4(){
  42. if($this->card_last_four) return $this->card_last_four;
  43. $parsed = json_decode($this->stripe_detail_json);
  44. if(!@$parsed->card) return '---';
  45. return $parsed->card->last4;
  46. }
  47. public function stripeDetails() {
  48. return json_decode($this->stripe_detail_json);
  49. }
  50. public function getWalletType(){
  51. $data = $this->stripeDetails();
  52. if(!@$data->card) return;
  53. if(!@$data->card->wallet) return;
  54. return toHumanReadable($data->card->wallet->type);
  55. }
  56. public function brandToCssClassName(){
  57. $brand = $this->brand();
  58. if(!$brand) return '';
  59. $brand = strtolower($brand);
  60. return preg_replace('/\s+/', '_', $brand);
  61. }
  62. public function financialTransactions(){
  63. return $this->hasMany(FinancialTransaction::class, 'payment_method_id', 'id');
  64. }
  65. public function isExpired() {
  66. $expirationMonth = $this->expiration_month;
  67. $expirationYear = $this->expiration_year;
  68. // If expiration details are missing, try to retrieve them from stripe_detail_json
  69. if (!$expirationYear || !$expirationMonth) {
  70. $stripeDetailJson = json_decode($this->stripe_detail_json);
  71. if (isset($stripeDetailJson->card->exp_month) && isset($stripeDetailJson->card->exp_year)) {
  72. $expirationMonth = (int) $stripeDetailJson->card->exp_month;
  73. $expirationYear = (int) $stripeDetailJson->card->exp_year;
  74. } else {
  75. // Return true if expiration details are not found
  76. return true;
  77. }
  78. }
  79. $currentYear = (int) date('Y');
  80. $currentMonth = (int) date('m');
  81. // Check if the card is expired
  82. return ($expirationYear < $currentYear) ||
  83. ($expirationYear === $currentYear && $expirationMonth < $currentMonth);
  84. }
  85. }