hasOne(User::class, 'id', 'user_id'); } public function displayName(){ if($this->paypal_order_id) return 'PayPal'; if($this->card_type){ return "{$this->card_type} ****-****-{$this->card_last_four}Expires: {$this->expiration_month} / {$this->expiration_year}"; } $parsed = json_decode($this->stripe_detail_json ?? $this->braintree_detail_json); if(@$parsed->tenant){ return "{$parsed->tenant} - {$parsed->email}"; } if(@$parsed->card) { return "{$parsed->card->brand} ****-****-{$parsed->card->last4}Expires: {$parsed->card->exp_month} / {$parsed->card->exp_year}"; } return 'Non-card Payment Method'; } public function displayNameShort(){ if ($this->card_type) { return "****-****-{$this->card_last_four}"; } $parsed = json_decode($this->stripe_detail_json); if(!@$parsed->card) return 'Non-card Payment Method'; return "****-****-{$parsed->card->last4}"; } public function brand(){ if($this->card_type) return $this->card_type; $parsed = json_decode($this->stripe_detail_json); if(!@$parsed->card) return; return $parsed->card->brand; } public function cardLast4(){ if($this->card_last_four) return $this->card_last_four; $parsed = json_decode($this->stripe_detail_json); if(!@$parsed->card) return '---'; return $parsed->card->last4; } public function stripeDetails() { return json_decode($this->stripe_detail_json); } public function getWalletType(){ $data = $this->stripeDetails(); if(!@$data->card) return; if(!@$data->card->wallet) return; return toHumanReadable($data->card->wallet->type); } public function brandToCssClassName(){ $brand = $this->brand(); if(!$brand) return ''; $brand = strtolower($brand); return preg_replace('/\s+/', '_', $brand); } public function financialTransactions(){ return $this->hasMany(FinancialTransaction::class, 'payment_method_id', 'id'); } public function isExpired() { $expirationMonth = $this->expiration_month; $expirationYear = $this->expiration_year; // If expiration details are missing, try to retrieve them from stripe_detail_json if (!$expirationYear || !$expirationMonth) { $stripeDetailJson = json_decode($this->stripe_detail_json); if (isset($stripeDetailJson->card->exp_month) && isset($stripeDetailJson->card->exp_year)) { $expirationMonth = (int) $stripeDetailJson->card->exp_month; $expirationYear = (int) $stripeDetailJson->card->exp_year; } else { // Return true if expiration details are not found return true; } } $currentYear = (int) date('Y'); $currentMonth = (int) date('m'); // Check if the card is expired return ($expirationYear < $currentYear) || ($expirationYear === $currentYear && $expirationMonth < $currentMonth); } }