85 lines
1.9 KiB
PHP
85 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
class CouponRedemption extends Model
|
|
{
|
|
/** @use HasFactory<\Database\Factories\CouponRedemptionFactory> */
|
|
use HasFactory;
|
|
|
|
public const STATUS_PENDING = 'pending';
|
|
|
|
public const STATUS_SUCCESS = 'success';
|
|
|
|
public const STATUS_FAILED = 'failed';
|
|
|
|
protected $fillable = [
|
|
'coupon_id',
|
|
'checkout_session_id',
|
|
'package_id',
|
|
'tenant_id',
|
|
'user_id',
|
|
'paddle_transaction_id',
|
|
'status',
|
|
'failure_reason',
|
|
'amount_discounted',
|
|
'currency',
|
|
'metadata',
|
|
'redeemed_at',
|
|
];
|
|
|
|
protected $casts = [
|
|
'amount_discounted' => 'decimal:2',
|
|
'metadata' => 'array',
|
|
'redeemed_at' => 'datetime',
|
|
];
|
|
|
|
public function coupon(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Coupon::class);
|
|
}
|
|
|
|
public function checkoutSession(): BelongsTo
|
|
{
|
|
return $this->belongsTo(CheckoutSession::class);
|
|
}
|
|
|
|
public function package(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Package::class);
|
|
}
|
|
|
|
public function tenant(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Tenant::class);
|
|
}
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function markSuccessful(?float $amount = null): void
|
|
{
|
|
if ($amount !== null) {
|
|
$this->amount_discounted = $amount;
|
|
}
|
|
|
|
$this->status = self::STATUS_SUCCESS;
|
|
$this->failure_reason = null;
|
|
$this->redeemed_at = now();
|
|
$this->save();
|
|
}
|
|
|
|
public function markFailed(string $reason): void
|
|
{
|
|
$this->status = self::STATUS_FAILED;
|
|
$this->failure_reason = $reason;
|
|
$this->save();
|
|
}
|
|
}
|