PaymentMethod.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. $stripeDetailJson = json_decode($this->stripe_detail_json);
  67. $expirationMonth = @$stripeDetailJson->card ? @$stripeDetailJson->card->exp_month : null;
  68. $expirationYear = @$stripeDetailJson->card ? @$stripeDetailJson->card->exp_year : null;
  69. if((int) $expirationYear >= (int) date('Y')){
  70. if((int) $expirationMonth >= (int) date('m')){
  71. return false;
  72. }
  73. }
  74. return true;
  75. }
  76. }