47 lines
1.5 KiB
PHP
47 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Database\Factories\BazaarSubscriptionFactory;
|
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
|
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
#[Fillable(['mobile_app_id', 'subscription_id', 'purchase_token', 'purchase_token_hash', 'linked_subscription_token', 'status', 'auto_renewing', 'starts_at', 'valid_until', 'validated_at', 'bazaar_response'])]
|
|
#[Hidden(['purchase_token', 'purchase_token_hash', 'linked_subscription_token'])]
|
|
class BazaarSubscription extends Model
|
|
{
|
|
/** @use HasFactory<BazaarSubscriptionFactory> */
|
|
use HasFactory;
|
|
|
|
protected $attributes = ['status' => 'unknown'];
|
|
|
|
public function mobileApp(): BelongsTo
|
|
{
|
|
return $this->belongsTo(MobileApp::class);
|
|
}
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::saving(function (BazaarSubscription $subscription): void {
|
|
if ($subscription->isDirty('purchase_token')) {
|
|
$subscription->purchase_token_hash = hash('sha256', $subscription->purchase_token);
|
|
}
|
|
});
|
|
}
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'auto_renewing' => 'boolean',
|
|
'purchase_token' => 'encrypted',
|
|
'starts_at' => 'datetime',
|
|
'valid_until' => 'datetime',
|
|
'validated_at' => 'datetime',
|
|
'bazaar_response' => 'array',
|
|
];
|
|
}
|
|
}
|