PaymentMethod.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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->card_type){
  15. return "<small>{$this->card_type} ****-****-{$this->card_last_four}</small><small class='text-muted d-block'>Expires: {$this->expiration_month} / {$this->expiration_year}</small>";
  16. }
  17. $parsed = json_decode($this->stripe_detail_json ?? $this->braintree_detail_json);
  18. if(@$parsed->tenant){
  19. return "<small class='text-uppercase'>{$parsed->tenant} - {$parsed->email}</small>";
  20. }
  21. if(@$parsed->card) {
  22. 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>";
  23. }
  24. return 'Non-card Payment Method';
  25. }
  26. public function displayNameShort(){
  27. if ($this->card_type) {
  28. return "****-****-{$this->card_last_four}";
  29. }
  30. $parsed = json_decode($this->stripe_detail_json);
  31. if(!@$parsed->card) return 'Non-card Payment Method';
  32. return "****-****-{$parsed->card->last4}";
  33. }
  34. public function brand(){
  35. if($this->card_type) return $this->card_type;
  36. $parsed = json_decode($this->stripe_detail_json);
  37. if(!@$parsed->card) return;
  38. return $parsed->card->brand;
  39. }
  40. public function cardLast4(){
  41. if($this->card_last_four) return $this->card_last_four;
  42. $parsed = json_decode($this->stripe_detail_json);
  43. if(!@$parsed->card) return '---';
  44. return $parsed->card->last4;
  45. }
  46. public function stripeDetails() {
  47. return json_decode($this->stripe_detail_json);
  48. }
  49. public function getWalletType(){
  50. $data = $this->stripeDetails();
  51. if(!@$data->card) return;
  52. if(!@$data->card->wallet) return;
  53. return toHumanReadable($data->card->wallet->type);
  54. }
  55. public function brandToCssClassName(){
  56. $brand = $this->brand();
  57. if(!$brand) return '';
  58. $brand = strtolower($brand);
  59. return preg_replace('/\s+/', '_', $brand);
  60. }
  61. public function financialTransactions(){
  62. return $this->hasMany(FinancialTransaction::class, 'payment_method_id', 'id');
  63. }
  64. public function isExpired() {
  65. $stripeDetailJson = json_decode($this->stripe_detail_json);
  66. $expirationMonth = @$stripeDetailJson->card ? @$stripeDetailJson->card->exp_month : null;
  67. $expirationYear = @$stripeDetailJson->card ? @$stripeDetailJson->card->exp_year : null;
  68. if((int) $expirationYear >= (int) date('Y')){
  69. if((int) $expirationMonth >= (int) date('m')){
  70. return false;
  71. }
  72. }
  73. return true;
  74. }
  75. }