123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use App\Models\BaseModel;
- class PaymentMethod extends BaseModel
- {
- use HasFactory;
- protected $table = 'payment_method';
- public function user(){
- return $this->hasOne(User::class, 'id', 'user_id');
- }
- public function displayName(){
- if($this->paypal_order_id) return 'PayPal';
- if($this->card_type){
- return "<small>{$this->card_type} ****-****-{$this->card_last_four}</small><small class='text-muted d-block'>Expires: {$this->expiration_month} / {$this->expiration_year}</small>";
- }
- $parsed = json_decode($this->stripe_detail_json ?? $this->braintree_detail_json);
- if(@$parsed->tenant){
- return "<small class='text-uppercase'>{$parsed->tenant} - {$parsed->email}</small>";
- }
- if(@$parsed->card) {
- 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>";
- }
- 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);
- }
- }
|